Closed ForItInNet closed 4 months ago
I have found that it should be propagated by hand like in this tutorial https://www.youtube.com/watch?v=6OKS36PSpho It can be something like this
public static void main(String[] args) {
Hooks.enableAutomaticContextPropagation();
SpringApplication.run(AuthorityServiceApplication.class, args);
}
Interceptor for trace ID creation
@Slf4j
@RequiredArgsConstructor
@GrpcGlobalServerInterceptor
public class TracerInterceptor implements ServerInterceptor, ClientInterceptor {
private final String TRACE_ID = "traceId";
private final Metadata.Key<String> TRACE_ID_KEY = Metadata.Key.of(TRACE_ID, Metadata.ASCII_STRING_MARSHALLER);
private final Tracer tracer;
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
Optional.ofNullable(headers.get(TRACE_ID_KEY)).ifPresentOrElse(traceId -> MDC.put(TRACE_ID, traceId), () -> tracer.withSpan(tracer.nextSpan()));
ContextRegistry.getInstance().registerThreadLocalAccessor(TRACE_ID, () -> MDC.get(TRACE_ID), traceId -> MDC.put(TRACE_ID, traceId), () -> MDC.remove(TRACE_ID));
return next.startCall(call, headers);
}
}
And add data into the context of your chain:
...
.contextCapture();
Is it expected that gRPC doesn't create trace ID automatically? How can we correctly propagate tracer context?
Thank you.
cc @chemicL @bclozel
AFAIK, Spring has no official gRPC support. Whichever community supported project that the GrpcGlobalServerInterceptor
comes from would need to support Observation
just like Spring does for this to work without user configuration.
Some remarks regarding the code above:
spring.reactor.context-propagation=auto
Boot config can be used instead of directly calling Hooks.enableAutomaticContextPropagation()
ThreadLocalAccessor
once per app lifecycle instead of registering one on every call.MDC
and using contextCapture()
afterwards, but instead write directly to the Reactor Context
. ThreadLocalAccessor
for such a use case.If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open.
We tried to use micrometer tracing with WebFlux gRPC and, it looks like, the context doesn't populate between executors during the one request.
Result
We can see that before DB-operation the log has a trace ID and executor ault-executor-0 but after selection, the race ID is empty and, as expected, we have another executor actor-tcp-nio-2.
There are dependencies which are used:
Spring Boot 3.1.5 The main method also has Hooks.enableAutomaticContextPropagation