-
Notifications
You must be signed in to change notification settings - Fork 75
feat: Add more attributes to golden signals metrics. #4135
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2bffab2
feat: Add basic golden telemetry metrics.
blakeli0 cb368ab
fix: Add error message for IllegalStateException in GoldenTelemetryMe…
blakeli0 d17ebd9
fix: Add opentelemetry_sdk_testing to dependencies.properties
blakeli0 2ba106a
fix: Rename to GoldenSignal
blakeli0 2bbfda3
feat: Add GoldenSignalsMetricsRecorder to wrap OpenTelemetry related …
blakeli0 6123e58
fix: Remove VisibleForTesting annotation.
blakeli0 58951af
fix: Remove unused fields in tests
blakeli0 3bd6f0a
test: Update unit tests.
blakeli0 59f320c
feat: Add more attributes to metrics.
blakeli0 ef59e3e
Merge branch 'main' into add-metrics-attributes
blakeli0 c0622b5
fix: Add more attributes. Refactor attributes from String to Object f…
blakeli0 9965f24
Merge branch 'main' into add-metrics-attributes
blakeli0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalMetricsTracer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * 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.base.Stopwatch; | ||
| 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 GoldenSignalMetricsTracer implements ApiTracer { | ||
| private final Stopwatch clientRequestTimer = Stopwatch.createStarted(); | ||
| private final GoldenSignalsMetricsRecorder metricsRecorder; | ||
| private final Map<String, String> attributes = new HashMap<>(); | ||
| private final ApiTracerContext apiTracerContext; | ||
|
|
||
| /** | ||
| * Creates the following instruments for the following metrics: | ||
| * | ||
| * <ul> | ||
| * <li>Client Request Duration: Histogram | ||
| * </ul> | ||
| * | ||
| * @param metricsRecorder OpenTelemetry | ||
| * @param apiTracerContext | ||
|
blakeli0 marked this conversation as resolved.
Outdated
|
||
| */ | ||
| GoldenSignalMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder, ApiTracerContext apiTracerContext) { | ||
| this.metricsRecorder = metricsRecorder; | ||
| this.apiTracerContext = apiTracerContext; | ||
| } | ||
|
|
||
| @Override | ||
| public void operationSucceeded() { | ||
| attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.OK.toString()); | ||
| attributes.putAll(apiTracerContext.getMetricsAttributes()); | ||
| metricsRecorder.recordOperationLatency( | ||
| clientRequestTimer.elapsed(TimeUnit.SECONDS), attributes); | ||
| } | ||
|
|
||
| @Override | ||
| public void operationCancelled() { | ||
| attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString()); | ||
| attributes.putAll(apiTracerContext.getMetricsAttributes()); | ||
| metricsRecorder.recordOperationLatency( | ||
| clientRequestTimer.elapsed(TimeUnit.SECONDS), attributes); | ||
| } | ||
|
|
||
| @Override | ||
| public void operationFailed(Throwable error) { | ||
| attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error)); | ||
| attributes.putAll(apiTracerContext.getMetricsAttributes()); | ||
| metricsRecorder.recordOperationLatency( | ||
| clientRequestTimer.elapsed(TimeUnit.SECONDS), attributes); | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalMetricsTracerFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 GoldenSignalMetricsTracer}. | ||
| * | ||
| * <p>This class is expected to be initialized once during client initialization. | ||
| */ | ||
| @BetaApi | ||
| @InternalApi | ||
| public class GoldenSignalMetricsTracerFactory implements ApiTracerFactory { | ||
|
|
||
| private ApiTracerContext apiTracerContext; | ||
| private final OpenTelemetry openTelemetry; | ||
| private GoldenSignalsMetricsRecorder metricsRecorder; | ||
|
|
||
| public GoldenSignalMetricsTracerFactory(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 GoldenSignalMetricsTracer(metricsRecorder, apiTracerContext); | ||
| } | ||
|
|
||
| @Override | ||
| public ApiTracerFactory withContext(ApiTracerContext context) { | ||
| this.apiTracerContext = context; | ||
| this.metricsRecorder = | ||
| new GoldenSignalsMetricsRecorder( | ||
| openTelemetry, apiTracerContext.libraryMetadata().artifactName()); | ||
| return this; | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalsMetricsRecorder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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.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"; | ||
|
|
||
| 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") | ||
| .build(); | ||
| } | ||
|
|
||
| void recordOperationLatency(double operationLatency, Map<String, String> attributes) { | ||
| clientRequestDurationRecorder.record( | ||
| operationLatency, ObservabilityUtils.toOtelAttributes(attributes)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.