Skip to content

Commit 4d8ca56

Browse files
authored
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-3
2 parents d8a92f8 + aa7c70f commit 4d8ca56

152 files changed

Lines changed: 5446 additions & 1921 deletions

File tree

Some content is hidden

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

buildSrc/src/main/kotlin/dd-trace-java.configure-tests.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ tasks.withType<Test>().configureEach {
4646
// Use a task-specific user prefs directory
4747
systemProperty("java.util.prefs.userRoot", layout.buildDirectory.dir("tmp/userPrefs/$name").get().asFile.absolutePath)
4848

49+
// Enable JUnit 5 auto-detection so ConfigInversionExtension (STRICT mode) is loaded automatically
50+
systemProperty("junit.jupiter.extensions.autodetection.enabled", "true")
51+
4952
// Split up tests that want to run forked in their own separate JVM for generated tasks
5053
if (name.startsWith("forkedTest") || name.endsWith("ForkedTest")) {
5154
setExcludes(emptyList())

communication/src/main/java/datadog/communication/http/OkHttpUtils.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.net.InetSocketAddress;
1616
import java.net.Proxy;
1717
import java.nio.ByteBuffer;
18+
import java.util.Arrays;
1819
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.Map;
@@ -27,6 +28,7 @@
2728
import okhttp3.HttpUrl;
2829
import okhttp3.MediaType;
2930
import okhttp3.OkHttpClient;
31+
import okhttp3.Protocol;
3032
import okhttp3.Request;
3133
import okhttp3.RequestBody;
3234
import okhttp3.Response;
@@ -62,7 +64,7 @@ public static OkHttpClient buildHttpClient(final HttpUrl url, final long timeout
6264
}
6365

6466
public static OkHttpClient buildHttpClient(
65-
final boolean isHttp,
67+
final boolean isPlainHttp,
6668
final String unixDomainSocketPath,
6769
final String namedPipe,
6870
final long timeoutMillis) {
@@ -71,7 +73,30 @@ public static OkHttpClient buildHttpClient(
7173
Config.get().isJdkSocketEnabled(),
7274
namedPipe,
7375
null,
74-
isHttp,
76+
isPlainHttp,
77+
false,
78+
null,
79+
null,
80+
null,
81+
null,
82+
null,
83+
null,
84+
timeoutMillis,
85+
Config.get().isAgentConfiguredUsingDefault());
86+
}
87+
88+
public static OkHttpClient buildHttp2Client(
89+
final boolean isPlainHttp,
90+
final String unixDomainSocketPath,
91+
final String namedPipe,
92+
final long timeoutMillis) {
93+
return buildHttpClient(
94+
unixDomainSocketPath,
95+
Config.get().isJdkSocketEnabled(),
96+
namedPipe,
97+
null,
98+
isPlainHttp,
99+
true,
75100
null,
76101
null,
77102
null,
@@ -99,6 +124,7 @@ public static OkHttpClient buildHttpClient(
99124
config.getAgentNamedPipe(),
100125
dispatcher,
101126
isPlainHttp(url),
127+
false,
102128
retryOnConnectionFailure,
103129
maxRunningRequests,
104130
proxyHost,
@@ -116,7 +142,8 @@ private static OkHttpClient buildHttpClient(
116142
final boolean useJdkUnixDomainSocket,
117143
final String namedPipe,
118144
final Dispatcher dispatcher,
119-
final boolean isHttp,
145+
final boolean isPlainHttp,
146+
final boolean isHttp2,
120147
final Boolean retryOnConnectionFailure,
121148
final Integer maxRunningRequests,
122149
final String proxyHost,
@@ -159,11 +186,19 @@ private static OkHttpClient buildHttpClient(
159186
log.debug("Using NamedPipe as http transport");
160187
}
161188

162-
if (isHttp) {
189+
if (isPlainHttp) {
163190
// force clear text when using http to avoid failures for JVMs without TLS
164191
builder.connectionSpecs(Collections.singletonList(ConnectionSpec.CLEARTEXT));
165192
}
166193

194+
if (isHttp2) {
195+
if (isPlainHttp) {
196+
builder.protocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE));
197+
} else {
198+
builder.protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1));
199+
}
200+
}
201+
167202
if (retryOnConnectionFailure != null) {
168203
builder.retryOnConnectionFailure(retryOnConnectionFailure);
169204
}

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/AsyncResultDecorator.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@
1111
*/
1212
public abstract class AsyncResultDecorator extends BaseDecorator {
1313

14-
private static final ClassValue<AsyncResultExtension> EXTENSION_CLASS_VALUE =
15-
new ClassValue<AsyncResultExtension>() {
16-
@Override
17-
protected AsyncResultExtension computeValue(Class<?> type) {
18-
return AsyncResultExtensions.registered().stream()
19-
.filter(extension -> extension.supports(type))
20-
.findFirst()
21-
.orElse(null);
22-
}
23-
};
24-
2514
/**
2615
* Look for asynchronous result and decorate it with span finisher. If the result is not
2716
* asynchronous, it will be return unmodified and span will be finished.
@@ -33,12 +22,9 @@ protected AsyncResultExtension computeValue(Class<?> type) {
3322
*/
3423
public Object wrapAsyncResultOrFinishSpan(
3524
final Object result, final Class<?> methodReturnType, final AgentSpan span) {
36-
AsyncResultExtension extension;
37-
if (result != null && (extension = EXTENSION_CLASS_VALUE.get(methodReturnType)) != null) {
38-
Object applied = extension.apply(result, span);
39-
if (applied != null) {
40-
return applied;
41-
}
25+
Object applied = AsyncResultExtensions.wrapAsyncResult(result, methodReturnType, span);
26+
if (applied != null) {
27+
return applied;
4228
}
4329
// If no extension was applied, immediately finish the span and return the original result
4430
span.finish();

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/java/concurrent/AsyncResultExtensions.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@ public final class AsyncResultExtensions {
1616
private static final List<AsyncResultExtension> EXTENSIONS =
1717
new CopyOnWriteArrayList<>(singletonList(new CompletableAsyncResultExtension()));
1818

19+
private static final ClassValue<AsyncResultExtension> EXTENSION_CLASS_VALUE =
20+
new ClassValue<AsyncResultExtension>() {
21+
@Override
22+
protected AsyncResultExtension computeValue(Class<?> type) {
23+
return EXTENSIONS.stream()
24+
.filter(extension -> extension.supports(type))
25+
.findFirst()
26+
.orElse(null);
27+
}
28+
};
29+
30+
/**
31+
* Wraps a supported async result so the span is finished when the async computation completes.
32+
*
33+
* @return the wrapped async result, or {@code null} if the result type is unsupported or no
34+
* wrapping is applied
35+
*/
36+
public static Object wrapAsyncResult(
37+
final Object result, final Class<?> resultType, final AgentSpan span) {
38+
AsyncResultExtension extension;
39+
if (result != null && (extension = EXTENSION_CLASS_VALUE.get(resultType)) != null) {
40+
return extension.apply(result, span);
41+
}
42+
return null;
43+
}
44+
1945
/**
2046
* Registers an extension to add supported async types.
2147
*
@@ -36,11 +62,6 @@ public static void register(AsyncResultExtension extension) {
3662
}
3763
}
3864

39-
/** Returns the list of currently registered extensions. */
40-
public static List<AsyncResultExtension> registered() {
41-
return EXTENSIONS;
42-
}
43-
4465
static final class CompletableAsyncResultExtension implements AsyncResultExtension {
4566
@Override
4667
public boolean supports(Class<?> result) {
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package datadog.trace.bootstrap.instrumentation.java.lang;
22

3+
/** This class is a helper for the java-lang-21.0 {@code VirtualThreadInstrumentation}. */
34
public final class VirtualThreadHelper {
45
public static final String VIRTUAL_THREAD_CLASS_NAME = "java.lang.VirtualThread";
56

67
/**
7-
* {@link datadog.trace.bootstrap.instrumentation.api.AgentScope} class name as string literal.
8-
* This is mandatory for {@link datadog.trace.bootstrap.ContextStore} API call.
8+
* {@link VirtualThreadState} class name as string literal. This is mandatory for {@link
9+
* datadog.trace.bootstrap.ContextStore} API call.
910
*/
10-
public static final String AGENT_SCOPE_CLASS_NAME =
11-
"datadog.trace.bootstrap.instrumentation.api.AgentScope";
11+
public static final String VIRTUAL_THREAD_STATE_CLASS_NAME =
12+
"datadog.trace.bootstrap.instrumentation.java.lang.VirtualThreadState";
1213
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package datadog.trace.bootstrap.instrumentation.java.lang;
2+
3+
import datadog.context.Context;
4+
import datadog.trace.bootstrap.instrumentation.api.AgentScope.Continuation;
5+
6+
/**
7+
* This class holds the saved context and scope continuation for a virtual thread.
8+
*
9+
* <p>Used by java-lang-21.0 {@code VirtualThreadInstrumentation} to swap the entire scope stack on
10+
* mount/unmount.
11+
*/
12+
public final class VirtualThreadState {
13+
/** The virtual thread's saved context (scope stack snapshot). */
14+
private Context context;
15+
16+
/** Prevents the enclosing context scope from completing before the virtual thread finishes. */
17+
private final Continuation continuation;
18+
19+
/** The carrier thread's saved context, set between mount and unmount. */
20+
private Context previousContext;
21+
22+
public VirtualThreadState(Context context, Continuation continuation) {
23+
this.context = context;
24+
this.continuation = continuation;
25+
}
26+
27+
/** Called on mount: swaps the virtual thread's context into the carrier thread. */
28+
public void onMount() {
29+
this.previousContext = this.context.swap();
30+
}
31+
32+
/** Called on unmount: restores the carrier thread's original context. */
33+
public void onUnmount() {
34+
if (this.previousContext != null) {
35+
this.context = this.previousContext.swap();
36+
this.previousContext = null;
37+
}
38+
}
39+
40+
/** Called on termination: releases the trace continuation. */
41+
public void onTerminate() {
42+
if (this.continuation != null) {
43+
this.continuation.cancel();
44+
}
45+
}
46+
}

dd-java-agent/agent-ci-visibility/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies {
3737
testImplementation group: 'org.skyscreamer', name: 'jsonassert', version: '1.5.1'
3838
testImplementation group: 'org.freemarker', name: 'freemarker', version: '2.3.31'
3939
testImplementation group: 'org.msgpack', name: 'jackson-dataformat-msgpack', version: '0.9.6'
40+
testImplementation libs.bundles.mockito
4041
}
4142

4243
tasks.named("shadowJar", ShadowJar) {

dd-java-agent/agent-ci-visibility/civisibility-instrumentation-test-fixtures/src/main/groovy/datadog/trace/civisibility/CiVisibilityInstrumentationTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ abstract class CiVisibilityInstrumentationTest extends InstrumentationSpecificat
128128
def metricCollector = Stub(CiVisibilityMetricCollectorImpl)
129129

130130
def sourcePathResolver = Stub(SourcePathResolver)
131-
sourcePathResolver.getSourcePath(_ as Class) >> DUMMY_SOURCE_PATH
132-
sourcePathResolver.getResourcePath(_ as String) >> {
133-
String path -> path
131+
sourcePathResolver.getSourcePaths(_ as Class) >> [DUMMY_SOURCE_PATH]
132+
sourcePathResolver.getResourcePaths(_ as String) >> {
133+
String path -> [path]
134134
}
135135

136136
def codeowners = Stub(Codeowners)

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/coverage/ConcurrentCoverageStore.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import datadog.trace.api.civisibility.coverage.CoverageProbes;
55
import datadog.trace.api.civisibility.coverage.CoverageStore;
66
import datadog.trace.api.civisibility.coverage.TestReport;
7-
import datadog.trace.civisibility.source.SourceResolutionException;
87
import java.util.Collection;
98
import java.util.Map;
109
import java.util.concurrent.ConcurrentHashMap;
@@ -37,18 +36,13 @@ private T create(Thread thread) {
3736

3837
@Override
3938
public boolean report(DDTraceId testSessionId, Long testSuiteId, long testSpanId) {
40-
try {
41-
report = report(testSessionId, testSuiteId, testSpanId, probes.values());
42-
return report != null && report.isNotEmpty();
43-
} catch (SourceResolutionException e) {
44-
return false;
45-
}
39+
report = report(testSessionId, testSuiteId, testSpanId, probes.values());
40+
return report != null && report.isNotEmpty();
4641
}
4742

4843
@Nullable
4944
protected abstract TestReport report(
50-
DDTraceId testSessionId, Long testSuiteId, long testSpanId, Collection<T> probes)
51-
throws SourceResolutionException;
45+
DDTraceId testSessionId, Long testSuiteId, long testSpanId, Collection<T> probes);
5246

5347
@Nullable
5448
@Override

0 commit comments

Comments
 (0)