-
Notifications
You must be signed in to change notification settings - Fork 74
feat: Add client request duration metric. #4132
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
Changes from all commits
2bffab2
cb368ab
d17ebd9
2ba106a
2bbfda3
6123e58
58951af
3bd6f0a
60b4ab8
4a7243e
a8deaa9
b026376
70a5560
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.tracing; | ||
|
|
||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.metrics.DoubleHistogram; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This class takes an OpenTelemetry object, and creates instruments (meters, histograms etc.) from | ||
| * it for recording golden signal metrics. There must be only one instance of | ||
| * GoldenSignalsMetricsRecorder per client, all the methods in this class are expected to be called | ||
| * from multiple threads, hence they need to be thread safe. | ||
| */ | ||
| class GoldenSignalsMetricsRecorder { | ||
| static final String CLIENT_REQUEST_DURATION_METRIC_NAME = "gcp.client.request.duration"; | ||
| static final String CLIENT_REQUEST_DURATION_METRIC_DESCRIPTION = | ||
| "Measures the total time taken for a logical client request, including any retries, backoff, and pre/post-processing"; | ||
|
|
||
| static final List<Double> BOUNDARIES = | ||
| Arrays.asList( | ||
| 0.0, 0.0001, 0.0005, 0.0010, 0.005, 0.010, 0.050, 0.100, 0.5, 1.0, 5.0, 10.0, 60.0, 300.0, | ||
| 900.0, 3600.0); | ||
| final DoubleHistogram clientRequestDurationRecorder; | ||
|
|
||
| GoldenSignalsMetricsRecorder(OpenTelemetry openTelemetry, String libraryName) { | ||
| Meter meter = openTelemetry.meterBuilder(libraryName).build(); | ||
|
|
||
| this.clientRequestDurationRecorder = | ||
| meter | ||
| .histogramBuilder(CLIENT_REQUEST_DURATION_METRIC_NAME) | ||
| .setDescription(CLIENT_REQUEST_DURATION_METRIC_DESCRIPTION) | ||
| .setUnit("s") | ||
| .setExplicitBucketBoundariesAdvice(BOUNDARIES) | ||
| .build(); | ||
| } | ||
|
|
||
| void recordOperationLatency(double operationLatency, Map<String, String> attributes) { | ||
| clientRequestDurationRecorder.record( | ||
| operationLatency, ObservabilityUtils.toOtelAttributes(attributes)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.tracing; | ||
|
|
||
| import static com.google.api.gax.tracing.ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE; | ||
|
|
||
| import com.google.api.gax.rpc.StatusCode; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Stopwatch; | ||
| import com.google.common.base.Ticker; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * This class computes golden signal metrics that can be observed in the lifecycle of an RPC | ||
| * operation. The responsibility of recording metrics should delegate to {@link | ||
| * GoldenSignalsMetricsRecorder}, hence this class should not have any knowledge about the | ||
| * observability framework (e.g. OpenTelemetry). | ||
| */ | ||
| class GoldenSignalsMetricsTracer implements ApiTracer { | ||
| private final Stopwatch clientRequestTimer; | ||
| private final GoldenSignalsMetricsRecorder metricsRecorder; | ||
| private final Map<String, String> attributes = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Creates the following instruments for the following metrics: | ||
| * | ||
| * <ul> | ||
| * <li>Client Request Duration: Histogram | ||
| * </ul> | ||
| * | ||
| * @param metricsRecorder OpenTelemetry | ||
| */ | ||
| GoldenSignalsMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder) { | ||
| this.clientRequestTimer = Stopwatch.createStarted(); | ||
| this.metricsRecorder = metricsRecorder; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| GoldenSignalsMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder, Ticker ticker) { | ||
| this.clientRequestTimer = Stopwatch.createStarted(ticker); | ||
| this.metricsRecorder = metricsRecorder; | ||
| } | ||
|
|
||
| /** | ||
| * The concept of "operation" and "client request" are the same. They both represent the total | ||
| * time taken for a logical client request, including any retries, backoff, and | ||
| * pre/post-processing | ||
| */ | ||
| @Override | ||
| public void operationSucceeded() { | ||
| attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.OK.toString()); | ||
| metricsRecorder.recordOperationLatency( | ||
| clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes); | ||
| } | ||
|
|
||
| @Override | ||
| public void operationCancelled() { | ||
| attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString()); | ||
| metricsRecorder.recordOperationLatency( | ||
| clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes); | ||
| } | ||
|
|
||
| @Override | ||
blakeli0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public void operationFailed(Throwable error) { | ||
| attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error)); | ||
| metricsRecorder.recordOperationLatency( | ||
| clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.tracing; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import com.google.api.core.InternalApi; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
|
|
||
| /** | ||
| * A {@link ApiTracerFactory} to build instances of {@link GoldenSignalsMetricsTracer}. | ||
| * | ||
| * <p>This class is expected to be initialized once during client initialization. | ||
| */ | ||
| @BetaApi | ||
| @InternalApi | ||
| public class GoldenSignalsMetricsTracerFactory implements ApiTracerFactory { | ||
|
|
||
| private ApiTracerContext apiTracerContext; | ||
|
Check warning on line 45 in gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalsMetricsTracerFactory.java
|
||
| private final OpenTelemetry openTelemetry; | ||
| private GoldenSignalsMetricsRecorder metricsRecorder; | ||
|
|
||
| public GoldenSignalsMetricsTracerFactory(OpenTelemetry openTelemetry) { | ||
| this.openTelemetry = openTelemetry; | ||
| } | ||
|
|
||
| @Override | ||
| public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) { | ||
| if (metricsRecorder == null) { | ||
| // This should never happen, in case it happens, create a no-op api tracer to not block | ||
| // regular requests. | ||
| return new BaseApiTracer(); | ||
| } | ||
| return new GoldenSignalsMetricsTracer(metricsRecorder); | ||
| } | ||
|
Comment on lines
+54
to
+61
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. The public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) {
if (apiTracerContext == null) {
throw new IllegalStateException(
"ApiTracerContext has not been set. Call withContext() before creating a new tracer.");
}
return new GoldenTelemetryMetricsTracer(openTelemetry, apiTracerContext);
}References
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. I think this should not happen with the current setup but +1 for the extra safety "against misuse" a null check brings. |
||
|
|
||
| @Override | ||
| public ApiTracerFactory withContext(ApiTracerContext context) { | ||
| this.apiTracerContext = context; | ||
| this.metricsRecorder = | ||
| new GoldenSignalsMetricsRecorder( | ||
| openTelemetry, apiTracerContext.libraryMetadata().artifactName()); | ||
| return this; | ||
| } | ||
| } | ||
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.
Add more doc that "Operation" is actually "Client Request" in the context of golden signal metrics.