Open hnvas opened 2 months ago
I had the same issue with previous lib version: sleuth context was closed before I could log traceId in exception handler. And had to implement own GlobalServerInterceptorRegistry and switch TracingServerInterceptor and GrpcExceptionInterceptor. It's not what I'd like to do manually, so I will upvote and follow the thread.
We have similar problem in our applications. Logs in @GrpcService are inside observation context but logs in GrpcExceptionHandler are not. This prevents us from correlating error to specific transaction. Example:
@GrpcService
public class GreeterService extends GreeterGrpc.GreeterImplBase {
Logger log = LoggerFactory.getLogger(GreeterService.class);
@Override
public void hello(Hello.HelloRequest request, StreamObserver<Hello.HelloReply> responseObserver) {
Hello.HelloReply.Builder replyBuilder = Hello.HelloReply.newBuilder().setMessage("OK");
responseObserver.onNext(replyBuilder.build());
log.info("@GrpcService MDC " + MDC.getCopyOfContextMap());
throw new RuntimeException();
}
}
@GrpcExceptionHandler
public StatusRuntimeException handleCompletionException(RuntimeException exception) {
com.google.rpc.Status status =
com.google.rpc.Status.newBuilder()
.setCode(Code.INTERNAL.getNumber())
.setMessage(INTERNAL_SERVER_ERROR.toString())
.build();
log.info("@GrpcExceptionHandler MDC " + MDC.getCopyOfContextMap());
return StatusProto.toStatusRuntimeException(status);
}
2024-06-06T16:24:53.408+02:00 INFO 21320 --- [demo] [ault-executor-0] [6661c6b5a565d0976496dafd60a17f77-f7d18210ee565ee4] com.example.demo.GreeterService : @GrpcService MDC {traceId=6661c6b5a565d0976496dafd60a17f77, spanId=f7d18210ee565ee4} 2024-06-06T16:24:53.411+02:00 INFO 21320 --- [demo] [ault-executor-0] [ ] c.example.demo.CustomExceptionHandler : @GrpcExceptionHandler MDC {}
I am also uploading a sample application: grpc-exception-handler.zip
The bug
The GrpcAdviceAutoConfiguration class initialises the global exception handler with higher precedence than the GrpcServerMicrometerTraceAutoConfiguration class, causing the GrpcExceptionListener to handle exceptions outside of observation scope and making it impossible for the exception handlers declared in @GrpcAdvice annotated classes to access the current tracing context.
Stacktrace and logs
The application's environment
Which versions do you use?
Additional context