1

i'm trying to add additional Baggage to the existing span on a HTTP server, i want to add a path variable to the span to be accessed from log MDC and to be propagated on the wire to the next server i call via http or kafka.

my setup : spring cloud sleuth Hoxton.SR5 and spring boot 2.2.5

i tried adding the following setup and configuration:

spring:
  sleuth:
    propagation-keys: context-id, context-type
    log:
      slf4j:
        whitelisted-mdc-keys: context-id, context-type

and added http interceptor :

public class HttpContextInterceptor implements HandlerInterceptor {

    
    private final Tracer tracer;
    private final HttpContextSupplier httpContextSupplier;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (httpContextSupplier != null) {
            addContext(request, handler);
        }
        return true;
    }

    private void addContext(HttpServletRequest request, Object handler) {
        final Context context = httpContextSupplier.getContext(request);
        if (!StringUtils.isEmpty(context.getContextId())) {
            ExtraFieldPropagation.set(tracer.currentSpan().context(), TracingHeadersConsts.HEADER_CONTEXT_ID, context.getContextId());
        }
        if (!StringUtils.isEmpty(context.getContextType())) {
            ExtraFieldPropagation.set(tracer.currentSpan().context(), TracingHeadersConsts.HEADER_CONTEXT_TYPE, context.getContextType());
        }
    }
}

and http filter to affect the current span(according to the spring docs)

public class TracingFilter extends OncePerRequestFilter {

    private final Tracer tracer;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try (Tracer.SpanInScope ws = tracer.withSpanInScope(tracer.currentSpan())){
            filterChain.doFilter(request, response);
        }
    }
}

the problem is the logs doesn't contain my custom context-id, context-type, although is see it in the span context.

what i'm missing ?

Elia Rohana
  • 326
  • 3
  • 16

2 Answers2

2

Similar question Spring cloud sleuth adding tag and answer to it https://stackoverflow.com/a/66554834

For some context: This is from the Spring Docs.

In order to automatically set the baggage values to Slf4j’s MDC, you have to set the spring.sleuth.baggage.correlation-fields property with a list of allowed local or remote keys. E.g. spring.sleuth.baggage.correlation-fields=country-code will set the value of the country-code baggage into MDC.

Note that the extra field is propagated and added to MDC starting with the next downstream trace context. To immediately add the extra field to MDC in the current trace context, configure the field to flush on update.

// configuration
@Bean
BaggageField countryCodeField() {
    return BaggageField.create("country-code");
}

@Bean
ScopeDecorator mdcScopeDecorator() {
    return MDCScopeDecorator.newBuilder()
            .clear()
            .add(SingleCorrelationField.newBuilder(countryCodeField())
                    .flushOnUpdate()
                    .build())
            .build();
}

// service
@Autowired
BaggageField countryCodeField;

countryCodeField.updateValue("new-value");
prohank
  • 21
  • 1
  • 3
  • this is supported in the newer version of sleuth. Hoxton.SR5 uses sleuth 2.2.4.RELEASE – Elia Rohana May 09 '21 at 06:34
  • 2
    If you can't upgrade your Spring Sleuth version and not able to get it working in the version you are using, you still have the choice of manually setting and clearing the MDC. For eg, try `MDC.put("context-id", context-id)`. This will insert the context-id in the current span. – prohank May 10 '21 at 09:12
0

A way to flush MDC in current span is also described in official Sleuth 2.0 -> 3.0 migration guide

@Configuration
class BusinessProcessBaggageConfiguration {
  BaggageField BUSINESS_PROCESS = BaggageField.create("bp");

  /** {@link BaggageField#updateValue(TraceContext, String)} now flushes to MDC */
  @Bean
  CorrelationScopeCustomizer flushBusinessProcessToMDCOnUpdate() {
    return b -> b.add(
        SingleCorrelationField.newBuilder(BUSINESS_PROCESS).flushOnUpdate().build()
    );
  }
}
Konstantin Ziubin
  • 660
  • 10
  • 17