Skip to content
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

Update RPC metrics view under stable http semconv #8948

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<AttributeKey> 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;
}

Original file line number Diff line number Diff line change
@@ -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"));
}
}