-
Notifications
You must be signed in to change notification settings - Fork 67
feat: Introduce OpenTelemetry Metrics Recording #2500
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 25 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
21407aa
feat: Implement OpentelemetryMetricsRecorder
lqiu96 f757811
chore: Update CONTRIBUTING.md
lqiu96 58fc09c
chore: Fix lint issues
lqiu96 d29ad1a
chore: Clean up ITOTelMetrics test
lqiu96 df7c611
chore: Clean up ITOTelMetrics test
lqiu96 9a3fc3c
chore: Ignore failing Otel tests
lqiu96 9121cb6
chore: Update OpentelemetryMetricsRecorderTest to use InMemoryMetricE…
lqiu96 9d099f6
chore: Update showcase tests
lqiu96 03acef3
chore: Add missing otel dependencies.properties values
lqiu96 a2d979b
chore: Add javadocs for OpentelemetryMetricsRecorder
lqiu96 d46fef7
Merge branch 'main' into implement-OpentelemetryMetricsRecorder
lqiu96 4edded1
chore: Use otel v1.34.1
lqiu96 78cc64a
chore: Ignore the HttpJson Otel tests
lqiu96 b620ffc
chore: Fix showcase tests
lqiu96 06a6bba
Merge branch 'main' into implement-OpentelemetryMetricsRecorder
lqiu96 07eeb3b
chore: Refactor showcase tests
lqiu96 6df9640
chore: Address PR comments
lqiu96 edd7cea
chore: Throw exception if operation completion call has been invoked …
lqiu96 32ee2fe
chore: Address PR comments
lqiu96 825175e
chore: Add otel bom to gapic-generator-java bom
lqiu96 09ea967
chore: Address PR comments
lqiu96 ea5a776
chore: Fix showcase tests
lqiu96 998eb68
Update gax-java/dependencies.properties
lqiu96 1453461
Merge branch 'main' into implement-OpentelemetryMetricsRecorder
lqiu96 6c7bca3
chore: Use gax-java as instrument scope name
lqiu96 fad8094
Update gax-java/gax/src/main/java/com/google/api/gax/tracing/OpenTele…
blakeli0 41bbf57
Merge branch 'main' into implement-OpentelemetryMetricsRecorder
blakeli0 a79e4e7
Update gax-java/gax/src/main/java/com/google/api/gax/tracing/OpenTele…
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
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
161 changes: 161 additions & 0 deletions
161
gax-java/gax/src/main/java/com/google/api/gax/tracing/OpenTelemetryMetricsRecorder.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,161 @@ | ||
/* | ||
* Copyright 2024 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.gax.core.GaxProperties; | ||
blakeli0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Preconditions; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.api.metrics.DoubleHistogram; | ||
import io.opentelemetry.api.metrics.LongCounter; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import java.util.Map; | ||
|
||
/** | ||
* OpenTelemetry implementation of recording metrics. This implementation collections the | ||
* measurements related to the lifecyle of an RPC. | ||
* | ||
* <p>For the Otel implementation, an attempt is a single RPC invocation and an operation is the | ||
* collection of all the attempts made before a response is returned (either as a success or an | ||
* error). A single call (i.e. `EchoClient.echo()`) should have an operation_count of 1 and may have | ||
* an attempt_count of 1+ (depending on the retry configurations). | ||
*/ | ||
public class OpenTelemetryMetricsRecorder implements MetricsRecorder { | ||
blakeli0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final DoubleHistogram attemptLatencyRecorder; | ||
private final DoubleHistogram operationLatencyRecorder; | ||
private final LongCounter operationCountRecorder; | ||
private final LongCounter attemptCountRecorder; | ||
|
||
/** | ||
* Creates the following instruments for the following metrics: | ||
* | ||
* <ul> | ||
* <li>Attempt Latency: Histogram | ||
* <li>Operation Latency: Histogram | ||
* <li>Attempt Count: Counter | ||
* <li>Operation Count: Counter | ||
* </ul> | ||
* | ||
* @param openTelemetry OpenTelemetry instance | ||
* @param serviceName Service Name | ||
*/ | ||
public OpenTelemetryMetricsRecorder(OpenTelemetry openTelemetry, String serviceName) { | ||
Meter meter = | ||
openTelemetry | ||
.meterBuilder("gax-java") | ||
.setInstrumentationVersion(GaxProperties.getGaxVersion()) | ||
.build(); | ||
this.attemptLatencyRecorder = | ||
meter | ||
.histogramBuilder(serviceName + "/attempt_latency") | ||
.setDescription("Time an individual attempt took") | ||
.setUnit("ms") | ||
.build(); | ||
this.operationLatencyRecorder = | ||
meter | ||
.histogramBuilder(serviceName + "/operation_latency") | ||
.setDescription( | ||
"Total time until final operation success or failure, including retries and backoff.") | ||
.setUnit("ms") | ||
.build(); | ||
this.attemptCountRecorder = | ||
meter | ||
.counterBuilder(serviceName + "/attempt_count") | ||
.setDescription("Number of Attempts") | ||
.setUnit("1") | ||
.build(); | ||
this.operationCountRecorder = | ||
meter | ||
.counterBuilder(serviceName + "/operation_count") | ||
.setDescription("Number of Operations") | ||
.setUnit("1") | ||
.build(); | ||
} | ||
|
||
/** | ||
* Record the latency for an individual attempt. Data is stored in a Histogram. | ||
* | ||
* @param attemptLatency Attempt Latency in ms | ||
* @param attributes Map of the attributes to store | ||
*/ | ||
@Override | ||
public void recordAttemptLatency(double attemptLatency, Map<String, String> attributes) { | ||
attemptLatencyRecorder.record(attemptLatency, toOtelAttributes(attributes)); | ||
} | ||
|
||
/** | ||
* Record an attempt made. The attempt count number is stored in a LongCounter. | ||
* | ||
* <p>The count should be set as 1 every time this is invoked (each retry attempt) | ||
* | ||
* @param count The number of attempts made | ||
* @param attributes Map of the attributes to store | ||
*/ | ||
@Override | ||
public void recordAttemptCount(long count, Map<String, String> attributes) { | ||
attemptCountRecorder.add(count, toOtelAttributes(attributes)); | ||
} | ||
|
||
/** | ||
* Record the latency for the entire operation. This is the latency for the entire RPC, including | ||
* all the retry attempts | ||
* | ||
* @param operationLatency Operation Latency in ms | ||
* @param attributes Map of the attributes to store | ||
*/ | ||
@Override | ||
public void recordOperationLatency(double operationLatency, Map<String, String> attributes) { | ||
operationLatencyRecorder.record(operationLatency, toOtelAttributes(attributes)); | ||
} | ||
|
||
/** | ||
* Record an operation made. The operation count number is stored in a LongCounter. | ||
* | ||
* <p>The operation count should always be 1 and this should be invoked once. | ||
* | ||
* @param count The number of operations made | ||
* @param attributes Map of the attributes to store | ||
*/ | ||
@Override | ||
public void recordOperationCount(long count, Map<String, String> attributes) { | ||
operationCountRecorder.add(count, toOtelAttributes(attributes)); | ||
} | ||
|
||
@VisibleForTesting | ||
Attributes toOtelAttributes(Map<String, String> attributes) { | ||
Preconditions.checkNotNull(attributes, "Attributes map cannot be null"); | ||
AttributesBuilder attributesBuilder = Attributes.builder(); | ||
attributes.forEach(attributesBuilder::put); | ||
return attributesBuilder.build(); | ||
} | ||
} |
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.