Skip to content

Commit 65f8541

Browse files
authored
Merge branch 'master' into mcculls/enable-kotlin-coroutines-by-default
2 parents ad97f20 + 4ca8a41 commit 65f8541

7 files changed

Lines changed: 344 additions & 125 deletions

File tree

.circleci/config.continue.yml.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ instrumentation_modules: &instrumentation_modules "dd-java-agent/instrumentation
3636
debugger_modules: &debugger_modules "dd-java-agent/agent-debugger|dd-java-agent/agent-bootstrap|dd-java-agent/agent-builder|internal-api|communication|dd-trace-core"
3737
profiling_modules: &profiling_modules "dd-java-agent/agent-profiling"
3838

39-
default_system_tests_commit: &default_system_tests_commit 9049791de92dcb72b104e99a80f6f9914c1b96fb
39+
default_system_tests_commit: &default_system_tests_commit ab6cf66b9c744f4fc273907423e93598440cd211
4040

4141
parameters:
4242
nightly:

.github/workflows/analyze-changes.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
${{ runner.os }}-gradle-
4141
4242
- name: Initialize CodeQL
43-
uses: github/codeql-action/init@60168efe1c415ce0f5521ea06d5c2062adbeed1b # v3.28.17
43+
uses: github/codeql-action/init@ff0a06e83cb2de871e5a09832bc6a81e7276941f # v3.28.18
4444
with:
4545
languages: 'java'
4646
build-mode: 'manual'
@@ -57,7 +57,7 @@ jobs:
5757
--build-cache --parallel --stacktrace --no-daemon --max-workers=4
5858
5959
- name: Perform CodeQL Analysis and upload results to GitHub Security tab
60-
uses: github/codeql-action/analyze@60168efe1c415ce0f5521ea06d5c2062adbeed1b # v3.28.17
60+
uses: github/codeql-action/analyze@ff0a06e83cb2de871e5a09832bc6a81e7276941f # v3.28.18
6161

6262
trivy:
6363
name: Analyze changes with Trivy
@@ -120,7 +120,7 @@ jobs:
120120
TRIVY_JAVA_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-java-db,public.ecr.aws/aquasecurity/trivy-java-db
121121

122122
- name: Upload Trivy scan results to GitHub Security tab
123-
uses: github/codeql-action/upload-sarif@60168efe1c415ce0f5521ea06d5c2062adbeed1b # v3.28.17
123+
uses: github/codeql-action/upload-sarif@ff0a06e83cb2de871e5a09832bc6a81e7276941f # v3.28.18
124124
if: always()
125125
with:
126126
sarif_file: 'trivy-results.sarif'

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ muzzle {
1010

1111
apply from: "$rootDir/gradle/java.gradle"
1212

13-
addTestSuiteForDir('latestDepTest', 'test')
13+
addTestSuite('latestDepTest')
14+
addTestSuiteForDir('latest1xDepTest', 'test')
1415

1516
dependencies {
1617
compileOnly group: 'io.opentelemetry', name: 'opentelemetry-api', version: openTelemetryVersion
@@ -19,5 +20,7 @@ dependencies {
1920

2021
testImplementation group: 'io.opentelemetry', name: 'opentelemetry-api', version: openTelemetryVersion
2122
testImplementation group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-instrumentation-annotations', version: openTelemetryVersion
22-
latestDepTestImplementation group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-instrumentation-annotations', version: '1+'
23+
latest1xDepTestImplementation group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-instrumentation-annotations', version: '1+'
24+
latestDepTestImplementation group: 'io.opentelemetry.instrumentation', name: 'opentelemetry-instrumentation-annotations', version: '+'
25+
latestDepTestImplementation sourceSets.test.output
2326
}

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/gradle.lockfile

Lines changed: 122 additions & 117 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import annotatedsample.latest.TracedMethods
2+
import datadog.trace.bootstrap.instrumentation.api.Tags
3+
4+
import static datadog.trace.agent.test.utils.TraceUtils.basicSpan
5+
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
6+
7+
class WithSpanAnnotationLatestDepTest extends WithSpanAnnotationTest {
8+
9+
def "test WithSpan annotated method inheritContext=false"() {
10+
setup:
11+
runUnderTrace("parent") {
12+
TracedMethods.sayHelloWithInANewTrace()
13+
}
14+
expect:
15+
assertTraces(2) {
16+
trace(1) {
17+
basicSpan(it, "parent")
18+
}
19+
trace(1) {
20+
span {
21+
resourceName "TracedMethods.sayHelloWithInANewTrace"
22+
operationName "TracedMethods.sayHelloWithInANewTrace"
23+
parent()
24+
errored false
25+
tags {
26+
defaultTags()
27+
"$Tags.COMPONENT" "opentelemetry"
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package annotatedsample.latest;
2+
3+
import static datadog.trace.api.DDTags.SERVICE_NAME;
4+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
5+
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
6+
import static io.opentelemetry.api.trace.SpanKind.CONSUMER;
7+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL;
8+
import static io.opentelemetry.api.trace.SpanKind.PRODUCER;
9+
import static io.opentelemetry.api.trace.SpanKind.SERVER;
10+
import static java.util.concurrent.TimeUnit.SECONDS;
11+
12+
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
13+
import io.opentelemetry.instrumentation.annotations.WithSpan;
14+
import java.util.List;
15+
import java.util.concurrent.Callable;
16+
import java.util.concurrent.CompletableFuture;
17+
import java.util.concurrent.CompletionStage;
18+
import java.util.concurrent.CountDownLatch;
19+
20+
public class TracedMethods {
21+
@WithSpan
22+
public static String sayHello() {
23+
activeSpan().setTag(SERVICE_NAME, "custom-service-name");
24+
return "hello!";
25+
}
26+
27+
@WithSpan(value = "custom-operation-name")
28+
public static String sayHelloWithCustomOperationName() {
29+
activeSpan().setTag(SERVICE_NAME, "custom-service-name");
30+
return "hello!";
31+
}
32+
33+
@WithSpan(kind = SERVER)
34+
public static String sayHelloWithServerKind() {
35+
return "hello!";
36+
}
37+
38+
@WithSpan(kind = CLIENT)
39+
public static String sayHelloWithClientKind() {
40+
return "hello!";
41+
}
42+
43+
@WithSpan(kind = PRODUCER)
44+
public static String sayHelloWithProducerKind() {
45+
return "hello!";
46+
}
47+
48+
@WithSpan(kind = CONSUMER)
49+
public static String sayHelloWithConsumerKind() {
50+
return "hello!";
51+
}
52+
53+
@WithSpan(kind = INTERNAL)
54+
public static String sayHelloWithInternalKind() {
55+
return "hello!";
56+
}
57+
58+
@WithSpan(inheritContext = false)
59+
public static String sayHelloWithInANewTrace() {
60+
return "hello!";
61+
}
62+
63+
@WithSpan
64+
public static String sayHelloWithStringAttribute(@SpanAttribute("custom-tag") String param) {
65+
return "hello!";
66+
}
67+
68+
@WithSpan
69+
public static String sayHelloWithIntAttribute(@SpanAttribute("custom-tag") int param) {
70+
return "hello!";
71+
}
72+
73+
@WithSpan
74+
public static String sayHelloWithLongAttribute(@SpanAttribute("custom-tag") long param) {
75+
return "hello!";
76+
}
77+
78+
@WithSpan
79+
public static String sayHelloWithListAttribute(@SpanAttribute("custom-tag") List<?> param) {
80+
return "hello!";
81+
}
82+
83+
@WithSpan
84+
public static void throwException() {
85+
throw new RuntimeException("Some exception");
86+
}
87+
88+
public static String traceAnonymousInnerClass() {
89+
return new Callable<String>() {
90+
@WithSpan
91+
@Override
92+
public String call() {
93+
return "hello!";
94+
}
95+
}.call();
96+
}
97+
98+
@WithSpan
99+
public static CompletableFuture<String> traceAsyncCompletableFuture(CountDownLatch latch) {
100+
return CompletableFuture.supplyAsync(
101+
() -> {
102+
await(latch);
103+
return "hello!";
104+
});
105+
}
106+
107+
@WithSpan
108+
public static CompletableFuture<String> traceAsyncFailingCompletableFuture(
109+
CountDownLatch latch, RuntimeException exception) {
110+
return CompletableFuture.supplyAsync(
111+
() -> {
112+
await(latch);
113+
throw exception;
114+
});
115+
}
116+
117+
@WithSpan
118+
public static CompletionStage<String> traceAsyncCompletionStage(CountDownLatch latch) {
119+
return CompletableFuture.supplyAsync(
120+
() -> {
121+
await(latch);
122+
return "hello!";
123+
});
124+
}
125+
126+
@WithSpan
127+
public static CompletionStage<String> traceAsyncFailingCompletionStage(
128+
CountDownLatch latch, RuntimeException exception) {
129+
return CompletableFuture.supplyAsync(
130+
() -> {
131+
await(latch);
132+
throw exception;
133+
});
134+
}
135+
136+
@WithSpan
137+
public static String sayHelloMeasured() {
138+
return "hello!";
139+
}
140+
141+
private static void await(CountDownLatch latch) {
142+
try {
143+
if (!latch.await(5, SECONDS)) {
144+
throw new IllegalStateException("Latch still locked");
145+
}
146+
} catch (InterruptedException e) {
147+
throw new RuntimeException(e);
148+
}
149+
}
150+
}

dd-java-agent/instrumentation/opentelemetry/opentelemetry-annotations-1.20/src/main/java/datadog/trace/instrumentation/opentelemetry/annotations/WithSpanDecorator.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,38 @@
44
import static datadog.trace.api.DDSpanTypes.HTTP_SERVER;
55
import static datadog.trace.api.DDSpanTypes.MESSAGE_CONSUMER;
66
import static datadog.trace.api.DDSpanTypes.MESSAGE_PRODUCER;
7-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
87
import static java.lang.Math.min;
98

109
import datadog.trace.api.InstrumenterConfig;
1110
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
11+
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1212
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
1313
import datadog.trace.bootstrap.instrumentation.decorator.AsyncResultDecorator;
14+
import datadog.trace.util.MethodHandles;
1415
import io.opentelemetry.api.trace.SpanKind;
1516
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
1617
import io.opentelemetry.instrumentation.annotations.WithSpan;
18+
import java.lang.invoke.MethodHandle;
19+
import java.lang.invoke.MethodType;
1720
import java.lang.reflect.Method;
1821
import java.lang.reflect.Parameter;
1922

2023
public class WithSpanDecorator extends AsyncResultDecorator {
2124
public static final WithSpanDecorator DECORATE = new WithSpanDecorator();
2225
private static final String INSTRUMENTATION_NAME = "opentelemetry-annotations";
2326
private static final CharSequence OPENTELEMETRY = UTF8BytesString.create("opentelemetry");
27+
private static final MethodHandle INHERIT_CONTEXT_MH = maybeGetInheritContextHandle();
28+
29+
private static MethodHandle maybeGetInheritContextHandle() {
30+
try {
31+
return new MethodHandles(WithSpan.class.getClassLoader())
32+
.method(WithSpan.class, "inheritContext")
33+
.asType(MethodType.methodType(boolean.class, WithSpan.class));
34+
} catch (Throwable ignored) {
35+
// not available before 2.14.0
36+
}
37+
return null;
38+
}
2439

2540
@Override
2641
protected String[] instrumentationNames() {
@@ -40,18 +55,31 @@ protected CharSequence component() {
4055
public AgentSpan startMethodSpan(Method method) {
4156
CharSequence operationName = null;
4257
CharSequence spanType = null;
58+
boolean inheritContext = true;
4359

4460
WithSpan withSpanAnnotation = method.getAnnotation(WithSpan.class);
4561
if (withSpanAnnotation != null) {
4662
operationName = withSpanAnnotation.value();
4763
spanType = convertToSpanType(withSpanAnnotation.kind());
64+
if (INHERIT_CONTEXT_MH != null) {
65+
try {
66+
inheritContext = (boolean) INHERIT_CONTEXT_MH.invokeExact(withSpanAnnotation);
67+
} catch (Throwable ignored) {
68+
}
69+
}
4870
}
4971

5072
if (operationName == null || operationName.length() == 0) {
5173
operationName = DECORATE.spanNameForMethod(method);
5274
}
5375

54-
AgentSpan span = startSpan(INSTRUMENTATION_NAME, operationName);
76+
AgentTracer.SpanBuilder spanBuilder =
77+
AgentTracer.get().buildSpan(INSTRUMENTATION_NAME, operationName);
78+
79+
if (!inheritContext) {
80+
spanBuilder = spanBuilder.ignoreActiveSpan();
81+
}
82+
final AgentSpan span = spanBuilder.start();
5583
DECORATE.afterStart(span);
5684

5785
if (spanType != null) {

0 commit comments

Comments
 (0)