Skip to content

Commit 84e3b25

Browse files
committed
refactor: use ObservabilityUtils.extractErrorInfo helper
1 parent e0b1b19 commit 84e3b25

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/LoggingTracer.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.google.api.core.InternalApi;
3535
import com.google.api.gax.logging.LoggerProvider;
3636
import com.google.api.gax.logging.LoggingUtils;
37-
import com.google.api.gax.rpc.ApiException;
3837
import com.google.rpc.ErrorInfo;
3938
import java.util.HashMap;
4039
import java.util.Map;
@@ -104,17 +103,12 @@ private void recordActionableError(Throwable error) {
104103
ObservabilityUtils.extractStatus(error));
105104
}
106105

107-
if (error instanceof ApiException) {
108-
ApiException apiException = (ApiException) error;
109-
if (apiException.getErrorDetails() != null) {
110-
ErrorInfo errorInfo = apiException.getErrorDetails().getErrorInfo();
111-
if (errorInfo != null) {
112-
logContext.put("error.type", errorInfo.getReason());
113-
logContext.put("gcp.errors.domain", errorInfo.getDomain());
114-
for (Map.Entry<String, String> entry : errorInfo.getMetadataMap().entrySet()) {
115-
logContext.put("gcp.errors.metadata." + entry.getKey(), entry.getValue());
116-
}
117-
}
106+
ErrorInfo errorInfo = ObservabilityUtils.extractErrorInfo(error);
107+
if (errorInfo != null) {
108+
logContext.put("error.type", errorInfo.getReason());
109+
logContext.put("gcp.errors.domain", errorInfo.getDomain());
110+
for (Map.Entry<String, String> entry : errorInfo.getMetadataMap().entrySet()) {
111+
logContext.put("gcp.errors.metadata." + entry.getKey(), entry.getValue());
118112
}
119113
}
120114

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/ObservabilityUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.google.api.gax.rpc.ApiException;
3333
import com.google.api.gax.rpc.StatusCode;
34+
import com.google.rpc.ErrorInfo;
3435
import io.opentelemetry.api.common.Attributes;
3536
import io.opentelemetry.api.common.AttributesBuilder;
3637
import java.util.Map;
@@ -56,6 +57,18 @@ static String extractStatus(@Nullable Throwable error) {
5657
return statusString;
5758
}
5859

60+
/** Function to extract the ErrorInfo payload from the error, if available */
61+
@Nullable
62+
static ErrorInfo extractErrorInfo(@Nullable Throwable error) {
63+
if (error instanceof ApiException) {
64+
ApiException apiException = (ApiException) error;
65+
if (apiException.getErrorDetails() != null) {
66+
return apiException.getErrorDetails().getErrorInfo();
67+
}
68+
}
69+
return null;
70+
}
71+
5972
static Attributes toOtelAttributes(Map<String, Object> attributes) {
6073
AttributesBuilder attributesBuilder = Attributes.builder();
6174
if (attributes == null) {

0 commit comments

Comments
 (0)