Skip to content

Commit e4b6d0d

Browse files
committed
gRPC old/stable semconv support
1 parent 56a4853 commit e4b6d0d

8 files changed

Lines changed: 722 additions & 216 deletions

File tree

instrumentation/armeria/armeria-grpc-1.14/javaagent/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ tasks.test {
6363
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
6464
}
6565

66+
tasks {
67+
val testStableSemconv by registering(Test::class) {
68+
testClassesDirs = sourceSets.test.get().output.classesDirs
69+
classpath = sourceSets.test.get().runtimeClasspath
70+
jvmArgs("-Dotel.semconv-stability.opt-in=rpc")
71+
systemProperty("metadataConfig", "otel.semconv-stability.opt-in=rpc")
72+
}
73+
74+
check {
75+
dependsOn(testStableSemconv)
76+
}
77+
}
78+
6679
if (findProperty("denyUnsafe") as Boolean) {
6780
tasks.withType<Test>().configureEach {
6881
enabled = false

instrumentation/armeria/armeria-grpc-1.14/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/armeria/grpc/v1_14/ArmeriaGrpcTest.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55

66
package io.opentelemetry.javaagent.instrumentation.armeria.grpc.v1_14;
77

8+
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitOldRpcSemconv;
9+
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableRpcSemconv;
10+
import static io.opentelemetry.instrumentation.testing.junit.rpc.SemconvRpcStabilityUtil.maybeStable;
811
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
912
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
1013
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
1114
import static io.opentelemetry.semconv.incubating.MessageIncubatingAttributes.MESSAGE_ID;
1215
import static io.opentelemetry.semconv.incubating.MessageIncubatingAttributes.MESSAGE_TYPE;
1316
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_GRPC_STATUS_CODE;
1417
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_METHOD;
18+
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_RESPONSE_STATUS_CODE;
1519
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SERVICE;
1620
import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SYSTEM;
1721
import static org.assertj.core.api.Assertions.assertThat;
@@ -78,10 +82,19 @@ void grpcInstrumentation() {
7882
.hasKind(SpanKind.CLIENT)
7983
.hasParent(trace.getSpan(0))
8084
.hasAttributesSatisfyingExactly(
81-
equalTo(RPC_SYSTEM, "grpc"),
82-
equalTo(RPC_SERVICE, "example.Greeter"),
83-
equalTo(RPC_METHOD, "SayHello"),
84-
equalTo(RPC_GRPC_STATUS_CODE, (long) Status.Code.OK.value()),
85+
equalTo(maybeStable(RPC_SYSTEM), "grpc"),
86+
equalTo(RPC_SERVICE, emitOldRpcSemconv() ? "example.Greeter" : null),
87+
equalTo(
88+
RPC_METHOD,
89+
emitStableRpcSemconv() ? "example.Greeter/SayHello" : "SayHello"),
90+
equalTo(
91+
RPC_GRPC_STATUS_CODE,
92+
emitOldRpcSemconv() ? (long) Status.Code.OK.value() : null),
93+
equalTo(
94+
RPC_RESPONSE_STATUS_CODE,
95+
emitStableRpcSemconv()
96+
? String.valueOf(Status.Code.OK.value())
97+
: null),
8598
equalTo(SERVER_ADDRESS, "127.0.0.1"),
8699
equalTo(SERVER_PORT, (long) server.httpPort()))
87100
.hasEventsSatisfyingExactly(
@@ -101,10 +114,19 @@ void grpcInstrumentation() {
101114
.hasKind(SpanKind.SERVER)
102115
.hasParent(trace.getSpan(1))
103116
.hasAttributesSatisfyingExactly(
104-
equalTo(RPC_SYSTEM, "grpc"),
105-
equalTo(RPC_SERVICE, "example.Greeter"),
106-
equalTo(RPC_METHOD, "SayHello"),
107-
equalTo(RPC_GRPC_STATUS_CODE, (long) Status.Code.OK.value()),
117+
equalTo(maybeStable(RPC_SYSTEM), "grpc"),
118+
equalTo(RPC_SERVICE, emitOldRpcSemconv() ? "example.Greeter" : null),
119+
equalTo(
120+
RPC_METHOD,
121+
emitStableRpcSemconv() ? "example.Greeter/SayHello" : "SayHello"),
122+
equalTo(
123+
RPC_GRPC_STATUS_CODE,
124+
emitOldRpcSemconv() ? (long) Status.Code.OK.value() : null),
125+
equalTo(
126+
RPC_RESPONSE_STATUS_CODE,
127+
emitStableRpcSemconv()
128+
? String.valueOf(Status.Code.OK.value())
129+
: null),
108130
equalTo(SERVER_ADDRESS, "127.0.0.1"),
109131
equalTo(SERVER_PORT, server.httpPort()))
110132
.hasEventsSatisfyingExactly(

instrumentation/grpc-1.6/javaagent/build.gradle.kts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131
val collectMetadata = findProperty("collectMetadata")?.toString() ?: "false"
3232

3333
tasks {
34-
test {
34+
withType<Test>().configureEach {
3535
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
3636
// The agent context debug mechanism isn't compatible with the bridge approach which may add a
3737
// gRPC context to the root.
@@ -56,26 +56,32 @@ tasks {
5656
testClassesDirs = sourceSets.test.get().output.classesDirs
5757
classpath = sourceSets.test.get().runtimeClasspath
5858

59-
// replicated base config from standard test task
60-
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
61-
jvmArgs("-Dotel.javaagent.experimental.thread-propagation-debugger.enabled=false")
62-
jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.client.request=some-client-key")
63-
jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.server.request=some-server-key")
64-
jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true")
65-
6659
// exclude our grpc library instrumentation, the ContextStorageOverride contained within it
6760
// breaks the tests
6861
classpath = classpath.filter {
6962
!it.absolutePath.contains("opentelemetry-grpc-1.6")
7063
}
7164

72-
systemProperty("collectMetadata", collectMetadata)
7365
systemProperty("metadataConfig", "otel.instrumentation.grpc.experimental-span-attributes=true")
7466
jvmArgs("-Dotel.instrumentation.grpc.experimental-span-attributes=true")
7567
}
7668

69+
val testStableSemconv by registering(Test::class) {
70+
testClassesDirs = sourceSets.test.get().output.classesDirs
71+
classpath = sourceSets.test.get().runtimeClasspath
72+
73+
// exclude our grpc library instrumentation, the ContextStorageOverride contained within it
74+
// breaks the tests
75+
classpath = classpath.filter {
76+
!it.absolutePath.contains("opentelemetry-grpc-1.6")
77+
}
78+
79+
jvmArgs("-Dotel.semconv-stability.opt-in=rpc")
80+
systemProperty("metadataConfig", "otel.semconv-stability.opt-in=rpc")
81+
}
82+
7783
check {
78-
dependsOn(testExperimental)
84+
dependsOn(testExperimental, testStableSemconv)
7985
}
8086
}
8187

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.instrumentation.grpc.v1_6;
77

8+
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitOldRpcSemconv;
9+
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableRpcSemconv;
810
import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.lowercase;
911
import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.requestAttributeKey;
1012

@@ -21,6 +23,8 @@ final class GrpcAttributesExtractor implements AttributesExtractor<GrpcRequest,
2123
// copied from RpcIncubatingAttributes
2224
private static final AttributeKey<Long> RPC_GRPC_STATUS_CODE =
2325
AttributeKey.longKey("rpc.grpc.status_code");
26+
private static final AttributeKey<String> RPC_RESPONSE_STATUS_CODE =
27+
AttributeKey.stringKey("rpc.response.status_code");
2428

2529
private final GrpcRpcAttributesGetter getter;
2630
private final List<String> capturedRequestMetadata;
@@ -44,7 +48,12 @@ public void onEnd(
4448
@Nullable Status status,
4549
@Nullable Throwable error) {
4650
if (status != null) {
47-
attributes.put(RPC_GRPC_STATUS_CODE, status.getCode().value());
51+
if (emitOldRpcSemconv()) {
52+
attributes.put(RPC_GRPC_STATUS_CODE, status.getCode().value());
53+
}
54+
if (emitStableRpcSemconv()) {
55+
attributes.put(RPC_RESPONSE_STATUS_CODE, String.valueOf(status.getCode().value()));
56+
}
4857
}
4958
for (String key : capturedRequestMetadata) {
5059
List<String> value = getter.metadataValue(request, key);

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public String getMethod(GrpcRequest request) {
4646
return fullMethodName.substring(slashIndex + 1);
4747
}
4848

49+
@Override
50+
public String getRpcMethod(GrpcRequest request) {
51+
return request.getMethod().getFullMethodName();
52+
}
53+
4954
@Override
5055
@Nullable
5156
public Long getRequestSize(GrpcRequest request) {

0 commit comments

Comments
 (0)