Repository navigation
feat(storage): add gcp.storage.client.ttfb metric #14745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nidhiii-27
wants to merge
8
commits into
feat/storage-http-request-duration
Choose a base branch
from
feat/storage-ttfb-metric
base: feat/storage-http-request-duration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+368
−17
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b55633f
feat: add gcp.storage.client.ttfb metric for read operations
nidhiii-27 4c2d80b
fix(storage): refine ttfb metric attributes, status guards, and inter…
nidhiii-27 aa1f7db
fix(storage): merge base branch and move recordTtfb to StorageMetrics…
nidhiii-27 eadfdc8
fix(storage): merge feat/storage-http-request-duration and use method…
nidhiii-27 4f56cc9
chore(storage): merge feat/storage-http-request-duration into feat/st…
nidhiii-27 09ab4ec
fix(storage): address PR review feedback on TTFB metric
nidhiii-27 25b18f6
Merge branch 'feat/storage-http-request-duration' into feat/storage-t…
nidhiii-27 062344a
fix(storage): merge base branch and address review comments on TTFB m…
nidhiii-27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import com.google.api.core.ApiClock; | ||
| import com.google.storage.v2.BidiWriteObjectRequest; | ||
| import com.google.storage.v2.WriteObjectRequest; | ||
| import io.grpc.Attributes; | ||
| import io.grpc.CallOptions; | ||
| import io.grpc.Channel; | ||
|
|
@@ -34,6 +36,8 @@ | |
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketAddress; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
|
|
@@ -58,11 +62,19 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( | |
| String rpcMethod = method.getFullMethodName(); | ||
| StorageMetricsContext metricsCtx = StorageMetricsContext.from(callOptions); | ||
| Context otelContext = metricsCtx != null ? metricsCtx.getOtelContext() : Context.current(); | ||
| String operationName = | ||
| metricsCtx != null | ||
| ? metricsCtx.getOperationName() | ||
| : defaultOperationName(method.getBareMethodName()); | ||
|
|
||
| return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { | ||
| private final AtomicBoolean ttfbRecorded = new AtomicBoolean(false); | ||
| private final AtomicLong ttfbStartNanos = new AtomicLong(); | ||
|
|
||
| @Override | ||
| public void start(Listener<RespT> responseListener, Metadata headers) { | ||
| long startTimeNanos = clock.nanoTime(); | ||
| ttfbStartNanos.set(startTimeNanos); | ||
|
|
||
| super.start( | ||
| new SimpleForwardingClientCallListener<RespT>(responseListener) { | ||
|
|
@@ -74,6 +86,18 @@ public void onHeaders(Metadata headers) { | |
| super.onHeaders(headers); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessage(@Nullable RespT message) { | ||
| // The first response message from the server (including metadata-only | ||
| // responses for 0-byte objects) marks Time To First Byte (TTFB). | ||
| if (ttfbRecorded.compareAndSet(false, true)) { | ||
| double ttfbSeconds = (clock.nanoTime() - ttfbStartNanos.get()) / 1_000_000_000.0; | ||
| String networkPath = detectNetworkPath(getAttributes()); | ||
| metrics.recordTtfb(otelContext, operationName, ttfbSeconds, networkPath); | ||
| } | ||
| super.onMessage(message); | ||
| } | ||
|
|
||
| @Override | ||
| public void onClose(Status status, Metadata trailers) { | ||
| try { | ||
|
|
@@ -94,9 +118,44 @@ public void onClose(Status status, Metadata trailers) { | |
| }, | ||
| headers); | ||
| } | ||
|
|
||
| @Override | ||
| public void sendMessage(ReqT message) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For non-finalizing resumable |
||
| if (!ttfbRecorded.get() && isWriteFlush(message)) { | ||
| ttfbStartNanos.set(clock.nanoTime()); | ||
| } | ||
| super.sendMessage(message); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| static String defaultOperationName(@Nullable String bareMethod) { | ||
| if (bareMethod == null || bareMethod.isEmpty()) { | ||
| return "UNKNOWN"; | ||
| } | ||
| switch (bareMethod) { | ||
| case "BidiReadObject": | ||
| return "ReadObject"; | ||
| case "BidiWriteObject": | ||
| case "StartResumableWrite": | ||
| case "QueryWriteStatus": | ||
| return "WriteObject"; | ||
| default: | ||
| return bareMethod; | ||
| } | ||
| } | ||
|
|
||
| static boolean isWriteFlush(@Nullable Object message) { | ||
| if (message instanceof BidiWriteObjectRequest) { | ||
| BidiWriteObjectRequest req = (BidiWriteObjectRequest) message; | ||
| return req.getFlush() || req.getFinishWrite(); | ||
| } | ||
| if (message instanceof WriteObjectRequest) { | ||
| return ((WriteObjectRequest) message).getFinishWrite(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| static String detectNetworkPath(@Nullable Attributes callAttributes) { | ||
| SocketAddress remoteAddr = | ||
| callAttributes != null ? callAttributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR) : null; | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
sendMessage()updatesttfbStartNanosconcurrently afterclock.nanoTime()is evaluated here, the elapsed time can go negative and get dropped by OTel. Can we readttfbStartNanos.get()beforeclock.nanoTime()and guard re-anchoring with anAtomicBooleanso only the first flush updates it?