admin管理员组

文章数量:1389871

In my GRPC server Context.current().toString() returns a different value everytime it is called even with in the same function for a single request.

Based on .html wouldn't it remain the same for the lifetime of the request. I'm not forking the context or creating a new context within the life time of the request. But everytime I call Context.current().toString() I get a different value. Can anyone let me know what is the scenario where this is observed?

My code looks something like this -

Header Interceptor

public class HeaderInterceptor implements ServerInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(HeaderInterceptor.class);
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
        ServerCall<ReqT, RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next
) {
        String currentContext = Context.current().toString();
        String traceIdByOtel = io.opentelemetry.api.trace.Span.current().getSpanContext().getTraceId();
        LOGGER.info("Context in the new request " + currentContext + " otelTraceId " + traceIdByOtel);
    
    return next.startCall(call, headers); 
   }
}

Completion and Cancel Interceptor

public class GlobalGrpcExceptionHandler implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
                                                             Metadata requestHeaders, ServerCallHandler<ReqT, RespT> next) {
    final Logger LOGGER = LoggerFactory.getLogger(GlobalGrpcExceptionHandler.class);
    ServerCall.Listener<ReqT> delegate = next.startCall(call, requestHeaders);
    return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(delegate) {
        @Override
        public void onComplete() {
            try {
                String currentContext = Context.current().toString();
                String traceIdByOtel = io.opentelemetry.api.trace.Span.current().getSpanContext().getTraceId();
                LOGGER.info("Context in the Completed request " + currentContext + " otelTraceId " + traceIdByOtel);
                super.onComplete();
            } catch (Exception e) {
                LOGGER.info("Logging metadata for error interceptor inside exception for Completion" + requestHeaders);
            }
        }
        @Override
        public void onCancel() {
            try {
                String currentContext = Context.current().toString();
                String traceIdByOtel = io.opentelemetry.api.trace.Span.current().getSpanContext().getTraceId();
                LOGGER.info("Context in the Cancelled request " + currentContext + " otelTraceId " + traceIdByOtel);

                super.onCancel();
            } catch (Exception e) {
                LOGGER.info("Logging metadata for error interceptor inside exception for cancel " + requestHeaders);
            }
        }
    };
}
}

I get the output something like

[thread1] Context in the new request io.grpc.Context@25c9f453 otelTraceId 4a0240c63096883c9bb4b27d63a722c9
[thread2] Context in the completed request io.grpc.Context@19886353 otelTraceId 4a0240c63096883c9bb4b27d63a722c9

I tried to print through the flow of the API each time I print the context seem to be different for the same request. Everytime I call Context.current().toString() I get a new GRPC context, sometimes even within the same function. Does anyone know any reason why something like this will happen?

本文标签: grpcContextcurrent() Changes every time it is calledStack Overflow