Skip to content

Commit 5dd106a

Browse files
author
Corvin Lasogga
committed
added support of code() and details()
1 parent c04959e commit 5dd106a

File tree

1 file changed

+34
-11
lines changed
  • instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc

1 file changed

+34
-11
lines changed

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_server.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class _OpenTelemetryServicerContext(grpc.ServicerContext):
6868
def __init__(self, servicer_context, active_span):
6969
self._servicer_context = servicer_context
7070
self._active_span = active_span
71-
self.code = grpc.StatusCode.OK
72-
self.details = None
71+
self._code = grpc.StatusCode.OK
72+
self._details = None
7373
super().__init__()
7474

7575
def __getattr__(self, attr):
@@ -118,45 +118,68 @@ def trailing_metadata(self):
118118
return self._servicer_context.trailing_metadata()
119119

120120
def abort(self, code, details):
121-
self.code = code
122-
self.details = details
121+
if not hasattr(self._servicer_context, "abort"):
122+
raise RuntimeError(
123+
"abort() is not supported with the installed version of grpcio"
124+
)
125+
self._code = code
126+
self._details = details
123127
self._active_span.set_attribute(
124128
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
125129
)
126130
self._active_span.set_status(
127131
Status(
128132
status_code=StatusCode.ERROR,
129-
description=f"{code}:{details}",
133+
description=f"{code}: {details}",
130134
)
131135
)
132136
return self._servicer_context.abort(code, details)
133137

134138
def abort_with_status(self, status):
139+
if not hasattr(self._servicer_context, "abort_with_status"):
140+
raise RuntimeError(
141+
"abort_with_status() is not supported with the installed version of grpcio"
142+
)
135143
return self._servicer_context.abort_with_status(status)
136144

145+
def code(self):
146+
if not hasattr(self._servicer_context, "code"):
147+
raise RuntimeError(
148+
"code() is not supported with the installed version of grpcio"
149+
)
150+
return self._servicer_context.code()
151+
137152
def set_code(self, code):
138-
self.code = code
153+
self._code = code
139154
# use details if we already have it, otherwise the status description
140-
details = self.details or code.value[1]
155+
details = self._details or code.value[1]
141156
self._active_span.set_attribute(
142157
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
143158
)
144159
if code != grpc.StatusCode.OK:
145160
self._active_span.set_status(
146161
Status(
147162
status_code=StatusCode.ERROR,
148-
description=f"{code}:{details}",
163+
description=f"{code}: {details}",
149164
)
150165
)
151166
return self._servicer_context.set_code(code)
152167

168+
def details(self):
169+
if not hasattr(self._servicer_context, "details"):
170+
raise RuntimeError(
171+
"details() is not supported with the installed version of "
172+
"grpcio"
173+
)
174+
return self._servicer_context.details()
175+
153176
def set_details(self, details):
154-
self.details = details
155-
if self.code != grpc.StatusCode.OK:
177+
self._details = details
178+
if self._code != grpc.StatusCode.OK:
156179
self._active_span.set_status(
157180
Status(
158181
status_code=StatusCode.ERROR,
159-
description=f"{self.code}:{details}",
182+
description=f"{self._code}: {details}",
160183
)
161184
)
162185
return self._servicer_context.set_details(details)

0 commit comments

Comments
 (0)