java-spring-jaeger icon indicating copy to clipboard operation
java-spring-jaeger copied to clipboard

Cannot override tracer bean properly

Open winster opened this issue 5 years ago • 0 comments

I want to override tracer bean to include MDCScopeManager as given in https://github.com/jaegertracing/jaeger-client-java/tree/master/jaeger-core#log-correlation

My code looks like this

@Configuration
public class TracerConfig {
    
    @ConditionalOnProperty(value = "opentracing.jaeger.enabled", havingValue = "true", matchIfMissing = true)
    @Bean
    @Primary
    public Tracer tracer() {
        MDCScopeManager scopeManager = new MDCScopeManager.Builder().build();
        JaegerTracer.Builder builder = (new JaegerTracer.Builder("myapp"))
            .withSampler(new ConstSampler(true))
            .withScopeManager(scopeManager);
        return builder.build();
    }
}

But I want to use the beans defined in JaegerAutoConfiguration such as sampler, reporter, metrics etc. So I rewrote my code as follows

@AutoConfigureAfter({JaegerAutoConfiguration.class})
public class TracerConfig {
    
    @ConditionalOnProperty(value = "opentracing.jaeger.enabled", havingValue = "true", matchIfMissing = true)
    @Bean
    @Primary
    public Tracer tracer(Sampler sampler, Reporter reporter, Metrics metrics, JaegerConfigurationProperties properties) {
        MDCScopeManager scopeManager = new MDCScopeManager.Builder().build();
        JaegerTracer.Builder builder = (new JaegerTracer.Builder(properties.getServiceName()))
            .withReporter(reporter).withSampler(sampler).withTags(properties.determineTags())
            .withScopeManager(scopeManager).withMetrics(metrics);
        return builder.build();
    }
}

With this bean, mdc context is missing traceId, spanId and sampled. Can you tell me what went wrong here?

winster avatar Sep 25 '20 14:09 winster