Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit f38a66d

Browse files
committed
fix: improve handling for INTERNAL
1 parent d3cd69d commit f38a66d

4 files changed

Lines changed: 24 additions & 48 deletions

File tree

gax-java/gax/src/main/java/com/google/api/gax/tracing/ErrorTypeUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public String toString() {
8888
* <li>{@code CLIENT_REDIRECT_ERROR}: Problem handling HTTP redirects.
8989
* <li>{@code CLIENT_AUTHENTICATION_ERROR}: Error during credential acquisition or
9090
* application.
91+
* <li>{@code CLIENT_UNKNOWN_ERROR}: For all other errors unknown to the client.
9192
* </ul>
9293
* <li><b>Language-specific error type:</b> The class or struct name of the exception or error
9394
* if available. This must be low-cardinality, meaning it returns the short name of the
@@ -103,7 +104,8 @@ public String toString() {
103104
*/
104105
public static String extractErrorType(@Nullable Throwable error) {
105106
if (error == null) {
106-
return null;
107+
// No information about the error; we default to INTERNAL.
108+
return ErrorType.INTERNAL.toString();
107109
}
108110

109111
// 1. & 2. Extract error info reason or server status code

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

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,8 @@ class ObservabilityUtils {
4141

4242
/**
4343
* Extracts a low-cardinality string representing the specific classification of the error to be
44-
* used in the {@link ObservabilityAttributes#ERROR_TYPE_ATTRIBUTE} attribute.
45-
*
46-
* <p>This value is determined based on the following priority:
47-
*
48-
* <ol>
49-
* <li><b>{@code google.rpc.ErrorInfo.reason}:</b> If the error response from the service
50-
* includes {@code google.rpc.ErrorInfo} details, the reason field (e.g.,
51-
* "RATE_LIMIT_EXCEEDED", "SERVICE_DISABLED") will be used. This offers the most precise
52-
* error cause.
53-
* <li><b>Specific Server Error Code:</b> If no {@code ErrorInfo.reason} is available, but a
54-
* server error code was received:
55-
* <ul>
56-
* <li>For HTTP: The HTTP status code (e.g., "403", "503").
57-
* <li>For gRPC: The gRPC status code name (e.g., "PERMISSION_DENIED", "UNAVAILABLE").
58-
* </ul>
59-
* <li><b>Client-Side Network/Operational Errors:</b> For errors occurring within the client
60-
* library or network stack, mapping to specific enum representations from {@link
61-
* ErrorTypeUtil.ErrorType}:
62-
* <ul>
63-
* <li>{@code CLIENT_TIMEOUT}: A client-configured timeout was reached.
64-
* <li>{@code CLIENT_CONNECTION_ERROR}: Failure to establish the network connection (DNS,
65-
* TCP, TLS).
66-
* <li>{@code CLIENT_REQUEST_ERROR}: Client-side issue forming or sending the request.
67-
* <li>{@code CLIENT_REQUEST_BODY_ERROR}: Error streaming the request body.
68-
* <li>{@code CLIENT_RESPONSE_DECODE_ERROR}: Client-side error decoding the response body.
69-
* <li>{@code CLIENT_REDIRECT_ERROR}: Problem handling HTTP redirects.
70-
* <li>{@code CLIENT_AUTHENTICATION_ERROR}: Error during credential acquisition or
71-
* application.
72-
* <li>{@code CLIENT_UNKNOWN_ERROR}: Other unclassified client-side network or protocol
73-
* errors.
74-
* </ul>
75-
* <li><b>Language-specific error type:</b> The class or struct name of the exception or error
76-
* if available. This must be low-cardinality, meaning it returns the short name of the
77-
* exception class (e.g. {@code "IllegalStateException"}) rather than its message.
78-
* <li><b>Internal Fallback:</b> If the error doesn't fit any of the above categories, {@code
79-
* "INTERNAL"} will be used, indicating an unexpected issue within the client library's own
80-
* logic.
81-
* </ol>
82-
*
83-
* @param error the Throwable from which to extract the error type string.
84-
* @return a low-cardinality string representing the specific error type, or {@code null} if the
85-
* provided error is {@code null}.
44+
* used in the {@link ObservabilityAttributes#ERROR_TYPE_ATTRIBUTE} attribute. See {@link
45+
* ErrorTypeUtil#extractErrorType} for extended documentation.
8646
*/
8747
static String extractErrorType(@Nullable Throwable error) {
8848
return ErrorTypeUtil.extractErrorType(error);

gax-java/gax/src/main/java/com/google/api/gax/tracing/SpanTracer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,8 @@ public void attemptPermanentFailure(Throwable error) {
107107

108108
private void recordErrorAndEndAttempt(Throwable error) {
109109
if (attemptHandle != null) {
110-
if (error != null) {
111-
attemptHandle.addAttribute(
112-
ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE,
113-
ObservabilityUtils.extractErrorType(error));
114-
}
110+
attemptHandle.addAttribute(
111+
ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE, ObservabilityUtils.extractErrorType(error));
115112
endAttempt();
116113
}
117114
}

gax-java/gax/src/test/java/com/google/api/gax/tracing/SpanTracerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,23 @@ void testAttemptFailed_internalFallback() {
330330
verify(attemptHandle).end();
331331
}
332332

333+
@Test
334+
void testAttemptFailed_internalFallback_nullError() {
335+
when(recorder.createSpan(eq(ATTEMPT_SPAN_NAME), anyMap())).thenReturn(attemptHandle);
336+
337+
tracer.attemptStarted(new Object(), 1);
338+
339+
tracer.attemptFailedRetriesExhausted(null);
340+
341+
// For an anonymous inner class Throwable, getSimpleName() is empty string, which triggers the
342+
// fallback
343+
verify(attemptHandle)
344+
.addAttribute(
345+
ObservabilityAttributes.ERROR_TYPE_ATTRIBUTE,
346+
ErrorTypeUtil.ErrorType.INTERNAL.toString());
347+
verify(attemptHandle).end();
348+
}
349+
333350
private static class TestGoogleAuthException extends RuntimeException {}
334351

335352
private static class RedirectException extends RuntimeException {

0 commit comments

Comments
 (0)