-
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
Changes from 4 commits
e0b1b19
84e3b25
a73c78b
1f13fb6
457f88c
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,108 @@ | ||
| /* | ||
| * 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.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 | ||
| public class LoggingTracer extends BaseApiTracer { | ||
| private static final LoggerProvider LOGGER_PROVIDER = | ||
| LoggerProvider.forClazz(LoggingTracer.class); | ||
|
|
||
| private final ApiTracerContext apiTracerContext; | ||
|
|
||
| public LoggingTracer(ApiTracerContext apiTracerContext) { | ||
| this.apiTracerContext = apiTracerContext; | ||
| } | ||
|
|
||
| @Override | ||
| public void attemptFailedDuration(Throwable error, java.time.Duration delay) { | ||
| recordActionableError(error); | ||
| } | ||
|
|
||
| @Override | ||
| public void attemptFailedRetriesExhausted(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. Can we add unit tests for this method
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. done |
||
| recordActionableError(error); | ||
| } | ||
|
|
||
| @Override | ||
| public void attemptPermanentFailure(Throwable error) { | ||
| recordActionableError(error); | ||
| } | ||
|
|
||
| private void recordActionableError(Throwable error) { | ||
| if (error == null) { | ||
| return; | ||
| } | ||
|
|
||
| Map<String, Object> logContext = new HashMap<>(apiTracerContext.getAttemptAttributes()); | ||
|
|
||
| if (apiTracerContext.serviceName() != null) { | ||
|
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. This is probably a miss. I think
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. added to getAttemptAttributes() |
||
| logContext.put( | ||
| ObservabilityAttributes.GCP_CLIENT_SERVICE_ATTRIBUTE, apiTracerContext.serviceName()); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
| 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)); | ||
| } | ||
| } |
| 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) { | ||
|
|
||
| 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()); | ||
| } | ||
| } |
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 change the scope of this class and the constructor from public to package private?
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