From 1e9b47b4c35f9046cec3718cadbc7410fdd9ffe1 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 14 Jul 2023 01:39:51 -0700 Subject: [PATCH] Update RPC metrics view under stable http semconv (#8948) --- .../api/instrumenter/rpc/MetricsView.java | 8 + .../api/instrumenter/rpc/MetricsViewTest.java | 144 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsViewTest.java diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsView.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsView.java index 16d427c536b5..66da434d99f6 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsView.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsView.java @@ -8,6 +8,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.util.HashSet; import java.util.Set; @@ -31,6 +32,13 @@ private static Set buildAlwaysInclude() { view.add(SemanticAttributes.RPC_SERVICE); view.add(SemanticAttributes.RPC_METHOD); view.add(SemanticAttributes.RPC_GRPC_STATUS_CODE); + // stable http semconv + view.add(NetworkAttributes.NETWORK_TYPE); + view.add(NetworkAttributes.NETWORK_TRANSPORT); + view.add(NetworkAttributes.SERVER_ADDRESS); + view.add(NetworkAttributes.SERVER_PORT); + view.add(NetworkAttributes.SERVER_SOCKET_ADDRESS); + view.add(NetworkAttributes.SERVER_SOCKET_PORT); return view; } diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsViewTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsViewTest.java new file mode 100644 index 000000000000..0197b66e1e3d --- /dev/null +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsViewTest.java @@ -0,0 +1,144 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.api.instrumenter.rpc; + +import static io.opentelemetry.api.common.AttributeKey.stringKey; +import static io.opentelemetry.instrumentation.api.instrumenter.rpc.MetricsView.applyClientView; +import static io.opentelemetry.instrumentation.api.instrumenter.rpc.MetricsView.applyServerView; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes; +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import org.junit.jupiter.api.Test; + +class MetricsViewTest { + + @Test + void shouldApplyClientView() { + Attributes startAttributes = + Attributes.builder() + .put(SemanticAttributes.RPC_SYSTEM, "grpc") + .put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService") + .put(SemanticAttributes.RPC_METHOD, "exampleMethod") + .put(stringKey("one"), "1") + .build(); + + Attributes endAttributes = + Attributes.builder() + .put(SemanticAttributes.NET_PEER_NAME, "example.com") + .put(SemanticAttributes.NET_PEER_PORT, 8080) + .put(SemanticAttributes.NET_TRANSPORT, "ip_tcp") + .put(stringKey("two"), "2") + .build(); + + assertThat(applyClientView(startAttributes, endAttributes)) + .containsOnly( + entry(SemanticAttributes.RPC_SYSTEM, "grpc"), + entry(SemanticAttributes.RPC_SERVICE, "myservice.EchoService"), + entry(SemanticAttributes.RPC_METHOD, "exampleMethod"), + entry(SemanticAttributes.NET_PEER_NAME, "example.com"), + entry(SemanticAttributes.NET_PEER_PORT, 8080L), + entry(SemanticAttributes.NET_TRANSPORT, "ip_tcp")); + } + + @Test + void shouldApplyClientView_stableHttpSemconv() { + Attributes startAttributes = + Attributes.builder() + .put(SemanticAttributes.RPC_SYSTEM, "grpc") + .put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService") + .put(SemanticAttributes.RPC_METHOD, "exampleMethod") + .put(stringKey("one"), "1") + .build(); + + Attributes endAttributes = + Attributes.builder() + .put(NetworkAttributes.SERVER_ADDRESS, "example.com") + .put(NetworkAttributes.SERVER_PORT, 8080) + .put(NetworkAttributes.SERVER_SOCKET_ADDRESS, "127.0.0.1") + .put(NetworkAttributes.SERVER_SOCKET_PORT, 12345) + .put(NetworkAttributes.NETWORK_TYPE, "ipv4") + .put(NetworkAttributes.NETWORK_TRANSPORT, "tcp") + .put(stringKey("two"), "2") + .build(); + + assertThat(applyClientView(startAttributes, endAttributes)) + .containsOnly( + entry(SemanticAttributes.RPC_SYSTEM, "grpc"), + entry(SemanticAttributes.RPC_SERVICE, "myservice.EchoService"), + entry(SemanticAttributes.RPC_METHOD, "exampleMethod"), + entry(NetworkAttributes.SERVER_ADDRESS, "example.com"), + entry(NetworkAttributes.SERVER_PORT, 8080L), + entry(NetworkAttributes.SERVER_SOCKET_ADDRESS, "127.0.0.1"), + entry(NetworkAttributes.SERVER_SOCKET_PORT, 12345L), + entry(NetworkAttributes.NETWORK_TYPE, "ipv4"), + entry(NetworkAttributes.NETWORK_TRANSPORT, "tcp")); + } + + @Test + void shouldApplyServerView() { + Attributes startAttributes = + Attributes.builder() + .put(SemanticAttributes.RPC_SYSTEM, "grpc") + .put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService") + .put(SemanticAttributes.RPC_METHOD, "exampleMethod") + .put(stringKey("one"), "1") + .build(); + + Attributes endAttributes = + Attributes.builder() + .put(SemanticAttributes.NET_HOST_NAME, "example.com") + .put(SemanticAttributes.NET_SOCK_HOST_ADDR, "127.0.0.1") + .put(SemanticAttributes.NET_HOST_PORT, 8080) + .put(SemanticAttributes.NET_TRANSPORT, "ip_tcp") + .put(stringKey("two"), "2") + .build(); + + assertThat(applyServerView(startAttributes, endAttributes)) + .containsOnly( + entry(SemanticAttributes.RPC_SYSTEM, "grpc"), + entry(SemanticAttributes.RPC_SERVICE, "myservice.EchoService"), + entry(SemanticAttributes.RPC_METHOD, "exampleMethod"), + entry(SemanticAttributes.NET_HOST_NAME, "example.com"), + entry(SemanticAttributes.NET_TRANSPORT, "ip_tcp")); + } + + @Test + void shouldApplyServerView_stableHttpSemconv() { + Attributes startAttributes = + Attributes.builder() + .put(SemanticAttributes.RPC_SYSTEM, "grpc") + .put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService") + .put(SemanticAttributes.RPC_METHOD, "exampleMethod") + .put(stringKey("one"), "1") + .build(); + + Attributes endAttributes = + Attributes.builder() + .put(NetworkAttributes.SERVER_ADDRESS, "example.com") + .put(NetworkAttributes.SERVER_PORT, 8080) + .put(NetworkAttributes.SERVER_SOCKET_ADDRESS, "127.0.0.1") + .put(NetworkAttributes.SERVER_SOCKET_PORT, 12345) + .put(NetworkAttributes.NETWORK_TYPE, "ipv4") + .put(NetworkAttributes.NETWORK_TRANSPORT, "tcp") + .put(stringKey("two"), "2") + .build(); + + assertThat(applyServerView(startAttributes, endAttributes)) + .containsOnly( + entry(SemanticAttributes.RPC_SYSTEM, "grpc"), + entry(SemanticAttributes.RPC_SERVICE, "myservice.EchoService"), + entry(SemanticAttributes.RPC_METHOD, "exampleMethod"), + entry(NetworkAttributes.SERVER_ADDRESS, "example.com"), + entry(NetworkAttributes.SERVER_PORT, 8080L), + entry(NetworkAttributes.SERVER_SOCKET_ADDRESS, "127.0.0.1"), + entry(NetworkAttributes.SERVER_SOCKET_PORT, 12345L), + entry(NetworkAttributes.NETWORK_TYPE, "ipv4"), + entry(NetworkAttributes.NETWORK_TRANSPORT, "tcp")); + } +}