I have upgraded Sleuth to version 3.0.2 and started getting NPEs. I know that there were breaking changes, and there is a migration guide, but for the life of me I cannot say how to resolve this issue.
[INFO] +- org.springframework.cloud:spring-cloud-starter-sleuth:jar:3.0.2:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:2.4.5:compile
[INFO] | | \- org.aspectj:aspectjweaver:jar:1.9.6:compile
[INFO] | +- org.springframework.cloud:spring-cloud-sleuth-autoconfigure:jar:3.0.2:compile
[INFO] | | +- org.springframework.cloud:spring-cloud-sleuth-instrumentation:jar:3.0.2:compile
[INFO] | | | \- org.springframework.cloud:spring-cloud-sleuth-api:jar:3.0.2:compile
[INFO] | | \- org.aspectj:aspectjrt:jar:1.9.6:compile
[INFO] | \- org.springframework.cloud:spring-cloud-sleuth-brave:jar:3.0.2:compile
[INFO] | +- io.zipkin.brave:brave:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-context-slf4j:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-messaging:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-rpc:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-spring-rabbit:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-kafka-clients:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-kafka-streams:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-httpclient:jar:5.13.2:compile
[INFO] | | \- io.zipkin.brave:brave-instrumentation-http:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-httpasyncclient:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-jms:jar:5.13.2:compile
[INFO] | +- io.zipkin.brave:brave-instrumentation-mongodb:jar:5.13.2:compile
[INFO] | +- io.zipkin.aws:brave-propagation-aws:jar:0.21.3:compile
[INFO] | \- io.zipkin.reporter2:zipkin-reporter-metrics-micrometer:jar:2.16.1:compile
[INFO] | \- io.zipkin.reporter2:zipkin-reporter:jar:2.16.1:compile
[INFO] | \- io.zipkin.zipkin2:zipkin:jar:2.23.0:compile
The field that is being injected.
@Autowired
protected Tracer tracer;
The line that is throwing NPE:
tracer.currentSpan().context().traceId();
The currentSpan() method, where ThreadContext is null.
@Nullable
public Span currentSpan() {
TraceContext context = this.currentTraceContext.get();
return context == null ? null : new LazySpan(this, context);
}
Also this line is producing null: String traceId = MDC.get("X-B3-TraceId");
MDC.getCopyOfContextMap();
returns an empty map.
Here is the code of the method for the completeness of the picture:
@GetMapping(path = "/user", produces = APPLICATION_JSON_VALUE)
@RequiresPermissions(IAM_USER_GET_PERMISSION)
public Object getMyself() {
UserContext userContext = (UserContext) SecurityUtils.getSubject().getPrincipal();
String traceId = MDC.get("X-B3-TraceId");
System.out.println(traceId);
System.out.println(MDC.getCopyOfContextMap());
OperationContext operationContext = OperationContext.newInstance(userContext, tracer.currentSpan().context().traceId());
UserBO user = identityManager.getMyself(operationContext);
return ResponseEntity.status(HttpStatus.OK).body(new UserEnvelopeDTO(iamDTOMapper.mapScoped(user, user.getActiveOrganizationId())));
}
As you can see this is an HTTP thread, that doesn't get MDC properly populated.
Also, I have this bean configured:
@Bean
public CurrentTraceContext.ScopeDecorator legacyMDCKeys() {
return MDCScopeDecorator.newBuilder()
.clear()
.add(CorrelationScopeConfig.SingleCorrelationField.newBuilder(BaggageFields.TRACE_ID)
.name("X-B3-TraceId").build())
.add(CorrelationScopeConfig.SingleCorrelationField.newBuilder(BaggageFields.PARENT_ID)
.name("X-B3-ParentSpanId").build())
.add(CorrelationScopeConfig.SingleCorrelationField.newBuilder(BaggageFields.SPAN_ID)
.name("X-B3-SpanId").build())
.add(CorrelationScopeConfig.SingleCorrelationField.newBuilder(BaggageFields.SAMPLED)
.name("X-Span-Export").build())
.build();
}