Skip to content

Commit ec185d6

Browse files
committed
refactor(observability): simplify response hook to record OK on successful RPCs
1 parent 9f491fa commit ec185d6

2 files changed

Lines changed: 5 additions & 69 deletions

File tree

packages/google-api-core/google/api_core/_observability.py

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -175,69 +175,19 @@ def client_request_hook(span: Any, request: Any) -> None:
175175

176176
_grpc_client_request_hook = _make_grpc_client_request_hook()
177177

178-
# Mapping of standard gRPC integer status codes to their canonical status name strings.
179-
# Used when stock gRPC wire spans encounter errors, guaranteeing mapping even in environments
180-
# where the optional `grpc` package is not installed (e.g. REST-only environments).
181-
_GRPC_INT_STATUS_CODE_TO_NAME = {
182-
0: "OK",
183-
1: "CANCELLED",
184-
2: "UNKNOWN",
185-
3: "INVALID_ARGUMENT",
186-
4: "DEADLINE_EXCEEDED",
187-
5: "NOT_FOUND",
188-
6: "ALREADY_EXISTS",
189-
7: "PERMISSION_DENIED",
190-
8: "RESOURCE_EXHAUSTED",
191-
9: "FAILED_PRECONDITION",
192-
10: "ABORTED",
193-
11: "OUT_OF_RANGE",
194-
12: "UNIMPLEMENTED",
195-
13: "INTERNAL",
196-
14: "UNAVAILABLE",
197-
15: "DATA_LOSS",
198-
16: "UNAUTHENTICATED",
199-
}
200-
201178

202179
def _grpc_client_response_hook(span: Any, response: Any) -> None:
203180
"""OpenTelemetry gRPC client response hook to record response status code.
204181
182+
Note: Upstream OpenTelemetry gRPC instrumentation only invokes this response_hook
183+
on successful RPC invocations. Failed RPCs raise an exception before this hook is reached.
184+
205185
Args:
206186
span: The OpenTelemetry span.
207187
response: The gRPC response object or details.
208188
"""
209-
if span is None or not hasattr(span, "set_attribute"):
210-
return
211-
212-
status = getattr(span, "status", None)
213-
status_code = getattr(status, "status_code", None)
214-
try:
215-
from opentelemetry.trace.status import StatusCode
216-
217-
if status_code == StatusCode.ERROR:
218-
span_attrs = (
219-
getattr(span, "attributes", None)
220-
or getattr(span, "_attributes", None)
221-
or {}
222-
)
223-
grpc_code = span_attrs.get("rpc.grpc.status_code")
224-
if grpc_code is not None:
225-
from google.api_core import exceptions
226-
227-
name = None
228-
if grpc_code in exceptions._INT_TO_GRPC_CODE:
229-
name = exceptions._INT_TO_GRPC_CODE[grpc_code].name
230-
elif grpc_code in _GRPC_INT_STATUS_CODE_TO_NAME:
231-
name = _GRPC_INT_STATUS_CODE_TO_NAME[grpc_code]
232-
if name:
233-
span.set_attribute("rpc.response.status_code", name)
234-
return
235-
span.set_attribute("rpc.response.status_code", "ERROR")
236-
return
237-
except Exception:
238-
pass
239-
240-
span.set_attribute("rpc.response.status_code", "OK")
189+
if span is not None and hasattr(span, "set_attribute"):
190+
span.set_attribute("rpc.response.status_code", "OK")
241191

242192

243193
def _get_tracer_provider(

packages/google-api-core/tests/unit/test_observability.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -497,20 +497,6 @@ def test_grpc_client_response_hook_success():
497497
mock_span.set_attribute.assert_called_once_with("rpc.response.status_code", "OK")
498498

499499

500-
def test_grpc_client_response_hook_error_mapped():
501-
"""Proves that _grpc_client_response_hook maps status code when span has error status."""
502-
from opentelemetry.trace.status import StatusCode
503-
504-
mock_span = mock.Mock()
505-
mock_span.status.status_code = StatusCode.ERROR
506-
mock_span.attributes = {"rpc.grpc.status_code": 5}
507-
508-
_observability._grpc_client_response_hook(mock_span, None)
509-
mock_span.set_attribute.assert_called_once_with(
510-
"rpc.response.status_code", "NOT_FOUND"
511-
)
512-
513-
514500
def test_grpc_client_response_hook_none_or_missing_set_attribute():
515501
"""Proves that _grpc_client_response_hook handles None or invalid span gracefully."""
516502
_observability._grpc_client_response_hook(None, mock.Mock())

0 commit comments

Comments
 (0)