Skip to content

Commit 1e9b47b

Browse files
authored
Update RPC metrics view under stable http semconv (open-telemetry#8948)
1 parent 6f745e8 commit 1e9b47b

File tree

2 files changed

+152
-0
lines changed
  • instrumentation-api-semconv/src

2 files changed

+152
-0
lines changed

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsView.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.opentelemetry.api.common.AttributeKey;
99
import io.opentelemetry.api.common.Attributes;
1010
import io.opentelemetry.api.common.AttributesBuilder;
11+
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes;
1112
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
1213
import java.util.HashSet;
1314
import java.util.Set;
@@ -31,6 +32,13 @@ private static Set<AttributeKey> buildAlwaysInclude() {
3132
view.add(SemanticAttributes.RPC_SERVICE);
3233
view.add(SemanticAttributes.RPC_METHOD);
3334
view.add(SemanticAttributes.RPC_GRPC_STATUS_CODE);
35+
// stable http semconv
36+
view.add(NetworkAttributes.NETWORK_TYPE);
37+
view.add(NetworkAttributes.NETWORK_TRANSPORT);
38+
view.add(NetworkAttributes.SERVER_ADDRESS);
39+
view.add(NetworkAttributes.SERVER_PORT);
40+
view.add(NetworkAttributes.SERVER_SOCKET_ADDRESS);
41+
view.add(NetworkAttributes.SERVER_SOCKET_PORT);
3442
return view;
3543
}
3644

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.api.instrumenter.rpc;
7+
8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
import static io.opentelemetry.instrumentation.api.instrumenter.rpc.MetricsView.applyClientView;
10+
import static io.opentelemetry.instrumentation.api.instrumenter.rpc.MetricsView.applyServerView;
11+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
12+
import static org.assertj.core.api.Assertions.entry;
13+
14+
import io.opentelemetry.api.common.Attributes;
15+
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.NetworkAttributes;
16+
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
17+
import org.junit.jupiter.api.Test;
18+
19+
class MetricsViewTest {
20+
21+
@Test
22+
void shouldApplyClientView() {
23+
Attributes startAttributes =
24+
Attributes.builder()
25+
.put(SemanticAttributes.RPC_SYSTEM, "grpc")
26+
.put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService")
27+
.put(SemanticAttributes.RPC_METHOD, "exampleMethod")
28+
.put(stringKey("one"), "1")
29+
.build();
30+
31+
Attributes endAttributes =
32+
Attributes.builder()
33+
.put(SemanticAttributes.NET_PEER_NAME, "example.com")
34+
.put(SemanticAttributes.NET_PEER_PORT, 8080)
35+
.put(SemanticAttributes.NET_TRANSPORT, "ip_tcp")
36+
.put(stringKey("two"), "2")
37+
.build();
38+
39+
assertThat(applyClientView(startAttributes, endAttributes))
40+
.containsOnly(
41+
entry(SemanticAttributes.RPC_SYSTEM, "grpc"),
42+
entry(SemanticAttributes.RPC_SERVICE, "myservice.EchoService"),
43+
entry(SemanticAttributes.RPC_METHOD, "exampleMethod"),
44+
entry(SemanticAttributes.NET_PEER_NAME, "example.com"),
45+
entry(SemanticAttributes.NET_PEER_PORT, 8080L),
46+
entry(SemanticAttributes.NET_TRANSPORT, "ip_tcp"));
47+
}
48+
49+
@Test
50+
void shouldApplyClientView_stableHttpSemconv() {
51+
Attributes startAttributes =
52+
Attributes.builder()
53+
.put(SemanticAttributes.RPC_SYSTEM, "grpc")
54+
.put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService")
55+
.put(SemanticAttributes.RPC_METHOD, "exampleMethod")
56+
.put(stringKey("one"), "1")
57+
.build();
58+
59+
Attributes endAttributes =
60+
Attributes.builder()
61+
.put(NetworkAttributes.SERVER_ADDRESS, "example.com")
62+
.put(NetworkAttributes.SERVER_PORT, 8080)
63+
.put(NetworkAttributes.SERVER_SOCKET_ADDRESS, "127.0.0.1")
64+
.put(NetworkAttributes.SERVER_SOCKET_PORT, 12345)
65+
.put(NetworkAttributes.NETWORK_TYPE, "ipv4")
66+
.put(NetworkAttributes.NETWORK_TRANSPORT, "tcp")
67+
.put(stringKey("two"), "2")
68+
.build();
69+
70+
assertThat(applyClientView(startAttributes, endAttributes))
71+
.containsOnly(
72+
entry(SemanticAttributes.RPC_SYSTEM, "grpc"),
73+
entry(SemanticAttributes.RPC_SERVICE, "myservice.EchoService"),
74+
entry(SemanticAttributes.RPC_METHOD, "exampleMethod"),
75+
entry(NetworkAttributes.SERVER_ADDRESS, "example.com"),
76+
entry(NetworkAttributes.SERVER_PORT, 8080L),
77+
entry(NetworkAttributes.SERVER_SOCKET_ADDRESS, "127.0.0.1"),
78+
entry(NetworkAttributes.SERVER_SOCKET_PORT, 12345L),
79+
entry(NetworkAttributes.NETWORK_TYPE, "ipv4"),
80+
entry(NetworkAttributes.NETWORK_TRANSPORT, "tcp"));
81+
}
82+
83+
@Test
84+
void shouldApplyServerView() {
85+
Attributes startAttributes =
86+
Attributes.builder()
87+
.put(SemanticAttributes.RPC_SYSTEM, "grpc")
88+
.put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService")
89+
.put(SemanticAttributes.RPC_METHOD, "exampleMethod")
90+
.put(stringKey("one"), "1")
91+
.build();
92+
93+
Attributes endAttributes =
94+
Attributes.builder()
95+
.put(SemanticAttributes.NET_HOST_NAME, "example.com")
96+
.put(SemanticAttributes.NET_SOCK_HOST_ADDR, "127.0.0.1")
97+
.put(SemanticAttributes.NET_HOST_PORT, 8080)
98+
.put(SemanticAttributes.NET_TRANSPORT, "ip_tcp")
99+
.put(stringKey("two"), "2")
100+
.build();
101+
102+
assertThat(applyServerView(startAttributes, endAttributes))
103+
.containsOnly(
104+
entry(SemanticAttributes.RPC_SYSTEM, "grpc"),
105+
entry(SemanticAttributes.RPC_SERVICE, "myservice.EchoService"),
106+
entry(SemanticAttributes.RPC_METHOD, "exampleMethod"),
107+
entry(SemanticAttributes.NET_HOST_NAME, "example.com"),
108+
entry(SemanticAttributes.NET_TRANSPORT, "ip_tcp"));
109+
}
110+
111+
@Test
112+
void shouldApplyServerView_stableHttpSemconv() {
113+
Attributes startAttributes =
114+
Attributes.builder()
115+
.put(SemanticAttributes.RPC_SYSTEM, "grpc")
116+
.put(SemanticAttributes.RPC_SERVICE, "myservice.EchoService")
117+
.put(SemanticAttributes.RPC_METHOD, "exampleMethod")
118+
.put(stringKey("one"), "1")
119+
.build();
120+
121+
Attributes endAttributes =
122+
Attributes.builder()
123+
.put(NetworkAttributes.SERVER_ADDRESS, "example.com")
124+
.put(NetworkAttributes.SERVER_PORT, 8080)
125+
.put(NetworkAttributes.SERVER_SOCKET_ADDRESS, "127.0.0.1")
126+
.put(NetworkAttributes.SERVER_SOCKET_PORT, 12345)
127+
.put(NetworkAttributes.NETWORK_TYPE, "ipv4")
128+
.put(NetworkAttributes.NETWORK_TRANSPORT, "tcp")
129+
.put(stringKey("two"), "2")
130+
.build();
131+
132+
assertThat(applyServerView(startAttributes, endAttributes))
133+
.containsOnly(
134+
entry(SemanticAttributes.RPC_SYSTEM, "grpc"),
135+
entry(SemanticAttributes.RPC_SERVICE, "myservice.EchoService"),
136+
entry(SemanticAttributes.RPC_METHOD, "exampleMethod"),
137+
entry(NetworkAttributes.SERVER_ADDRESS, "example.com"),
138+
entry(NetworkAttributes.SERVER_PORT, 8080L),
139+
entry(NetworkAttributes.SERVER_SOCKET_ADDRESS, "127.0.0.1"),
140+
entry(NetworkAttributes.SERVER_SOCKET_PORT, 12345L),
141+
entry(NetworkAttributes.NETWORK_TYPE, "ipv4"),
142+
entry(NetworkAttributes.NETWORK_TRANSPORT, "tcp"));
143+
}
144+
}

0 commit comments

Comments
 (0)