Skip to content

Commit f05000a

Browse files
committed
check for well-known method
1 parent e9f2745 commit f05000a

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcAttributesGetter.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ default Long getResponseSize(REQUEST request) {
4646
* method is unavailable
4747
*/
4848
@Nullable
49-
default String getFullMethod(REQUEST request) {
50-
String service = getService(request);
51-
String method = getMethod(request);
52-
if (service == null || method == null) {
53-
return null;
54-
}
55-
return service + "/" + method;
49+
default String getRpcMethod(REQUEST request) {
50+
return null;
51+
}
52+
53+
/**
54+
* Returns whether the RPC method is a well-known method.
55+
*
56+
* <p>This is used to determine whether to set the {@code rpc.method} attribute to the actual
57+
* method name, or to set it to {@code "_OTHER"} and store the actual method name in {@code
58+
* rpc.method_original}.
59+
*/
60+
default boolean isWellKnownMethod(REQUEST request) {
61+
return false;
5662
}
5763
}

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcCommonAttributesExtractor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ abstract class RpcCommonAttributesExtractor<REQUEST, RESPONSE>
2929
// use RPC_SYSTEM_NAME for stable semconv
3030
static final AttributeKey<String> RPC_SYSTEM = AttributeKey.stringKey("rpc.system");
3131

32+
static final AttributeKey<String> RPC_METHOD_ORIGINAL =
33+
AttributeKey.stringKey("rpc.method_original");
34+
3235
private final RpcAttributesGetter<REQUEST> getter;
3336

3437
RpcCommonAttributesExtractor(RpcAttributesGetter<REQUEST> getter) {
@@ -44,7 +47,13 @@ public final void onStart(AttributesBuilder attributes, Context parentContext, R
4447
attributes,
4548
RPC_SYSTEM_NAME,
4649
system == null ? null : SemconvStability.stableRpcSystemName(system));
47-
internalSet(attributes, RPC_METHOD, getter.getFullMethod(request));
50+
String method = getter.getRpcMethod(request);
51+
if (getter.isWellKnownMethod(request)) {
52+
internalSet(attributes, RPC_METHOD, method);
53+
} else {
54+
internalSet(attributes, RPC_METHOD_ORIGINAL, method);
55+
internalSet(attributes, RPC_METHOD, "_OTHER");
56+
}
4857
}
4958

5059
if (SemconvStability.emitOldRpcSemconv()) {

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcSpanNameExtractor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.instrumentation.api.incubator.semconv.rpc;
77

88
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
9+
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
910

1011
/** A {@link SpanNameExtractor} for RPC requests. */
1112
public final class RpcSpanNameExtractor<REQUEST> implements SpanNameExtractor<REQUEST> {
@@ -27,6 +28,10 @@ private RpcSpanNameExtractor(RpcAttributesGetter<REQUEST> getter) {
2728

2829
@Override
2930
public String extract(REQUEST request) {
31+
if (SemconvStability.emitStableRpcSemconv()) {
32+
return getter.getRpcMethod(request);
33+
}
34+
3035
String service = getter.getService(request);
3136
String method = getter.getMethod(request);
3237
if (service == null || method == null) {

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcAttributesExtractorTest.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.instrumentation.api.incubator.semconv.rpc;
77

8+
import static io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcCommonAttributesExtractor.RPC_METHOD_ORIGINAL;
89
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
910
import static org.assertj.core.api.Assertions.entry;
1011

@@ -18,12 +19,18 @@
1819
import java.util.HashMap;
1920
import java.util.List;
2021
import java.util.Map;
22+
import javax.annotation.Nullable;
2123
import org.junit.jupiter.api.Test;
2224

2325
class RpcAttributesExtractorTest {
2426

25-
enum TestGetter implements RpcAttributesGetter<Map<String, String>> {
26-
INSTANCE;
27+
private static class TestGetter implements RpcAttributesGetter<Map<String, String>> {
28+
29+
private final boolean wellKnownMethod;
30+
31+
private TestGetter(boolean wellKnownMethod) {
32+
this.wellKnownMethod = wellKnownMethod;
33+
}
2734

2835
@Override
2936
public String getSystem(Map<String, String> request) {
@@ -39,16 +46,42 @@ public String getService(Map<String, String> request) {
3946
public String getMethod(Map<String, String> request) {
4047
return request.get("method");
4148
}
49+
50+
@Nullable
51+
@Override
52+
public String getRpcMethod(Map<String, String> request) {
53+
String service = getService(request);
54+
String method = getMethod(request);
55+
if (service == null || method == null) {
56+
return null;
57+
}
58+
return service + "/" + method;
59+
}
60+
61+
@Override
62+
public boolean isWellKnownMethod(Map<String, String> stringStringMap) {
63+
return wellKnownMethod;
64+
}
4265
}
4366

4467
@Test
4568
void server() {
46-
testExtractor(RpcServerAttributesExtractor.create(TestGetter.INSTANCE));
69+
testExtractor(RpcServerAttributesExtractor.create(new TestGetter(false)), "my.Service/Method");
70+
}
71+
72+
@Test
73+
void serverWellKnown() {
74+
testExtractor(RpcServerAttributesExtractor.create(new TestGetter(true)), null);
4775
}
4876

4977
@Test
5078
void client() {
51-
testExtractor(RpcClientAttributesExtractor.create(TestGetter.INSTANCE));
79+
testExtractor(RpcClientAttributesExtractor.create(new TestGetter(false)), "my.Service/Method");
80+
}
81+
82+
@Test
83+
void clientWellKnown() {
84+
testExtractor(RpcClientAttributesExtractor.create(new TestGetter(true)), null);
5285
}
5386

5487
// Stable semconv keys
@@ -65,7 +98,8 @@ void client() {
6598
private static final AttributeKey<String> RPC_METHOD =
6699
io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_METHOD;
67100

68-
private static void testExtractor(AttributesExtractor<Map<String, String>, Void> extractor) {
101+
private static void testExtractor(
102+
AttributesExtractor<Map<String, String>, Void> extractor, @Nullable String originalMethod) {
69103
Map<String, String> request = new HashMap<>();
70104
request.put("service", "my.Service");
71105
request.put("method", "Method");
@@ -80,7 +114,12 @@ private static void testExtractor(AttributesExtractor<Map<String, String>, Void>
80114

81115
if (SemconvStability.emitStableRpcSemconv()) {
82116
expectedEntries.add(entry(RPC_SYSTEM_NAME, "test"));
83-
expectedEntries.add(entry(RPC_METHOD, "my.Service/Method"));
117+
if (originalMethod != null) {
118+
expectedEntries.add(entry(RPC_METHOD_ORIGINAL, originalMethod));
119+
expectedEntries.add(entry(RPC_METHOD, "_OTHER"));
120+
} else {
121+
expectedEntries.add(entry(RPC_METHOD, "my.Service/Method"));
122+
}
84123
}
85124

86125
if (SemconvStability.emitOldRpcSemconv()) {

0 commit comments

Comments
 (0)