Skip to content

Commit 2460284

Browse files
committed
refactor(core): remove duplicate error attribute extraction in favor of method spans
1 parent 2b5161d commit 2460284

2 files changed

Lines changed: 0 additions & 114 deletions

File tree

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -127,43 +127,6 @@ def _extract_grpc_request_attributes(request: Any) -> dict[str, Any]:
127127
return attrs
128128

129129

130-
def _extract_error_attributes(exc: Any) -> dict[str, Any]:
131-
"""Extracts gcp.errors.domain, gcp.errors.metadata.*, and error.type from an exception or ErrorInfo.
132-
133-
Args:
134-
exc: An exception (such as GoogleAPICallError or grpc.RpcError) or ErrorInfo object.
135-
136-
Returns:
137-
dict[str, Any]: Extracted error attributes.
138-
"""
139-
attrs: dict[str, Any] = {}
140-
if exc is None:
141-
return attrs
142-
143-
error_info = getattr(exc, "error_info", None)
144-
if error_info is None and hasattr(exc, "trailing_metadata"):
145-
try:
146-
from google.api_core import exceptions
147-
148-
_, error_info = exceptions._parse_grpc_error_details(exc)
149-
except Exception:
150-
pass
151-
152-
if error_info is not None:
153-
domain = getattr(error_info, "domain", None)
154-
if domain and isinstance(domain, str):
155-
attrs["gcp.errors.domain"] = domain
156-
reason = getattr(error_info, "reason", None)
157-
if reason and isinstance(reason, str):
158-
attrs["error.type"] = reason
159-
metadata = getattr(error_info, "metadata", None)
160-
if metadata and hasattr(metadata, "items"):
161-
for k, v in metadata.items():
162-
attrs[f"gcp.errors.metadata.{k}"] = str(v)
163-
164-
return attrs
165-
166-
167130
def _make_grpc_client_request_hook(
168131
endpoint_attrs: dict[str, Any] | None = None,
169132
) -> Callable[[Any, Any], None]:

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

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -416,83 +416,6 @@ def test_grpc_client_request_hook():
416416
mock_span_custom.update_name.assert_not_called()
417417

418418

419-
def test_extract_error_attributes_none():
420-
"""Proves that _extract_error_attributes returns an empty dict when exception is None."""
421-
assert _observability._extract_error_attributes(None) == {}
422-
423-
424-
def test_extract_error_attributes_standard_exception():
425-
"""Proves that _extract_error_attributes returns an empty dict for standard exceptions without ErrorInfo."""
426-
assert (
427-
_observability._extract_error_attributes(ValueError("unexpected error")) == {}
428-
)
429-
430-
431-
def test_extract_error_attributes_with_error_info():
432-
"""Proves that _extract_error_attributes extracts domain, error.type, and metadata from ErrorInfo."""
433-
error_info = types.SimpleNamespace(
434-
domain="googleapis.com",
435-
reason="SERVICE_DISABLED",
436-
metadata={
437-
"service": "secretmanager.googleapis.com",
438-
"consumer": "projects/123",
439-
},
440-
)
441-
exc = types.SimpleNamespace(error_info=error_info)
442-
attrs = _observability._extract_error_attributes(exc)
443-
assert attrs == {
444-
"gcp.errors.domain": "googleapis.com",
445-
"error.type": "SERVICE_DISABLED",
446-
"gcp.errors.metadata.service": "secretmanager.googleapis.com",
447-
"gcp.errors.metadata.consumer": "projects/123",
448-
}
449-
450-
451-
def test_extract_error_attributes_from_grpc_trailing_metadata(monkeypatch):
452-
"""Proves that _extract_error_attributes parses error_info from gRPC trailing metadata."""
453-
from google.api_core import exceptions
454-
455-
mock_exc = mock.Mock()
456-
mock_exc.error_info = None
457-
mock_exc.trailing_metadata = mock.Mock()
458-
459-
parsed_error_info = types.SimpleNamespace(
460-
domain="googleapis.com",
461-
reason="RESOURCE_EXHAUSTED",
462-
metadata={"quota_limit": "100"},
463-
)
464-
465-
monkeypatch.setattr(
466-
exceptions,
467-
"_parse_grpc_error_details",
468-
mock.Mock(return_value=(None, parsed_error_info)),
469-
)
470-
471-
attrs = _observability._extract_error_attributes(mock_exc)
472-
assert attrs == {
473-
"gcp.errors.domain": "googleapis.com",
474-
"error.type": "RESOURCE_EXHAUSTED",
475-
"gcp.errors.metadata.quota_limit": "100",
476-
}
477-
478-
479-
def test_extract_error_attributes_trailing_metadata_failure(monkeypatch):
480-
"""Proves that _extract_error_attributes safely handles exceptions during trailing metadata parsing."""
481-
from google.api_core import exceptions
482-
483-
mock_exc = mock.Mock()
484-
mock_exc.error_info = None
485-
mock_exc.trailing_metadata = mock.Mock()
486-
487-
monkeypatch.setattr(
488-
exceptions,
489-
"_parse_grpc_error_details",
490-
mock.Mock(side_effect=RuntimeError("Parse failed")),
491-
)
492-
493-
assert _observability._extract_error_attributes(mock_exc) == {}
494-
495-
496419
def test_get_otel_interceptor_with_api_endpoint(monkeypatch):
497420
"""Proves that get_otel_interceptor injects server.address, server.port, and url.domain when api_endpoint is set."""
498421
monkeypatch.setenv("GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED", "true")

0 commit comments

Comments
 (0)