Skip to content

feat(storage): record gcp.storage.client.request.active metric - #15227

Open
nidhiii-27 wants to merge 5 commits into
feat/storage-retries-metric-streams-httpfrom
feat/storage-request-active-metric
Open

nidhiii-27 wants to merge 5 commits into
feat/storage-retries-metric-streams-httpfrom
feat/storage-request-active-metric

Conversation

@nidhiii-27

@nidhiii-27 nidhiii-27 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stacked on #15226.

Adds Debug-tier gcp.storage.client.request.active (LongUpDownCounter, unit {request}) across both gRPC and HTTP/JSON transports to track in-flight requests with balanced +1/-1 accounting.

Changes

  • StorageClientMetrics: add recordRequestActive(Context, String operationName, long delta) emitting exact 6-key attributes (gcp.client.service, gcp.client.version, gcp.client.artifact, rpc.system.name, server.address, gcp.storage.operation).
  • StorageGrpcMetricsInterceptor: +1 in start(); the -1 goes through a per-call releaseActive() guard shared by the start() catch (when start throws) and onClose, so it is recorded exactly once.
  • StorageHttpMetricsInterceptor: reuse the single AtomicBoolean recorded (start CAS unconditional) for balanced +1/-1 with the operation captured at request start, normalize //batch/storage/v1 paths, and keep response.getRequest() == req guards.
  • spi/v1/HttpStorageRpc: batch parts use a client without the metrics interceptor (batchPartStorage) so they never leak request.active; the outer batch request is instrumented.
  • Unit tests in StorageClientMetricsTest, StorageGrpcMetricsInterceptorTest, and StorageHttpMetricsInterceptorTest.

[Generated-by: AI]

@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 tracking for active in-flight requests (request.active metric) across both gRPC and HTTP storage client interceptors, and refactors HttpStorageRpc to prevent double-counting of metrics during batch operations. The review feedback highlights two key improvements: first, enhancing encapsulation in StorageHttpMetricsInterceptor by removing an unnecessary public getter check for requestActive (as the record method already handles null checks); second, preventing a potential metric leak in StorageGrpcMetricsInterceptor by wrapping super.start in a try-catch block to ensure the active request count is decremented if starting the call throws an exception.

@nidhiii-27
nidhiii-27 marked this pull request as ready for review November 25, 2026 10:25
@nidhiii-27
nidhiii-27 requested review from a team as code owners November 25, 2026 10:25
…nerated-by: AI]

If the delegate ClientCall.start throws synchronously, gRPC never delivers
onClose to the listener, so the request.active +1 recorded in start was never
balanced. Decrement and rethrow in that case, and add a unit test.
…est [Generated-by: AI]

Child interceptors may decorate CallOptions (e.g. stream tracers) before
calling newCall, so stub newCall on the method only.

@cpriti-os cpriti-os 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.

Initial review

} catch (RuntimeException | Error e) {
// Per the ClientCall contract, onClose is not delivered when start throws, so balance
// the +1 here.
metrics.recordRequestActive(otelContext, operationName, -1L);

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.

Can we guard the -1 decrement with an AtomicBoolean so a synchronous onClose() throw inside super.start() can't decrement twice?

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.

Done in 88ec8a8: request.active is now released through a single per-call AtomicBoolean behind releaseActive(), shared by the start() catch and the onClose finally, so it can only be decremented once even if a downstream call closes the listener inline and then throws. This is different from the redundant onClose single-shot guard we avoided earlier: it dedups two distinct decrement sites (start-catch vs onClose), not repeated onClose calls. Added interceptCall_requestActive_releasesOnceWhenClosedInlineBeforeStartThrows; the guard is carried through the later onClose restructures in #15229 and #15231.

Co-authored by AI Agent

throwingHandlerTransport
.createRequestFactory(debugInterceptor)
.buildGetRequest(new GenericUrl("https://storage.googleapis.com/storage/v1/b/my-b"));
assertThrows(IOException.class, failingReq::execute);

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.

Can we assert the thrown message contains "refresh failed" (since HttpResponseException is also an IOException), check no retry was recorded, and add a throwing HttpResponseInterceptor case?

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.

Done in 88ec8a8: the test now checks the "refresh failed" message and that no retry was recorded, and adds a case where a delegate HttpResponseInterceptor throws (request.active still returns to 0).

Co-authored by AI Agent

storageBuilder.setRootUrl(host);
}
storage = storageBuilder.build();
batchPartStorage =

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.

Nit: Can we add a short comment on why batchPartStorage intentionally skips decorateWithMetrics?

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.

Done in 88ec8a8: added a comment. Batch parts are serialized into the outer multipart request and never run as their own HTTP exchange, so instrumenting them would increment request.active with no matching response; the outer batch request is instrumented instead.

Co-authored by AI Agent

}
startTimeNanos.set(clock.nanoTime());
recorded.set(false);
if (recorded.compareAndSet(true, false) && metrics.getRequestActive() != null) {

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.

Nit: Can we pull boolean started = recorded.compareAndSet(true, false); onto its own line so the side effect isn't coupled to short-circuit order with metrics.getRequestActive() != null?

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.

Done in 9853ac2: boolean started = recorded.compareAndSet(true, false); is on its own line and stays unconditional (the response/IO handlers rely on it), before the getRequestActive() check.

Co-authored by AI Agent

.build());
}

@Test

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.

Nit: Can we move the isMonotonic() == false check into inMemoryMetricReader_verifiesViewsAndBoundaries and drop this test, since StorageGrpcMetricsInterceptorTest already covers the exact 6 attributes and +1/-1 balance?

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.

Done in 9853ac2: moved the non-monotonic check into inMemoryMetricReader_verifiesViewsAndBoundaries and dropped recordRequestActive_recordsExactSixAttributes; the exact 6-key coverage stays in the gRPC/HTTP interceptor tests.

Co-authored by AI Agent

…rage-request-active-metric

Resolve conflicts in StorageHttpMetricsInterceptor by dropping the isRetry plumbing in favor of
the pending-retry hook, keeping the request.active +1/-1 lifecycle (with the start CAS on its
own line), and drop the duplicated StorageClientMetricsTest exact-attribute tests.

[Generated-by: AI]
…view feedback

- StorageGrpcMetricsInterceptor releases request.active through a per-call
  releaseActive() guard shared by the start() catch and onClose, so a downstream
  call that closes the listener inline and then throws decrements only once.
- Document why batch parts skip decorateWithMetrics in HttpStorageRpc.
- Strengthen the request.active HTTP test (handler failure message, no retry,
  throwing delegate HttpResponseInterceptor) and move the non-monotonic check
  into inMemoryMetricReader_verifiesViewsAndBoundaries.

[Generated-by: AI]
@nidhiii-27
nidhiii-27 requested a review from cpriti-os November 26, 2026 06:04

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.

2 participants