0

In my application I use the Tracer bean to get trace and span IDs:

@Service
@RequiredArgsConstructor
public class SomeService {
    private final Tracer tracer;

    private TracingContext getTracingContext() {
        var context = Optional.ofNullable(tracer.currentTraceContext().context());
        return TracingContext.builder()
                .traceId(context.map(TraceContext::traceId).orElse("null"))
                .spanId(context.map(TraceContext::spanId).orElse("null"))
                .build();
    }

    public void doSomeJbob() {
      ...
      var context = getTracingContext();

      send_request_using<org.springframework.web.reactive.function.client.WebClient>();

      ...
    }
}

The problem is when management.tracing.enabled property is set to false, the application doesn't start with Bean not found exception. I created a workaround, I've created a @Bean in configuration:

@Bean
@ConditionalOnClass(Tracer.class)
@ConditionalOnProperty(prefix = "management.tracing", name = "enabled", havingValue = "false")
public Tracer defaultTracer() {
    return Tracer.NOOP;
}

The application starts, but then request called with send_request_using<org.springframework.web.reactive.function.client.WebClient>(); starts failing: Context does not have an entry for key [class io.micrometer.tracing.handler.TracingObservationHandler$TracingContext]

What do I do wrong? Isn't tracing supposed to be disabled sometimes?

Mitsuder
  • 13
  • 3
  • update micrometer dependency and put Hooks.enableAutomaticContextPropagation(); in main function – YWILLS Apr 13 '23 at 07:00

2 Answers2

0

Your application mandates a Tracer bean to be created, but disabling tracing will result the absence of that bean. Instead of making it mandatory, you can use ObjectProvider instead:

private final ObjectProvider<Tracer> tracerProvider;
Jonatan Ivanov
  • 4,895
  • 2
  • 15
  • 30
0

In my case it helped to define also NOOP ObservationRegistry bean like this:

@ConditionalOnProperty(prefix = "management.tracing", name = "enabled", havingValue = "false")
@Configuration
public class NoopTracingConfig {

    @Bean
    Tracer tracer() {
        return Tracer.NOOP;
    }

    @Bean
    ObservationRegistry observationRegistry() {
        return ObservationRegistry.NOOP;
    }
}
martinsefcik
  • 346
  • 9
  • 20