-
Notifications
You must be signed in to change notification settings - Fork 906
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Describe your environment
python version: 3.7.12
grpcio: 1.43.0
Steps to reproduce
Call inside of a RPC-method-implementation of a grpc service the methods:
context.code()context.details()
which are supported since grpcio v1.38.0 (s. here).
Example:
Add the functions into e.g. helloworld/greeter_server.py:
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
print("code before: %r" % context.code())
context.set_code(grpc.StatusCode.OK)
print("code after: %r" % context.code())
print("details before: %r" % context.details())
context.set_details("test")
print("details after: %r" % context.details())
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)What is the expected behavior?
This should print:
code before: None
code after: <StatusCode.OK: (0, 'ok')>
details before: None
details after: 'test'What is the actual behavior?
There are TypeError raised:
TypeError: 'StatusCode' object is not callableTypeError: 'NoneType' object is not callableAdditional context
The _OpenTelemetryServicerContext inside of _server.py implements code and details as attributes while the official grpcio API implements them as methods.
Proposed Bugfix
Stick with the attributes to support older grpcio versions, but make them private. Furthermore, add code() and details() with RuntimeError:
class _OpenTelemetryServicerContext(grpc.ServicerContext):
def __init__(self, servicer_context, active_span):
self._servicer_context = servicer_context
self._active_span = active_span
self._code = grpc.StatusCode.OK
self._details = None
super().__init__()
# ...
def abort(self, code, details):
self._code = code
self._details = details
self._active_span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
)
self._active_span.set_status(
Status(
status_code=StatusCode.ERROR,
description=f"{code}:{details}",
)
)
return self._servicer_context.abort(code, details)
def code(self):
if not hasattr(self._servicer_context, "code"):
raise RuntimeError(
"code() is not supported with the installed version of grpcio"
)
return self._servicer_context.code()
def set_code(self, code):
self._code = code
# use details if we already have it, otherwise the status description
details = self._details or code.value[1]
self._active_span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
)
if code != grpc.StatusCode.OK:
self._active_span.set_status(
Status(
status_code=StatusCode.ERROR,
description=f"{code}:{details}",
)
)
return self._servicer_context.set_code(code)
def details(self):
if not hasattr(self._servicer_context, "details"):
raise RuntimeError(
"details() is not supported with the installed version of "
"grpcio"
)
return self._servicer_context.details()
def set_details(self, details):
self._details = details
if self._code != grpc.StatusCode.OK:
self._active_span.set_status(
Status(
status_code=StatusCode.ERROR,
description=f"{self._code}:{details}",
)
)
return self._servicer_context.set_details(details)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working