admin管理员组文章数量:1392007
I have a GRPC server that handles large number of requests per second. I have header interceptors, Completion and cancelled interceptors created. My understanding is that context of GRPC request would remain same throughout the of request. However I see context in header interceptor, function that handles the request and the completion interceptor all seem to have different contexts. I'm not creating any new contexts inside the code. I have tested the same code locally for a single request, the context remains same for the entire request and even between the threads as well. However, production GRPC server seem to be printing different values for context for different location.
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?
本文标签: GRPC context keeps changing within a single requestStack Overflow
版权声明:本文标题:GRPC context keeps changing within a single request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744663777a2618407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论