Skip to content

feat(storage): record gcp.storage.client.ttfb for HTTP unary and upload requests - #15066

Open
nidhiii-27 wants to merge 1 commit into
feat/storage-response-body-sizefrom
feat/storage-http-ttfb-unary-upload
Open

nidhiii-27 wants to merge 1 commit into
feat/storage-response-body-sizefrom
feat/storage-http-ttfb-unary-upload

Conversation

@nidhiii-27

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #14745 (#14745 (comment)) extending gcp.storage.client.ttfb instrumentation in StorageHttpMetricsInterceptor beyond media downloads so all HTTP responses (unary metadata/control requests, direct/multipart uploads, resumable upload session open/chunk PUT/status query requests, and outer /batch/storage/v1 requests) record Time To First Byte when HTTP response headers arrive.

Key Changes

  • StorageMetricsContext & Retrying:
    • Added package-private StorageMetricsContext.makeCurrent() and StorageMetricsContext.current() combining a ThreadLocal<StorageMetricsContext> with otelContext.makeCurrent() in a single Scope (preserving Context.current() == otelContext reference identity and cleaning up the ThreadLocal on scope close).
    • Activated metricsContext.makeCurrent() in Retrying.DefaultRetrier.run and Retrying.AttemptOnceRetrier.run so canonical gcp.storage.operation names (e.g., "UpdateBucket" / "UpdateObject" when StorageImpl.update executes an HTTP GET with no modified fields) and active otelContext trace exemplars propagate automatically to StorageHttpMetricsInterceptor.
  • StorageHttpMetricsInterceptor:
    • Removed the isMediaDownloadRequest-only filter on metrics.recordTtfb(...) in recordResponse(...), computing durationSeconds and extractUrlTemplate(request.getUrl()) once per response across both http.client.request.duration and gcp.storage.client.ttfb.
    • Resolved gcp.storage.operation from StorageMetricsContext.current() first, falling back to isMediaDownloadRequest ("ReadObject"), isMediaUploadRequest ("WriteObject"), and ROUTE_OPERATIONS (honoring X-HTTP-Method-Override for NetHttpTransport PATCH and >2,048-char GET requests) for context-free transport calls (PCU part hooks, BlobWriteSession, StorageBatch).
    • Kept setIOExceptionHandler recording http.client.request.duration only when no HTTP response is received.
  • Tests:
    • Extended StorageMetricsContextTest, RetryingMetricsTest, and StorageHttpMetricsInterceptorTest with nested makeCurrent() scope tests, table-driven isMediaUploadRequest and resolveOperationName (including X-HTTP-Method-Override and context override) cases, status-code/batch/retry TTFB assertions, and an end-to-end StorageImpl + MockHttpTransport test covering unary, direct multipart upload, resumable multi-chunk upload (308 + 200), and trace exemplar propagation.

Stack Base

Stacked on #14747 (feat/storage-response-body-size).

…ad requests

Extend HTTP `gcp.storage.client.ttfb` instrumentation in `StorageHttpMetricsInterceptor` beyond media downloads so all HTTP responses (unary metadata/control operations, direct/multipart uploads, resumable upload session open/chunk/query requests, and outer `/batch/storage/v1` requests) record Time To First Byte when response headers arrive:

- Add package-private `StorageMetricsContext.makeCurrent()` / `StorageMetricsContext.current()` combining a `ThreadLocal<StorageMetricsContext>` with `otelContext.makeCurrent()` in a single `Scope`, and activate it in `Retrying.DefaultRetrier.run` and `Retrying.AttemptOnceRetrier.run`.
- Resolve `gcp.storage.operation` and `otelContext` in `StorageHttpMetricsInterceptor` from `StorageMetricsContext.current()` first, falling back to `isMediaDownloadRequest`, `isMediaUploadRequest`, and `ROUTE_OPERATIONS` (honoring `X-HTTP-Method-Override` for `NetHttpTransport` `PATCH` and long `GET` requests) for context-free transport calls (PCU part hooks, `BlobWriteSession`, `StorageBatch`).
- Keep `setIOExceptionHandler` recording `http.client.request.duration` only when no HTTP response is received.

[Generated-by: AI]
@nidhiii-27 nidhiii-27 added the ai-generated Pull Request generated by AI label Nov 18, 2026

@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 introduces thread-local propagation of StorageMetricsContext to allow resolving operation names for HTTP metrics (such as TTFB) across all HTTP operations. It adds a ThreadLocal storage to StorageMetricsContext and implements makeCurrent() to manage the context scope. Additionally, StorageHttpMetricsInterceptor is updated to resolve operation names dynamically using either the active StorageMetricsContext or a route-based fallback map (ROUTE_OPERATIONS), with support for media uploads and X-HTTP-Method-Override. Extensive unit and integration tests are added to verify these changes. There are no review comments, and I have no feedback to provide.

@nidhiii-27
nidhiii-27 marked this pull request as ready for review November 19, 2026 06:26
@nidhiii-27
nidhiii-27 requested review from a team as code owners November 19, 2026 06:26

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

ai-generated Pull Request generated by AI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant