Repository navigation
feat(storage): record gcp.storage.client.network.io debug metric on gRPC #15230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/storage-server-unreached-metric
Are you sure you want to change the base?
Changes from all commits
ce78c47
c33b60f
f624139
de62aa9
7151189
6e5b1da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| import io.grpc.Channel; | ||
| import io.grpc.ClientCall; | ||
| import io.grpc.ClientInterceptor; | ||
| import io.grpc.ClientStreamTracer; | ||
| import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; | ||
| import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener; | ||
| import io.grpc.Grpc; | ||
|
|
@@ -74,7 +75,14 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( | |
| ? metricsCtx.getOperationName() | ||
| : defaultOperationName(method.getBareMethodName()); | ||
|
|
||
| return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { | ||
| NetworkIoTracerFactory networkIoTracerFactory = | ||
| metrics.getNetworkIo() != null ? new NetworkIoTracerFactory() : null; | ||
| CallOptions effectiveCallOptions = | ||
| networkIoTracerFactory != null | ||
| ? callOptions.withStreamTracerFactory(networkIoTracerFactory) | ||
| : callOptions; | ||
|
|
||
| return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, effectiveCallOptions)) { | ||
| private final AtomicBoolean ttfbRecorded = new AtomicBoolean(false); | ||
| private final AtomicLong ttfbStartNanos = new AtomicLong(); | ||
| private final AtomicBoolean activeReleased = new AtomicBoolean(false); | ||
|
|
@@ -162,6 +170,13 @@ public void onClose(Status status, Metadata trailers) { | |
| metrics.recordServerUnreached( | ||
| otelContext, operationName, networkPath, errorType); | ||
| } | ||
| if (networkIoTracerFactory != null) { | ||
| metrics.recordNetworkIo( | ||
| otelContext, | ||
| networkPath, | ||
| networkIoTracerFactory.getOutboundBytes(), | ||
| networkIoTracerFactory.getInboundBytes()); | ||
| } | ||
| } finally { | ||
| releaseActive(); | ||
| super.onClose(status, trailers); | ||
|
|
@@ -287,4 +302,37 @@ private static boolean hasNonEmptyMetadata(Metadata metadata, Metadata.Key<Strin | |
| String value = metadata.get(key); | ||
| return value != null && !value.isEmpty(); | ||
| } | ||
|
|
||
| private static final class NetworkIoTracerFactory extends ClientStreamTracer.Factory { | ||
| private final AtomicLong outboundBytes = new AtomicLong(); | ||
| private final AtomicLong inboundBytes = new AtomicLong(); | ||
|
|
||
| @Override | ||
| public ClientStreamTracer newClientStreamTracer( | ||
| ClientStreamTracer.StreamInfo info, Metadata headers) { | ||
| return new ClientStreamTracer() { | ||
| @Override | ||
| public void outboundWireSize(long bytes) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (bytes > 0) { | ||
| outboundBytes.addAndGet(bytes); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void inboundWireSize(long bytes) { | ||
| if (bytes > 0) { | ||
| inboundBytes.addAndGet(bytes); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| long getOutboundBytes() { | ||
| return outboundBytes.get(); | ||
| } | ||
|
|
||
| long getInboundBytes() { | ||
| return inboundBytes.get(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| import io.grpc.Channel; | ||
| import io.grpc.ClientCall; | ||
| import io.grpc.ClientInterceptor; | ||
| import io.grpc.ClientStreamTracer; | ||
| import io.grpc.Grpc; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.MethodDescriptor; | ||
|
|
@@ -66,10 +67,12 @@ | |
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.stream.Collectors; | ||
| import javax.net.ssl.SSLException; | ||
| import javax.security.sasl.AuthenticationException; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
@@ -669,6 +672,84 @@ public void interceptCall_serverUnreached_directPathAndCloudPathRules() throws E | |
| null); | ||
| } | ||
|
|
||
| @Test | ||
| public void interceptCall_networkIo_tracksWireBytesWhenDebugEnabled() throws Exception { | ||
| // Debug disabled: must not attach a stream tracer or record network.io. | ||
| TestClientCall<Void, Void> standardCall = new TestClientCall<>(Attributes.EMPTY); | ||
| TestChannel standardChannel = new TestChannel(standardCall); | ||
| interceptor | ||
| .interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, standardChannel) | ||
| .start(new TestListener<>(), new Metadata()); | ||
| standardCall.close(Status.OK, new Metadata()); | ||
| assertThat(standardChannel.lastCallOptions.getStreamTracerFactories()).isEmpty(); | ||
| assertThat(findMetric(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_NETWORK_IO)).isNull(); | ||
|
|
||
| // Debug enabled: accumulates across streams and skips non-positive/zero-byte calls. | ||
| StorageGrpcMetricsInterceptor debugInterceptor = createDebugInterceptor(); | ||
| Attributes directPathAttrs = | ||
| Attributes.newBuilder() | ||
| .set( | ||
| Grpc.TRANSPORT_ATTR_REMOTE_ADDR, | ||
| new InetSocketAddress(InetAddress.getByName("34.126.1.2"), 443)) | ||
| .build(); | ||
| TestClientCall<Void, Void> testCall = new TestClientCall<>(directPathAttrs); | ||
| TestChannel testChannel = new TestChannel(testCall); | ||
|
|
||
| debugInterceptor | ||
| .interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, testChannel) | ||
| .start(new TestListener<>(), new Metadata()); | ||
| List<ClientStreamTracer.Factory> factories = | ||
| testChannel.lastCallOptions.getStreamTracerFactories(); | ||
| assertThat(factories).hasSize(1); | ||
| ClientStreamTracer.StreamInfo streamInfo = ClientStreamTracer.StreamInfo.newBuilder().build(); | ||
| ClientStreamTracer stream1 = factories.get(0).newClientStreamTracer(streamInfo, new Metadata()); | ||
| stream1.outboundWireSize(128L); | ||
| stream1.outboundWireSize(0L); | ||
| stream1.outboundWireSize(-10L); | ||
| stream1.inboundWireSize(512L); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Can we also call |
||
| ClientStreamTracer stream2 = factories.get(0).newClientStreamTracer(streamInfo, new Metadata()); | ||
| stream2.outboundWireSize(256L); | ||
| stream2.inboundWireSize(1024L); | ||
| testCall.close(Status.OK, new Metadata()); | ||
|
|
||
| MetricData metricData = findMetric(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_NETWORK_IO); | ||
| assertThat(metricData).isNotNull(); | ||
| assertThat(metricData.getUnit()).isEqualTo("By"); | ||
| Map<io.opentelemetry.api.common.Attributes, Long> pointsByAttrs = | ||
| metricData.getLongSumData().getPoints().stream() | ||
| .collect(Collectors.toMap(LongPointData::getAttributes, LongPointData::getValue)); | ||
| io.opentelemetry.api.common.Attributes transmitAttrs = | ||
| io.opentelemetry.api.common.Attributes.builder() | ||
| .put(StorageClientMetrics.KEY_GCP_CLIENT_SERVICE, StorageClientMetrics.SERVICE_STORAGE) | ||
| .put(StorageClientMetrics.KEY_GCP_CLIENT_VERSION, StorageOptions.version()) | ||
| .put( | ||
| StorageClientMetrics.KEY_GCP_CLIENT_ARTIFACT, StorageClientMetrics.ARTIFACT_STORAGE) | ||
| .put(StorageClientMetrics.KEY_RPC_SYSTEM_NAME, "grpc") | ||
| .put(StorageClientMetrics.KEY_SERVER_ADDRESS, "storage.googleapis.com") | ||
| .put( | ||
| StorageClientMetrics.KEY_NETWORK_PATH, StorageClientMetrics.NETWORK_PATH_DIRECTPATH) | ||
| .put( | ||
| StorageClientMetrics.KEY_NETWORK_IO_DIRECTION, | ||
| StorageClientMetrics.NETWORK_IO_DIRECTION_TRANSMIT) | ||
| .build(); | ||
| io.opentelemetry.api.common.Attributes receiveAttrs = | ||
| transmitAttrs.toBuilder() | ||
| .put( | ||
| StorageClientMetrics.KEY_NETWORK_IO_DIRECTION, | ||
| StorageClientMetrics.NETWORK_IO_DIRECTION_RECEIVE) | ||
| .build(); | ||
| assertThat(pointsByAttrs).containsExactly(transmitAttrs, 384L, receiveAttrs, 1536L); | ||
|
|
||
| // Zero-byte call should not alter existing counts or create new points. | ||
| TestClientCall<Void, Void> zeroCall = new TestClientCall<>(Attributes.EMPTY); | ||
| debugInterceptor | ||
| .interceptCall(READ_OBJECT_METHOD, CallOptions.DEFAULT, new TestChannel(zeroCall)) | ||
| .start(new TestListener<>(), new Metadata()); | ||
| zeroCall.close(Status.UNAVAILABLE, new Metadata()); | ||
| MetricData afterZero = findMetric(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_NETWORK_IO); | ||
| assertThat(afterZero.getLongSumData().getPoints()).hasSize(2); | ||
| } | ||
|
|
||
| @Test | ||
| public void interceptCall_requestActive_balancesWhenStartThrows() { | ||
| StorageGrpcMetricsInterceptor debugInterceptor = createDebugInterceptor(); | ||
|
|
@@ -1132,6 +1213,7 @@ void close(Status status, Metadata trailers) { | |
|
|
||
| private static final class TestChannel extends Channel { | ||
| private final TestClientCall<?, ?> call; | ||
| CallOptions lastCallOptions; | ||
|
|
||
| TestChannel(TestClientCall<?, ?> call) { | ||
| this.call = call; | ||
|
|
@@ -1141,6 +1223,7 @@ private static final class TestChannel extends Channel { | |
| @Override | ||
| public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall( | ||
| MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) { | ||
| this.lastCallOptions = callOptions; | ||
| return (ClientCall<RequestT, ResponseT>) call; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we track bytes and
network.pathperClientStreamTracerinstreamCreatedandstreamClosedinstead of aggregating across streams and flushing inonClose, so retried or hedged streams on different paths aren't misattributed and late callbacks aren't missed?