spring-boottracemicrometerzipkinmicrometer-tracing

Micrometer tracing disabled error: required a bean of type 'io.micrometer.tracing.Tracer' that could not be found


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?


Solution

  • 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;