Skip to content

Commit 00d27ef

Browse files
committed
WIP
1 parent 9f05195 commit 00d27ef

1 file changed

Lines changed: 30 additions & 34 deletions

File tree

dd-smoke-tests/src/main/java/datadog/smoketest/trace/SpanMatcher.java

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package datadog.smoketest.trace;
22

3-
import static datadog.trace.test.junit.utils.assertions.Matchers.any;
43
import static datadog.trace.test.junit.utils.assertions.Matchers.assertValue;
54
import static datadog.trace.test.junit.utils.assertions.Matchers.is;
65
import static datadog.trace.test.junit.utils.assertions.Matchers.isFalse;
@@ -39,15 +38,20 @@ public final class SpanMatcher {
3938
private Matcher<CharSequence> resourceNameMatcher;
4039
private Matcher<String> typeMatcher;
4140
private Matcher<Boolean> errorMatcher;
42-
private Long parentId; // explicit expected parent id (childOf/root); null => not checked by id
43-
private int parentSpanIndex = -1; // >= 0 => parent is the span at this index in the sorted trace
44-
private boolean childOfPrevious; // parent is the preceding span in the sorted trace
45-
private final Map<String, Matcher<String>> metaMatchers = new HashMap<>();
46-
private final Map<String, Matcher<Number>> metricMatchers = new HashMap<>();
47-
// meta_struct values are nested (map/list/scalar), so matched as Object.
48-
private final Map<String, Matcher<?>> metaStructMatchers = new HashMap<>();
41+
private Matcher<Long> parentIdMatcher;
42+
private int parentSpanIndex;
43+
private final Map<String, Matcher<String>> metaMatchers;
44+
private final Map<String, Matcher<Number>> metricMatchers;
45+
private final Map<String, Matcher<?>> metaStructMatchers;
4946

50-
private SpanMatcher() {}
47+
private static final Matcher<Long> CHILD_OF_PREVIOUS_MATCHER = is(0L);
48+
49+
private SpanMatcher() {
50+
this.parentSpanIndex = -1;
51+
this.metaMatchers = new HashMap<>();
52+
this.metricMatchers = new HashMap<>();
53+
this.metaStructMatchers = new HashMap<>();
54+
}
5155

5256
/** Creates a new span matcher; configure it with the fluent methods below. */
5357
public static SpanMatcher span() {
@@ -97,32 +101,28 @@ public SpanMatcher error(boolean errored) {
97101

98102
/** Matches a root span (no parent). */
99103
public SpanMatcher root() {
100-
this.parentId = 0L;
104+
this.parentIdMatcher = is(0L);
101105
this.parentSpanIndex = -1;
102-
this.childOfPrevious = false;
103106
return this;
104107
}
105108

106109
/** Matches a span whose parent is the given span id. */
107110
public SpanMatcher childOf(long parentSpanId) {
108-
this.parentId = parentSpanId;
111+
this.parentIdMatcher = is(parentSpanId);
109112
this.parentSpanIndex = -1;
110-
this.childOfPrevious = false;
111113
return this;
112114
}
113115

114116
/** Matches a span whose parent is the span at {@code parentSpanIndex} in the (sorted) trace. */
115117
public SpanMatcher childOfIndex(int parentSpanIndex) {
118+
this.parentIdMatcher = null;
116119
this.parentSpanIndex = parentSpanIndex;
117-
this.parentId = null;
118-
this.childOfPrevious = false;
119120
return this;
120121
}
121122

122123
/** Matches a span whose parent is the immediately preceding span in the (sorted) trace. */
123124
public SpanMatcher childOfPrevious() {
124-
this.childOfPrevious = true;
125-
this.parentId = null;
125+
this.parentIdMatcher = CHILD_OF_PREVIOUS_MATCHER;
126126
this.parentSpanIndex = -1;
127127
return this;
128128
}
@@ -151,16 +151,25 @@ public SpanMatcher metaStruct(String name, Matcher<?> matcher) {
151151
}
152152

153153
void assertSpan(List<DecodedSpan> trace, int spanIndex) {
154-
assertValue(
155-
parentIdMatcher(trace, spanIndex),
156-
trace.get(spanIndex).getParentId(),
157-
"Unexpected parent id");
154+
// Apply parent span index
155+
if (this.parentSpanIndex >= 0) {
156+
this.parentIdMatcher = is(trace.get(this.parentSpanIndex).getSpanId());
157+
}
158+
// Apply parent id matcher from the previous span
159+
else if (this.parentIdMatcher == CHILD_OF_PREVIOUS_MATCHER) {
160+
if (spanIndex == 0) {
161+
throw new IllegalStateException("Cannot use childOfPrevious() matcher on the first span");
162+
}
163+
DecodedSpan previousSpan = trace.get(spanIndex - 1);
164+
this.parentIdMatcher = is(previousSpan.getSpanId());
165+
}
158166
DecodedSpan span = trace.get(spanIndex);
159167
assertValue(this.serviceMatcher, span.getService(), "Unexpected service name");
160168
assertValue(this.operationNameMatcher, span.getName(), "Unexpected operation name");
161169
assertValue(this.resourceNameMatcher, span.getResource(), "Unexpected resource name");
162170
assertValue(this.typeMatcher, span.getType(), "Unexpected span type");
163171
assertValue(this.errorMatcher, span.getError() != 0, "Unexpected error status");
172+
assertValue(this.parentIdMatcher, span.getParentId(), "Unexpected parent id");
164173
assertSpanTags(span.getMeta());
165174
assertSpanMetrics(span.getMetrics());
166175
assertSpanMetaStruct(span.getMetaStruct());
@@ -190,17 +199,4 @@ private void assertSpanMetaStruct(Map<String, Object> metaStruct) {
190199
(Matcher<Object>) entry.getValue(), metaStruct.get(key), "Unexpected meta_struct '" + key + "'");
191200
}
192201
}
193-
194-
private Matcher<Long> parentIdMatcher(List<DecodedSpan> trace, int spanIndex) {
195-
if (this.parentSpanIndex >= 0) {
196-
return is(trace.get(parentSpanIndex).getSpanId());
197-
}
198-
if (this.childOfPrevious) {
199-
if (spanIndex == 0) {
200-
throw new IllegalStateException("childOfPrevious() cannot be used on the first span");
201-
}
202-
return is(trace.get(spanIndex - 1).getSpanId());
203-
}
204-
return this.parentId == null ? any() : is(this.parentId);
205-
}
206202
}

0 commit comments

Comments
 (0)