Skip to content

refactor(api_core): centralize RPC identity and HTTP status helpers in _observability - #19008

Draft
chalmerlowe wants to merge 3 commits into
feat/otel-logging-m2-buildersfrom
refactor/otel-logging-m3-s1-types-identity
Draft

chalmerlowe wants to merge 3 commits into
feat/otel-logging-m2-buildersfrom
refactor/otel-logging-m3-s1-types-identity

Conversation

@chalmerlowe

@chalmerlowe chalmerlowe commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Context

To support structured telemetry and logging across synchronous and asynchronous gRPC and REST call paths, both gapic_v1.method and _observability need shared helpers for resolving RPC service/method identity, normalizing transport system names, and extracting HTTP status codes.

Bug: b/479827845

Solution

  • Move RPC identity extraction, transport kind normalization, HTTP status validation, and gRPC OpenTelemetry hook setup into google/api_core/_observability.py and delegate to them from google/api_core/gapic_v1/method.py.
  • Broaden HTTP headers annotations to MutableMapping[str, Any] | None for case-insensitive header mappings and clean up module-level constants and PEP 604 docstring types.
  • Add parameterized unit tests covering RPC identity parsing, HTTP status extraction, and transport system normalization in tests/unit/test_observability.py.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the observability module by extracting helper functions for HTTP status code extraction, RPC identity resolution, and OpenTelemetry gRPC hook arguments building. It also modernizes type annotations and removes duplicate code across the codebase. One improvement was suggested to restore a null check on http_response before evaluating its attributes and extracting its status code to avoid unnecessary lookups.

Comment on lines 282 to 286
http_status = (
getattr(http_response, "status_code", getattr(http_response, "status", None))
if http_response is not None and not hasattr(http_response, "trailing_metadata")
_extract_http_status_code(http_response)
if not hasattr(http_response, "trailing_metadata")
else None
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If http_response is None, evaluating not hasattr(http_response, "trailing_metadata") will check hasattr(None, "trailing_metadata") (which is False) and then call _extract_http_status_code(None). To avoid unnecessary attribute lookups and function calls on None, we should explicitly check if http_response is not None first, matching the original behavior.

Suggested change
http_status = (
getattr(http_response, "status_code", getattr(http_response, "status", None))
if http_response is not None and not hasattr(http_response, "trailing_metadata")
_extract_http_status_code(http_response)
if not hasattr(http_response, "trailing_metadata")
else None
)
http_status = (
_extract_http_status_code(http_response)
if http_response is not None and not hasattr(http_response, "trailing_metadata")
else None
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored the http_response is not None guard before checking trailing_metadata and calling _extract_http_status_code(http_response) to avoid unnecessary attribute lookups when response is None.

@chalmerlowe chalmerlowe changed the title refactor(api_core): modernize observability type annotations and centralize RPC identity helpers refactor(api_core): centralize RPC identity and HTTP status helpers in _observability Nov 10, 2026

This branch has not been deployed

No deployments
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.

1 participant