Repository navigation
feat(api_core): add HTTP transport attempt logging in _TraceContext - #19011
Draft
chalmerlowe wants to merge 1 commit into
Draft
chalmerlowe wants to merge 1 commit into
chalmerlowe wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces HTTP attempt logging for failed requests and dispatch exceptions within _TraceContext by adding the _log_http_attempt helper and updating record_response, record_error, and __exit__ methods, alongside adding corresponding unit tests. The review feedback suggests improving _log_http_attempt by logging exceptions instead of silencing them, avoiding defensive getattr checks for attributes initialized in __init__, and preventing duplicate logs.
Comment on lines
+1390
to
+1414
| try: | ||
| resolved_url = ( | ||
| getattr(self._request, "url", None) | ||
| if self._request is not None | ||
| else self._url | ||
| ) | ||
| resolved_method = ( | ||
| getattr(self._request, "method", None) | ||
| if self._request is not None | ||
| else self._method | ||
| ) | ||
| log_transport_attempt( | ||
| span=self._span, | ||
| target=resolved_url, | ||
| status_code=status_code, | ||
| kind="http", | ||
| http_method=resolved_method, | ||
| exc=exc, | ||
| client_options=self._client_options, | ||
| status_message=status_message, | ||
| ) | ||
| # Intentionally catch broad Exception so telemetry logging fails open | ||
| # and never disrupts the caller's RPC flow. | ||
| except Exception: | ||
| pass |
Contributor
There was a problem hiding this comment.
There are two potential issues in _log_http_attempt:
- Double-logging / Duplicate attempts: Tracking the attempt status is a good approach to prevent duplicate logs.
- Silent failures: The use of a broad
except Exception: passblock masks potential issues. Please log the exception usinglogger.warninginstead of silencing it. Additionally, avoid usinggetattrfor attributes that should be initialized in__init__to keep the code clean and explicit.
if getattr(self, "_attempt_logged", False):
return
self._attempt_logged = True
try:
request = self._request
resolved_url = getattr(request, "url", None) if request is not None else self._url
resolved_method = getattr(request, "method", None) if request is not None else self._method
log_transport_attempt(
span=self._span,
target=resolved_url,
status_code=status_code,
kind="http",
http_method=resolved_method,
exc=exc,
client_options=self._client_options,
status_message=status_message,
)
except Exception as e:
logger.warning("Telemetry logging failed: %s", e)References
- Do not use defensive getattr checks for attributes that are guaranteed to be initialized in the class's init method.
- Avoid broad except Exception blocks that silently return None. Instead, log the exception to aid in debugging.
Contributor
Author
There was a problem hiding this comment.
Thanks for the review!
- In GAPIC REST transports (
_shared_macros.j2),trace_http_requestonly wraps the rawsessiondispatch andrecord_response(response), while HTTP status exceptions are raised outside thewithblock after it exits. As a result,record_responseand__exit__(record_error) are mutually exclusive in transport execution and a deduplication flag is not required here. except Exception: passis intentional and matches the fail-open observability contract across_observability.py(log_transport_attempt,record_response,record_error, and the gRPC interceptors) so internal telemetry failures never emit unexpected warnings into user logs.self._request,self._url, andself._methodare accessed directly onself;getattris only used on the externalself._requestobject because caller-supplied request objects are not guaranteed to define.urlor.method.
chalmerlowe
force-pushed
the
feat/otel-logging-m3-s3-grpc-attempt-logging
branch
from
November 6, 2026 16:17
0b90e49 to
42a6423
Compare
chalmerlowe
force-pushed
the
feat/otel-logging-m3-s4-http-attempt-logging
branch
from
November 6, 2026 16:17
fd80c07 to
cfdc057
Compare
chalmerlowe
force-pushed
the
feat/otel-logging-m3-s4-http-attempt-logging
branch
from
November 6, 2026 16:26
cfdc057 to
5aea0b9
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
To capture failed HTTP wire attempts (status codes
>= 400and transport dispatch exceptions) across synchronous and asynchronous REST transports — including when OpenTelemetry tracing is disabled (self._span is None) —_TraceContextingoogle.api_core._observabilityneeds to invokelog_transport_attemptfromrecord_response,record_error, and__exit__.Bug: b/479827845
Solution
_TraceContext._log_http_attemptingoogle/api_core/_observability.pyto emit fail-openDEBUGtransport attempt logs vialog_transport_attempt._TraceContext.record_responseto log HTTP error responses (code_int >= 400) before checkingself._span, ensuring HTTP attempt logs are emitted even when OpenTelemetry tracing is disabled._TraceContext.record_errorand_TraceContext.__exit__to log unhandled HTTP dispatch exceptions whether or not an active OpenTelemetry span is present.test_trace_context_http_logging_on_error_status_and_exceptionintests/unit/test_observability.py.Verification
Relying on GitHub Actions CI for full matrix verification, alongside local unit test (
unit-3.14) and lint verification.