Repository navigation
feat(storage): record gcp.storage.client.ttfb for HTTP unary and upload requests - #15066
nidhiii-27 wants to merge 1 commit into
Conversation
…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]
There was a problem hiding this comment.
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.
| // may append query parameters (e.g. alt=media, uploadType) after buildRequest(). | ||
| StorageMetricsContext ctx = StorageMetricsContext.current(); | ||
| Context otelContext = ctx != null ? ctx.getOtelContext() : Context.current(); | ||
| metrics.recordTtfb( |
There was a problem hiding this comment.
For HTTP uploads, measuring TTFB from startTimeNanos includes the time spent writing the request body. Should we wrap HttpContent to record the timestamp after writeTo finishes and measure upload TTFB from there instead?
| StorageClientMetrics.NETWORK_PATH_CLOUDPATH); | ||
| } | ||
|
|
||
| private void recordDuration( |
There was a problem hiding this comment.
recordDuration is just a one-line wrapper around metrics.recordHttpRequestDuration now. Can we inline it at the two call sites and remove this helper?
Summary
Follow-up to #14745 (#14745 (comment)) extending
gcp.storage.client.ttfbinstrumentation inStorageHttpMetricsInterceptorbeyond media downloads so all HTTP responses (unary metadata/control requests, direct/multipart uploads, resumable upload sessionopen/chunkPUT/statusqueryrequests, and outer/batch/storage/v1requests) record Time To First Byte when HTTP response headers arrive.Key Changes
StorageMetricsContext&Retrying:StorageMetricsContext.makeCurrent()andStorageMetricsContext.current()combining aThreadLocal<StorageMetricsContext>withotelContext.makeCurrent()in a singleScope(preservingContext.current() == otelContextreference identity and cleaning up theThreadLocalon scope close).metricsContext.makeCurrent()inRetrying.DefaultRetrier.runandRetrying.AttemptOnceRetrier.runso canonicalgcp.storage.operationnames (e.g.,"UpdateBucket"/"UpdateObject"whenStorageImpl.updateexecutes an HTTPGETwith no modified fields) and activeotelContexttrace exemplars propagate automatically toStorageHttpMetricsInterceptor.StorageHttpMetricsInterceptor:isMediaDownloadRequest-only filter onmetrics.recordTtfb(...)inrecordResponse(...), computingdurationSecondsandextractUrlTemplate(request.getUrl())once per response across bothhttp.client.request.durationandgcp.storage.client.ttfb.gcp.storage.operationfromStorageMetricsContext.current()first, falling back toisMediaDownloadRequest("ReadObject"),isMediaUploadRequest("WriteObject"), andROUTE_OPERATIONS(honoringX-HTTP-Method-OverrideforNetHttpTransportPATCHand >2,048-charGETrequests) for context-free transport calls (PCU part hooks,BlobWriteSession,StorageBatch).setIOExceptionHandlerrecordinghttp.client.request.durationonly when no HTTP response is received.StorageMetricsContextTest,RetryingMetricsTest, andStorageHttpMetricsInterceptorTestwith nestedmakeCurrent()scope tests, table-drivenisMediaUploadRequestandresolveOperationName(includingX-HTTP-Method-Overrideand context override) cases, status-code/batch/retry TTFB assertions, and an end-to-endStorageImpl+MockHttpTransporttest 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).