Conversation
This PR enhances the OpenTelemetry integration for HTTP JSON transports to explicitly unpack and record the `http.response.body.size` attributes globally inside `google-cloud-java`. * Safeguarded Extractions: Solidified `extractContentLength` in both `SpanTracer` and `TestApiTracer` to parse `List`-type strings safely and silently skip payload sizes when chunked serialization omits `Content-Length` headers entirely! * True-Positive Validation Mappings: In `ITOtelTracing`, cleanly simulates exception routing natively while introducing algorithmic boundary estimations `isAtLeast(expectedMagnitude * 0.85)` using actual `Message` object size fingerprints.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the OpenTelemetry integration for HTTP JSON transports by introducing the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request implements tracing for HTTP JSON calls, specifically to record the response body size. This involves adding ApiTracer support to HttpJsonCallOptions and HttpJsonCallContext, updating HttpJsonClientCallImpl to report response headers to the tracer, and enhancing ApiTracer and SpanTracer to process Content-Length from response headers. New tests were added to verify this functionality. Feedback includes addressing code duplication in the extractContentLength method, correcting a flawed test case in SpanTracerTest for bad Content-Length formats, and improving the robustness of a test in ITOtelTracing by asserting the presence of the http.response.body.size attribute.
| private long extractContentLength(java.util.Map<String, Object> headers) { | ||
| if (headers == null || headers.isEmpty()) return -1; | ||
| Object value = | ||
| headers.entrySet().stream() | ||
| .filter(e -> "Content-Length".equalsIgnoreCase(e.getKey())) | ||
| .map(java.util.Map.Entry::getValue) | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| if (value instanceof java.util.Collection) { | ||
| value = ((java.util.Collection<?>) value).stream().findFirst().orElse(null); | ||
| } | ||
|
|
||
| try { | ||
| return Long.parseLong(String.valueOf(value)); | ||
| } catch (NumberFormatException | NullPointerException e) { | ||
| return -1; | ||
| } | ||
| } |
There was a problem hiding this comment.
This extractContentLength method is nearly identical to the one in SpanTracer. This code duplication could lead to maintenance issues if the logic needs to be updated in the future.
Consider extracting this logic into a public static helper method in a utility class within the com.google.api.gax.tracing package (e.g., HttpTracingUtils.extractContentLength(headers)). This would allow both SpanTracer and TestApiTracer to reuse the same implementation, improving maintainability.
| void testResponseHeadersReceived_badFormat() { | ||
| spanTracer.attemptStarted(new Object(), 1); | ||
|
|
||
| java.util.Map<String, Object> headers = new java.util.HashMap<>(); | ||
| headers.put("Other-Header", "12X3"); | ||
| spanTracer.responseHeadersReceived(headers); | ||
|
|
||
| verify(span, org.mockito.Mockito.never()) | ||
| .setAttribute( | ||
| org.mockito.ArgumentMatchers.eq(ObservabilityAttributes.HTTP_RESPONSE_BODY_SIZE), | ||
| org.mockito.ArgumentMatchers.eq(-1)); | ||
| } |
There was a problem hiding this comment.
The test testResponseHeadersReceived_badFormat appears to be flawed. It sets the Other-Header instead of Content-Length, so it's not testing a malformed content length value. Additionally, the Mockito verification could be stronger to ensure no value is set for the attribute.
To properly test the bad format scenario, the Content-Length header should be set with an invalid value, and the verification should check that setAttribute is never called for the response body size attribute.
| void testResponseHeadersReceived_badFormat() { | |
| spanTracer.attemptStarted(new Object(), 1); | |
| java.util.Map<String, Object> headers = new java.util.HashMap<>(); | |
| headers.put("Other-Header", "12X3"); | |
| spanTracer.responseHeadersReceived(headers); | |
| verify(span, org.mockito.Mockito.never()) | |
| .setAttribute( | |
| org.mockito.ArgumentMatchers.eq(ObservabilityAttributes.HTTP_RESPONSE_BODY_SIZE), | |
| org.mockito.ArgumentMatchers.eq(-1)); | |
| } | |
| @Test | |
| void testResponseHeadersReceived_badFormat() { | |
| spanTracer.attemptStarted(new Object(), 1); | |
| java.util.Map<String, Object> headers = new java.util.HashMap<>(); | |
| headers.put("Content-Length", "12X3"); | |
| spanTracer.responseHeadersReceived(headers); | |
| verify(span, org.mockito.Mockito.never()) | |
| .setAttribute( | |
| org.mockito.ArgumentMatchers.eq(ObservabilityAttributes.HTTP_RESPONSE_BODY_SIZE), | |
| org.mockito.ArgumentMatchers.anyLong()); | |
| } |
| if (observedMagnitude != null) { | ||
| assertThat(observedMagnitude).isAtLeast((long) (expectedMagnitude * (1 - 0.15))); | ||
| assertThat(observedMagnitude).isAtMost((long) (expectedMagnitude * (1 + 0.15))); | ||
| } |
There was a problem hiding this comment.
The check for observedMagnitude is wrapped in an if (observedMagnitude != null) block, which means the test will pass silently if the http.response.body.size attribute is missing. For a unary call where a Content-Length header is expected, it would be more robust to assert that observedMagnitude is not null. This ensures the attribute is being set as expected and prevents potential regressions from going unnoticed.
assertThat(observedMagnitude).isNotNull();
assertThat(observedMagnitude).isAtLeast((long) (expectedMagnitude * (1 - 0.15)));
assertThat(observedMagnitude).isAtMost((long) (expectedMagnitude * (1 + 0.15)));
Description
This PR enhances the OpenTelemetry integration for HTTP JSON transports to explicitly unpack and record the
http.response.body.sizeattributes globally insidegoogle-cloud-java.Key Enhancements:
extractContentLengthin bothSpanTracerandTestApiTracerto parseList-type strings safely and silently skip payload sizes when chunked serialization omitsContent-Lengthheaders entirely!ITOtelTracing, cleanly simulates exception routing natively while introducing algorithmic boundary estimationsisAtLeast(expectedMagnitude * 0.85)using actualMessageobject size fingerprints.Testing Instructions
SpanTracerTest.javaITOtelTracingin showcase to test true-positive generation bounding.