Skip to content

Commit d8723a4

Browse files
semconv stable code (#13860)
Co-authored-by: otelbot <[email protected]>
1 parent c6b60c7 commit d8723a4

File tree

79 files changed

+1010
-1218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1010
-1218
lines changed

instrumentation-annotations-support-testing/src/main/java/io/opentelemetry/javaagent/instrumentation/otelannotations/AbstractWithSpanTest.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
package io.opentelemetry.javaagent.instrumentation.otelannotations;
77

88
import static io.opentelemetry.api.common.AttributeKey.booleanKey;
9+
import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFunctionAssertions;
910
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
10-
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION;
11-
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE;
1211
import static org.assertj.core.api.Assertions.assertThat;
1312
import static org.assertj.core.api.Assertions.catchThrowable;
1413

1514
import io.opentelemetry.api.trace.SpanKind;
1615
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
1716
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
17+
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
1818
import io.opentelemetry.sdk.trace.data.StatusData;
19+
import java.util.List;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.extension.RegisterExtension;
2122

22-
@SuppressWarnings("deprecation") // CodeIncubatingAttributes.CODE_FUNCTION is deprecated
2323
public abstract class AbstractWithSpanTest<T extends U, U> {
2424

2525
@RegisterExtension
@@ -59,8 +59,7 @@ void success() {
5959
.hasKind(SpanKind.INTERNAL)
6060
.hasNoParent()
6161
.hasAttributesSatisfyingExactly(
62-
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
63-
equalTo(CODE_FUNCTION, "completable"))));
62+
codeFunctionAssertions(traced.getClass(), "completable"))));
6463
}
6564

6665
@Test
@@ -82,8 +81,7 @@ void failure() {
8281
.hasStatus(StatusData.error())
8382
.hasException(AbstractTraced.FAILURE)
8483
.hasAttributesSatisfyingExactly(
85-
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
86-
equalTo(CODE_FUNCTION, "completable"))));
84+
codeFunctionAssertions(traced.getClass(), "completable"))));
8785
}
8886

8987
@Test
@@ -92,17 +90,18 @@ void canceled() {
9290
T future = traced.completable();
9391
cancel(future);
9492

93+
List<AttributeAssertion> attributeAssertions =
94+
codeFunctionAssertions(traced.getClass(), "completable");
95+
attributeAssertions.add(equalTo(booleanKey(canceledKey()), true));
96+
9597
testing.waitAndAssertTraces(
9698
trace ->
9799
trace.hasSpansSatisfyingExactly(
98100
span ->
99101
span.hasName("Traced.completable")
100102
.hasKind(SpanKind.INTERNAL)
101103
.hasNoParent()
102-
.hasAttributesSatisfyingExactly(
103-
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
104-
equalTo(CODE_FUNCTION, "completable"),
105-
equalTo(booleanKey(canceledKey()), true))));
104+
.hasAttributesSatisfyingExactly(attributeAssertions)));
106105
}
107106

108107
@Test
@@ -118,8 +117,7 @@ void immediateSuccess() {
118117
.hasKind(SpanKind.INTERNAL)
119118
.hasNoParent()
120119
.hasAttributesSatisfyingExactly(
121-
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
122-
equalTo(CODE_FUNCTION, "alreadySucceeded"))));
120+
codeFunctionAssertions(traced.getClass(), "alreadySucceeded"))));
123121
}
124122

125123
@Test
@@ -138,7 +136,6 @@ void immediateFailure() {
138136
.hasStatus(StatusData.error())
139137
.hasException(AbstractTraced.FAILURE)
140138
.hasAttributesSatisfyingExactly(
141-
equalTo(CODE_NAMESPACE, traced.getClass().getName()),
142-
equalTo(CODE_FUNCTION, "alreadyFailed"))));
139+
codeFunctionAssertions(traced.getClass(), "alreadyFailed"))));
143140
}
144141
}

instrumentation-api-incubator/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ tasks {
4242
}
4343

4444
val testStableSemconv by registering(Test::class) {
45-
jvmArgs("-Dotel.semconv-stability.opt-in=database")
45+
jvmArgs("-Dotel.semconv-stability.opt-in=database,code")
4646
}
4747

4848
val testBothSemconv by registering(Test::class) {
49-
jvmArgs("-Dotel.semconv-stability.opt-in=database/dup")
49+
jvmArgs("-Dotel.semconv-stability.opt-in=database/dup,code/dup")
5050
}
5151

5252
check {

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/code/CodeAttributesExtractor.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import io.opentelemetry.api.common.AttributesBuilder;
1212
import io.opentelemetry.context.Context;
1313
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
14+
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
15+
import io.opentelemetry.semconv.CodeAttributes;
1416
import javax.annotation.Nullable;
1517

1618
/**
@@ -22,9 +24,9 @@ public final class CodeAttributesExtractor<REQUEST, RESPONSE>
2224
implements AttributesExtractor<REQUEST, RESPONSE> {
2325

2426
// copied from CodeIncubatingAttributes
25-
private static final AttributeKey<String> CODE_FUNCTION = AttributeKey.stringKey("code.function");
2627
private static final AttributeKey<String> CODE_NAMESPACE =
2728
AttributeKey.stringKey("code.namespace");
29+
private static final AttributeKey<String> CODE_FUNCTION = AttributeKey.stringKey("code.function");
2830

2931
/** Creates the code attributes extractor. */
3032
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
@@ -40,11 +42,28 @@ private CodeAttributesExtractor(CodeAttributesGetter<REQUEST> getter) {
4042

4143
@Override
4244
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
45+
StringBuilder sb = new StringBuilder();
4346
Class<?> cls = getter.getCodeClass(request);
4447
if (cls != null) {
45-
internalSet(attributes, CODE_NAMESPACE, cls.getName());
48+
sb.append(cls.getName());
49+
50+
if (SemconvStability.isEmitOldCodeSemconv()) {
51+
internalSet(attributes, CODE_NAMESPACE, cls.getName());
52+
}
53+
}
54+
String methodName = getter.getMethodName(request);
55+
if (methodName != null) {
56+
if (sb.length() > 0) {
57+
sb.append(".");
58+
}
59+
sb.append(methodName);
60+
if (SemconvStability.isEmitOldCodeSemconv()) {
61+
internalSet(attributes, CODE_FUNCTION, methodName);
62+
}
63+
}
64+
if (SemconvStability.isEmitStableCodeSemconv() && sb.length() > 0) {
65+
internalSet(attributes, CodeAttributes.CODE_FUNCTION_NAME, sb.toString());
4666
}
47-
internalSet(attributes, CODE_FUNCTION, getter.getMethodName(request));
4867
}
4968

5069
@Override

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/code/CodeAttributesExtractorTest.java

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

88
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
9-
import static org.assertj.core.api.Assertions.entry;
109

1110
import io.opentelemetry.api.common.Attributes;
1211
import io.opentelemetry.api.common.AttributesBuilder;
1312
import io.opentelemetry.context.Context;
1413
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
14+
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
15+
import io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil;
16+
import io.opentelemetry.semconv.CodeAttributes;
1517
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
1618
import java.util.Collections;
1719
import java.util.HashMap;
@@ -58,11 +60,19 @@ void shouldExtractAllAttributes() {
5860
underTest.onEnd(endAttributes, context, request, null, null);
5961

6062
// then
61-
assertThat(startAttributes.build())
62-
.containsOnly(
63-
entry(CodeIncubatingAttributes.CODE_NAMESPACE, TestClass.class.getName()),
64-
entry(CodeIncubatingAttributes.CODE_FUNCTION, "doSomething"));
63+
Attributes attributes = startAttributes.build();
64+
SemconvCodeStabilityUtil.codeFunctionAssertions(TestClass.class, "doSomething");
6565

66+
if (SemconvStability.isEmitStableCodeSemconv()) {
67+
assertThat(attributes)
68+
.containsEntry(
69+
CodeAttributes.CODE_FUNCTION_NAME, TestClass.class.getName() + ".doSomething");
70+
}
71+
if (SemconvStability.isEmitOldCodeSemconv()) {
72+
assertThat(attributes)
73+
.containsEntry(CodeIncubatingAttributes.CODE_NAMESPACE, TestClass.class.getName())
74+
.containsEntry(CodeIncubatingAttributes.CODE_FUNCTION, "doSomething");
75+
}
6676
assertThat(endAttributes.build().isEmpty()).isTrue();
6777
}
6878

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/SemconvStability.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,47 @@ public final class SemconvStability {
2121
private static final boolean emitOldDatabaseSemconv;
2222
private static final boolean emitStableDatabaseSemconv;
2323

24+
private static final boolean emitOldCodeSemconv;
25+
private static final boolean emitStableCodeSemconv;
26+
2427
static {
2528
boolean oldDatabase = true;
2629
boolean stableDatabase = false;
2730

31+
boolean oldCode = true;
32+
boolean stableCode = false;
33+
2834
String value = ConfigPropertiesUtil.getString("otel.semconv-stability.opt-in");
2935
if (value != null) {
3036
Set<String> values = new HashSet<>(asList(value.split(",")));
37+
38+
// no else -- technically it's possible to set "XXX,XXX/dup", in which case we
39+
// should emit both sets of attributes for XXX
40+
3141
if (values.contains("database")) {
3242
oldDatabase = false;
3343
stableDatabase = true;
3444
}
35-
// no else -- technically it's possible to set "database,database/dup", in which case we
36-
// should emit both sets of attributes
3745
if (values.contains("database/dup")) {
3846
oldDatabase = true;
3947
stableDatabase = true;
4048
}
49+
50+
if (values.contains("code")) {
51+
oldCode = false;
52+
stableCode = true;
53+
}
54+
if (values.contains("code/dup")) {
55+
oldCode = true;
56+
stableCode = true;
57+
}
4158
}
4259

4360
emitOldDatabaseSemconv = oldDatabase;
4461
emitStableDatabaseSemconv = stableDatabase;
62+
63+
emitOldCodeSemconv = oldCode;
64+
emitStableCodeSemconv = stableCode;
4565
}
4666

4767
public static boolean emitOldDatabaseSemconv() {
@@ -77,5 +97,13 @@ public static String stableDbSystemName(String oldDbSystem) {
7797
return dbSystemName != null ? dbSystemName : oldDbSystem;
7898
}
7999

100+
public static boolean isEmitOldCodeSemconv() {
101+
return emitOldCodeSemconv;
102+
}
103+
104+
public static boolean isEmitStableCodeSemconv() {
105+
return emitStableCodeSemconv;
106+
}
107+
80108
private SemconvStability() {}
81109
}

instrumentation/elasticsearch/elasticsearch-transport-5.3/javaagent/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ tasks {
7878
}
7979

8080
val testStableSemconv by registering(Test::class) {
81-
jvmArgs("-Dotel.semconv-stability.opt-in=database")
81+
jvmArgs("-Dotel.semconv-stability.opt-in=database,code")
8282
}
8383

8484
check {

instrumentation/elasticsearch/elasticsearch-transport-5.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/v5_3/springdata/Elasticsearch53SpringRepositoryTest.java

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static io.opentelemetry.api.common.AttributeKey.longKey;
99
import static io.opentelemetry.api.common.AttributeKey.stringKey;
10+
import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFunctionAssertions;
1011
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
1112
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
1213
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
@@ -17,9 +18,10 @@
1718
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
1819
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
1920
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
20-
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
21+
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
2122
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
2223
import java.io.File;
24+
import java.util.List;
2325
import org.junit.jupiter.api.Assumptions;
2426
import org.junit.jupiter.api.BeforeAll;
2527
import org.junit.jupiter.api.Test;
@@ -79,11 +81,7 @@ void emptyRepository() {
7981
span.hasName("DocRepository.findAll")
8082
.hasKind(SpanKind.INTERNAL)
8183
.hasNoParent()
82-
.hasAttributesSatisfyingExactly(
83-
equalTo(
84-
CodeIncubatingAttributes.CODE_NAMESPACE,
85-
DocRepository.class.getName()),
86-
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "findAll")),
84+
.hasAttributesSatisfyingExactly(assertFunctionName("findAll")),
8785
span ->
8886
span.hasName("SearchAction")
8987
.hasKind(SpanKind.CLIENT)
@@ -113,11 +111,7 @@ void crud() {
113111
span.hasName("DocRepository.index")
114112
.hasKind(SpanKind.INTERNAL)
115113
.hasNoParent()
116-
.hasAttributesSatisfyingExactly(
117-
equalTo(
118-
CodeIncubatingAttributes.CODE_NAMESPACE,
119-
DocRepository.class.getName()),
120-
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "index")),
114+
.hasAttributesSatisfyingExactly(assertFunctionName("index")),
121115
span ->
122116
span.hasName("IndexAction")
123117
.hasKind(SpanKind.CLIENT)
@@ -162,11 +156,7 @@ void crud() {
162156
span.hasName("DocRepository.findById")
163157
.hasKind(SpanKind.INTERNAL)
164158
.hasNoParent()
165-
.hasAttributesSatisfyingExactly(
166-
equalTo(
167-
CodeIncubatingAttributes.CODE_NAMESPACE,
168-
DocRepository.class.getName()),
169-
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "findById")),
159+
.hasAttributesSatisfyingExactly(assertFunctionName("findById")),
170160
span ->
171161
span.hasName("GetAction")
172162
.hasKind(SpanKind.CLIENT)
@@ -196,11 +186,7 @@ void crud() {
196186
span.hasName("DocRepository.index")
197187
.hasKind(SpanKind.INTERNAL)
198188
.hasNoParent()
199-
.hasAttributesSatisfyingExactly(
200-
equalTo(
201-
CodeIncubatingAttributes.CODE_NAMESPACE,
202-
DocRepository.class.getName()),
203-
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "index")),
189+
.hasAttributesSatisfyingExactly(assertFunctionName("index")),
204190
span ->
205191
span.hasName("IndexAction")
206192
.hasKind(SpanKind.CLIENT)
@@ -240,11 +226,7 @@ void crud() {
240226
span.hasName("DocRepository.findById")
241227
.hasKind(SpanKind.INTERNAL)
242228
.hasNoParent()
243-
.hasAttributesSatisfyingExactly(
244-
equalTo(
245-
CodeIncubatingAttributes.CODE_NAMESPACE,
246-
DocRepository.class.getName()),
247-
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "findById")),
229+
.hasAttributesSatisfyingExactly(assertFunctionName("findById")),
248230
span ->
249231
span.hasName("GetAction")
250232
.hasKind(SpanKind.CLIENT)
@@ -272,11 +254,7 @@ void crud() {
272254
span.hasName("DocRepository.deleteById")
273255
.hasKind(SpanKind.INTERNAL)
274256
.hasNoParent()
275-
.hasAttributesSatisfyingExactly(
276-
equalTo(
277-
CodeIncubatingAttributes.CODE_NAMESPACE,
278-
DocRepository.class.getName()),
279-
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "deleteById")),
257+
.hasAttributesSatisfyingExactly(assertFunctionName("deleteById")),
280258
span ->
281259
span.hasName("DeleteAction")
282260
.hasKind(SpanKind.CLIENT)
@@ -315,11 +293,7 @@ void crud() {
315293
span.hasName("DocRepository.findAll")
316294
.hasKind(SpanKind.INTERNAL)
317295
.hasNoParent()
318-
.hasAttributesSatisfyingExactly(
319-
equalTo(
320-
CodeIncubatingAttributes.CODE_NAMESPACE,
321-
DocRepository.class.getName()),
322-
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "findAll")),
296+
.hasAttributesSatisfyingExactly(assertFunctionName("findAll")),
323297
span ->
324298
span.hasName("SearchAction")
325299
.hasKind(SpanKind.CLIENT)
@@ -334,4 +308,8 @@ void crud() {
334308
equalTo(stringKey("elasticsearch.request.indices"), "test-index"),
335309
equalTo(stringKey("elasticsearch.request.search.types"), "doc"))));
336310
}
311+
312+
private static List<AttributeAssertion> assertFunctionName(String methodName) {
313+
return codeFunctionAssertions(DocRepository.class, methodName);
314+
}
337315
}

0 commit comments

Comments
 (0)