-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(gax): Actionable Errors Logging API Tracer #12202
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
Open
westarle
wants to merge
5
commits into
googleapis:main
Choose a base branch
from
westarle:feat/actionable-errors-api-tracer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e0b1b19
feat(gax): implement actionable errors logging in ApiTracer framework
westarle 84e3b25
refactor: use ObservabilityUtils.extractErrorInfo helper
westarle a73c78b
fix(test): provide libraryMetadata for ApiTracerContext.newBuilder in…
westarle 1f13fb6
Merge branch 'main' into feat/actionable-errors-api-tracer
westarle 457f88c
fix(gax): address PR3 feedback on actionable errors
westarle 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
105 changes: 105 additions & 0 deletions
105
sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/LoggingTracer.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,105 @@ | ||
| /* | ||
| * 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 com.google.api.gax.logging.LoggerProvider; | ||
| import com.google.api.gax.logging.LoggingUtils; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.rpc.ErrorInfo; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * An {@link ApiTracer} that logs actionable errors using {@link LoggingUtils} when an RPC attempt | ||
| * fails. | ||
| */ | ||
| @BetaApi | ||
| @InternalApi | ||
| class LoggingTracer extends BaseApiTracer { | ||
| private static final LoggerProvider LOGGER_PROVIDER = | ||
| LoggerProvider.forClazz(LoggingTracer.class); | ||
|
|
||
| private final ApiTracerContext apiTracerContext; | ||
|
|
||
| LoggingTracer(ApiTracerContext apiTracerContext) { | ||
| this.apiTracerContext = apiTracerContext; | ||
| } | ||
|
|
||
| @Override | ||
| public void attemptFailedDuration(Throwable error, java.time.Duration delay) { | ||
| recordActionableError(error); | ||
| } | ||
|
|
||
| @Override | ||
| public void attemptFailedRetriesExhausted(Throwable error) { | ||
| recordActionableError(error); | ||
| } | ||
|
|
||
| @Override | ||
| public void attemptPermanentFailure(Throwable error) { | ||
| recordActionableError(error); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| void recordActionableError(Throwable error) { | ||
| if (error == null) { | ||
| return; | ||
| } | ||
|
|
||
| Map<String, Object> logContext = new HashMap<>(apiTracerContext.getAttemptAttributes()); | ||
|
|
||
| logContext.put( | ||
| ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE, | ||
| ObservabilityUtils.extractStatus(error)); | ||
|
|
||
| ErrorInfo errorInfo = ObservabilityUtils.extractErrorInfo(error); | ||
| if (errorInfo != null) { | ||
| if (errorInfo.getReason() != null && !errorInfo.getReason().isEmpty()) { | ||
| logContext.put(ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE, errorInfo.getReason()); | ||
| } | ||
| if (errorInfo.getDomain() != null && !errorInfo.getDomain().isEmpty()) { | ||
| logContext.put(ObservabilityAttributes.ERROR_DOMAIN_ATTRIBUTE, errorInfo.getDomain()); | ||
| } | ||
| if (errorInfo.getMetadataMap() != null) { | ||
| for (Map.Entry<String, String> entry : errorInfo.getMetadataMap().entrySet()) { | ||
| logContext.put( | ||
| ObservabilityAttributes.ERROR_METADATA_ATTRIBUTE_PREFIX + entry.getKey(), | ||
| entry.getValue()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| String message = error.getMessage() != null ? error.getMessage() : error.getClass().getName(); | ||
| LoggingUtils.logActionableError(logContext, LOGGER_PROVIDER, message); | ||
| } | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
...form-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/LoggingTracerFactory.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,69 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** A {@link ApiTracerFactory} that creates instances of {@link LoggingTracer}. */ | ||
| @BetaApi | ||
| @InternalApi | ||
| public class LoggingTracerFactory implements ApiTracerFactory { | ||
| private final ApiTracerContext apiTracerContext; | ||
|
|
||
| public LoggingTracerFactory() { | ||
| this(ApiTracerContext.empty()); | ||
| } | ||
|
|
||
| private LoggingTracerFactory(ApiTracerContext apiTracerContext) { | ||
| this.apiTracerContext = apiTracerContext; | ||
| } | ||
|
|
||
| @Override | ||
| public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) { | ||
| return new LoggingTracer(apiTracerContext); | ||
| } | ||
|
|
||
| @Override | ||
| public ApiTracer newTracer(ApiTracer parent, ApiTracerContext context) { | ||
| return new LoggingTracer(apiTracerContext.merge(context)); | ||
| } | ||
|
|
||
| @Override | ||
| public ApiTracerContext getApiTracerContext() { | ||
| return apiTracerContext; | ||
| } | ||
|
|
||
| @Override | ||
| public ApiTracerFactory withContext(ApiTracerContext context) { | ||
| return new LoggingTracerFactory(apiTracerContext.merge(context)); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
|
|
||
| import com.google.api.gax.rpc.ApiException; | ||
| import com.google.api.gax.rpc.StatusCode; | ||
| import com.google.rpc.ErrorInfo; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import java.util.Map; | ||
|
|
@@ -56,6 +57,18 @@ static String extractStatus(@Nullable Throwable error) { | |
| return statusString; | ||
| } | ||
|
|
||
| /** Function to extract the ErrorInfo payload from the error, if available */ | ||
| @Nullable | ||
| static ErrorInfo extractErrorInfo(@Nullable Throwable error) { | ||
|
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'm adding similar logic in #12189. Let me know if you'd prefer a different surface for my util.
Contributor
Author
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. happy to move to your logic when it's ready. |
||
| if (error instanceof ApiException) { | ||
| ApiException apiException = (ApiException) error; | ||
| if (apiException.getErrorDetails() != null) { | ||
| return apiException.getErrorDetails().getErrorInfo(); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| static Attributes toOtelAttributes(Map<String, Object> attributes) { | ||
| AttributesBuilder attributesBuilder = Attributes.builder(); | ||
| if (attributes == null) { | ||
|
|
||
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
74 changes: 74 additions & 0 deletions
74
...-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/LoggingTracerFactoryTest.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,74 @@ | ||
| /* | ||
| * 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 org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class LoggingTracerFactoryTest { | ||
|
|
||
| @Test | ||
| void testNewTracer_CreatesLoggingTracer() { | ||
| LoggingTracerFactory factory = new LoggingTracerFactory(); | ||
| ApiTracer tracer = | ||
| factory.newTracer( | ||
| BaseApiTracer.getInstance(), | ||
| SpanName.of("client", "method"), | ||
| ApiTracerFactory.OperationType.Unary); | ||
|
|
||
| assertNotNull(tracer); | ||
| assertTrue(tracer instanceof LoggingTracer); | ||
| } | ||
|
|
||
| @Test | ||
| void testNewTracer_WithContext_CreatesLoggingTracer() { | ||
| LoggingTracerFactory factory = new LoggingTracerFactory(); | ||
| ApiTracer tracer = factory.newTracer(BaseApiTracer.getInstance(), ApiTracerContext.empty()); | ||
|
|
||
| assertNotNull(tracer); | ||
| assertTrue(tracer instanceof LoggingTracer); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithContext_ReturnsNewFactoryWithMergedContext() { | ||
| LoggingTracerFactory factory = new LoggingTracerFactory(); | ||
| ApiTracerContext context = | ||
| ApiTracerContext.empty().toBuilder().setServerAddress("address").build(); | ||
| ApiTracerFactory updatedFactory = factory.withContext(context); | ||
|
|
||
| assertNotNull(updatedFactory); | ||
| assertTrue(updatedFactory instanceof LoggingTracerFactory); | ||
| assertEquals("address", updatedFactory.getApiTracerContext().serverAddress()); | ||
| } | ||
| } |
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.
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.
Can we add unit tests for this method
attemptFailedRetriesExhaustedand method belowattemptPermanentFailure?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.
done