-
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
4ef3d9f
c00e49a
c6da324
72be770
7865574
1265da3
94e55db
2cb0479
dc84757
cba2159
9435824
0ae245d
9f1bcd8
eb959bb
7bc8aa0
11950af
6e8b311
6035f29
960ddd1
097e5ee
ab61cbb
869bd2a
c721960
53117a3
247f871
fad817a
c2fdd2a
e2df169
2bf6326
43a12ab
c0427b3
c93eb4d
bf13631
f92c66d
a882735
4a02b34
03d905a
6e88a5a
cdb3f84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| 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 | ||
| public static void initialize() { | ||
|
Check warning on line 92 in gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/BodySizeRecordingTest.java
|
||
| executorService = Executors.newFixedThreadPool(2); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void destroy() { | ||
|
Check warning on line 97 in gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/BodySizeRecordingTest.java
|
||
| executorService.shutdownNow(); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws IOException { | ||
|
Check warning on line 102 in gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/BodySizeRecordingTest.java
|
||
| 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); | ||
| } | ||
|
|
||
| @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) {} | ||
|
Check failure on line 192 in gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/BodySizeRecordingTest.java
|
||
|
|
||
| @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("UTF-8").length; | ||
|
|
||
| assertThat(tracer.getResponseReceivedSize()).isEqualTo(expectedTotalResponseSize); | ||
| streamingChannel.shutdownNow(); | ||
| } | ||
| } | ||
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