Skip to content

Commit 1123a3b

Browse files
authored
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-5-jersey-resteasy
2 parents 86c4da5 + 534f75e commit 1123a3b

65 files changed

Lines changed: 6296 additions & 130 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.

.github/workflows/run-system-tests.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ jobs:
7676
scenarios_groups: tracer-release
7777
excluded_scenarios: APM_TRACING_E2E_OTEL,APM_TRACING_E2E_SINGLE_SPAN,PROFILING # exclude flaky scenarios
7878
skip_empty_scenarios: true
79-
push_to_test_optimization: false # disabled to avoid pushing to Test Optimization while API key is transitioning to system-tests
79+
push_to_test_optimization: true
80+
secrets:
81+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
82+
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
8083

8184
# Ensure the main job is run to completion
8285
check:

dd-java-agent/appsec/src/main/java/com/datadog/appsec/config/AppSecConfigServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public void maybeSubscribeConfigPolling() {
422422
} else {
423423
subscribeConfigurationPoller();
424424
}
425-
} else {
425+
} else if (!tracerConfig.isAwsServerless()) {
426426
log.info("Remote config is disabled; AppSec will not be able to use it");
427427
}
428428
}

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/AbstractInstrumentationTest.java

Lines changed: 79 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import datadog.trace.api.Config;
1515
import datadog.trace.api.IdGenerationStrategy;
1616
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
17-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
17+
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.TracerAPI;
1818
import datadog.trace.common.writer.ListWriter;
1919
import datadog.trace.core.CoreTracer;
2020
import datadog.trace.core.DDSpan;
@@ -30,48 +30,56 @@
3030
import java.util.function.Function;
3131
import java.util.function.Predicate;
3232
import net.bytebuddy.agent.ByteBuddyAgent;
33+
import org.junit.jupiter.api.AfterAll;
3334
import org.junit.jupiter.api.AfterEach;
35+
import org.junit.jupiter.api.BeforeAll;
3436
import org.junit.jupiter.api.BeforeEach;
3537
import org.junit.jupiter.api.extension.ExtendWith;
3638
import org.opentest4j.AssertionFailedError;
3739

3840
/**
39-
* This class is an experimental base to run instrumentation tests using JUnit Jupiter. It is still
40-
* early development, and the overall API is expected to change to leverage its extension model. The
41-
* current implementation is inspired and kept close to it Groovy / Spock counterpart, the {@code
42-
* InstrumentationSpecification}.
41+
* Base class for instrumentation tests using JUnit Jupiter.
42+
*
43+
* <p>It is still early development, and the overall API might change to leverage its extension
44+
* model. The current implementation is inspired and kept close to its Groovy / Spock counterpart,
45+
* the {@code InstrumentationSpecification}.
46+
*
47+
* <ul>
48+
* <li>{@code @BeforeAll}: Installs the agent and creates a shared tracer
49+
* <li>{@code @BeforeEach}: Flushes and resets the writer
50+
* <li>{@code @AfterEach}: Flushes the tracer
51+
* <li>{@code @AfterAll}: Closes the tracer and removes the agent transformer
52+
* </ul>
4353
*/
4454
@ExtendWith({TestClassShadowingExtension.class, AllowContextTestingExtension.class})
4555
public abstract class AbstractInstrumentationTest {
4656
static final Instrumentation INSTRUMENTATION = ByteBuddyAgent.getInstrumentation();
4757

4858
static final long TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(20);
4959

50-
protected AgentTracer.TracerAPI tracer;
60+
protected static final InstrumentationTestConfig testConfig = new InstrumentationTestConfig();
5161

52-
protected ListWriter writer;
62+
protected static TracerAPI tracer;
63+
protected static ListWriter writer;
64+
private static ClassFileTransformer activeTransformer;
65+
private static ClassFileTransformerListener transformerListener;
5366

54-
protected ClassFileTransformer activeTransformer;
55-
protected ClassFileTransformerListener transformerLister;
56-
57-
@BeforeEach
58-
public void init() {
67+
@BeforeAll
68+
static void initAll() {
5969
// If this fails, it's likely the result of another test loading Config before it can be
6070
// injected into the bootstrap classpath.
61-
// If one test extends AgentTestRunner in a module, all tests must extend
6271
assertNull(Config.class.getClassLoader(), "Config must load on the bootstrap classpath.");
6372

64-
// Initialize test tracer
65-
this.writer = new ListWriter();
66-
// Initialize test tracer
67-
CoreTracer tracer =
73+
// Create shared test writer and tracer
74+
writer = new ListWriter();
75+
CoreTracer coreTracer =
6876
CoreTracer.builder()
69-
.writer(this.writer)
70-
.idGenerationStrategy(IdGenerationStrategy.fromName(idGenerationStrategyName()))
71-
.strictTraceWrites(useStrictTraceWrites())
77+
.writer(writer)
78+
.idGenerationStrategy(IdGenerationStrategy.fromName(testConfig.idGenerationStrategy))
79+
.strictTraceWrites(testConfig.strictTraceWrites)
7280
.build();
73-
TracerInstaller.forceInstallGlobalTracer(tracer);
74-
this.tracer = tracer;
81+
TracerInstaller.forceInstallGlobalTracer(coreTracer);
82+
tracer = coreTracer;
7583

7684
ClassInjector.enableClassInjection(INSTRUMENTATION);
7785

@@ -85,33 +93,43 @@ public void init() {
8593
.iterator()
8694
.hasNext(),
8795
"No instrumentation found");
88-
this.transformerLister = new ClassFileTransformerListener();
89-
this.activeTransformer =
96+
transformerListener = new ClassFileTransformerListener();
97+
activeTransformer =
9098
AgentInstaller.installBytebuddyAgent(
91-
INSTRUMENTATION, true, AgentInstaller.getEnabledSystems(), this.transformerLister);
92-
}
93-
94-
protected String idGenerationStrategyName() {
95-
return "SEQUENTIAL";
99+
INSTRUMENTATION, true, AgentInstaller.getEnabledSystems(), transformerListener);
96100
}
97101

98-
private boolean useStrictTraceWrites() {
99-
return true;
102+
@BeforeEach
103+
public void init() {
104+
tracer.flush();
105+
writer.start();
100106
}
101107

102108
@AfterEach
103109
public void tearDown() {
104-
this.tracer.close();
105-
this.writer.close();
106-
if (this.activeTransformer != null) {
107-
INSTRUMENTATION.removeTransformer(this.activeTransformer);
108-
this.activeTransformer = null;
109-
}
110+
tracer.flush();
111+
}
110112

111-
// All cleanups should happen before these assertions.
113+
@AfterAll
114+
static void tearDownAll() {
115+
if (tracer != null) {
116+
tracer.close();
117+
tracer = null;
118+
}
119+
if (writer != null) {
120+
writer.close();
121+
writer = null;
122+
}
123+
if (activeTransformer != null) {
124+
INSTRUMENTATION.removeTransformer(activeTransformer);
125+
activeTransformer = null;
126+
}
127+
// All cleanups should happen before this verify call.
112128
// If not, a failing assertion may prevent cleanup
113-
this.transformerLister.verify();
114-
this.transformerLister = null;
129+
if (transformerListener != null) {
130+
transformerListener.verify();
131+
transformerListener = null;
132+
}
115133
}
116134

117135
/**
@@ -134,11 +152,11 @@ protected void assertTraces(
134152
TraceMatcher... matchers) {
135153
int expectedTraceCount = matchers.length;
136154
try {
137-
this.writer.waitForTraces(expectedTraceCount);
155+
writer.waitForTraces(expectedTraceCount);
138156
} catch (InterruptedException | TimeoutException e) {
139157
throw new AssertionFailedError("Timeout while waiting for traces", e);
140158
}
141-
TraceAssertions.assertTraces(this.writer, options, matchers);
159+
TraceAssertions.assertTraces(writer, options, matchers);
142160
}
143161

144162
/**
@@ -149,7 +167,7 @@ protected void assertTraces(
149167
*/
150168
protected void blockUntilTracesMatch(Predicate<List<List<DDSpan>>> predicate) {
151169
long deadline = System.currentTimeMillis() + TIMEOUT_MILLIS;
152-
while (!predicate.test(this.writer)) {
170+
while (!predicate.test(writer)) {
153171
if (System.currentTimeMillis() > deadline) {
154172
throw new RuntimeException(new TimeoutException("Timed out waiting for traces/spans."));
155173
}
@@ -161,8 +179,8 @@ protected void blockUntilTracesMatch(Predicate<List<List<DDSpan>>> predicate) {
161179
}
162180
}
163181

164-
protected void blockUntilChildSpansFinished(final int numberOfSpans) {
165-
blockUntilChildSpansFinished(this.tracer.activeSpan(), numberOfSpans);
182+
protected void blockUntilChildSpansFinished(int numberOfSpans) {
183+
blockUntilChildSpansFinished(tracer.activeSpan(), numberOfSpans);
166184
}
167185

168186
static void blockUntilChildSpansFinished(AgentSpan span, int numberOfSpans) {
@@ -190,4 +208,20 @@ static void blockUntilChildSpansFinished(AgentSpan span, int numberOfSpans) {
190208
}
191209
}
192210
}
211+
212+
/** Configuration for {@link AbstractInstrumentationTest}. */
213+
protected static class InstrumentationTestConfig {
214+
private String idGenerationStrategy = "SEQUENTIAL";
215+
private boolean strictTraceWrites = true;
216+
217+
public InstrumentationTestConfig idGenerationStrategy(String strategy) {
218+
this.idGenerationStrategy = strategy;
219+
return this;
220+
}
221+
222+
public InstrumentationTestConfig strictTraceWrites(boolean strict) {
223+
this.strictTraceWrites = strict;
224+
return this;
225+
}
226+
}
193227
}

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/assertions/Matchers.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package datadog.trace.agent.test.assertions;
22

3+
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
4+
35
import java.util.Optional;
46
import java.util.function.Predicate;
57
import java.util.regex.Pattern;
6-
import org.opentest4j.AssertionFailedError;
78

89
/** This class is a utility class to create generic matchers. */
910
public final class Matchers {
@@ -103,12 +104,11 @@ public static <T> Matcher<T> any() {
103104
static <T> void assertValue(Matcher<T> matcher, T value, String message) {
104105
if (matcher != null && !matcher.test(value)) {
105106
Optional<T> expected = matcher.expected();
106-
if (expected.isPresent()) {
107-
throw new AssertionFailedError(
108-
message + ". " + matcher.failureReason(), expected.get(), value);
109-
} else {
110-
throw new AssertionFailedError(message + ": " + value + ". " + matcher.failureReason());
111-
}
107+
assertionFailure()
108+
.message(message + ". " + matcher.failureReason())
109+
.expected(expected.orElse(null))
110+
.actual(value)
111+
.buildAndThrow();
112112
}
113113
}
114114
}

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/assertions/SpanMatcher.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static datadog.trace.agent.test.assertions.Matchers.validates;
1111
import static datadog.trace.core.DDSpanAccessor.spanLinks;
1212
import static java.time.Duration.ofNanos;
13+
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
1314

1415
import datadog.trace.api.TagMap;
1516
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
@@ -322,7 +323,7 @@ private void assertSpanTags(TagMap tags) {
322323
if (matcher == null) {
323324
uncheckedTagNames.add(key);
324325
} else {
325-
assertValue(matcher, value, "Unexpected " + key + " tag value.");
326+
assertValue(matcher, value, "Unexpected " + key + " tag value");
326327
}
327328
});
328329
// Remove matchers that accept missing tags
@@ -344,10 +345,18 @@ private void assertSpanTags(TagMap tags) {
344345
* It might evolve into partial link collection testing, matching links using TID/SIP.
345346
*/
346347
private void assertSpanLinks(List<AgentSpanLink> links) {
348+
// Check if links should be asserted at all
349+
if (this.linkMatchers == null) {
350+
return;
351+
}
347352
int linkCount = links == null ? 0 : links.size();
348-
int expectedLinkCount = this.linkMatchers == null ? 0 : this.linkMatchers.length;
353+
int expectedLinkCount = this.linkMatchers.length;
349354
if (linkCount != expectedLinkCount) {
350-
throw new AssertionFailedError("Unexpected span link count", expectedLinkCount, linkCount);
355+
assertionFailure()
356+
.message("Unexpected span link count")
357+
.expected(expectedLinkCount)
358+
.actual(linkCount)
359+
.buildAndThrow();
351360
}
352361
for (int i = 0; i < expectedLinkCount; i++) {
353362
SpanLinkMatcher linkMatcher = this.linkMatchers[expectedLinkCount];

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/assertions/TagsMatcher.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
import static datadog.trace.agent.test.assertions.Matchers.any;
44
import static datadog.trace.agent.test.assertions.Matchers.is;
55
import static datadog.trace.agent.test.assertions.Matchers.isNonNull;
6+
import static datadog.trace.api.DDTags.BASE_SERVICE;
7+
import static datadog.trace.api.DDTags.DD_INTEGRATION;
8+
import static datadog.trace.api.DDTags.DJM_ENABLED;
9+
import static datadog.trace.api.DDTags.DSM_ENABLED;
610
import static datadog.trace.api.DDTags.ERROR_MSG;
711
import static datadog.trace.api.DDTags.ERROR_STACK;
812
import static datadog.trace.api.DDTags.ERROR_TYPE;
913
import static datadog.trace.api.DDTags.LANGUAGE_TAG_KEY;
14+
import static datadog.trace.api.DDTags.PARENT_ID;
15+
import static datadog.trace.api.DDTags.PID_TAG;
16+
import static datadog.trace.api.DDTags.PROFILING_CONTEXT_ENGINE;
17+
import static datadog.trace.api.DDTags.PROFILING_ENABLED;
1018
import static datadog.trace.api.DDTags.REQUIRED_CODE_ORIGIN_TAGS;
1119
import static datadog.trace.api.DDTags.RUNTIME_ID_TAG;
20+
import static datadog.trace.api.DDTags.SCHEMA_VERSION_TAG_KEY;
21+
import static datadog.trace.api.DDTags.SPAN_LINKS;
1222
import static datadog.trace.api.DDTags.THREAD_ID;
1323
import static datadog.trace.api.DDTags.THREAD_NAME;
24+
import static datadog.trace.api.DDTags.TRACER_HOST;
1425
import static datadog.trace.common.sampling.RateByServiceTraceSampler.SAMPLING_AGENT_RATE;
1526
import static datadog.trace.common.writer.ddagent.TraceMapper.SAMPLING_PRIORITY_KEY;
1627

17-
import datadog.trace.api.DDTags;
1828
import java.util.HashMap;
1929
import java.util.Map;
2030

@@ -34,15 +44,17 @@ public static TagsMatcher defaultTags() {
3444
tagMatchers.put(SAMPLING_AGENT_RATE, any());
3545
tagMatchers.put(SAMPLING_PRIORITY_KEY.toString(), any());
3646
tagMatchers.put("_sample_rate", any());
37-
tagMatchers.put(DDTags.PID_TAG, any());
38-
tagMatchers.put(DDTags.SCHEMA_VERSION_TAG_KEY, any());
39-
tagMatchers.put(DDTags.PROFILING_ENABLED, any());
40-
tagMatchers.put(DDTags.PROFILING_CONTEXT_ENGINE, any());
41-
tagMatchers.put(DDTags.BASE_SERVICE, any());
42-
tagMatchers.put(DDTags.DSM_ENABLED, any());
43-
tagMatchers.put(DDTags.DJM_ENABLED, any());
44-
tagMatchers.put(DDTags.PARENT_ID, any());
45-
tagMatchers.put(DDTags.SPAN_LINKS, any()); // this is checked by LinksAsserter
47+
tagMatchers.put(PID_TAG, any());
48+
tagMatchers.put(SCHEMA_VERSION_TAG_KEY, any());
49+
tagMatchers.put(PROFILING_ENABLED, any());
50+
tagMatchers.put(PROFILING_CONTEXT_ENGINE, any());
51+
tagMatchers.put(BASE_SERVICE, any());
52+
tagMatchers.put(DSM_ENABLED, any());
53+
tagMatchers.put(DJM_ENABLED, any());
54+
tagMatchers.put(PARENT_ID, any());
55+
tagMatchers.put(SPAN_LINKS, any()); // this is checked by LinksAsserter
56+
tagMatchers.put(DD_INTEGRATION, any());
57+
tagMatchers.put(TRACER_HOST, any());
4658

4759
for (String tagName : REQUIRED_CODE_ORIGIN_TAGS) {
4860
tagMatchers.put(tagName, any());

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/assertions/TraceAssertions.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package datadog.trace.agent.test.assertions;
22

33
import static java.util.function.Function.identity;
4+
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
45

56
import datadog.trace.core.DDSpan;
67
import java.util.Comparator;
78
import java.util.List;
89
import java.util.function.Function;
9-
import org.opentest4j.AssertionFailedError;
1010

1111
/**
1212
* This class is a helper class to verify traces structure.
@@ -87,11 +87,19 @@ public static void assertTraces(
8787
int traceCount = traces.size();
8888
if (opts.ignoredAdditionalTraces) {
8989
if (traceCount < expectedTraceCount) {
90-
throw new AssertionFailedError("Not enough of traces", expectedTraceCount, traceCount);
90+
assertionFailure()
91+
.message("Not enough of traces")
92+
.expected(expectedTraceCount)
93+
.actual(traceCount)
94+
.buildAndThrow();
9195
}
9296
} else {
9397
if (traceCount != expectedTraceCount) {
94-
throw new AssertionFailedError("Invalid number of traces", expectedTraceCount, traceCount);
98+
assertionFailure()
99+
.message("Invalid number of traces")
100+
.expected(expectedTraceCount)
101+
.actual(traceCount)
102+
.buildAndThrow();
95103
}
96104
}
97105
if (opts.sorter != null) {

0 commit comments

Comments
 (0)