-
Notifications
You must be signed in to change notification settings - Fork 75
impl(o11y): introduce http.response.body_size
#4157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
diegomarquezp
wants to merge
39
commits into
main
Choose a base branch
from
observability/tracing-attr/body-sze
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
4ef3d9f
impl(o11y): introduce body size attributes
diegomarquezp c00e49a
impl(httpjson): record response and request sizes
diegomarquezp c6da324
test: expand tests
diegomarquezp 72be770
fix revert request size attribute
diegomarquezp 7865574
Merge remote-tracking branch 'origin/main' into observability/tracing…
diegomarquezp 1265da3
chore: cleanup
diegomarquezp 94e55db
fix: use separate logic for unary resposne body size
diegomarquezp 2cb0479
test: improvements
diegomarquezp dc84757
test: improve coverage
diegomarquezp cba2159
fix: revert changes in opencensus
diegomarquezp 9435824
test: cover baseapitracer
diegomarquezp 0ae245d
test: test setAttribute
diegomarquezp 9f1bcd8
fix: address code quality flags
diegomarquezp eb959bb
refactor: use content lenght header
diegomarquezp 7bc8aa0
Merge origin/main into observability/tracing-attr/body-sze (accept ma…
diegomarquezp 11950af
fix(tracing): add body_size tracking to OpenTelemetry SpanTracer
diegomarquezp 6e8b311
chore(tracing): revert HttpJson tracing and merge test files
diegomarquezp 6035f29
chore: format
diegomarquezp 960ddd1
refactor(o11y): remove redundant responseHeadersReceived from BaseApi…
diegomarquezp 097e5ee
refactor: update responseHeadersReceived to use Map<String, Object> a…
diegomarquezp ab61cbb
test: add responseHeadersReceived tests to SpanTracerTest
diegomarquezp 869bd2a
refactor: extract CONTENT_LENGTH_KEY to static final string
diegomarquezp c721960
refactor: remove NumberFormatException handling in tracers
diegomarquezp 53117a3
fix: simplify logic of header getter
diegomarquezp 247f871
fix: use number format exception
diegomarquezp fad817a
merge: resolve conflicts with origin/main
diegomarquezp c2fdd2a
chore: format code
diegomarquezp e2df169
fix: handle array content length
diegomarquezp 2bf6326
Merge remote-tracking branch 'origin/main' into observability/tracing…
diegomarquezp 43a12ab
test: fix test
diegomarquezp c0427b3
fix(tracing): gracefully unwrap Content-Length tracing attribute
diegomarquezp c93eb4d
fix: remove unintended files
diegomarquezp bf13631
test: Implement exact expected magnitude calculation for HTTP payload…
diegomarquezp f92c66d
chore: generate libraries at Tue Mar 24 20:46:46 UTC 2026
cloud-java-bot a882735
test: relax expected body size
diegomarquezp 4a02b34
Merge branch 'observability/tracing-attr/body-sze' of https://github.…
diegomarquezp 03d905a
chore: generate libraries at Tue Mar 24 21:09:57 UTC 2026
cloud-java-bot 6e88a5a
Merge branch 'observability/tracing-attr/body-sze' of https://github.…
diegomarquezp cdb3f84
fix: address comments
diegomarquezp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/BodySizeRecordingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.httpjson; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import com.google.api.gax.httpjson.testing.MockHttpService; | ||
| import com.google.api.gax.httpjson.testing.TestApiTracer; | ||
| import com.google.api.gax.rpc.EndpointContext; | ||
| import com.google.api.gax.rpc.ResponseObserver; | ||
| import com.google.api.gax.rpc.StreamController; | ||
| import com.google.auth.Credentials; | ||
| import com.google.protobuf.Field; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| class BodySizeRecordingTest { | ||
| private static final ApiMethodDescriptor<Field, Field> FAKE_METHOD_DESCRIPTOR = | ||
| ApiMethodDescriptor.<Field, Field>newBuilder() | ||
| .setFullMethodName("google.cloud.v1.Fake/FakeMethod") | ||
| .setHttpMethod("POST") | ||
| .setRequestFormatter( | ||
| ProtoMessageRequestFormatter.<Field>newBuilder() | ||
| .setPath( | ||
| "/fake/v1/name/{name}", | ||
| request -> { | ||
| Map<String, String> fields = new HashMap<>(); | ||
| ProtoRestSerializer<Field> serializer = ProtoRestSerializer.create(); | ||
| serializer.putPathParam(fields, "name", request.getName()); | ||
| return fields; | ||
| }) | ||
| .setQueryParamsExtractor(request -> new HashMap<>()) | ||
| .setRequestBodyExtractor( | ||
| request -> | ||
| ProtoRestSerializer.create() | ||
| .toBody("*", request.toBuilder().clearName().build(), false)) | ||
| .build()) | ||
| .setResponseParser( | ||
| ProtoMessageResponseParser.<Field>newBuilder() | ||
| .setDefaultInstance(Field.getDefaultInstance()) | ||
| .build()) | ||
| .build(); | ||
|
|
||
| private static final MockHttpService MOCK_SERVICE = | ||
| new MockHttpService(Collections.singletonList(FAKE_METHOD_DESCRIPTOR), "google.com:443"); | ||
|
|
||
| private static ExecutorService executorService; | ||
| private ManagedHttpJsonChannel channel; | ||
| private TestApiTracer tracer; | ||
|
|
||
| @BeforeAll | ||
| static void initialize() { | ||
| executorService = Executors.newFixedThreadPool(2); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void destroy() { | ||
| executorService.shutdownNow(); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| channel = | ||
| ManagedHttpJsonChannel.newBuilder() | ||
| .setEndpoint("google.com:443") | ||
| .setExecutor(executorService) | ||
| .setHttpTransport(MOCK_SERVICE) | ||
| .build(); | ||
| tracer = new TestApiTracer(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| MOCK_SERVICE.reset(); | ||
| } | ||
|
|
||
| @Test | ||
| void testBodySizeRecording() throws Exception { | ||
| HttpJsonDirectCallable<Field, Field> callable = | ||
| new HttpJsonDirectCallable<>(FAKE_METHOD_DESCRIPTOR); | ||
|
|
||
| EndpointContext endpointContext = Mockito.mock(EndpointContext.class); | ||
| Mockito.doNothing() | ||
| .when(endpointContext) | ||
| .validateUniverseDomain( | ||
| Mockito.any(Credentials.class), Mockito.any(HttpJsonStatusCode.class)); | ||
|
|
||
| HttpJsonCallContext callContext = | ||
| HttpJsonCallContext.createDefault() | ||
| .withChannel(channel) | ||
| .withEndpointContext(endpointContext) | ||
| .withTracer(tracer); | ||
|
|
||
| Field request = Field.newBuilder().setName("bob").setNumber(42).build(); | ||
| Field response = Field.newBuilder().setName("alice").setNumber(43).build(); | ||
|
|
||
| MOCK_SERVICE.addResponse(response); | ||
|
|
||
| callable.futureCall(request, callContext).get(); | ||
|
|
||
| // Verify response size | ||
| // MockHttpService uses ProtoRestSerializer which pretty-prints. | ||
| String expectedResponseBody = ProtoRestSerializer.create().toBody("*", response, false); | ||
| long expectedResponseSize = expectedResponseBody.getBytes("UTF-8").length; | ||
| assertThat(tracer.getResponseReceivedSize()).isEqualTo(expectedResponseSize); | ||
| // Unary calls should NOT call responseReceived() (reserved for streaming) | ||
| assertThat(tracer.getResponsesReceived()).isEqualTo(0); | ||
| } | ||
|
|
||
| @Test | ||
| void testBodySizeRecordingServerStreaming() throws Exception { | ||
| ApiMethodDescriptor<Field, Field> methodServerStreaming = | ||
| FAKE_METHOD_DESCRIPTOR.toBuilder() | ||
| .setType(ApiMethodDescriptor.MethodType.SERVER_STREAMING) | ||
| .build(); | ||
|
|
||
| MockHttpService streamingMockService = | ||
| new MockHttpService(Collections.singletonList(methodServerStreaming), "google.com:443"); | ||
| ManagedHttpJsonChannel streamingChannel = | ||
| ManagedHttpJsonChannel.newBuilder() | ||
| .setEndpoint("google.com:443") | ||
| .setExecutor(executorService) | ||
| .setHttpTransport(streamingMockService) | ||
| .build(); | ||
|
|
||
| HttpJsonDirectServerStreamingCallable<Field, Field> callable = | ||
| new HttpJsonDirectServerStreamingCallable<>(methodServerStreaming); | ||
|
|
||
| EndpointContext endpointContext = Mockito.mock(EndpointContext.class); | ||
| Mockito.doNothing() | ||
| .when(endpointContext) | ||
| .validateUniverseDomain( | ||
| Mockito.any(Credentials.class), Mockito.any(HttpJsonStatusCode.class)); | ||
|
|
||
| HttpJsonCallContext callContext = | ||
| HttpJsonCallContext.createDefault() | ||
| .withChannel(streamingChannel) | ||
| .withEndpointContext(endpointContext) | ||
| .withTracer(tracer); | ||
|
|
||
| Field request = Field.newBuilder().setName("bob").setNumber(42).build(); | ||
| Field response1 = Field.newBuilder().setName("alice1").setNumber(43).build(); | ||
| Field response2 = Field.newBuilder().setName("alice2").setNumber(44).build(); | ||
|
|
||
| streamingMockService.addResponse(new Field[] {response1, response2}); | ||
|
|
||
| final List<Field> receivedResponses = new java.util.ArrayList<>(); | ||
| final CountDownLatch latch = new CountDownLatch(1); | ||
|
|
||
| callable.call( | ||
| request, | ||
| new ResponseObserver<Field>() { | ||
| @Override | ||
| public void onStart(StreamController controller) { | ||
| // no behavior needed | ||
| } | ||
|
|
||
| @Override | ||
| public void onResponse(Field response) { | ||
| receivedResponses.add(response); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable t) { | ||
| latch.countDown(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onComplete() { | ||
| latch.countDown(); | ||
| } | ||
| }, | ||
| callContext); | ||
|
|
||
| latch.await(10, TimeUnit.SECONDS); | ||
|
|
||
| assertThat(receivedResponses).hasSize(2); | ||
|
|
||
| // Verify response size | ||
| // MockHttpService server-streaming response construction adds [ ] and , | ||
| String resp1Json = methodServerStreaming.getResponseParser().serialize(response1); | ||
| String resp2Json = methodServerStreaming.getResponseParser().serialize(response2); | ||
| long expectedTotalResponseSize = | ||
| ("[" + resp1Json + "," + resp2Json + "]").getBytes(StandardCharsets.UTF_8).length; | ||
|
|
||
| assertThat(tracer.getResponseReceivedSize()).isEqualTo(expectedTotalResponseSize); | ||
| // Server-streaming calls should call responseReceived() for EACH message | ||
| assertThat(tracer.getResponsesReceived()).isEqualTo(2); | ||
| streamingChannel.shutdownNow(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not seem to be an intuitive approach, can we rely on the ContentLength header instead? See discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done