Repository navigation
feat(storage): add rpc.client.call.duration metric for gRPC calls #14743
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
16
commits into
feat/storage-grpc-veneer-operation-metrics
Choose a base branch
from
feat/storage-rpc-call-duration
base: feat/storage-grpc-veneer-operation-metrics
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.
+757
−1
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e50ed9d
feat: add rpc.client.call.duration metric for gRPC calls
nidhiii-27 154841b
fix(storage): address PR #14743 review feedback in StorageGrpcMetrics…
nidhiii-27 7527ba2
fix(storage): merge base branch and use OpenTelemetryBootstrappingUti…
nidhiii-27 01d37cf
docs(storage): refine StorageGrpcMetricsInterceptor Javadoc description
nidhiii-27 792fc8a
refactor(storage): use DirectPath IP string prefixes in isDirectPathIp
nidhiii-27 98f8bcc
refactor(storage): move recordRpcCallDuration to StorageMetricsContext
nidhiii-27 bda1d6e
Merge commit 'fd0a88f67b57e2bd69185d16a250f8bf57208dd5' into feat/sto…
nidhiii-27 e5cc1b8
fix(storage): resolve PR #14743 review comments in StorageGrpcMetrics…
nidhiii-27 6a75474
test(storage): align RetryingMetricsTest with DEADLINE_EXCEEDED error…
nidhiii-27 90af99d
chore(storage): merge feat/storage-grpc-veneer-operation-metrics into…
nidhiii-27 f321b31
Merge branch 'feat/storage-grpc-veneer-operation-metrics' into feat/s…
nidhiii-27 4420530
fix(storage): address PR review feedback on gRPC metrics interceptor
nidhiii-27 63952f4
Merge branch 'feat/storage-grpc-veneer-operation-metrics' into feat/s…
nidhiii-27 4bf512c
Merge remote-tracking branch 'origin/feat/storage-grpc-veneer-operati…
nidhiii-27 f515297
fix(storage): address follow-up review comments on PR #14743
nidhiii-27 a0b0d5b
fix(storage): address review comments on gRPC call duration metrics
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
131 changes: 131 additions & 0 deletions
131
...e-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java
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 |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.storage; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import com.google.api.core.ApiClock; | ||
| import io.grpc.Attributes; | ||
| import io.grpc.CallOptions; | ||
| import io.grpc.Channel; | ||
| import io.grpc.ClientCall; | ||
| import io.grpc.ClientInterceptor; | ||
| import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; | ||
| import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener; | ||
| import io.grpc.Grpc; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.MethodDescriptor; | ||
| import io.grpc.Status; | ||
| import io.opentelemetry.context.Context; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketAddress; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * {@link ClientInterceptor} that instruments outgoing gRPC calls to record OpenTelemetry client | ||
| * metrics. | ||
| */ | ||
| @NullMarked | ||
| final class StorageGrpcMetricsInterceptor implements ClientInterceptor { | ||
|
|
||
| private final StorageClientMetrics metrics; | ||
| private final ApiClock clock; | ||
|
|
||
| StorageGrpcMetricsInterceptor(StorageClientMetrics metrics, ApiClock clock) { | ||
| this.metrics = requireNonNull(metrics, "metrics cannot be null"); | ||
| this.clock = requireNonNull(clock, "clock cannot be null"); | ||
| } | ||
|
|
||
| @Override | ||
| public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( | ||
| MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { | ||
| String rpcMethod = method.getFullMethodName(); | ||
| StorageMetricsContext metricsCtx = StorageMetricsContext.from(callOptions); | ||
|
nidhiii-27 marked this conversation as resolved.
Resolved
|
||
| Context otelContext = metricsCtx != null ? metricsCtx.getOtelContext() : Context.current(); | ||
|
|
||
| return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { | ||
| @Override | ||
| public void start(Listener<RespT> responseListener, Metadata headers) { | ||
| long startTimeNanos = clock.nanoTime(); | ||
|
|
||
| super.start( | ||
| new SimpleForwardingClientCallListener<RespT>(responseListener) { | ||
| @Override | ||
| public void onHeaders(Metadata headers) { | ||
| if (metricsCtx != null) { | ||
| metricsCtx.setNetworkPath(detectNetworkPath(getAttributes())); | ||
| } | ||
| super.onHeaders(headers); | ||
| } | ||
|
|
||
| @Override | ||
| public void onClose(Status status, Metadata trailers) { | ||
| try { | ||
| double durationSeconds = (clock.nanoTime() - startTimeNanos) / 1_000_000_000.0; | ||
| String networkPath = detectNetworkPath(getAttributes()); | ||
| if (metricsCtx != null) { | ||
| metricsCtx.setNetworkPath(networkPath); | ||
| } | ||
| String statusCode = status.getCode().name(); | ||
| String errorType = StorageErrorTypeMapper.mapGrpcStatus(status); | ||
|
|
||
| metrics.recordRpcCallDuration( | ||
| otelContext, durationSeconds, rpcMethod, statusCode, networkPath, errorType); | ||
| } finally { | ||
| super.onClose(status, trailers); | ||
| } | ||
| } | ||
| }, | ||
| headers); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| static String detectNetworkPath(@Nullable Attributes callAttributes) { | ||
| SocketAddress remoteAddr = | ||
| callAttributes != null ? callAttributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR) : null; | ||
| if (remoteAddr instanceof InetSocketAddress) { | ||
| InetAddress inetAddr = ((InetSocketAddress) remoteAddr).getAddress(); | ||
| if (inetAddr != null) { | ||
| return isDirectPathIp(inetAddr) | ||
| ? StorageClientMetrics.NETWORK_PATH_DIRECTPATH | ||
| : StorageClientMetrics.NETWORK_PATH_CLOUDPATH; | ||
| } | ||
| } | ||
| return StorageClientMetrics.NETWORK_PATH_UNKNOWN; | ||
| } | ||
|
|
||
| static boolean isDirectPathIp(InetAddress addr) { | ||
|
nidhiii-27 marked this conversation as resolved.
Resolved
|
||
| byte[] b = addr.getAddress(); | ||
| if (b.length == 4) { | ||
| // 34.126.0.0/18 | ||
| return b[0] == 34 && b[1] == 126 && (b[2] & 0xC0) == 0; | ||
| } | ||
| if (b.length == 16) { | ||
| // 2001:4860:8040::/42 | ||
| return b[0] == 0x20 | ||
| && b[1] == 0x01 | ||
| && b[2] == 0x48 | ||
| && b[3] == 0x60 | ||
| && b[4] == (byte) 0x80 | ||
| && (b[5] & 0xC0) == 0x40; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
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.
The Noop and empty-list branches end up identical to the
addAllpath (NoopGrpcInterceptorProvider.getInterceptors()is an empty list), so this can be a single provider lambda thataddAll()s the user interceptors and appends ours.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.
Done
Co-authored by AI Agent