Search code examples
spring-cloud-sleuth

Is there a way to rename traceId and spanId?


Given the following logging layout pattern:

appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - [%X{newTraceIdPlaceholder}, %X{newSpanIdPlaceholder}] - %X{Request-Uri} - %msg%n

Is there a way to tell Sleuth to consider the "newTraceIdPlaceholder" and "newSpanIdPlaceholder" fields as traceId and spanId?


Solution

  • You would have to register your own bean of Brave's CorrelationScopeCustomizer type and over there you can provide additional fields in a similar manner to this:

    CurrentTraceContext.ScopeDecorator create() {
        return new Builder()
          .clear()
          .add(SingleCorrelationField.create(BaggageFields.TRACE_ID))
          .add(SingleCorrelationField.create(BaggageFields.PARENT_ID))
          .add(SingleCorrelationField.create(BaggageFields.SPAN_ID))
          .add(SingleCorrelationField.create(BaggageFields.SAMPLED))
          .build();
      }
    

    You can provide your own fields there