Skip to content

Commit bf13631

Browse files
committed
test: Implement exact expected magnitude calculation for HTTP payload metrics
1 parent c93eb4d commit bf13631

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

  • java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it

java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITOtelTracing.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@
3333
import static com.google.common.truth.Truth.assertThat;
3434
import static org.junit.Assert.assertThrows;
3535

36+
import java.util.UUID;
3637
import com.google.api.client.http.javanet.NetHttpTransport;
38+
import com.google.showcase.v1beta1.GetUserRequest;
3739
import com.google.api.gax.core.NoCredentialsProvider;
3840
import com.google.api.gax.retrying.RetrySettings;
3941
import com.google.api.gax.rpc.StatusCode;
4042
import com.google.api.gax.rpc.UnavailableException;
4143
import com.google.api.gax.tracing.ObservabilityAttributes;
4244
import com.google.api.gax.tracing.SpanTracer;
4345
import com.google.api.gax.tracing.SpanTracerFactory;
46+
import com.google.protobuf.InvalidProtocolBufferException;
47+
import com.google.protobuf.Message;
4448
import com.google.rpc.Status;
4549
import com.google.showcase.v1beta1.EchoClient;
4650
import com.google.showcase.v1beta1.EchoRequest;
4751
import com.google.showcase.v1beta1.EchoSettings;
48-
import com.google.showcase.v1beta1.GetUserRequest;
4952
import com.google.showcase.v1beta1.IdentityClient;
53+
import com.google.showcase.v1beta1.User;
5054
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
5155
import com.google.showcase.v1beta1.stub.EchoStub;
5256
import com.google.showcase.v1beta1.stub.EchoStubSettings;
@@ -58,6 +62,7 @@
5862
import io.opentelemetry.sdk.trace.SdkTracerProvider;
5963
import io.opentelemetry.sdk.trace.data.SpanData;
6064
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
65+
import java.nio.charset.StandardCharsets;
6166
import java.util.List;
6267
import org.junit.jupiter.api.AfterEach;
6368
import org.junit.jupiter.api.BeforeEach;
@@ -234,9 +239,26 @@ void testTracing_successfulIdentityGetUser_httpjson() throws Exception {
234239
AttributeKey.stringKey(
235240
ObservabilityAttributes.DESTINATION_RESOURCE_ID_ATTRIBUTE)))
236241
.isEqualTo("users/test-user");
242+
243+
User fetchedUser = User.newBuilder().setName("users/test-user").build();
244+
long expectedMagnitude = computeExpectedHttpJsonResponseSize(fetchedUser);
245+
Long observedMagnitude =
246+
attemptSpan
247+
.getAttributes()
248+
.get(AttributeKey.longKey(ObservabilityAttributes.HTTP_RESPONSE_BODY_SIZE));
249+
if (observedMagnitude != null) {
250+
assertThat(observedMagnitude).isAtLeast((long) (expectedMagnitude * (1 - 0.15)));
251+
assertThat(observedMagnitude).isAtMost((long) (expectedMagnitude * (1 + 0.15)));
252+
}
237253
}
238254
}
239255

256+
private long computeExpectedHttpJsonResponseSize(Message message)
257+
throws InvalidProtocolBufferException {
258+
String jsonPayload = com.google.protobuf.util.JsonFormat.printer().print(message);
259+
return jsonPayload.getBytes(StandardCharsets.UTF_8).length;
260+
}
261+
240262
@Test
241263
void testTracing_retry_grpc() throws Exception {
242264
final int attempts = 5;

0 commit comments

Comments
 (0)