Skip to content

feat(google-api-core): extend ErrorInfo dict accessors, centralize status code extraction, and update StructuredLogFormatter - #18884

Merged
chalmerlowe merged 1 commit into
mainfrom
feat/otel-logging-m1-foundation
Nov 4, 2026
Merged

chalmerlowe merged 1 commit into
mainfrom
feat/otel-logging-m1-foundation

Conversation

@chalmerlowe

@chalmerlowe chalmerlowe commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Context

Internal issue: b/479827845

To support uniform structured error logging and OpenTelemetry telemetry across both gRPC and REST transports in google-api-core, several foundational utilities need to handle both transport formats consistently:

  1. REST ErrorInfo Accessors: GoogleAPICallError.reason, .domain, and .metadata previously assumed self._error_info was always a protobuf ErrorInfo message (as returned by gRPC google.rpc.status_pb2.Status). For REST JSON error payloads, ErrorInfo entries in error["details"] are parsed as standard Python dict objects, causing attribute access (self._error_info.reason) to fail or return None.
  2. Duplicate Status Code Extraction: Status code normalization across GoogleAPICallError and raw grpc.RpcError instances (_extract_status_code) lived inside google.api_core.gapic_v1.method, preventing reuse by transport-level observability helpers.
  3. Structured Log Field Allowlist: StructuredLogFormatter only recognized legacy camelCase fields and matched fields via exact set membership (in _recognized_logging_fields). It did not unpack record.json_fields or support OpenTelemetry semantic conventions (such as rpc.*, http.*, url.*, server.*, error.type, exception.*, and dynamic gcp.errors.metadata.* keys).

Solution

  • google/api_core/exceptions.py:
    • Updated GoogleAPICallError.reason, .domain, and .metadata to inspect isinstance(self._error_info, dict) first before falling back to protobuf attribute access.
    • Moved _extract_status_code(exc, default=None) to google.api_core.exceptions as the single source of truth so both gapic_v1.method and transport observability helpers share one status code normalization implementation.
  • google/api_core/gapic_v1/method.py:
    • Updated callers to invoke exceptions._extract_status_code directly and removed duplicate status code extraction logic and unit tests from method.py / test_method.py.
  • google/api_core/client_logging.py:
    • Extended _recognized_logging_fields with OpenTelemetry semantic convention attributes, Google Cloud error domain/metadata fields (gcp.errors.domain, gcp.errors.metadata, gcp.errors.metadata.*), and trace correlation keys (logging.googleapis.com/trace, logging.googleapis.com/spanId, logging.googleapis.com/trace_sampled).
    • Pre-compiled _EXACT_FIELDS (frozenset) and _PREFIX_FIELDS (tuple) at module import time and updated StructuredLogFormatter.format to extract exact fields via getattr() (supporting @property and class-level attributes), extract dynamic wildcard prefix fields via vars(record), and unpack record.json_fields last so explicit user-provided structured fields take precedence.
  • Unit Tests:
    • Added parametrized unit tests in tests/unit/test_exceptions.py and tests/unit/test_client_logging.py achieving 100% line and branch coverage on modified modules.
    • See CI results for test verification.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.