Skip to content

Commit 8775743

Browse files
committed
refactor(observability): simplify response hook status check to idiomatic is_recording and span.status
1 parent 2e7fb2c commit 8775743

2 files changed

Lines changed: 1 addition & 29 deletions

File tree

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from __future__ import annotations
2020

2121
import urllib.parse
22-
from collections.abc import Mapping
2322
from typing import TYPE_CHECKING, Any, Callable, Sequence
2423

2524
from google.api_core import _feature_gating_helpers
@@ -189,11 +188,7 @@ def _grpc_client_response_hook(span: Any, response: Any) -> None:
189188
span: The OpenTelemetry span.
190189
response: The gRPC response object or details.
191190
"""
192-
if span is None or not hasattr(span, "set_attribute"):
193-
return
194-
195-
is_recording = getattr(span, "is_recording", None)
196-
if callable(is_recording) and not is_recording():
191+
if not span.is_recording():
197192
return
198193

199194
# Verify the RPC succeeded before recording the OK response status.
@@ -207,13 +202,6 @@ def _grpc_client_response_hook(span: Any, response: Any) -> None:
207202
):
208203
return
209204

210-
# Confirm the gRPC status code attribute indicates success (0 is StatusCode.OK).
211-
attrs = getattr(span, "attributes", None)
212-
if not isinstance(attrs, Mapping):
213-
attrs = getattr(span, "_attributes", None)
214-
if isinstance(attrs, Mapping) and attrs.get("rpc.grpc.status_code", 0) != 0:
215-
return
216-
217205
span.set_attribute("rpc.response.status_code", "OK")
218206

219207

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -561,19 +561,3 @@ def test_grpc_client_response_hook_error_status_value():
561561
mock_span.status.status_code.value = 2
562562
_observability._grpc_client_response_hook(mock_span, mock.Mock())
563563
mock_span.set_attribute.assert_not_called()
564-
565-
566-
def test_grpc_client_response_hook_error_attribute():
567-
"""Proves that _grpc_client_response_hook skips spans with non-zero rpc.grpc.status_code."""
568-
mock_span = mock.Mock()
569-
mock_span.is_recording.return_value = True
570-
mock_span.status = None
571-
mock_span._attributes = {"rpc.grpc.status_code": 14}
572-
_observability._grpc_client_response_hook(mock_span, mock.Mock())
573-
mock_span.set_attribute.assert_not_called()
574-
575-
576-
def test_grpc_client_response_hook_none_or_missing_set_attribute():
577-
"""Proves that _grpc_client_response_hook handles None or invalid span gracefully."""
578-
_observability._grpc_client_response_hook(None, mock.Mock())
579-
_observability._grpc_client_response_hook(object(), mock.Mock())

0 commit comments

Comments
 (0)