mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-30 13:43:03 +08:00
bugfix(opentelemetry-instrumentation-grpc): Add code() and details() to ServicerContext (#1578)
This commit is contained in:
@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- `opentelemetry-instrumentation-grpc` Fix code()/details() of _OpentelemetryServicerContext.
|
||||||
|
([#1578](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1578))
|
||||||
- Fix aiopg instrumentation to work with aiopg < 2.0.0
|
- Fix aiopg instrumentation to work with aiopg < 2.0.0
|
||||||
([#1473](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1473))
|
([#1473](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1473))
|
||||||
- `opentelemetry-instrumentation-aws-lambda` Adds an option to configure `disable_aws_context_propagation` by
|
- `opentelemetry-instrumentation-aws-lambda` Adds an option to configure `disable_aws_context_propagation` by
|
||||||
|
@ -69,8 +69,8 @@ class _OpenTelemetryServicerContext(grpc.ServicerContext):
|
|||||||
def __init__(self, servicer_context, active_span):
|
def __init__(self, servicer_context, active_span):
|
||||||
self._servicer_context = servicer_context
|
self._servicer_context = servicer_context
|
||||||
self._active_span = active_span
|
self._active_span = active_span
|
||||||
self.code = grpc.StatusCode.OK
|
self._code = grpc.StatusCode.OK
|
||||||
self.details = None
|
self._details = None
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
@ -119,8 +119,8 @@ class _OpenTelemetryServicerContext(grpc.ServicerContext):
|
|||||||
return self._servicer_context.trailing_metadata()
|
return self._servicer_context.trailing_metadata()
|
||||||
|
|
||||||
def abort(self, code, details):
|
def abort(self, code, details):
|
||||||
self.code = code
|
self._code = code
|
||||||
self.details = details
|
self._details = details
|
||||||
self._active_span.set_attribute(
|
self._active_span.set_attribute(
|
||||||
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
|
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
|
||||||
)
|
)
|
||||||
@ -135,10 +135,25 @@ class _OpenTelemetryServicerContext(grpc.ServicerContext):
|
|||||||
def abort_with_status(self, status):
|
def abort_with_status(self, status):
|
||||||
return self._servicer_context.abort_with_status(status)
|
return self._servicer_context.abort_with_status(status)
|
||||||
|
|
||||||
|
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 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_code(self, code):
|
def set_code(self, code):
|
||||||
self.code = code
|
self._code = code
|
||||||
# use details if we already have it, otherwise the status description
|
# use details if we already have it, otherwise the status description
|
||||||
details = self.details or code.value[1]
|
details = self._details or code.value[1]
|
||||||
self._active_span.set_attribute(
|
self._active_span.set_attribute(
|
||||||
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
|
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
|
||||||
)
|
)
|
||||||
@ -152,12 +167,12 @@ class _OpenTelemetryServicerContext(grpc.ServicerContext):
|
|||||||
return self._servicer_context.set_code(code)
|
return self._servicer_context.set_code(code)
|
||||||
|
|
||||||
def set_details(self, details):
|
def set_details(self, details):
|
||||||
self.details = details
|
self._details = details
|
||||||
if self.code != grpc.StatusCode.OK:
|
if self._code != grpc.StatusCode.OK:
|
||||||
self._active_span.set_status(
|
self._active_span.set_status(
|
||||||
Status(
|
Status(
|
||||||
status_code=StatusCode.ERROR,
|
status_code=StatusCode.ERROR,
|
||||||
description=f"{self.code}:{details}",
|
description=f"{self._code}:{details}",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return self._servicer_context.set_details(details)
|
return self._servicer_context.set_details(details)
|
||||||
|
Reference in New Issue
Block a user