Skip to content

fix: Add error attributes to logging#12685

Merged
blakeli0 merged 12 commits intomainfrom
fix-logging-tracer-exception-details
Apr 6, 2026
Merged

fix: Add error attributes to logging#12685
blakeli0 merged 12 commits intomainfrom
fix-logging-tracer-exception-details

Conversation

@blakeli0
Copy link
Copy Markdown
Contributor

@blakeli0 blakeli0 commented Apr 6, 2026

This PR

@blakeli0 blakeli0 requested a review from a team as a code owner April 6, 2026 18:27
@blakeli0 blakeli0 requested review from lqiu96 and westarle April 6, 2026 18:34
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors observability tracing by centralizing response attribute population in ObservabilityUtils and removing artifact name and version metadata from tracing attributes. The updates ensure consistent logging of exception types and messages across GoldenSignalsMetricsTracer, LoggingTracer, and SpanTracer. Feedback suggests using Strings.isNullOrEmpty() for message checks, defining a constant for the exception message attribute key, and specifying an initial capacity for the HashMap in ObservabilityUtils for better efficiency.

I am having trouble creating individual review comments. Click here to see my feedback.

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/LoggingTracer.java (82-84)

medium

It is recommended to use Strings.isNullOrEmpty() to check the exception message. This ensures that empty strings are not added to the log context, maintaining consistency with SpanTracer.java. Additionally, using a constant for the attribute key is preferred over a hardcoded string.

    if (error != null && !Strings.isNullOrEmpty(error.getMessage())) {
      logContext.put(ObservabilityAttributes.EXCEPTION_MESSAGE_ATTRIBUTE, error.getMessage());
    }

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/ObservabilityAttributes.java (51)

medium

Define a constant for the exception message attribute key to be used in logging, following the naming convention of other exception-related attributes.

  public static final String REPO_ATTRIBUTE = "gcp.client.repo";

  /** The exception message. */
  public static final String EXCEPTION_MESSAGE_ATTRIBUTE = "exception.message";

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

medium

Specifying an initial capacity for the HashMap is a good practice when the number of entries is small and known, as it avoids potential resizing and is slightly more efficient for high-frequency utility methods.

    Map<String, Object> attributes = new HashMap<>(8);

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/LoggingTracerTest.java (199)

medium

Use the constant ObservabilityAttributes.EXCEPTION_MESSAGE_ATTRIBUTE instead of a hardcoded string for consistency.

    assertEquals("test error message", attributesMap.get(ObservabilityAttributes.EXCEPTION_MESSAGE_ATTRIBUTE));


Map<String, ?> attributesMap = getAttributesMap();

assertTrue(attributesMap != null && !attributesMap.isEmpty());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can this be broken into two assert: assertNotNull(attributeMap) and assertFalse(attributeMaps.isEmpty()).

IIRC, the error message for assertTrue isn't as clear when indicating which condition failed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I was following the existing pattern but I don't think these assertions are needed anymore. Because attributeMap can never be null and we are already asserting a key later so not empty is also not necessary.
Removed this assertion altogether.

@blakeli0 blakeli0 merged commit a9198ee into main Apr 6, 2026
125 of 126 checks passed
@blakeli0 blakeli0 deleted the fix-logging-tracer-exception-details branch April 6, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants