Skip to content

Commit 5e0f9d5

Browse files
committed
feat(test): Improve trace assert API
1 parent eb30f2b commit 5e0f9d5

4 files changed

Lines changed: 35 additions & 19 deletions

File tree

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

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

3-
import static java.util.function.Function.identity;
3+
import static java.util.function.UnaryOperator.identity;
44
import static org.junit.jupiter.api.Assertions.assertNull;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

@@ -27,8 +27,8 @@
2727
import java.util.ServiceLoader;
2828
import java.util.concurrent.TimeUnit;
2929
import java.util.concurrent.TimeoutException;
30-
import java.util.function.Function;
3130
import java.util.function.Predicate;
31+
import java.util.function.UnaryOperator;
3232
import net.bytebuddy.agent.ByteBuddyAgent;
3333
import org.junit.jupiter.api.AfterAll;
3434
import org.junit.jupiter.api.AfterEach;
@@ -148,8 +148,7 @@ protected void assertTraces(TraceMatcher... matchers) {
148148
* @param matchers The matchers to verify the trace collection, one matcher by expected trace.
149149
*/
150150
protected void assertTraces(
151-
Function<TraceAssertions.Options, TraceAssertions.Options> options,
152-
TraceMatcher... matchers) {
151+
UnaryOperator<TraceAssertions.Options> options, TraceMatcher... matchers) {
153152
int expectedTraceCount = matchers.length;
154153
try {
155154
writer.waitForTraces(expectedTraceCount);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static java.time.Duration.ofNanos;
1313
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
1414

15+
import datadog.trace.api.DDTraceId;
1516
import datadog.trace.api.TagMap;
1617
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
1718
import datadog.trace.core.DDSpan;
@@ -49,6 +50,7 @@
4950
* </ul>
5051
*/
5152
public final class SpanMatcher {
53+
private Matcher<DDTraceId> traceIdMatcher;
5254
private Matcher<Long> idMatcher;
5355
private Matcher<Long> parentIdMatcher;
5456
private Matcher<String> serviceNameMatcher;
@@ -77,6 +79,18 @@ public static SpanMatcher span() {
7779
return new SpanMatcher();
7880
}
7981

82+
/**
83+
* Checks the trace identifier matches the given value.
84+
*
85+
* @param traceId The trace identifier to match against.
86+
* @return The current {@link SpanMatcher} instance with the specified trace identifier constraint
87+
* applied.
88+
*/
89+
public SpanMatcher traceId(DDTraceId traceId) {
90+
this.traceIdMatcher = Matchers.is(traceId);
91+
return this;
92+
}
93+
8094
/**
8195
* Checks the span identifier matches the given value.
8296
*
@@ -293,6 +307,7 @@ void assertSpan(DDSpan span, DDSpan previousSpan) {
293307
this.parentIdMatcher = is(previousSpan.getSpanId());
294308
}
295309
// Assert span values
310+
assertValue(this.traceIdMatcher, span.getTraceId(), "Expected trace identifier");
296311
assertValue(this.idMatcher, span.getSpanId(), "Expected identifier");
297312
assertValue(this.parentIdMatcher, span.getParentId(), "Expected parent identifier");
298313
assertValue(this.serviceNameMatcher, span.getServiceName(), "Expected service name");

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

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

3-
import static java.util.function.Function.identity;
3+
import static java.util.function.UnaryOperator.identity;
44
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
55

66
import datadog.trace.core.DDSpan;
77
import java.util.Comparator;
88
import java.util.List;
9-
import java.util.function.Function;
9+
import java.util.function.UnaryOperator;
1010

1111
/**
12-
* This class is a helper class to verify traces structure.
12+
* This class is a helper class to verify trace structure.
1313
*
14-
* <p>To check for traces structure, use the static factory methods: {@link #assertTraces(List,
14+
* <p>To check for trace structure, use the static factory methods: {@link #assertTraces(List,
1515
* TraceMatcher...)} with the expected {@link TraceMatcher}s (one per trace), or {@link
16-
* #assertTraces(List, Function, TraceMatcher...)} to configure the checks with a {@link Options}
17-
* object.
16+
* #assertTraces(List, UnaryOperator, TraceMatcher...)} to configure the checks with a {@link
17+
* Options} object.
1818
*
1919
* <p>The following predefined configurations:
2020
*
@@ -40,15 +40,15 @@ public final class TraceAssertions {
4040
* Trace assertions options.
4141
*/
4242
/** Ignores addition traces. If there are more traces than expected, do not fail. */
43-
public static final Function<Options, Options> IGNORE_ADDITIONAL_TRACES =
43+
public static final UnaryOperator<Options> IGNORE_ADDITIONAL_TRACES =
4444
Options::ignoredAdditionalTraces;
4545

4646
/** Sorts traces by start time. */
47-
public static final Function<Options, Options> SORT_BY_START_TIME =
47+
public static final UnaryOperator<Options> SORT_BY_START_TIME =
4848
options -> options.sorter(TRACE_START_TIME_COMPARATOR);
4949

5050
/** Sorts traces by their root span identifier. */
51-
public static final Function<Options, Options> SORT_BY_ROOT_SPAN_ID =
51+
public static final UnaryOperator<Options> SORT_BY_ROOT_SPAN_ID =
5252
options -> options.sorter(TRACE_ROOT_SPAN_ID_COMPARATOR);
5353

5454
private TraceAssertions() {}
@@ -81,7 +81,7 @@ public static void assertTraces(List<List<DDSpan>> traces, TraceMatcher... match
8181
* @param matchers The matchers to verify the trace collection, one matcher by expected trace.
8282
*/
8383
public static void assertTraces(
84-
List<List<DDSpan>> traces, Function<Options, Options> options, TraceMatcher... matchers) {
84+
List<List<DDSpan>> traces, UnaryOperator<Options> options, TraceMatcher... matchers) {
8585
Options opts = options.apply(new Options());
8686
int expectedTraceCount = matchers.length;
8787
int traceCount = traces.size();

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

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

3+
import static java.util.Comparator.comparingLong;
4+
35
import datadog.trace.core.DDSpan;
46
import java.util.Comparator;
57
import java.util.List;
6-
import java.util.function.Function;
8+
import java.util.function.UnaryOperator;
79
import org.opentest4j.AssertionFailedError;
810

911
/**
1012
* This class is a helper class to verify a trace structure.
1113
*
1214
* <p>To get a {@code TraceMatcher}, use the static factory methods: {@link #trace(SpanMatcher...)}
13-
* with the expected {@link SpanMatcher}s (one per expected span), or {@link #trace(Function,
15+
* with the expected {@link SpanMatcher}s (one per expected span), or {@link #trace(UnaryOperator,
1416
* SpanMatcher...)} to configure the checks with a {@link Options} object.
1517
*
1618
* <p>{@link #SORT_BY_START_TIME} can be used as predefined configuration to sort spans by start
@@ -21,8 +23,8 @@
2123
*/
2224
public final class TraceMatcher {
2325
public static final Comparator<DDSpan> START_TIME_COMPARATOR =
24-
Comparator.comparingLong(DDSpan::getStartTime);
25-
public static Function<Options, Options> SORT_BY_START_TIME =
26+
comparingLong(DDSpan::getStartTime);
27+
public static UnaryOperator<Options> SORT_BY_START_TIME =
2628
options -> options.sorter(START_TIME_COMPARATOR);
2729

2830
private final Options options;
@@ -51,7 +53,7 @@ public static TraceMatcher trace(SpanMatcher... matchers) {
5153
* @param options The {@link TraceAssertions.Options} to configure the checks.
5254
* @param matchers The matchers to verify the trace structure.
5355
*/
54-
public static TraceMatcher trace(Function<Options, Options> options, SpanMatcher... matchers) {
56+
public static TraceMatcher trace(UnaryOperator<Options> options, SpanMatcher... matchers) {
5557
return new TraceMatcher(options.apply(new Options()), matchers);
5658
}
5759

0 commit comments

Comments
 (0)