From e50ed9d51ee715b16b1292cb18f1f0a5050df3d9 Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Thu, 22 Oct 2026 07:31:11 +0000 Subject: [PATCH 01/10] feat: add rpc.client.call.duration metric for gRPC calls --- .../cloud/storage/GrpcStorageOptions.java | 15 +- .../StorageGrpcMetricsInterceptor.java | 163 +++++++++ .../StorageGrpcMetricsInterceptorTest.java | 325 ++++++++++++++++++ 3 files changed, 502 insertions(+), 1 deletion(-) create mode 100644 java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java create mode 100644 java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java index ce0ec9fbc487..9cd814a15e3c 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java @@ -331,7 +331,20 @@ private Tuple> resolveSettingsAndOpts() throw Collections.singletonList(InstantiatingGrpcChannelProvider.HardBoundTokenTypes.ALTS)); } - if (!NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider)) { + StorageClientMetrics clientMetrics = getStorageClientMetrics(); + if (clientMetrics != null) { + StorageGrpcMetricsInterceptor metricsInterceptor = + new StorageGrpcMetricsInterceptor(clientMetrics, getClock(), endpoint); + GrpcInterceptorProvider combinedProvider = + NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider) + ? () -> ImmutableList.of(metricsInterceptor) + : () -> + ImmutableList.builder() + .addAll(grpcInterceptorProvider.getInterceptors()) + .add(metricsInterceptor) + .build(); + channelProviderBuilder.setInterceptorProvider(combinedProvider); + } else if (!NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider)) { channelProviderBuilder.setInterceptorProvider(grpcInterceptorProvider); } diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java new file mode 100644 index 000000000000..a09d6c37b06e --- /dev/null +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -0,0 +1,163 @@ +/* + * 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 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.api.common.AttributesBuilder; +import io.opentelemetry.context.Context; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.concurrent.atomic.AtomicBoolean; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** gRPC client interceptor that records call metrics such as rpc.client.call.duration. */ +final class StorageGrpcMetricsInterceptor implements ClientInterceptor { + + private final StorageClientMetrics metrics; + private final ApiClock clock; + @Nullable private final String defaultEndpoint; + + StorageGrpcMetricsInterceptor( + StorageClientMetrics metrics, ApiClock clock, @Nullable String defaultEndpoint) { + this.metrics = metrics; + this.clock = clock; + this.defaultEndpoint = defaultEndpoint; + } + + @Override + public ClientCall interceptCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { + if (metrics == null || metrics.getRpcClientCallDuration() == null) { + return next.newCall(method, callOptions); + } + + String shortMethod = extractShortMethodName(method.getFullMethodName()); + String initialServerAddress = + StorageOptions.extractServerAddress( + defaultEndpoint != null ? defaultEndpoint : next.authority()); + final String serverAddress = + initialServerAddress != null + ? initialServerAddress + : StorageOptions.extractServerAddress(next.authority()); + + StorageMetricsContext metricsCtx = StorageMetricsContext.from(callOptions); + Context otelContext = metricsCtx != null ? metricsCtx.getOtelContext() : Context.current(); + + return new SimpleForwardingClientCall(next.newCall(method, callOptions)) { + @Override + public void start(Listener responseListener, Metadata headers) { + long startTimeNanos = clock.nanoTime(); + AtomicBoolean callRecorded = new AtomicBoolean(false); + + super.start( + new SimpleForwardingClientCallListener(responseListener) { + @Override + public void onClose(Status status, Metadata trailers) { + try { + if (callRecorded.compareAndSet(false, true) + && metrics.getRpcClientCallDuration() != null) { + double durationSeconds = + Math.max(0.0, (clock.nanoTime() - startTimeNanos) / 1_000_000_000.0); + String networkPath = detectNetworkPath(getAttributes(), defaultEndpoint); + + AttributesBuilder attrBuilder = + metrics + .clientAttributesBuilder() + .put(StorageClientMetrics.KEY_RPC_SYSTEM_NAME, "grpc") + .put(StorageClientMetrics.KEY_RPC_METHOD, shortMethod) + .put( + StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE, + status.getCode().name()) + .put(StorageClientMetrics.KEY_NETWORK_PATH, networkPath) + .put( + StorageClientMetrics.KEY_SERVER_ADDRESS, + serverAddress != null ? serverAddress : "") + .put( + StorageClientMetrics.KEY_ERROR_TYPE, + StorageErrorTypeMapper.mapGrpcStatus(status)); + + metrics + .getRpcClientCallDuration() + .record(durationSeconds, attrBuilder.build(), otelContext); + } + } finally { + super.onClose(status, trailers); + } + } + }, + headers); + } + }; + } + + static String extractShortMethodName(@Nullable String fullMethodName) { + if (fullMethodName == null || fullMethodName.isEmpty()) { + return "UNKNOWN"; + } + int idx = fullMethodName.lastIndexOf('/'); + if (idx >= 0 && idx < fullMethodName.length() - 1) { + return fullMethodName.substring(idx + 1); + } + return fullMethodName; + } + + static String detectNetworkPath( + @Nullable Attributes callAttributes, @Nullable String defaultEndpoint) { + 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; + } + } + if (defaultEndpoint != null && defaultEndpoint.trim().startsWith("google-c2p:///")) { + return StorageClientMetrics.NETWORK_PATH_DIRECTPATH; + } + return StorageClientMetrics.NETWORK_PATH_UNKNOWN; + } + + static boolean isDirectPathIp(InetAddress addr) { + byte[] b = addr.getAddress(); + if (b.length == 4) { + return (b[0] & 0xFF) == 34 && (b[1] & 0xFF) == 126 && ((b[2] & 0xFF) & 0xC0) == 0; + } + if (b.length == 16) { + return (b[0] & 0xFF) == 0x20 + && (b[1] & 0xFF) == 0x01 + && (b[2] & 0xFF) == 0x48 + && (b[3] & 0xFF) == 0x60 + && (b[4] & 0xFF) == 0x80 + && ((b[5] & 0xFF) & 0xC0) == 0x40; + } + return false; + } +} diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java new file mode 100644 index 000000000000..6bc17c0df9da --- /dev/null +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java @@ -0,0 +1,325 @@ +/* + * 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 com.google.common.truth.Truth.assertThat; + +import com.google.api.core.ApiClock; +import io.grpc.CallOptions; +import io.grpc.Channel; +import io.grpc.ClientCall; +import io.grpc.Grpc; +import io.grpc.Metadata; +import io.grpc.MethodDescriptor; +import io.grpc.Status; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.sdk.metrics.SdkMeterProvider; +import io.opentelemetry.sdk.metrics.data.HistogramPointData; +import io.opentelemetry.sdk.metrics.data.MetricData; +import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.Collection; +import java.util.concurrent.atomic.AtomicLong; +import org.junit.Before; +import org.junit.Test; + +public final class StorageGrpcMetricsInterceptorTest { + + private static final MethodDescriptor READ_OBJECT_METHOD = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.SERVER_STREAMING) + .setFullMethodName("google.storage.v2.Storage/ReadObject") + .setRequestMarshaller(new NoopMarshaller()) + .setResponseMarshaller(new NoopMarshaller()) + .build(); + + private InMemoryMetricReader reader; + private SdkMeterProvider meterProvider; + private FakeClock clock; + + @Before + public void setUp() { + reader = InMemoryMetricReader.create(); + meterProvider = SdkMeterProvider.builder().registerMetricReader(reader).build(); + clock = new FakeClock(); + } + + @Test + public void extractShortMethodName_variousInputs() { + assertThat( + StorageGrpcMetricsInterceptor.extractShortMethodName( + "google.storage.v2.Storage/ReadObject")) + .isEqualTo("ReadObject"); + assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName("ReadObject")) + .isEqualTo("ReadObject"); + assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName("a/b/c")).isEqualTo("c"); + assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName(null)).isEqualTo("UNKNOWN"); + assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName("")).isEqualTo("UNKNOWN"); + assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName("/")).isEqualTo("/"); + } + + @Test + public void isDirectPathIp_ipv4AndIpv6() throws Exception { + assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("34.126.0.1"))) + .isTrue(); + assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("34.126.63.254"))) + .isTrue(); + assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("34.126.64.1"))) + .isFalse(); + assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("142.250.1.1"))) + .isFalse(); + + assertThat( + StorageGrpcMetricsInterceptor.isDirectPathIp( + InetAddress.getByName("2001:4860:8040:0000:0000:0000:0000:0001"))) + .isTrue(); + assertThat( + StorageGrpcMetricsInterceptor.isDirectPathIp( + InetAddress.getByName("2001:4860:807f:ffff:ffff:ffff:ffff:ffff"))) + .isTrue(); + assertThat( + StorageGrpcMetricsInterceptor.isDirectPathIp( + InetAddress.getByName("2001:4860:8080:0000:0000:0000:0000:0001"))) + .isFalse(); + assertThat( + StorageGrpcMetricsInterceptor.isDirectPathIp( + InetAddress.getByName("2607:f8b0:4005:805::200e"))) + .isFalse(); + } + + @Test + public void detectNetworkPath_cases() throws Exception { + io.grpc.Attributes directPathAttrs = + io.grpc.Attributes.newBuilder() + .set( + Grpc.TRANSPORT_ATTR_REMOTE_ADDR, + new InetSocketAddress(InetAddress.getByName("34.126.1.2"), 443)) + .build(); + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(directPathAttrs, null)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); + + io.grpc.Attributes cloudPathAttrs = + io.grpc.Attributes.newBuilder() + .set( + Grpc.TRANSPORT_ATTR_REMOTE_ADDR, + new InetSocketAddress(InetAddress.getByName("142.250.1.1"), 443)) + .build(); + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(cloudPathAttrs, null)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_CLOUDPATH); + + assertThat( + StorageGrpcMetricsInterceptor.detectNetworkPath( + null, "google-c2p:///storage.googleapis.com")) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); + assertThat( + StorageGrpcMetricsInterceptor.detectNetworkPath(null, "https://storage.googleapis.com")) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_UNKNOWN); + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(null, null)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_UNKNOWN); + } + + @Test + public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws Exception { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, "https://storage.googleapis.com"); + + io.grpc.Attributes callAttrs = + io.grpc.Attributes.newBuilder() + .set( + Grpc.TRANSPORT_ATTR_REMOTE_ADDR, + new InetSocketAddress(InetAddress.getByName("142.250.1.1"), 443)) + .build(); + TestClientCall testCall = new TestClientCall<>(callAttrs); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com:443"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + TestListener listener = new TestListener<>(); + call.start(listener, new Metadata()); + + clock.advanceNanos(500_000_000L); // 0.5s + testCall.close(Status.OK, new Metadata()); + + Collection metricData = reader.collectAllMetrics(); + MetricData rpcDurationData = + metricData.stream() + .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) + .findFirst() + .orElse(null); + + assertThat(rpcDurationData).isNotNull(); + assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); + HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isWithin(1e-6).of(0.5); + + Attributes attrs = point.getAttributes(); + assertThat(attrs.get(StorageClientMetrics.KEY_RPC_SYSTEM_NAME)).isEqualTo("grpc"); + assertThat(attrs.get(StorageClientMetrics.KEY_RPC_METHOD)).isEqualTo("ReadObject"); + assertThat(attrs.get(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE)).isEqualTo("OK"); + assertThat(attrs.get(StorageClientMetrics.KEY_NETWORK_PATH)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_CLOUDPATH); + assertThat(attrs.get(StorageClientMetrics.KEY_SERVER_ADDRESS)) + .isEqualTo("storage.googleapis.com"); + assertThat(attrs.get(StorageClientMetrics.KEY_ERROR_TYPE)).isEqualTo("OK"); + } + + @Test + public void interceptCall_callFails_recordsErrorType() throws Exception { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + + TestClientCall testCall = new TestClientCall<>(io.grpc.Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + call.start(new TestListener<>(), new Metadata()); + + clock.advanceNanos(100_000_000L); // 0.1s + testCall.close(Status.NOT_FOUND, new Metadata()); + + Collection metricData = reader.collectAllMetrics(); + MetricData rpcDurationData = + metricData.stream() + .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) + .findFirst() + .orElse(null); + + assertThat(rpcDurationData).isNotNull(); + HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); + assertThat(point.getAttributes().get(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE)) + .isEqualTo("NOT_FOUND"); + assertThat(point.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) + .isEqualTo("NOT_FOUND"); + assertThat(point.getAttributes().get(StorageClientMetrics.KEY_SERVER_ADDRESS)) + .isEqualTo("storage.googleapis.com"); + } + + @Test + public void interceptCall_disabledMetrics_doesNotRecord() { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, false, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + + TestClientCall testCall = new TestClientCall<>(io.grpc.Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + call.start(new TestListener<>(), new Metadata()); + testCall.close(Status.OK, new Metadata()); + + assertThat(reader.collectAllMetrics()).isEmpty(); + } + + private static final class TestClientCall extends ClientCall { + private Listener listener; + private final io.grpc.Attributes attributes; + + TestClientCall(io.grpc.Attributes attributes) { + this.attributes = attributes; + } + + @Override + public void start(Listener responseListener, Metadata headers) { + this.listener = responseListener; + } + + @Override + public void request(int numMessages) {} + + @Override + public void cancel(String message, Throwable cause) {} + + @Override + public void halfClose() {} + + @Override + public void sendMessage(ReqT message) {} + + @Override + public io.grpc.Attributes getAttributes() { + return attributes; + } + + void close(Status status, Metadata trailers) { + if (listener != null) { + listener.onClose(status, trailers); + } + } + } + + private static final class TestChannel extends Channel { + private final TestClientCall call; + private final String authority; + + TestChannel(TestClientCall call, String authority) { + this.call = call; + this.authority = authority; + } + + @SuppressWarnings("unchecked") + @Override + public ClientCall newCall( + MethodDescriptor methodDescriptor, CallOptions callOptions) { + return (ClientCall) call; + } + + @Override + public String authority() { + return authority; + } + } + + private static final class TestListener extends ClientCall.Listener {} + + private static final class NoopMarshaller implements MethodDescriptor.Marshaller { + @Override + public InputStream stream(Void value) { + return new ByteArrayInputStream(new byte[0]); + } + + @Override + public Void parse(InputStream stream) { + return null; + } + } + + private static final class FakeClock implements ApiClock { + private final AtomicLong nanos = new AtomicLong(1_000_000_000L); + + @Override + public long nanoTime() { + return nanos.get(); + } + + @Override + public long millisTime() { + return nanos.get() / 1_000_000L; + } + + void advanceNanos(long amount) { + nanos.addAndGet(amount); + } + } +} From 154841b0659b48bc36be743bfe85cdde0f5c7999 Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Tue, 3 Nov 2026 10:34:38 +0000 Subject: [PATCH 02/10] fix(storage): address PR #14743 review feedback in StorageGrpcMetricsInterceptor - Precompute defaultServerAddress and isDirectPathEndpoint in StorageGrpcMetricsInterceptor constructor - Guard grpcInterceptorProvider and its getInterceptors() against null in GrpcStorageOptions - Add defensive null checks and cause-aware error mapping in StorageGrpcMetricsInterceptor.onClose - Use JSpecify @Nullable and expand unit test coverage in StorageGrpcMetricsInterceptorTest [Generated-by: AI] --- .../cloud/storage/GrpcStorageOptions.java | 21 +- .../StorageGrpcMetricsInterceptor.java | 45 +-- .../StorageGrpcMetricsInterceptorTest.java | 281 ++++++++++++++++-- 3 files changed, 297 insertions(+), 50 deletions(-) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java index 9cd814a15e3c..416467fdc191 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java @@ -336,15 +336,22 @@ private Tuple> resolveSettingsAndOpts() throw StorageGrpcMetricsInterceptor metricsInterceptor = new StorageGrpcMetricsInterceptor(clientMetrics, getClock(), endpoint); GrpcInterceptorProvider combinedProvider = - NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider) + grpcInterceptorProvider == null + || NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider) ? () -> ImmutableList.of(metricsInterceptor) - : () -> - ImmutableList.builder() - .addAll(grpcInterceptorProvider.getInterceptors()) - .add(metricsInterceptor) - .build(); + : () -> { + List existing = grpcInterceptorProvider.getInterceptors(); + if (existing == null || existing.isEmpty()) { + return ImmutableList.of(metricsInterceptor); + } + return ImmutableList.builder() + .addAll(existing) + .add(metricsInterceptor) + .build(); + }; channelProviderBuilder.setInterceptorProvider(combinedProvider); - } else if (!NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider)) { + } else if (grpcInterceptorProvider != null + && !NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider)) { channelProviderBuilder.setInterceptorProvider(grpcInterceptorProvider); } diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java index a09d6c37b06e..dc0a47a3909d 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -34,20 +34,23 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.concurrent.atomic.AtomicBoolean; -import org.checkerframework.checker.nullness.qual.Nullable; +import org.jspecify.annotations.Nullable; /** gRPC client interceptor that records call metrics such as rpc.client.call.duration. */ final class StorageGrpcMetricsInterceptor implements ClientInterceptor { private final StorageClientMetrics metrics; private final ApiClock clock; - @Nullable private final String defaultEndpoint; + @Nullable private final String defaultServerAddress; + private final boolean isDirectPathEndpoint; StorageGrpcMetricsInterceptor( StorageClientMetrics metrics, ApiClock clock, @Nullable String defaultEndpoint) { this.metrics = metrics; this.clock = clock; - this.defaultEndpoint = defaultEndpoint; + this.defaultServerAddress = StorageOptions.extractServerAddress(defaultEndpoint); + this.isDirectPathEndpoint = + defaultEndpoint != null && defaultEndpoint.trim().startsWith("google-c2p:///"); } @Override @@ -58,13 +61,11 @@ public ClientCall interceptCall( } String shortMethod = extractShortMethodName(method.getFullMethodName()); - String initialServerAddress = - StorageOptions.extractServerAddress( - defaultEndpoint != null ? defaultEndpoint : next.authority()); - final String serverAddress = - initialServerAddress != null - ? initialServerAddress + String resolvedServerAddress = + defaultServerAddress != null + ? defaultServerAddress : StorageOptions.extractServerAddress(next.authority()); + final String serverAddress = resolvedServerAddress != null ? resolvedServerAddress : ""; StorageMetricsContext metricsCtx = StorageMetricsContext.from(callOptions); Context otelContext = metricsCtx != null ? metricsCtx.getOtelContext() : Context.current(); @@ -84,23 +85,25 @@ public void onClose(Status status, Metadata trailers) { && metrics.getRpcClientCallDuration() != null) { double durationSeconds = Math.max(0.0, (clock.nanoTime() - startTimeNanos) / 1_000_000_000.0); - String networkPath = detectNetworkPath(getAttributes(), defaultEndpoint); + String networkPath = detectNetworkPath(getAttributes(), isDirectPathEndpoint); + String statusCode = + status != null && status.getCode() != null + ? status.getCode().name() + : StorageErrorTypeMapper.STATUS_UNKNOWN; + String errorType = + status != null && !status.isOk() && status.getCause() != null + ? StorageErrorTypeMapper.mapThrowable(status.asRuntimeException()) + : StorageErrorTypeMapper.mapGrpcStatus(status); AttributesBuilder attrBuilder = metrics .clientAttributesBuilder() .put(StorageClientMetrics.KEY_RPC_SYSTEM_NAME, "grpc") .put(StorageClientMetrics.KEY_RPC_METHOD, shortMethod) - .put( - StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE, - status.getCode().name()) + .put(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE, statusCode) .put(StorageClientMetrics.KEY_NETWORK_PATH, networkPath) - .put( - StorageClientMetrics.KEY_SERVER_ADDRESS, - serverAddress != null ? serverAddress : "") - .put( - StorageClientMetrics.KEY_ERROR_TYPE, - StorageErrorTypeMapper.mapGrpcStatus(status)); + .put(StorageClientMetrics.KEY_SERVER_ADDRESS, serverAddress) + .put(StorageClientMetrics.KEY_ERROR_TYPE, errorType); metrics .getRpcClientCallDuration() @@ -128,7 +131,7 @@ static String extractShortMethodName(@Nullable String fullMethodName) { } static String detectNetworkPath( - @Nullable Attributes callAttributes, @Nullable String defaultEndpoint) { + @Nullable Attributes callAttributes, boolean isDirectPathEndpoint) { SocketAddress remoteAddr = callAttributes != null ? callAttributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR) : null; if (remoteAddr instanceof InetSocketAddress) { @@ -139,7 +142,7 @@ static String detectNetworkPath( : StorageClientMetrics.NETWORK_PATH_CLOUDPATH; } } - if (defaultEndpoint != null && defaultEndpoint.trim().startsWith("google-c2p:///")) { + if (isDirectPathEndpoint) { return StorageClientMetrics.NETWORK_PATH_DIRECTPATH; } return StorageClientMetrics.NETWORK_PATH_UNKNOWN; diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java index 6bc17c0df9da..c76ad46d41e1 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java @@ -19,23 +19,32 @@ import static com.google.common.truth.Truth.assertThat; import com.google.api.core.ApiClock; +import com.google.api.gax.grpc.GrpcInterceptorProvider; +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.cloud.NoCredentials; +import com.google.common.collect.ImmutableList; +import io.grpc.Attributes; import io.grpc.CallOptions; import io.grpc.Channel; import io.grpc.ClientCall; +import io.grpc.ClientInterceptor; import io.grpc.Grpc; import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.Status; -import io.opentelemetry.api.common.Attributes; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.metrics.data.HistogramPointData; import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.lang.reflect.Field; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.UnknownHostException; import java.util.Collection; +import java.util.Collections; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import org.junit.Before; import org.junit.Test; @@ -106,32 +115,27 @@ public void isDirectPathIp_ipv4AndIpv6() throws Exception { @Test public void detectNetworkPath_cases() throws Exception { - io.grpc.Attributes directPathAttrs = - io.grpc.Attributes.newBuilder() + Attributes directPathAttrs = + Attributes.newBuilder() .set( Grpc.TRANSPORT_ATTR_REMOTE_ADDR, new InetSocketAddress(InetAddress.getByName("34.126.1.2"), 443)) .build(); - assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(directPathAttrs, null)) + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(directPathAttrs, false)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); - io.grpc.Attributes cloudPathAttrs = - io.grpc.Attributes.newBuilder() + Attributes cloudPathAttrs = + Attributes.newBuilder() .set( Grpc.TRANSPORT_ATTR_REMOTE_ADDR, new InetSocketAddress(InetAddress.getByName("142.250.1.1"), 443)) .build(); - assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(cloudPathAttrs, null)) + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(cloudPathAttrs, false)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_CLOUDPATH); - assertThat( - StorageGrpcMetricsInterceptor.detectNetworkPath( - null, "google-c2p:///storage.googleapis.com")) + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(null, true)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); - assertThat( - StorageGrpcMetricsInterceptor.detectNetworkPath(null, "https://storage.googleapis.com")) - .isEqualTo(StorageClientMetrics.NETWORK_PATH_UNKNOWN); - assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(null, null)) + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(null, false)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_UNKNOWN); } @@ -141,8 +145,8 @@ public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock, "https://storage.googleapis.com"); - io.grpc.Attributes callAttrs = - io.grpc.Attributes.newBuilder() + Attributes callAttrs = + Attributes.newBuilder() .set( Grpc.TRANSPORT_ATTR_REMOTE_ADDR, new InetSocketAddress(InetAddress.getByName("142.250.1.1"), 443)) @@ -171,7 +175,7 @@ public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws assertThat(point.getCount()).isEqualTo(1L); assertThat(point.getSum()).isWithin(1e-6).of(0.5); - Attributes attrs = point.getAttributes(); + io.opentelemetry.api.common.Attributes attrs = point.getAttributes(); assertThat(attrs.get(StorageClientMetrics.KEY_RPC_SYSTEM_NAME)).isEqualTo("grpc"); assertThat(attrs.get(StorageClientMetrics.KEY_RPC_METHOD)).isEqualTo("ReadObject"); assertThat(attrs.get(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE)).isEqualTo("OK"); @@ -188,7 +192,7 @@ public void interceptCall_callFails_recordsErrorType() throws Exception { StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); - TestClientCall testCall = new TestClientCall<>(io.grpc.Attributes.EMPTY); + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); ClientCall call = @@ -221,7 +225,7 @@ public void interceptCall_disabledMetrics_doesNotRecord() { StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); - TestClientCall testCall = new TestClientCall<>(io.grpc.Attributes.EMPTY); + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); ClientCall call = @@ -232,11 +236,244 @@ public void interceptCall_disabledMetrics_doesNotRecord() { assertThat(reader.collectAllMetrics()).isEmpty(); } + @Test + public void interceptCall_nullDefaultEndpoint_fallsBackToChannelAuthority() { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, null); + + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com:443"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + call.start(new TestListener<>(), new Metadata()); + testCall.close(Status.OK, new Metadata()); + + MetricData rpcDurationData = + reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) + .findFirst() + .orElse(null); + + assertThat(rpcDurationData).isNotNull(); + HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); + assertThat(point.getAttributes().get(StorageClientMetrics.KEY_SERVER_ADDRESS)) + .isEqualTo("storage.googleapis.com"); + } + + @Test + public void interceptCall_directPathEndpoint_recordsDirectPathAndServerAddress() { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor( + metrics, clock, " google-c2p:///storage.googleapis.com "); + + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + call.start(new TestListener<>(), new Metadata()); + testCall.close(Status.OK, new Metadata()); + + MetricData rpcDurationData = + reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) + .findFirst() + .orElse(null); + + assertThat(rpcDurationData).isNotNull(); + HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); + assertThat(point.getAttributes().get(StorageClientMetrics.KEY_NETWORK_PATH)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); + assertThat(point.getAttributes().get(StorageClientMetrics.KEY_SERVER_ADDRESS)) + .isEqualTo("storage.googleapis.com"); + } + + @Test + public void interceptCall_statusWithNetworkCause_recordsCauseErrorType() { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + call.start(new TestListener<>(), new Metadata()); + testCall.close( + Status.UNAVAILABLE.withCause(new UnknownHostException("storage.googleapis.com")), + new Metadata()); + + MetricData rpcDurationData = + reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) + .findFirst() + .orElse(null); + + assertThat(rpcDurationData).isNotNull(); + HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); + assertThat(point.getAttributes().get(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE)) + .isEqualTo("UNAVAILABLE"); + assertThat(point.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) + .isEqualTo(StorageErrorTypeMapper.STATUS_DNS_FAILURE); + } + + @Test + public void interceptCall_duplicateOnClose_recordsOnlyOnce() { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + call.start(new TestListener<>(), new Metadata()); + testCall.close(Status.OK, new Metadata()); + testCall.close(Status.OK, new Metadata()); + + MetricData rpcDurationData = + reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) + .findFirst() + .orElse(null); + + assertThat(rpcDurationData).isNotNull(); + assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); + HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); + assertThat(point.getCount()).isEqualTo(1L); + } + + @Test + public void interceptCall_withStorageMetricsContext_propagatesContext() { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + + StorageMetricsContext metricsContext = StorageMetricsContext.create("ReadObject"); + CallOptions callOptions = metricsContext.attach(CallOptions.DEFAULT); + + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, callOptions, testChannel); + call.start(new TestListener<>(), new Metadata()); + testCall.close(Status.OK, new Metadata()); + + MetricData rpcDurationData = + reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) + .findFirst() + .orElse(null); + + assertThat(rpcDurationData).isNotNull(); + assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); + } + + @Test + public void grpcStorageOptions_interceptorProviderWiring() throws Exception { + GrpcStorageOptions defaultOptions = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(true) + .setMeterProvider(meterProvider) + .build(); + InstantiatingGrpcChannelProvider defaultChannelProvider = + (InstantiatingGrpcChannelProvider) + defaultOptions.getStorageSettings().getTransportChannelProvider(); + GrpcInterceptorProvider defaultInterceptorProvider = + extractInterceptorProvider(defaultChannelProvider); + assertThat(defaultInterceptorProvider).isNotNull(); + assertThat(defaultInterceptorProvider.getInterceptors()).hasSize(1); + assertThat(defaultInterceptorProvider.getInterceptors().get(0)) + .isInstanceOf(StorageGrpcMetricsInterceptor.class); + + GrpcStorageOptions nullProviderOptions = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(true) + .setMeterProvider(meterProvider) + .build(); + Field providerField = GrpcStorageOptions.class.getDeclaredField("grpcInterceptorProvider"); + providerField.setAccessible(true); + providerField.set(nullProviderOptions, null); + InstantiatingGrpcChannelProvider nullChannelProvider = + (InstantiatingGrpcChannelProvider) + nullProviderOptions.getStorageSettings().getTransportChannelProvider(); + GrpcInterceptorProvider nullCombinedProvider = extractInterceptorProvider(nullChannelProvider); + assertThat(nullCombinedProvider).isNotNull(); + assertThat(nullCombinedProvider.getInterceptors()).hasSize(1); + assertThat(nullCombinedProvider.getInterceptors().get(0)) + .isInstanceOf(StorageGrpcMetricsInterceptor.class); + + AtomicBoolean customCalled = new AtomicBoolean(false); + ClientInterceptor customInterceptor = + new ClientInterceptor() { + @Override + public ClientCall interceptCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { + customCalled.set(true); + return next.newCall(method, callOptions); + } + }; + + GrpcStorageOptions customOptions = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(true) + .setMeterProvider(meterProvider) + .setGrpcInterceptorProvider(() -> ImmutableList.of(customInterceptor)) + .build(); + InstantiatingGrpcChannelProvider customChannelProvider = + (InstantiatingGrpcChannelProvider) + customOptions.getStorageSettings().getTransportChannelProvider(); + GrpcInterceptorProvider combinedProvider = extractInterceptorProvider(customChannelProvider); + assertThat(combinedProvider).isNotNull(); + assertThat(combinedProvider.getInterceptors()).hasSize(2); + assertThat(combinedProvider.getInterceptors().get(0)).isSameInstanceAs(customInterceptor); + assertThat(combinedProvider.getInterceptors().get(1)) + .isInstanceOf(StorageGrpcMetricsInterceptor.class); + + GrpcStorageOptions emptyCustomOptions = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(true) + .setMeterProvider(meterProvider) + .setGrpcInterceptorProvider(Collections::emptyList) + .build(); + InstantiatingGrpcChannelProvider emptyCustomChannelProvider = + (InstantiatingGrpcChannelProvider) + emptyCustomOptions.getStorageSettings().getTransportChannelProvider(); + GrpcInterceptorProvider emptyCombinedProvider = + extractInterceptorProvider(emptyCustomChannelProvider); + assertThat(emptyCombinedProvider).isNotNull(); + assertThat(emptyCombinedProvider.getInterceptors()).hasSize(1); + assertThat(emptyCombinedProvider.getInterceptors().get(0)) + .isInstanceOf(StorageGrpcMetricsInterceptor.class); + assertThat(customCalled.get()).isFalse(); + } + + private static GrpcInterceptorProvider extractInterceptorProvider( + InstantiatingGrpcChannelProvider channelProvider) throws Exception { + Field field = InstantiatingGrpcChannelProvider.class.getDeclaredField("interceptorProvider"); + field.setAccessible(true); + return (GrpcInterceptorProvider) field.get(channelProvider); + } + private static final class TestClientCall extends ClientCall { private Listener listener; - private final io.grpc.Attributes attributes; + private final Attributes attributes; - TestClientCall(io.grpc.Attributes attributes) { + TestClientCall(Attributes attributes) { this.attributes = attributes; } @@ -258,7 +495,7 @@ public void halfClose() {} public void sendMessage(ReqT message) {} @Override - public io.grpc.Attributes getAttributes() { + public Attributes getAttributes() { return attributes; } From 01d37cfe2c1ca02f6e4b6639356eb462d106b8fa Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Thu, 5 Nov 2026 09:21:43 +0000 Subject: [PATCH 03/10] docs(storage): refine StorageGrpcMetricsInterceptor Javadoc description [Generated-by: AI] --- .../google/cloud/storage/StorageGrpcMetricsInterceptor.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java index f233b7645787..1ee1e02641d0 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -36,7 +36,10 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.jspecify.annotations.Nullable; -/** gRPC client interceptor that records call metrics such as rpc.client.call.duration. */ +/** + * {@link ClientInterceptor} that instruments outgoing gRPC calls to record OpenTelemetry client + * metrics. + */ final class StorageGrpcMetricsInterceptor implements ClientInterceptor { private final StorageClientMetrics metrics; From 792fc8ab0e49726e52c96be596e3beb4091967b0 Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Thu, 5 Nov 2026 09:31:00 +0000 Subject: [PATCH 04/10] refactor(storage): use DirectPath IP string prefixes in isDirectPathIp [Generated-by: AI] --- .../StorageGrpcMetricsInterceptor.java | 19 ++++++------------- .../StorageGrpcMetricsInterceptorTest.java | 4 ++-- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java index 1ee1e02641d0..4c620d932112 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -42,6 +42,9 @@ */ final class StorageGrpcMetricsInterceptor implements ClientInterceptor { + private static final String DP_IPV6_PREFIX = "2001:4860:8040"; + private static final String DP_IPV4_PREFIX = "34.126"; + private final StorageClientMetrics metrics; private final ApiClock clock; @Nullable private final String defaultServerAddress; @@ -153,18 +156,8 @@ static String detectNetworkPath( } static boolean isDirectPathIp(InetAddress addr) { - byte[] b = addr.getAddress(); - if (b.length == 4) { - return (b[0] & 0xFF) == 34 && (b[1] & 0xFF) == 126 && ((b[2] & 0xFF) & 0xC0) == 0; - } - if (b.length == 16) { - return (b[0] & 0xFF) == 0x20 - && (b[1] & 0xFF) == 0x01 - && (b[2] & 0xFF) == 0x48 - && (b[3] & 0xFF) == 0x60 - && (b[4] & 0xFF) == 0x80 - && ((b[5] & 0xFF) & 0xC0) == 0x40; - } - return false; + String hostAddr = addr.getHostAddress(); + return hostAddr != null + && (hostAddr.startsWith(DP_IPV4_PREFIX) || hostAddr.startsWith(DP_IPV6_PREFIX)); } } diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java index c76ad46d41e1..214bb5462175 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java @@ -90,7 +90,7 @@ public void isDirectPathIp_ipv4AndIpv6() throws Exception { .isTrue(); assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("34.126.63.254"))) .isTrue(); - assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("34.126.64.1"))) + assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("35.190.1.1"))) .isFalse(); assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("142.250.1.1"))) .isFalse(); @@ -101,7 +101,7 @@ public void isDirectPathIp_ipv4AndIpv6() throws Exception { .isTrue(); assertThat( StorageGrpcMetricsInterceptor.isDirectPathIp( - InetAddress.getByName("2001:4860:807f:ffff:ffff:ffff:ffff:ffff"))) + InetAddress.getByName("2001:4860:8040:ffff:ffff:ffff:ffff:ffff"))) .isTrue(); assertThat( StorageGrpcMetricsInterceptor.isDirectPathIp( From 98f8bcca4bc8c7d643bca52dcb979ce80bdd0351 Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Thu, 5 Nov 2026 09:47:57 +0000 Subject: [PATCH 05/10] refactor(storage): move recordRpcCallDuration to StorageMetricsContext [Generated-by: AI] --- .../StorageGrpcMetricsInterceptor.java | 25 +++++++---------- .../cloud/storage/StorageMetricsContext.java | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java index 4c620d932112..c2b36f5f8c9f 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -28,8 +28,6 @@ import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.Status; -import io.opentelemetry.api.common.AttributesBuilder; -import io.opentelemetry.context.Context; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; @@ -75,7 +73,6 @@ public ClientCall interceptCall( final String serverAddress = resolvedServerAddress != null ? resolvedServerAddress : ""; StorageMetricsContext metricsCtx = StorageMetricsContext.from(callOptions); - Context otelContext = metricsCtx != null ? metricsCtx.getOtelContext() : Context.current(); return new SimpleForwardingClientCall(next.newCall(method, callOptions)) { @Override @@ -102,19 +99,15 @@ public void onClose(Status status, Metadata trailers) { ? StorageErrorTypeMapper.mapThrowable(status.asRuntimeException()) : StorageErrorTypeMapper.mapGrpcStatus(status); - AttributesBuilder attrBuilder = - metrics - .clientAttributesBuilder() - .put(StorageClientMetrics.KEY_RPC_SYSTEM_NAME, "grpc") - .put(StorageClientMetrics.KEY_RPC_METHOD, shortMethod) - .put(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE, statusCode) - .put(StorageClientMetrics.KEY_NETWORK_PATH, networkPath) - .put(StorageClientMetrics.KEY_SERVER_ADDRESS, serverAddress) - .put(StorageClientMetrics.KEY_ERROR_TYPE, errorType); - - metrics - .getRpcClientCallDuration() - .record(durationSeconds, attrBuilder.build(), otelContext); + StorageMetricsContext.recordRpcCallDuration( + metrics, + metricsCtx, + durationSeconds, + shortMethod, + statusCode, + networkPath, + serverAddress, + errorType); } } finally { super.onClose(status, trailers); diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java index 81332c7aeca1..93715ad69c2e 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java @@ -268,6 +268,33 @@ void recordAttemptFailure(@Nullable StorageClientMetrics metrics, Throwable e) { } } + static void recordRpcCallDuration( + @Nullable StorageClientMetrics metrics, + @Nullable StorageMetricsContext metricsContext, + double durationSeconds, + String shortMethod, + String statusCode, + String networkPath, + String serverAddress, + String errorType) { + if (metrics == null || metrics.getRpcClientCallDuration() == null) { + return; + } + Context otelContext = + metricsContext != null ? metricsContext.getOtelContext() : Context.current(); + Attributes attributes = + metrics + .clientAttributesBuilder() + .put(StorageClientMetrics.KEY_RPC_SYSTEM_NAME, "grpc") + .put(StorageClientMetrics.KEY_RPC_METHOD, shortMethod) + .put(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE, statusCode) + .put(StorageClientMetrics.KEY_NETWORK_PATH, networkPath) + .put(StorageClientMetrics.KEY_SERVER_ADDRESS, serverAddress) + .put(StorageClientMetrics.KEY_ERROR_TYPE, errorType) + .build(); + metrics.getRpcClientCallDuration().record(durationSeconds, attributes, otelContext); + } + private Attributes buildAttemptBaseAttributes(StorageClientMetrics metrics) { AttributesBuilder baseBuilder = Attributes.builder(); baseBuilder.putAll(metrics.getClientAttributes()); From e5cc1b8a1f8bd522884a445a4390807669ecfb0f Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Sat, 7 Nov 2026 19:07:53 +0000 Subject: [PATCH 06/10] fix(storage): resolve PR #14743 review comments in StorageGrpcMetricsInterceptor - Use full gRPC method name (`method.getFullMethodName()`) for `rpc.method` and remove `extractShortMethodName` - Capture fallback `Context.current()` in `interceptCall` on the caller thread - Remove `isDirectPathEndpoint` and return `NETWORK_PATH_UNKNOWN` when `remoteAddr` is absent - Restore `/18` and `/42` CIDR byte-mask check in `isDirectPathIp` [Generated-by: AI] --- .../StorageGrpcMetricsInterceptor.java | 66 +++---- .../cloud/storage/StorageMetricsContext.java | 8 +- .../StorageGrpcMetricsInterceptorTest.java | 177 +++++++++++++++--- 3 files changed, 186 insertions(+), 65 deletions(-) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java index c2b36f5f8c9f..f1cdd8a164c8 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -28,34 +28,31 @@ 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 java.util.concurrent.atomic.AtomicBoolean; +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 static final String DP_IPV6_PREFIX = "2001:4860:8040"; - private static final String DP_IPV4_PREFIX = "34.126"; - - private final StorageClientMetrics metrics; + @Nullable private final StorageClientMetrics metrics; private final ApiClock clock; @Nullable private final String defaultServerAddress; - private final boolean isDirectPathEndpoint; StorageGrpcMetricsInterceptor( - StorageClientMetrics metrics, ApiClock clock, @Nullable String defaultEndpoint) { + @Nullable StorageClientMetrics metrics, ApiClock clock, @Nullable String defaultEndpoint) { this.metrics = metrics; this.clock = clock; this.defaultServerAddress = OpenTelemetryBootstrappingUtils.extractServerAddress(defaultEndpoint); - this.isDirectPathEndpoint = - defaultEndpoint != null && defaultEndpoint.trim().startsWith("google-c2p:///"); } @Override @@ -65,7 +62,9 @@ public ClientCall interceptCall( return next.newCall(method, callOptions); } - String shortMethod = extractShortMethodName(method.getFullMethodName()); + String fullMethodName = method.getFullMethodName(); + String rpcMethod = + fullMethodName != null && !fullMethodName.isEmpty() ? fullMethodName : "UNKNOWN"; String resolvedServerAddress = defaultServerAddress != null ? defaultServerAddress @@ -73,6 +72,7 @@ public ClientCall interceptCall( final String serverAddress = resolvedServerAddress != null ? resolvedServerAddress : ""; StorageMetricsContext metricsCtx = StorageMetricsContext.from(callOptions); + Context otelContext = metricsCtx != null ? metricsCtx.getOtelContext() : Context.current(); return new SimpleForwardingClientCall(next.newCall(method, callOptions)) { @Override @@ -83,27 +83,24 @@ public void start(Listener responseListener, Metadata headers) { super.start( new SimpleForwardingClientCallListener(responseListener) { @Override - public void onClose(Status status, Metadata trailers) { + public void onClose(@Nullable Status status, @Nullable Metadata trailers) { try { if (callRecorded.compareAndSet(false, true) && metrics.getRpcClientCallDuration() != null) { double durationSeconds = Math.max(0.0, (clock.nanoTime() - startTimeNanos) / 1_000_000_000.0); - String networkPath = detectNetworkPath(getAttributes(), isDirectPathEndpoint); + String networkPath = detectNetworkPath(getAttributes()); String statusCode = status != null && status.getCode() != null ? status.getCode().name() : StorageErrorTypeMapper.STATUS_UNKNOWN; - String errorType = - status != null && !status.isOk() && status.getCause() != null - ? StorageErrorTypeMapper.mapThrowable(status.asRuntimeException()) - : StorageErrorTypeMapper.mapGrpcStatus(status); + String errorType = StorageErrorTypeMapper.mapGrpcStatus(status); StorageMetricsContext.recordRpcCallDuration( metrics, - metricsCtx, + otelContext, durationSeconds, - shortMethod, + rpcMethod, statusCode, networkPath, serverAddress, @@ -119,19 +116,7 @@ public void onClose(Status status, Metadata trailers) { }; } - static String extractShortMethodName(@Nullable String fullMethodName) { - if (fullMethodName == null || fullMethodName.isEmpty()) { - return "UNKNOWN"; - } - int idx = fullMethodName.lastIndexOf('/'); - if (idx >= 0 && idx < fullMethodName.length() - 1) { - return fullMethodName.substring(idx + 1); - } - return fullMethodName; - } - - static String detectNetworkPath( - @Nullable Attributes callAttributes, boolean isDirectPathEndpoint) { + static String detectNetworkPath(@Nullable Attributes callAttributes) { SocketAddress remoteAddr = callAttributes != null ? callAttributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR) : null; if (remoteAddr instanceof InetSocketAddress) { @@ -142,15 +127,24 @@ static String detectNetworkPath( : StorageClientMetrics.NETWORK_PATH_CLOUDPATH; } } - if (isDirectPathEndpoint) { - return StorageClientMetrics.NETWORK_PATH_DIRECTPATH; - } return StorageClientMetrics.NETWORK_PATH_UNKNOWN; } static boolean isDirectPathIp(InetAddress addr) { - String hostAddr = addr.getHostAddress(); - return hostAddr != null - && (hostAddr.startsWith(DP_IPV4_PREFIX) || hostAddr.startsWith(DP_IPV6_PREFIX)); + 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] & 0xFC) == 0x40; + } + return false; } } diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java index 93715ad69c2e..a9407acacd84 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java @@ -270,9 +270,9 @@ void recordAttemptFailure(@Nullable StorageClientMetrics metrics, Throwable e) { static void recordRpcCallDuration( @Nullable StorageClientMetrics metrics, - @Nullable StorageMetricsContext metricsContext, + Context otelContext, double durationSeconds, - String shortMethod, + String rpcMethod, String statusCode, String networkPath, String serverAddress, @@ -280,13 +280,11 @@ static void recordRpcCallDuration( if (metrics == null || metrics.getRpcClientCallDuration() == null) { return; } - Context otelContext = - metricsContext != null ? metricsContext.getOtelContext() : Context.current(); Attributes attributes = metrics .clientAttributesBuilder() .put(StorageClientMetrics.KEY_RPC_SYSTEM_NAME, "grpc") - .put(StorageClientMetrics.KEY_RPC_METHOD, shortMethod) + .put(StorageClientMetrics.KEY_RPC_METHOD, rpcMethod) .put(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE, statusCode) .put(StorageClientMetrics.KEY_NETWORK_PATH, networkPath) .put(StorageClientMetrics.KEY_SERVER_ADDRESS, serverAddress) diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java index 214bb5462175..e24aa237a197 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java @@ -32,6 +32,14 @@ import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.Status; +import io.opentelemetry.api.metrics.DoubleHistogram; +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.MeterBuilder; +import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.ContextKey; +import io.opentelemetry.context.Scope; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.metrics.data.HistogramPointData; import io.opentelemetry.sdk.metrics.data.MetricData; @@ -44,20 +52,42 @@ import java.net.UnknownHostException; import java.util.Collection; import java.util.Collections; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; public final class StorageGrpcMetricsInterceptorTest { private static final MethodDescriptor READ_OBJECT_METHOD = - MethodDescriptor.newBuilder() - .setType(MethodDescriptor.MethodType.SERVER_STREAMING) - .setFullMethodName("google.storage.v2.Storage/ReadObject") - .setRequestMarshaller(new NoopMarshaller()) - .setResponseMarshaller(new NoopMarshaller()) - .build(); + createMethodDescriptor( + MethodDescriptor.MethodType.SERVER_STREAMING, "google.storage.v2.Storage/ReadObject"); + + private static final MethodDescriptor WRITE_OBJECT_METHOD = + createMethodDescriptor( + MethodDescriptor.MethodType.CLIENT_STREAMING, "google.storage.v2.Storage/WriteObject"); + + private static final MethodDescriptor GET_BUCKET_METHOD = + createMethodDescriptor( + MethodDescriptor.MethodType.UNARY, "google.storage.v2.Storage/GetBucket"); + + private static final MethodDescriptor DELETE_OBJECT_METHOD = + createMethodDescriptor( + MethodDescriptor.MethodType.UNARY, "google.storage.v2.Storage/DeleteObject"); + + private static MethodDescriptor createMethodDescriptor( + MethodDescriptor.MethodType type, String fullMethodName) { + return MethodDescriptor.newBuilder() + .setType(type) + .setFullMethodName(fullMethodName) + .setRequestMarshaller(new NoopMarshaller()) + .setResponseMarshaller(new NoopMarshaller()) + .build(); + } private InMemoryMetricReader reader; private SdkMeterProvider meterProvider; @@ -71,17 +101,37 @@ public void setUp() { } @Test - public void extractShortMethodName_variousInputs() { + public void interceptCall_recordsFullMethodNameAcrossOperations() { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + + for (MethodDescriptor method : + ImmutableList.of( + READ_OBJECT_METHOD, WRITE_OBJECT_METHOD, GET_BUCKET_METHOD, DELETE_OBJECT_METHOD)) { + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + ClientCall call = + interceptor.interceptCall(method, CallOptions.DEFAULT, testChannel); + call.start(new TestListener<>(), new Metadata()); + testCall.close(Status.OK, new Metadata()); + } + + MetricData rpcDurationData = + reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) + .findFirst() + .orElse(null); + + assertThat(rpcDurationData).isNotNull(); assertThat( - StorageGrpcMetricsInterceptor.extractShortMethodName( - "google.storage.v2.Storage/ReadObject")) - .isEqualTo("ReadObject"); - assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName("ReadObject")) - .isEqualTo("ReadObject"); - assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName("a/b/c")).isEqualTo("c"); - assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName(null)).isEqualTo("UNKNOWN"); - assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName("")).isEqualTo("UNKNOWN"); - assertThat(StorageGrpcMetricsInterceptor.extractShortMethodName("/")).isEqualTo("/"); + rpcDurationData.getHistogramData().getPoints().stream() + .map(p -> p.getAttributes().get(StorageClientMetrics.KEY_RPC_METHOD))) + .containsExactly( + "google.storage.v2.Storage/ReadObject", + "google.storage.v2.Storage/WriteObject", + "google.storage.v2.Storage/GetBucket", + "google.storage.v2.Storage/DeleteObject"); } @Test @@ -90,6 +140,8 @@ public void isDirectPathIp_ipv4AndIpv6() throws Exception { .isTrue(); assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("34.126.63.254"))) .isTrue(); + assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("34.126.64.1"))) + .isFalse(); assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("35.190.1.1"))) .isFalse(); assertThat(StorageGrpcMetricsInterceptor.isDirectPathIp(InetAddress.getByName("142.250.1.1"))) @@ -103,6 +155,14 @@ public void isDirectPathIp_ipv4AndIpv6() throws Exception { StorageGrpcMetricsInterceptor.isDirectPathIp( InetAddress.getByName("2001:4860:8040:ffff:ffff:ffff:ffff:ffff"))) .isTrue(); + assertThat( + StorageGrpcMetricsInterceptor.isDirectPathIp( + InetAddress.getByName("2001:4860:8041::1"))) + .isTrue(); + assertThat( + StorageGrpcMetricsInterceptor.isDirectPathIp( + InetAddress.getByName("2001:4860:8044::1"))) + .isFalse(); assertThat( StorageGrpcMetricsInterceptor.isDirectPathIp( InetAddress.getByName("2001:4860:8080:0000:0000:0000:0000:0001"))) @@ -121,7 +181,7 @@ public void detectNetworkPath_cases() throws Exception { Grpc.TRANSPORT_ATTR_REMOTE_ADDR, new InetSocketAddress(InetAddress.getByName("34.126.1.2"), 443)) .build(); - assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(directPathAttrs, false)) + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(directPathAttrs)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); Attributes cloudPathAttrs = @@ -130,12 +190,12 @@ public void detectNetworkPath_cases() throws Exception { Grpc.TRANSPORT_ATTR_REMOTE_ADDR, new InetSocketAddress(InetAddress.getByName("142.250.1.1"), 443)) .build(); - assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(cloudPathAttrs, false)) + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(cloudPathAttrs)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_CLOUDPATH); - assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(null, true)) - .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); - assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(null, false)) + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(Attributes.EMPTY)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_UNKNOWN); + assertThat(StorageGrpcMetricsInterceptor.detectNetworkPath(null)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_UNKNOWN); } @@ -177,7 +237,8 @@ public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws io.opentelemetry.api.common.Attributes attrs = point.getAttributes(); assertThat(attrs.get(StorageClientMetrics.KEY_RPC_SYSTEM_NAME)).isEqualTo("grpc"); - assertThat(attrs.get(StorageClientMetrics.KEY_RPC_METHOD)).isEqualTo("ReadObject"); + assertThat(attrs.get(StorageClientMetrics.KEY_RPC_METHOD)) + .isEqualTo("google.storage.v2.Storage/ReadObject"); assertThat(attrs.get(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE)).isEqualTo("OK"); assertThat(attrs.get(StorageClientMetrics.KEY_NETWORK_PATH)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_CLOUDPATH); @@ -263,7 +324,7 @@ public void interceptCall_nullDefaultEndpoint_fallsBackToChannelAuthority() { } @Test - public void interceptCall_directPathEndpoint_recordsDirectPathAndServerAddress() { + public void googleC2pEndpoint_setsUnknownWhenRemoteAddrAbsent() { StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor( @@ -286,7 +347,7 @@ public void interceptCall_directPathEndpoint_recordsDirectPathAndServerAddress() assertThat(rpcDurationData).isNotNull(); HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); assertThat(point.getAttributes().get(StorageClientMetrics.KEY_NETWORK_PATH)) - .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); + .isEqualTo(StorageClientMetrics.NETWORK_PATH_UNKNOWN); assertThat(point.getAttributes().get(StorageClientMetrics.KEY_SERVER_ADDRESS)) .isEqualTo("storage.googleapis.com"); } @@ -375,6 +436,74 @@ public void interceptCall_withStorageMetricsContext_propagatesContext() { assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); } + @Test + public void interceptCall_withoutStorageMetricsContext_asyncOnCloseUsesCallerContext() + throws Exception { + ContextKey testKey = ContextKey.named("caller-trace-key"); + Context callerContext = Context.root().with(testKey, "caller-trace-value"); + Context callbackThreadContext = Context.root().with(testKey, "callback-thread-value"); + + AtomicReference recordedContext = new AtomicReference<>(); + DoubleHistogram mockHistogram = Mockito.mock(DoubleHistogram.class); + Mockito.doAnswer( + invocation -> { + recordedContext.set(invocation.getArgument(2)); + return null; + }) + .when(mockHistogram) + .record( + Mockito.anyDouble(), + Mockito.any(io.opentelemetry.api.common.Attributes.class), + Mockito.any(Context.class)); + + DoubleHistogramBuilder mockHistBuilder = + Mockito.mock(DoubleHistogramBuilder.class, Mockito.RETURNS_SELF); + Mockito.when(mockHistBuilder.build()).thenReturn(mockHistogram); + Mockito.when(mockHistBuilder.ofLongs()) + .thenReturn( + Mockito.mock( + io.opentelemetry.api.metrics.LongHistogramBuilder.class, + Mockito.RETURNS_DEEP_STUBS)); + + Meter mockMeter = Mockito.mock(Meter.class, Mockito.RETURNS_DEEP_STUBS); + Mockito.when(mockMeter.histogramBuilder(Mockito.anyString())).thenReturn(mockHistBuilder); + + MeterBuilder mockMeterBuilder = Mockito.mock(MeterBuilder.class, Mockito.RETURNS_SELF); + Mockito.when(mockMeterBuilder.build()).thenReturn(mockMeter); + + MeterProvider mockMeterProvider = Mockito.mock(MeterProvider.class); + Mockito.when(mockMeterProvider.meterBuilder(Mockito.anyString())).thenReturn(mockMeterBuilder); + + StorageClientMetrics metrics = StorageClientMetrics.create(mockMeterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = + new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + + TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + + final ClientCall call; + try (Scope ignored = callerContext.makeCurrent()) { + call = interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + } + call.start(new TestListener<>(), new Metadata()); + + CountDownLatch latch = new CountDownLatch(1); + Thread asyncCallbackThread = + new Thread( + () -> { + try (Scope ignored = callbackThreadContext.makeCurrent()) { + testCall.close(Status.OK, new Metadata()); + } finally { + latch.countDown(); + } + }); + asyncCallbackThread.start(); + assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue(); + + assertThat(recordedContext.get()).isSameInstanceAs(callerContext); + assertThat(recordedContext.get().get(testKey)).isEqualTo("caller-trace-value"); + } + @Test public void grpcStorageOptions_interceptorProviderWiring() throws Exception { GrpcStorageOptions defaultOptions = From 6a754745485028974338f751c9e7db113990d584 Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Sat, 7 Nov 2026 19:46:05 +0000 Subject: [PATCH 07/10] test(storage): align RetryingMetricsTest with DEADLINE_EXCEEDED error mapping Update recordAttemptError_specificExceptions_mappedToCanonicalTypes to assert STATUS_DEADLINE_EXCEEDED (1) and STATUS_TIMEOUT (1) following the StorageErrorTypeMapper update. [Generated-by: AI] --- .../java/com/google/cloud/storage/RetryingMetricsTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/RetryingMetricsTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/RetryingMetricsTest.java index 719096ed405c..5fb160ec4a60 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/RetryingMetricsTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/RetryingMetricsTest.java @@ -25,6 +25,7 @@ import static com.google.cloud.storage.StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS; import static com.google.cloud.storage.StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ERRORS; import static com.google.cloud.storage.StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS; +import static com.google.cloud.storage.StorageErrorTypeMapper.STATUS_DEADLINE_EXCEEDED; import static com.google.cloud.storage.StorageErrorTypeMapper.STATUS_DNS_FAILURE; import static com.google.cloud.storage.StorageErrorTypeMapper.STATUS_NOT_FOUND; import static com.google.cloud.storage.StorageErrorTypeMapper.STATUS_OK; @@ -286,7 +287,8 @@ public void recordAttemptError_specificExceptions_mappedToCanonicalTypes() { assertThat(attemptCounter.get()).isEqualTo(3); assertThat(se).isNotNull(); assertOutcomeMetrics(STATUS_DNS_FAILURE, 3L, 3L); - assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_TIMEOUT, 2L); + assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_DEADLINE_EXCEEDED, 1L); + assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_TIMEOUT, 1L); assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_DNS_FAILURE, 1L); } From 4420530ecd58083407ea238ec07af7defb86979e Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Thu, 12 Nov 2026 07:34:23 +0000 Subject: [PATCH 08/10] fix(storage): address PR review feedback on gRPC metrics interceptor [Generated-by: AI] --- .../cloud/storage/GrpcStorageOptions.java | 2 +- .../cloud/storage/StorageClientMetrics.java | 26 +++- .../StorageGrpcMetricsInterceptor.java | 38 ++--- .../cloud/storage/StorageMetricsContext.java | 30 +--- .../StorageGrpcMetricsInterceptorTest.java | 131 +++++++++++------- 5 files changed, 128 insertions(+), 99 deletions(-) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java index 89123b1e8398..cbb7782355d1 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java @@ -337,7 +337,7 @@ private Tuple> resolveSettingsAndOpts() throw StorageClientMetrics clientMetrics = getStorageClientMetrics(); if (clientMetrics != null) { StorageGrpcMetricsInterceptor metricsInterceptor = - new StorageGrpcMetricsInterceptor(clientMetrics, getClock(), endpoint); + new StorageGrpcMetricsInterceptor(clientMetrics, getClock()); GrpcInterceptorProvider combinedProvider = grpcInterceptorProvider == null || NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java index 36175a3ee4f4..55f33c1ba7e4 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java @@ -26,6 +26,7 @@ import io.opentelemetry.api.metrics.LongUpDownCounter; import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.context.Context; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import java.util.logging.Logger; import org.checkerframework.checker.nullness.qual.Nullable; @@ -171,10 +172,12 @@ static StorageClientMetrics create( .put(KEY_GCP_CLIENT_VERSION, StorageOptions.version()) .put(KEY_GCP_CLIENT_ARTIFACT, ARTIFACT_STORAGE) .build(); + Attributes baseAttributes = + clientAttributes.toBuilder().put(KEY_SERVER_ADDRESS, "storage.googleapis.com").build(); return new StorageClientMetrics( meter, clientAttributes, - clientAttributes, + baseAttributes, ownedMeterProvider, enableOtelMetrics, enableOtelDebugMetrics); @@ -448,6 +451,27 @@ StorageMetricsContext createMultiStepContext(String operationName) { return StorageMetricsContext.createForMultiStep(this, operationName, baseAttributes); } + void recordRpcCallDuration( + Context otelContext, + double durationSeconds, + String rpcMethod, + String statusCode, + String networkPath, + String errorType) { + if (rpcClientCallDuration == null) { + return; + } + Attributes attributes = + baseAttributes.toBuilder() + .put(KEY_RPC_SYSTEM_NAME, "grpc") + .put(KEY_RPC_METHOD, rpcMethod) + .put(KEY_RPC_RESPONSE_STATUS_CODE, statusCode) + .put(KEY_NETWORK_PATH, networkPath) + .put(KEY_ERROR_TYPE, errorType) + .build(); + rpcClientCallDuration.record(durationSeconds, attributes, otelContext); + } + @Nullable static StorageMetricsContext createContext( @Nullable StorageClientMetrics metrics, String operationName) { diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java index f1cdd8a164c8..a610262201d3 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -16,6 +16,8 @@ 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; @@ -43,34 +45,22 @@ @NullMarked final class StorageGrpcMetricsInterceptor implements ClientInterceptor { - @Nullable private final StorageClientMetrics metrics; + private final StorageClientMetrics metrics; private final ApiClock clock; - @Nullable private final String defaultServerAddress; - StorageGrpcMetricsInterceptor( - @Nullable StorageClientMetrics metrics, ApiClock clock, @Nullable String defaultEndpoint) { - this.metrics = metrics; - this.clock = clock; - this.defaultServerAddress = - OpenTelemetryBootstrappingUtils.extractServerAddress(defaultEndpoint); + StorageGrpcMetricsInterceptor(StorageClientMetrics metrics, ApiClock clock) { + this.metrics = requireNonNull(metrics, "metrics cannot be null"); + this.clock = requireNonNull(clock, "clock cannot be null"); } @Override public ClientCall interceptCall( MethodDescriptor method, CallOptions callOptions, Channel next) { - if (metrics == null || metrics.getRpcClientCallDuration() == null) { + if (metrics.getRpcClientCallDuration() == null) { return next.newCall(method, callOptions); } - String fullMethodName = method.getFullMethodName(); - String rpcMethod = - fullMethodName != null && !fullMethodName.isEmpty() ? fullMethodName : "UNKNOWN"; - String resolvedServerAddress = - defaultServerAddress != null - ? defaultServerAddress - : OpenTelemetryBootstrappingUtils.extractServerAddress(next.authority()); - final String serverAddress = resolvedServerAddress != null ? resolvedServerAddress : ""; - + String rpcMethod = method.getFullMethodName(); StorageMetricsContext metricsCtx = StorageMetricsContext.from(callOptions); Context otelContext = metricsCtx != null ? metricsCtx.getOtelContext() : Context.current(); @@ -85,25 +75,25 @@ public void start(Listener responseListener, Metadata headers) { @Override public void onClose(@Nullable Status status, @Nullable Metadata trailers) { try { - if (callRecorded.compareAndSet(false, true) - && metrics.getRpcClientCallDuration() != null) { + if (callRecorded.compareAndSet(false, true)) { double durationSeconds = Math.max(0.0, (clock.nanoTime() - startTimeNanos) / 1_000_000_000.0); String networkPath = detectNetworkPath(getAttributes()); + if (metricsCtx != null) { + metricsCtx.setNetworkPath(networkPath); + } String statusCode = status != null && status.getCode() != null ? status.getCode().name() : StorageErrorTypeMapper.STATUS_UNKNOWN; String errorType = StorageErrorTypeMapper.mapGrpcStatus(status); - StorageMetricsContext.recordRpcCallDuration( - metrics, + metrics.recordRpcCallDuration( otelContext, durationSeconds, rpcMethod, statusCode, networkPath, - serverAddress, errorType); } } finally { @@ -143,7 +133,7 @@ static boolean isDirectPathIp(InetAddress addr) { && b[2] == 0x48 && b[3] == 0x60 && b[4] == (byte) 0x80 - && (b[5] & 0xFC) == 0x40; + && (b[5] & 0xC0) == 0x40; } return false; } diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java index 8a3ca6863f12..37d97629ab94 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java @@ -57,6 +57,7 @@ final class StorageMetricsContext { private final AtomicInteger attemptCount = new AtomicInteger(0); private final AtomicLong requestBytes = new AtomicLong(0L); private final AtomicLong responseBytes = new AtomicLong(0L); + @Nullable private volatile String networkPath; private StorageMetricsContext( @Nullable StorageClientMetrics metrics, @@ -268,29 +269,8 @@ void recordAttemptFailure(@Nullable StorageClientMetrics metrics, Throwable e) { } } - static void recordRpcCallDuration( - @Nullable StorageClientMetrics metrics, - Context otelContext, - double durationSeconds, - String rpcMethod, - String statusCode, - String networkPath, - String serverAddress, - String errorType) { - if (metrics == null || metrics.getRpcClientCallDuration() == null) { - return; - } - Attributes attributes = - metrics - .clientAttributesBuilder() - .put(StorageClientMetrics.KEY_RPC_SYSTEM_NAME, "grpc") - .put(StorageClientMetrics.KEY_RPC_METHOD, rpcMethod) - .put(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE, statusCode) - .put(StorageClientMetrics.KEY_NETWORK_PATH, networkPath) - .put(StorageClientMetrics.KEY_SERVER_ADDRESS, serverAddress) - .put(StorageClientMetrics.KEY_ERROR_TYPE, errorType) - .build(); - metrics.getRpcClientCallDuration().record(durationSeconds, attributes, otelContext); + void setNetworkPath(String networkPath) { + this.networkPath = networkPath; } /** @@ -349,6 +329,10 @@ private Attributes buildAttemptBaseAttributes(StorageClientMetrics metrics) { } private String resolveNetworkPath() { + String detected = this.networkPath; + if (detected != null && !detected.isEmpty()) { + return detected; + } String path = baseAttributes.get(StorageClientMetrics.KEY_NETWORK_PATH); if (path != null && !path.isEmpty()) { return path; diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java index e24aa237a197..790431b33895 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java @@ -103,8 +103,7 @@ public void setUp() { @Test public void interceptCall_recordsFullMethodNameAcrossOperations() { StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); for (MethodDescriptor method : ImmutableList.of( @@ -162,7 +161,11 @@ public void isDirectPathIp_ipv4AndIpv6() throws Exception { assertThat( StorageGrpcMetricsInterceptor.isDirectPathIp( InetAddress.getByName("2001:4860:8044::1"))) - .isFalse(); + .isTrue(); + assertThat( + StorageGrpcMetricsInterceptor.isDirectPathIp( + InetAddress.getByName("2001:4860:807f::1"))) + .isTrue(); assertThat( StorageGrpcMetricsInterceptor.isDirectPathIp( InetAddress.getByName("2001:4860:8080:0000:0000:0000:0000:0001"))) @@ -202,8 +205,7 @@ public void detectNetworkPath_cases() throws Exception { @Test public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws Exception { StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, "https://storage.googleapis.com"); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); Attributes callAttrs = Attributes.newBuilder() @@ -250,8 +252,7 @@ public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws @Test public void interceptCall_callFails_recordsErrorType() throws Exception { StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); @@ -283,8 +284,7 @@ public void interceptCall_callFails_recordsErrorType() throws Exception { @Test public void interceptCall_disabledMetrics_doesNotRecord() { StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, false, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); @@ -297,38 +297,19 @@ public void interceptCall_disabledMetrics_doesNotRecord() { assertThat(reader.collectAllMetrics()).isEmpty(); } - @Test - public void interceptCall_nullDefaultEndpoint_fallsBackToChannelAuthority() { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, null); - - TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com:443"); - - ClientCall call = - interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); - call.start(new TestListener<>(), new Metadata()); - testCall.close(Status.OK, new Metadata()); - - MetricData rpcDurationData = - reader.collectAllMetrics().stream() - .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) - .findFirst() - .orElse(null); - - assertThat(rpcDurationData).isNotNull(); - HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); - assertThat(point.getAttributes().get(StorageClientMetrics.KEY_SERVER_ADDRESS)) - .isEqualTo("storage.googleapis.com"); - } - @Test public void googleC2pEndpoint_setsUnknownWhenRemoteAddrAbsent() { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor( - metrics, clock, " google-c2p:///storage.googleapis.com "); + GrpcStorageOptions grpcOptions = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(true) + .setMeterProvider(meterProvider) + .setHost(" google-c2p:///storage.googleapis.com ") + .build(); + StorageClientMetrics metrics = grpcOptions.getStorageClientMetrics(); + assertThat(metrics).isNotNull(); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); @@ -355,8 +336,7 @@ public void googleC2pEndpoint_setsUnknownWhenRemoteAddrAbsent() { @Test public void interceptCall_statusWithNetworkCause_recordsCauseErrorType() { StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); @@ -385,8 +365,7 @@ public void interceptCall_statusWithNetworkCause_recordsCauseErrorType() { @Test public void interceptCall_duplicateOnClose_recordsOnlyOnce() { StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); @@ -410,15 +389,21 @@ public void interceptCall_duplicateOnClose_recordsOnlyOnce() { } @Test - public void interceptCall_withStorageMetricsContext_propagatesContext() { + public void interceptCall_withStorageMetricsContext_propagatesContextAndFeedsNetworkPath() + throws Exception { StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); StorageMetricsContext metricsContext = StorageMetricsContext.create("ReadObject"); CallOptions callOptions = metricsContext.attach(CallOptions.DEFAULT); - TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); + Attributes directPathAttrs = + Attributes.newBuilder() + .set( + Grpc.TRANSPORT_ATTR_REMOTE_ADDR, + new InetSocketAddress(InetAddress.getByName("34.126.1.2"), 443)) + .build(); + TestClientCall testCall = new TestClientCall<>(directPathAttrs); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); ClientCall call = @@ -426,14 +411,34 @@ public void interceptCall_withStorageMetricsContext_propagatesContext() { call.start(new TestListener<>(), new Metadata()); testCall.close(Status.OK, new Metadata()); + metricsContext.recordAttemptSuccess(metrics); + + Collection allMetrics = reader.collectAllMetrics(); MetricData rpcDurationData = - reader.collectAllMetrics().stream() + allMetrics.stream() .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) .findFirst() .orElse(null); assertThat(rpcDurationData).isNotNull(); assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); + + MetricData attemptsData = + allMetrics.stream() + .filter( + m -> m.getName().equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS)) + .findFirst() + .orElse(null); + assertThat(attemptsData).isNotNull(); + assertThat( + attemptsData + .getLongSumData() + .getPoints() + .iterator() + .next() + .getAttributes() + .get(StorageClientMetrics.KEY_NETWORK_PATH)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); } @Test @@ -475,8 +480,7 @@ public void interceptCall_withoutStorageMetricsContext_asyncOnCloseUsesCallerCon Mockito.when(mockMeterProvider.meterBuilder(Mockito.anyString())).thenReturn(mockMeterBuilder); StorageClientMetrics metrics = StorageClientMetrics.create(mockMeterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = - new StorageGrpcMetricsInterceptor(metrics, clock, "storage.googleapis.com"); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); @@ -589,6 +593,33 @@ public ClientCall interceptCall( assertThat(emptyCombinedProvider.getInterceptors().get(0)) .isInstanceOf(StorageGrpcMetricsInterceptor.class); assertThat(customCalled.get()).isFalse(); + + GrpcInterceptorProvider userProvider = () -> ImmutableList.of(customInterceptor); + GrpcStorageOptions disabledCustomOptions = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(false) + .setGrpcInterceptorProvider(userProvider) + .build(); + InstantiatingGrpcChannelProvider disabledCustomChannelProvider = + (InstantiatingGrpcChannelProvider) + disabledCustomOptions.getStorageSettings().getTransportChannelProvider(); + GrpcInterceptorProvider disabledCustomExtracted = + extractInterceptorProvider(disabledCustomChannelProvider); + assertThat(disabledCustomExtracted).isSameInstanceAs(userProvider); + assertThat(disabledCustomExtracted.getInterceptors()).containsExactly(customInterceptor); + + GrpcStorageOptions disabledDefaultOptions = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(false) + .build(); + InstantiatingGrpcChannelProvider disabledDefaultChannelProvider = + (InstantiatingGrpcChannelProvider) + disabledDefaultOptions.getStorageSettings().getTransportChannelProvider(); + assertThat(extractInterceptorProvider(disabledDefaultChannelProvider)).isNull(); } private static GrpcInterceptorProvider extractInterceptorProvider( From f5152975dcb2d79c5e11ddce5f6693227d31b23f Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Mon, 16 Nov 2026 12:12:49 +0000 Subject: [PATCH 09/10] fix(storage): address follow-up review comments on PR #14743 - Set network.path on StorageMetricsContext in onHeaders so streaming ReadObject attempts capture peer path before onClose - Remove @Nullable from onClose parameters and simplify status.getCode().name() - Assert network.path=directpath on rpc.client.call.duration point in StorageGrpcMetricsInterceptorTest [Generated-by: AI] --- .../StorageGrpcMetricsInterceptor.java | 15 +++-- .../StorageGrpcMetricsInterceptorTest.java | 64 ++++++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java index a610262201d3..c923a0e75948 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -73,7 +73,15 @@ public void start(Listener responseListener, Metadata headers) { super.start( new SimpleForwardingClientCallListener(responseListener) { @Override - public void onClose(@Nullable Status status, @Nullable Metadata trailers) { + public void onHeaders(Metadata headers) { + if (metricsCtx != null) { + metricsCtx.setNetworkPath(detectNetworkPath(getAttributes())); + } + super.onHeaders(headers); + } + + @Override + public void onClose(Status status, Metadata trailers) { try { if (callRecorded.compareAndSet(false, true)) { double durationSeconds = @@ -82,10 +90,7 @@ public void onClose(@Nullable Status status, @Nullable Metadata trailers) { if (metricsCtx != null) { metricsCtx.setNetworkPath(networkPath); } - String statusCode = - status != null && status.getCode() != null - ? status.getCode().name() - : StorageErrorTypeMapper.STATUS_UNKNOWN; + String statusCode = status.getCode().name(); String errorType = StorageErrorTypeMapper.mapGrpcStatus(status); metrics.recordRpcCallDuration( diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java index 790431b33895..6f289a90b0d5 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java @@ -422,6 +422,10 @@ public void interceptCall_withStorageMetricsContext_propagatesContextAndFeedsNet assertThat(rpcDurationData).isNotNull(); assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); + HistogramPointData durationPoint = + rpcDurationData.getHistogramData().getPoints().iterator().next(); + assertThat(durationPoint.getAttributes().get(StorageClientMetrics.KEY_NETWORK_PATH)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); MetricData attemptsData = allMetrics.stream() @@ -441,6 +445,51 @@ public void interceptCall_withStorageMetricsContext_propagatesContextAndFeedsNet .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); } + @Test + public void interceptCall_streamingReadOnHeaders_setsNetworkPathBeforeOnClose() throws Exception { + StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); + StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); + + StorageMetricsContext metricsContext = StorageMetricsContext.create("ReadObject"); + CallOptions callOptions = metricsContext.attach(CallOptions.DEFAULT); + + Attributes directPathAttrs = + Attributes.newBuilder() + .set( + Grpc.TRANSPORT_ATTR_REMOTE_ADDR, + new InetSocketAddress(InetAddress.getByName("34.126.0.1"), 443)) + .build(); + TestClientCall testCall = new TestClientCall<>(directPathAttrs); + TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + + ClientCall call = + interceptor.interceptCall(READ_OBJECT_METHOD, callOptions, testChannel); + TestListener listener = new TestListener<>(); + call.start(listener, new Metadata()); + + // Simulate streaming ReadObject where headers arrive and the attempt completes before onClose + testCall.receiveHeaders(new Metadata()); + assertThat(listener.headersReceived).isTrue(); + metricsContext.recordAttemptSuccess(metrics); + + MetricData attemptsData = + reader.collectAllMetrics().stream() + .filter( + m -> m.getName().equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS)) + .findFirst() + .orElse(null); + assertThat(attemptsData).isNotNull(); + assertThat( + attemptsData + .getLongSumData() + .getPoints() + .iterator() + .next() + .getAttributes() + .get(StorageClientMetrics.KEY_NETWORK_PATH)) + .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); + } + @Test public void interceptCall_withoutStorageMetricsContext_asyncOnCloseUsesCallerContext() throws Exception { @@ -659,6 +708,12 @@ public Attributes getAttributes() { return attributes; } + void receiveHeaders(Metadata headers) { + if (listener != null) { + listener.onHeaders(headers); + } + } + void close(Status status, Metadata trailers) { if (listener != null) { listener.onClose(status, trailers); @@ -688,7 +743,14 @@ public String authority() { } } - private static final class TestListener extends ClientCall.Listener {} + private static final class TestListener extends ClientCall.Listener { + boolean headersReceived = false; + + @Override + public void onHeaders(Metadata headers) { + headersReceived = true; + } + } private static final class NoopMarshaller implements MethodDescriptor.Marshaller { @Override From a0b0d5b6755774b933c46855211d9d9bce657229 Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Wed, 18 Nov 2026 08:39:37 +0000 Subject: [PATCH 10/10] fix(storage): address review comments on gRPC call duration metrics - Simplify GrpcInterceptorProvider wiring in GrpcStorageOptions to a single lambda - Remove unreachable null/duplicate guards and Math.max clamping in StorageGrpcMetricsInterceptor - Rely on StorageClientMetrics.fromOptions baseAttributes for rpc.system.name and server.address - Deduplicate and streamline StorageGrpcMetricsInterceptorTest [Generated-by: AI] --- .../cloud/storage/GrpcStorageOptions.java | 25 +- .../cloud/storage/StorageClientMetrics.java | 5 +- .../StorageGrpcMetricsInterceptor.java | 32 +- .../StorageGrpcMetricsInterceptorTest.java | 318 ++++-------------- 4 files changed, 77 insertions(+), 303 deletions(-) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java index cbb7782355d1..514fc77fb8f1 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java @@ -338,23 +338,14 @@ private Tuple> resolveSettingsAndOpts() throw if (clientMetrics != null) { StorageGrpcMetricsInterceptor metricsInterceptor = new StorageGrpcMetricsInterceptor(clientMetrics, getClock()); - GrpcInterceptorProvider combinedProvider = - grpcInterceptorProvider == null - || NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider) - ? () -> ImmutableList.of(metricsInterceptor) - : () -> { - List existing = grpcInterceptorProvider.getInterceptors(); - if (existing == null || existing.isEmpty()) { - return ImmutableList.of(metricsInterceptor); - } - return ImmutableList.builder() - .addAll(existing) - .add(metricsInterceptor) - .build(); - }; - channelProviderBuilder.setInterceptorProvider(combinedProvider); - } else if (grpcInterceptorProvider != null - && !NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider)) { + GrpcInterceptorProvider userProvider = grpcInterceptorProvider; + channelProviderBuilder.setInterceptorProvider( + () -> + ImmutableList.builder() + .addAll(userProvider.getInterceptors()) + .add(metricsInterceptor) + .build()); + } else if (!NoopGrpcInterceptorProvider.INSTANCE.equals(grpcInterceptorProvider)) { channelProviderBuilder.setInterceptorProvider(grpcInterceptorProvider); } diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java index 55f33c1ba7e4..efcb506b4be1 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java @@ -172,12 +172,10 @@ static StorageClientMetrics create( .put(KEY_GCP_CLIENT_VERSION, StorageOptions.version()) .put(KEY_GCP_CLIENT_ARTIFACT, ARTIFACT_STORAGE) .build(); - Attributes baseAttributes = - clientAttributes.toBuilder().put(KEY_SERVER_ADDRESS, "storage.googleapis.com").build(); return new StorageClientMetrics( meter, clientAttributes, - baseAttributes, + clientAttributes, ownedMeterProvider, enableOtelMetrics, enableOtelDebugMetrics); @@ -463,7 +461,6 @@ void recordRpcCallDuration( } Attributes attributes = baseAttributes.toBuilder() - .put(KEY_RPC_SYSTEM_NAME, "grpc") .put(KEY_RPC_METHOD, rpcMethod) .put(KEY_RPC_RESPONSE_STATUS_CODE, statusCode) .put(KEY_NETWORK_PATH, networkPath) diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java index c923a0e75948..b33f1f1a4300 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageGrpcMetricsInterceptor.java @@ -34,7 +34,6 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; -import java.util.concurrent.atomic.AtomicBoolean; import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; @@ -56,10 +55,6 @@ final class StorageGrpcMetricsInterceptor implements ClientInterceptor { @Override public ClientCall interceptCall( MethodDescriptor method, CallOptions callOptions, Channel next) { - if (metrics.getRpcClientCallDuration() == null) { - return next.newCall(method, callOptions); - } - String rpcMethod = method.getFullMethodName(); StorageMetricsContext metricsCtx = StorageMetricsContext.from(callOptions); Context otelContext = metricsCtx != null ? metricsCtx.getOtelContext() : Context.current(); @@ -68,7 +63,6 @@ public ClientCall interceptCall( @Override public void start(Listener responseListener, Metadata headers) { long startTimeNanos = clock.nanoTime(); - AtomicBoolean callRecorded = new AtomicBoolean(false); super.start( new SimpleForwardingClientCallListener(responseListener) { @@ -83,24 +77,16 @@ public void onHeaders(Metadata headers) { @Override public void onClose(Status status, Metadata trailers) { try { - if (callRecorded.compareAndSet(false, true)) { - double durationSeconds = - Math.max(0.0, (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); + 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); } diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java index 6f289a90b0d5..fe0217406cb6 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageGrpcMetricsInterceptorTest.java @@ -34,6 +34,7 @@ import io.grpc.Status; import io.opentelemetry.api.metrics.DoubleHistogram; import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.LongHistogramBuilder; import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.MeterBuilder; import io.opentelemetry.api.metrics.MeterProvider; @@ -49,14 +50,12 @@ import java.lang.reflect.Field; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.UnknownHostException; -import java.util.Collection; import java.util.Collections; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; +import org.jspecify.annotations.Nullable; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -64,73 +63,33 @@ public final class StorageGrpcMetricsInterceptorTest { private static final MethodDescriptor READ_OBJECT_METHOD = - createMethodDescriptor( - MethodDescriptor.MethodType.SERVER_STREAMING, "google.storage.v2.Storage/ReadObject"); - - private static final MethodDescriptor WRITE_OBJECT_METHOD = - createMethodDescriptor( - MethodDescriptor.MethodType.CLIENT_STREAMING, "google.storage.v2.Storage/WriteObject"); - - private static final MethodDescriptor GET_BUCKET_METHOD = - createMethodDescriptor( - MethodDescriptor.MethodType.UNARY, "google.storage.v2.Storage/GetBucket"); - - private static final MethodDescriptor DELETE_OBJECT_METHOD = - createMethodDescriptor( - MethodDescriptor.MethodType.UNARY, "google.storage.v2.Storage/DeleteObject"); - - private static MethodDescriptor createMethodDescriptor( - MethodDescriptor.MethodType type, String fullMethodName) { - return MethodDescriptor.newBuilder() - .setType(type) - .setFullMethodName(fullMethodName) - .setRequestMarshaller(new NoopMarshaller()) - .setResponseMarshaller(new NoopMarshaller()) - .build(); - } + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.SERVER_STREAMING) + .setFullMethodName("google.storage.v2.Storage/ReadObject") + .setRequestMarshaller(new NoopMarshaller()) + .setResponseMarshaller(new NoopMarshaller()) + .build(); private InMemoryMetricReader reader; private SdkMeterProvider meterProvider; private FakeClock clock; + private StorageClientMetrics metrics; + private StorageGrpcMetricsInterceptor interceptor; @Before public void setUp() { reader = InMemoryMetricReader.create(); meterProvider = SdkMeterProvider.builder().registerMetricReader(reader).build(); clock = new FakeClock(); - } - - @Test - public void interceptCall_recordsFullMethodNameAcrossOperations() { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - - for (MethodDescriptor method : - ImmutableList.of( - READ_OBJECT_METHOD, WRITE_OBJECT_METHOD, GET_BUCKET_METHOD, DELETE_OBJECT_METHOD)) { - TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); - ClientCall call = - interceptor.interceptCall(method, CallOptions.DEFAULT, testChannel); - call.start(new TestListener<>(), new Metadata()); - testCall.close(Status.OK, new Metadata()); - } - - MetricData rpcDurationData = - reader.collectAllMetrics().stream() - .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) - .findFirst() - .orElse(null); - - assertThat(rpcDurationData).isNotNull(); - assertThat( - rpcDurationData.getHistogramData().getPoints().stream() - .map(p -> p.getAttributes().get(StorageClientMetrics.KEY_RPC_METHOD))) - .containsExactly( - "google.storage.v2.Storage/ReadObject", - "google.storage.v2.Storage/WriteObject", - "google.storage.v2.Storage/GetBucket", - "google.storage.v2.Storage/DeleteObject"); + metrics = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(true) + .setMeterProvider(meterProvider) + .build() + .getStorageClientMetrics(); + interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); } @Test @@ -204,9 +163,6 @@ public void detectNetworkPath_cases() throws Exception { @Test public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws Exception { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - Attributes callAttrs = Attributes.newBuilder() .set( @@ -214,7 +170,7 @@ public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws new InetSocketAddress(InetAddress.getByName("142.250.1.1"), 443)) .build(); TestClientCall testCall = new TestClientCall<>(callAttrs); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com:443"); + TestChannel testChannel = new TestChannel(testCall); ClientCall call = interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); @@ -224,12 +180,7 @@ public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws clock.advanceNanos(500_000_000L); // 0.5s testCall.close(Status.OK, new Metadata()); - Collection metricData = reader.collectAllMetrics(); - MetricData rpcDurationData = - metricData.stream() - .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) - .findFirst() - .orElse(null); + MetricData rpcDurationData = findMetric(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION); assertThat(rpcDurationData).isNotNull(); assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); @@ -250,12 +201,9 @@ public void interceptCall_unaryCallSuccess_recordsMetricWithErrorTypeOk() throws } @Test - public void interceptCall_callFails_recordsErrorType() throws Exception { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - + public void interceptCall_callFails_recordsErrorType() { TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + TestChannel testChannel = new TestChannel(testCall); ClientCall call = interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); @@ -264,12 +212,7 @@ public void interceptCall_callFails_recordsErrorType() throws Exception { clock.advanceNanos(100_000_000L); // 0.1s testCall.close(Status.NOT_FOUND, new Metadata()); - Collection metricData = reader.collectAllMetrics(); - MetricData rpcDurationData = - metricData.stream() - .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) - .findFirst() - .orElse(null); + MetricData rpcDurationData = findMetric(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION); assertThat(rpcDurationData).isNotNull(); HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); @@ -281,120 +224,10 @@ public void interceptCall_callFails_recordsErrorType() throws Exception { .isEqualTo("storage.googleapis.com"); } - @Test - public void interceptCall_disabledMetrics_doesNotRecord() { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, false, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - - TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); - - ClientCall call = - interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); - call.start(new TestListener<>(), new Metadata()); - testCall.close(Status.OK, new Metadata()); - - assertThat(reader.collectAllMetrics()).isEmpty(); - } - - @Test - public void googleC2pEndpoint_setsUnknownWhenRemoteAddrAbsent() { - GrpcStorageOptions grpcOptions = - StorageOptions.grpc() - .setProjectId("test-project") - .setCredentials(NoCredentials.getInstance()) - .setEnableOtelMetrics(true) - .setMeterProvider(meterProvider) - .setHost(" google-c2p:///storage.googleapis.com ") - .build(); - StorageClientMetrics metrics = grpcOptions.getStorageClientMetrics(); - assertThat(metrics).isNotNull(); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - - TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); - - ClientCall call = - interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); - call.start(new TestListener<>(), new Metadata()); - testCall.close(Status.OK, new Metadata()); - - MetricData rpcDurationData = - reader.collectAllMetrics().stream() - .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) - .findFirst() - .orElse(null); - - assertThat(rpcDurationData).isNotNull(); - HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); - assertThat(point.getAttributes().get(StorageClientMetrics.KEY_NETWORK_PATH)) - .isEqualTo(StorageClientMetrics.NETWORK_PATH_UNKNOWN); - assertThat(point.getAttributes().get(StorageClientMetrics.KEY_SERVER_ADDRESS)) - .isEqualTo("storage.googleapis.com"); - } - - @Test - public void interceptCall_statusWithNetworkCause_recordsCauseErrorType() { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - - TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); - - ClientCall call = - interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); - call.start(new TestListener<>(), new Metadata()); - testCall.close( - Status.UNAVAILABLE.withCause(new UnknownHostException("storage.googleapis.com")), - new Metadata()); - - MetricData rpcDurationData = - reader.collectAllMetrics().stream() - .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) - .findFirst() - .orElse(null); - - assertThat(rpcDurationData).isNotNull(); - HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); - assertThat(point.getAttributes().get(StorageClientMetrics.KEY_RPC_RESPONSE_STATUS_CODE)) - .isEqualTo("UNAVAILABLE"); - assertThat(point.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) - .isEqualTo(StorageErrorTypeMapper.STATUS_DNS_FAILURE); - } - - @Test - public void interceptCall_duplicateOnClose_recordsOnlyOnce() { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - - TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); - - ClientCall call = - interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); - call.start(new TestListener<>(), new Metadata()); - testCall.close(Status.OK, new Metadata()); - testCall.close(Status.OK, new Metadata()); - - MetricData rpcDurationData = - reader.collectAllMetrics().stream() - .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) - .findFirst() - .orElse(null); - - assertThat(rpcDurationData).isNotNull(); - assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); - HistogramPointData point = rpcDurationData.getHistogramData().getPoints().iterator().next(); - assertThat(point.getCount()).isEqualTo(1L); - } - @Test public void interceptCall_withStorageMetricsContext_propagatesContextAndFeedsNetworkPath() throws Exception { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - - StorageMetricsContext metricsContext = StorageMetricsContext.create("ReadObject"); + StorageMetricsContext metricsContext = metrics.createContext("ReadObject"); CallOptions callOptions = metricsContext.attach(CallOptions.DEFAULT); Attributes directPathAttrs = @@ -404,7 +237,7 @@ public void interceptCall_withStorageMetricsContext_propagatesContextAndFeedsNet new InetSocketAddress(InetAddress.getByName("34.126.1.2"), 443)) .build(); TestClientCall testCall = new TestClientCall<>(directPathAttrs); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + TestChannel testChannel = new TestChannel(testCall); ClientCall call = interceptor.interceptCall(READ_OBJECT_METHOD, callOptions, testChannel); @@ -413,13 +246,7 @@ public void interceptCall_withStorageMetricsContext_propagatesContextAndFeedsNet metricsContext.recordAttemptSuccess(metrics); - Collection allMetrics = reader.collectAllMetrics(); - MetricData rpcDurationData = - allMetrics.stream() - .filter(m -> m.getName().equals(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION)) - .findFirst() - .orElse(null); - + MetricData rpcDurationData = findMetric(StorageClientMetrics.METRIC_RPC_CLIENT_CALL_DURATION); assertThat(rpcDurationData).isNotNull(); assertThat(rpcDurationData.getHistogramData().getPoints()).hasSize(1); HistogramPointData durationPoint = @@ -427,12 +254,7 @@ public void interceptCall_withStorageMetricsContext_propagatesContextAndFeedsNet assertThat(durationPoint.getAttributes().get(StorageClientMetrics.KEY_NETWORK_PATH)) .isEqualTo(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); - MetricData attemptsData = - allMetrics.stream() - .filter( - m -> m.getName().equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS)) - .findFirst() - .orElse(null); + MetricData attemptsData = findMetric(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS); assertThat(attemptsData).isNotNull(); assertThat( attemptsData @@ -447,10 +269,7 @@ public void interceptCall_withStorageMetricsContext_propagatesContextAndFeedsNet @Test public void interceptCall_streamingReadOnHeaders_setsNetworkPathBeforeOnClose() throws Exception { - StorageClientMetrics metrics = StorageClientMetrics.create(meterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); - - StorageMetricsContext metricsContext = StorageMetricsContext.create("ReadObject"); + StorageMetricsContext metricsContext = metrics.createContext("ReadObject"); CallOptions callOptions = metricsContext.attach(CallOptions.DEFAULT); Attributes directPathAttrs = @@ -460,7 +279,7 @@ public void interceptCall_streamingReadOnHeaders_setsNetworkPathBeforeOnClose() new InetSocketAddress(InetAddress.getByName("34.126.0.1"), 443)) .build(); TestClientCall testCall = new TestClientCall<>(directPathAttrs); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + TestChannel testChannel = new TestChannel(testCall); ClientCall call = interceptor.interceptCall(READ_OBJECT_METHOD, callOptions, testChannel); @@ -472,12 +291,7 @@ public void interceptCall_streamingReadOnHeaders_setsNetworkPathBeforeOnClose() assertThat(listener.headersReceived).isTrue(); metricsContext.recordAttemptSuccess(metrics); - MetricData attemptsData = - reader.collectAllMetrics().stream() - .filter( - m -> m.getName().equals(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS)) - .findFirst() - .orElse(null); + MetricData attemptsData = findMetric(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS); assertThat(attemptsData).isNotNull(); assertThat( attemptsData @@ -514,10 +328,7 @@ public void interceptCall_withoutStorageMetricsContext_asyncOnCloseUsesCallerCon Mockito.mock(DoubleHistogramBuilder.class, Mockito.RETURNS_SELF); Mockito.when(mockHistBuilder.build()).thenReturn(mockHistogram); Mockito.when(mockHistBuilder.ofLongs()) - .thenReturn( - Mockito.mock( - io.opentelemetry.api.metrics.LongHistogramBuilder.class, - Mockito.RETURNS_DEEP_STUBS)); + .thenReturn(Mockito.mock(LongHistogramBuilder.class, Mockito.RETURNS_DEEP_STUBS)); Meter mockMeter = Mockito.mock(Meter.class, Mockito.RETURNS_DEEP_STUBS); Mockito.when(mockMeter.histogramBuilder(Mockito.anyString())).thenReturn(mockHistBuilder); @@ -528,15 +339,23 @@ public void interceptCall_withoutStorageMetricsContext_asyncOnCloseUsesCallerCon MeterProvider mockMeterProvider = Mockito.mock(MeterProvider.class); Mockito.when(mockMeterProvider.meterBuilder(Mockito.anyString())).thenReturn(mockMeterBuilder); - StorageClientMetrics metrics = StorageClientMetrics.create(mockMeterProvider, true, false); - StorageGrpcMetricsInterceptor interceptor = new StorageGrpcMetricsInterceptor(metrics, clock); + StorageClientMetrics mockClientMetrics = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setEnableOtelMetrics(true) + .setMeterProvider(mockMeterProvider) + .build() + .getStorageClientMetrics(); + StorageGrpcMetricsInterceptor mockInterceptor = + new StorageGrpcMetricsInterceptor(mockClientMetrics, clock); TestClientCall testCall = new TestClientCall<>(Attributes.EMPTY); - TestChannel testChannel = new TestChannel(testCall, "storage.googleapis.com"); + TestChannel testChannel = new TestChannel(testCall); final ClientCall call; try (Scope ignored = callerContext.makeCurrent()) { - call = interceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); + call = mockInterceptor.interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel); } call.start(new TestListener<>(), new Metadata()); @@ -576,35 +395,7 @@ public void grpcStorageOptions_interceptorProviderWiring() throws Exception { assertThat(defaultInterceptorProvider.getInterceptors().get(0)) .isInstanceOf(StorageGrpcMetricsInterceptor.class); - GrpcStorageOptions nullProviderOptions = - StorageOptions.grpc() - .setProjectId("test-project") - .setCredentials(NoCredentials.getInstance()) - .setEnableOtelMetrics(true) - .setMeterProvider(meterProvider) - .build(); - Field providerField = GrpcStorageOptions.class.getDeclaredField("grpcInterceptorProvider"); - providerField.setAccessible(true); - providerField.set(nullProviderOptions, null); - InstantiatingGrpcChannelProvider nullChannelProvider = - (InstantiatingGrpcChannelProvider) - nullProviderOptions.getStorageSettings().getTransportChannelProvider(); - GrpcInterceptorProvider nullCombinedProvider = extractInterceptorProvider(nullChannelProvider); - assertThat(nullCombinedProvider).isNotNull(); - assertThat(nullCombinedProvider.getInterceptors()).hasSize(1); - assertThat(nullCombinedProvider.getInterceptors().get(0)) - .isInstanceOf(StorageGrpcMetricsInterceptor.class); - - AtomicBoolean customCalled = new AtomicBoolean(false); - ClientInterceptor customInterceptor = - new ClientInterceptor() { - @Override - public ClientCall interceptCall( - MethodDescriptor method, CallOptions callOptions, Channel next) { - customCalled.set(true); - return next.newCall(method, callOptions); - } - }; + ClientInterceptor customInterceptor = StorageGrpcMetricsInterceptorTest::passthroughCall; GrpcStorageOptions customOptions = StorageOptions.grpc() @@ -641,7 +432,6 @@ public ClientCall interceptCall( assertThat(emptyCombinedProvider.getInterceptors()).hasSize(1); assertThat(emptyCombinedProvider.getInterceptors().get(0)) .isInstanceOf(StorageGrpcMetricsInterceptor.class); - assertThat(customCalled.get()).isFalse(); GrpcInterceptorProvider userProvider = () -> ImmutableList.of(customInterceptor); GrpcStorageOptions disabledCustomOptions = @@ -671,6 +461,18 @@ public ClientCall interceptCall( assertThat(extractInterceptorProvider(disabledDefaultChannelProvider)).isNull(); } + private @Nullable MetricData findMetric(String name) { + return reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals(name)) + .findFirst() + .orElse(null); + } + + private static ClientCall passthroughCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { + return next.newCall(method, callOptions); + } + private static GrpcInterceptorProvider extractInterceptorProvider( InstantiatingGrpcChannelProvider channelProvider) throws Exception { Field field = InstantiatingGrpcChannelProvider.class.getDeclaredField("interceptorProvider"); @@ -723,11 +525,9 @@ void close(Status status, Metadata trailers) { private static final class TestChannel extends Channel { private final TestClientCall call; - private final String authority; - TestChannel(TestClientCall call, String authority) { + TestChannel(TestClientCall call) { this.call = call; - this.authority = authority; } @SuppressWarnings("unchecked") @@ -739,7 +539,7 @@ public ClientCall newCall( @Override public String authority() { - return authority; + return "storage.googleapis.com"; } }