Repository navigation
refactor(api_core): centralize RPC identity and HTTP status helpers in _observability - #19008
chalmerlowe wants to merge 3 commits into
Conversation
…ralize RPC identity helpers
There was a problem hiding this comment.
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.
| 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 | ||
| ) |
There was a problem hiding this comment.
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.
| 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 | |
| ) |
There was a problem hiding this comment.
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.
…_resolve_rpc_system and refine docstrings
Problem / Context
To support structured telemetry and logging across synchronous and asynchronous gRPC and REST call paths, both
gapic_v1.methodand_observabilityneed shared helpers for resolving RPC service/method identity, normalizing transport system names, and extracting HTTP status codes.Bug: b/479827845
Solution
google/api_core/_observability.pyand delegate to them fromgoogle/api_core/gapic_v1/method.py.headersannotations toMutableMapping[str, Any] | Nonefor case-insensitive header mappings and clean up module-level constants and PEP 604 docstring types.tests/unit/test_observability.py.