Repository navigation
feat(google-api-core): extend ErrorInfo dict accessors, centralize status code extraction, and update StructuredLogFormatter - #18884
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:ErrorInfoAccessors:GoogleAPICallError.reason,.domain, and.metadatapreviously assumedself._error_infowas always a protobufErrorInfomessage (as returned by gRPCgoogle.rpc.status_pb2.Status). For REST JSON error payloads,ErrorInfoentries inerror["details"]are parsed as standard Pythondictobjects, causing attribute access (self._error_info.reason) to fail or returnNone.GoogleAPICallErrorand rawgrpc.RpcErrorinstances (_extract_status_code) lived insidegoogle.api_core.gapic_v1.method, preventing reuse by transport-level observability helpers.StructuredLogFormatteronly recognized legacy camelCase fields and matched fields via exact set membership (in _recognized_logging_fields). It did not unpackrecord.json_fieldsor support OpenTelemetry semantic conventions (such asrpc.*,http.*,url.*,server.*,error.type,exception.*, and dynamicgcp.errors.metadata.*keys).Solution
google/api_core/exceptions.py:GoogleAPICallError.reason,.domain, and.metadatato inspectisinstance(self._error_info, dict)first before falling back to protobuf attribute access._extract_status_code(exc, default=None)togoogle.api_core.exceptionsas the single source of truth so bothgapic_v1.methodand transport observability helpers share one status code normalization implementation.google/api_core/gapic_v1/method.py:exceptions._extract_status_codedirectly and removed duplicate status code extraction logic and unit tests frommethod.py/test_method.py.google/api_core/client_logging.py:_recognized_logging_fieldswith 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)._EXACT_FIELDS(frozenset) and_PREFIX_FIELDS(tuple) at module import time and updatedStructuredLogFormatter.formatto extract exact fields viagetattr()(supporting@propertyand class-level attributes), extract dynamic wildcard prefix fields viavars(record), and unpackrecord.json_fieldslast so explicit user-provided structured fields take precedence.tests/unit/test_exceptions.pyandtests/unit/test_client_logging.pyachieving 100% line and branch coverage on modified modules.