Skip to content

Commit 76d0900

Browse files
committed
feat(gax): implement actionable errors logging in ApiTracer framework
1 parent b51ef3c commit 76d0900

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Google LLC nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package com.google.api.gax.tracing;
32+
33+
import com.google.api.core.BetaApi;
34+
import com.google.api.core.InternalApi;
35+
import com.google.api.gax.logging.LoggerProvider;
36+
import com.google.api.gax.logging.LoggingUtils;
37+
import com.google.api.gax.rpc.ApiException;
38+
import com.google.rpc.ErrorInfo;
39+
import java.util.HashMap;
40+
import java.util.Map;
41+
42+
/**
43+
* An {@link ApiTracer} that logs actionable errors using {@link LoggingUtils} when an RPC attempt
44+
* fails.
45+
*/
46+
@BetaApi
47+
@InternalApi
48+
public class LoggingTracer extends BaseApiTracer {
49+
private static final LoggerProvider LOGGER_PROVIDER =
50+
LoggerProvider.forClazz(LoggingTracer.class);
51+
52+
private final ApiTracerContext apiTracerContext;
53+
54+
public LoggingTracer(ApiTracerContext apiTracerContext) {
55+
this.apiTracerContext = apiTracerContext;
56+
}
57+
58+
@Override
59+
public void attemptFailed(Throwable error, org.threeten.bp.Duration delay) {
60+
recordActionableError(error);
61+
}
62+
63+
@Override
64+
public void attemptFailedDuration(Throwable error, java.time.Duration delay) {
65+
recordActionableError(error);
66+
}
67+
68+
@Override
69+
public void attemptFailedRetriesExhausted(Throwable error) {
70+
recordActionableError(error);
71+
}
72+
73+
@Override
74+
public void attemptPermanentFailure(Throwable error) {
75+
recordActionableError(error);
76+
}
77+
78+
private void recordActionableError(Throwable error) {
79+
Map<String, Object> logContext = new HashMap<>();
80+
81+
if (apiTracerContext.rpcSystemName() != null) {
82+
logContext.put(
83+
ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE, apiTracerContext.rpcSystemName());
84+
}
85+
if (apiTracerContext.fullMethodName() != null) {
86+
logContext.put(
87+
ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE, apiTracerContext.fullMethodName());
88+
}
89+
if (apiTracerContext.serverPort() != null) {
90+
logContext.put(ObservabilityAttributes.SERVER_PORT_ATTRIBUTE, apiTracerContext.serverPort());
91+
}
92+
if (apiTracerContext.libraryMetadata() != null
93+
&& !apiTracerContext.libraryMetadata().isEmpty()) {
94+
if (apiTracerContext.libraryMetadata().repository() != null) {
95+
logContext.put(
96+
ObservabilityAttributes.REPO_ATTRIBUTE,
97+
apiTracerContext.libraryMetadata().repository());
98+
}
99+
}
100+
101+
if (error instanceof ApiException) {
102+
ApiException apiException = (ApiException) error;
103+
logContext.put(
104+
ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE,
105+
apiException.getStatusCode().getCode().toString());
106+
107+
if (apiException.getErrorDetails() != null) {
108+
ErrorInfo errorInfo = apiException.getErrorDetails().getErrorInfo();
109+
if (errorInfo != null) {
110+
logContext.put("error.type", errorInfo.getReason());
111+
logContext.put("gcp.errors.domain", errorInfo.getDomain());
112+
for (Map.Entry<String, String> entry : errorInfo.getMetadataMap().entrySet()) {
113+
logContext.put("gcp.errors.metadata." + entry.getKey(), entry.getValue());
114+
}
115+
}
116+
}
117+
}
118+
119+
String message = "Unknown Error";
120+
if (error != null) {
121+
message = error.getMessage() != null ? error.getMessage() : error.getClass().getName();
122+
}
123+
LoggingUtils.logActionableError(logContext, LOGGER_PROVIDER, message);
124+
}
125+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Google LLC nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package com.google.api.gax.tracing;
32+
33+
import com.google.api.core.BetaApi;
34+
import com.google.api.core.InternalApi;
35+
36+
/** A {@link ApiTracerFactory} that creates instances of {@link LoggingTracer}. */
37+
@BetaApi
38+
@InternalApi
39+
public class LoggingTracerFactory implements ApiTracerFactory {
40+
private final ApiTracerContext apiTracerContext;
41+
42+
public LoggingTracerFactory() {
43+
this(ApiTracerContext.empty());
44+
}
45+
46+
private LoggingTracerFactory(ApiTracerContext apiTracerContext) {
47+
this.apiTracerContext = apiTracerContext;
48+
}
49+
50+
@Override
51+
public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) {
52+
return new LoggingTracer(apiTracerContext);
53+
}
54+
55+
@Override
56+
public ApiTracer newTracer(ApiTracer parent, ApiTracerContext context) {
57+
return new LoggingTracer(context);
58+
}
59+
60+
@Override
61+
public ApiTracerContext getApiTracerContext() {
62+
return apiTracerContext;
63+
}
64+
65+
@Override
66+
public ApiTracerFactory withContext(ApiTracerContext context) {
67+
return new LoggingTracerFactory(apiTracerContext.merge(context));
68+
}
69+
}

0 commit comments

Comments
 (0)