python-grpc icon indicating copy to clipboard operation
python-grpc copied to clipboard

No good way to get ServicerContext code/details from interceptor when replaced with _OpenTracingServicerContext

Open oscdc opened this issue 5 years ago • 0 comments

Hi, we've been using grpcext.intercept_server() UnaryServerInterceptor and inspecting the server's return code like this:

def intercept_unary(self, request, servicer_context, server_info, handler):
    try:
      handler(request, servicer_context)
    except:
      code = servicer_context._state.code
      ...

We know this is not great: technically _state is private, but the ServicerContext offers no other public API for getting the code out of it.

The problem comes when we ALSO try to use open_tracing_server_interceptor(). When we do that, the servicer_context is replaced with an _OpenTracingServicerContext. And _OpenTracingServicerContext does not expose _state, so the above code breaks.

Yes, we know it's our fault for relying on the private _state field. But would it be possible to request a way to read the code/details from within a UnaryServerInterceptor, that would work whether or not we're using open_tracing_server_interceptor()?

oscdc avatar Jul 09 '19 02:07 oscdc