|
1 | 1 | package datadog.smoketest.trace; |
2 | 2 |
|
3 | | -import static datadog.trace.test.junit.utils.assertions.Matchers.any; |
4 | 3 | import static datadog.trace.test.junit.utils.assertions.Matchers.assertValue; |
5 | 4 | import static datadog.trace.test.junit.utils.assertions.Matchers.is; |
6 | 5 | import static datadog.trace.test.junit.utils.assertions.Matchers.isFalse; |
@@ -39,15 +38,20 @@ public final class SpanMatcher { |
39 | 38 | private Matcher<CharSequence> resourceNameMatcher; |
40 | 39 | private Matcher<String> typeMatcher; |
41 | 40 | 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; |
49 | 46 |
|
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 | + } |
51 | 55 |
|
52 | 56 | /** Creates a new span matcher; configure it with the fluent methods below. */ |
53 | 57 | public static SpanMatcher span() { |
@@ -97,32 +101,28 @@ public SpanMatcher error(boolean errored) { |
97 | 101 |
|
98 | 102 | /** Matches a root span (no parent). */ |
99 | 103 | public SpanMatcher root() { |
100 | | - this.parentId = 0L; |
| 104 | + this.parentIdMatcher = is(0L); |
101 | 105 | this.parentSpanIndex = -1; |
102 | | - this.childOfPrevious = false; |
103 | 106 | return this; |
104 | 107 | } |
105 | 108 |
|
106 | 109 | /** Matches a span whose parent is the given span id. */ |
107 | 110 | public SpanMatcher childOf(long parentSpanId) { |
108 | | - this.parentId = parentSpanId; |
| 111 | + this.parentIdMatcher = is(parentSpanId); |
109 | 112 | this.parentSpanIndex = -1; |
110 | | - this.childOfPrevious = false; |
111 | 113 | return this; |
112 | 114 | } |
113 | 115 |
|
114 | 116 | /** Matches a span whose parent is the span at {@code parentSpanIndex} in the (sorted) trace. */ |
115 | 117 | public SpanMatcher childOfIndex(int parentSpanIndex) { |
| 118 | + this.parentIdMatcher = null; |
116 | 119 | this.parentSpanIndex = parentSpanIndex; |
117 | | - this.parentId = null; |
118 | | - this.childOfPrevious = false; |
119 | 120 | return this; |
120 | 121 | } |
121 | 122 |
|
122 | 123 | /** Matches a span whose parent is the immediately preceding span in the (sorted) trace. */ |
123 | 124 | public SpanMatcher childOfPrevious() { |
124 | | - this.childOfPrevious = true; |
125 | | - this.parentId = null; |
| 125 | + this.parentIdMatcher = CHILD_OF_PREVIOUS_MATCHER; |
126 | 126 | this.parentSpanIndex = -1; |
127 | 127 | return this; |
128 | 128 | } |
@@ -151,16 +151,25 @@ public SpanMatcher metaStruct(String name, Matcher<?> matcher) { |
151 | 151 | } |
152 | 152 |
|
153 | 153 | 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 | + } |
158 | 166 | DecodedSpan span = trace.get(spanIndex); |
159 | 167 | assertValue(this.serviceMatcher, span.getService(), "Unexpected service name"); |
160 | 168 | assertValue(this.operationNameMatcher, span.getName(), "Unexpected operation name"); |
161 | 169 | assertValue(this.resourceNameMatcher, span.getResource(), "Unexpected resource name"); |
162 | 170 | assertValue(this.typeMatcher, span.getType(), "Unexpected span type"); |
163 | 171 | assertValue(this.errorMatcher, span.getError() != 0, "Unexpected error status"); |
| 172 | + assertValue(this.parentIdMatcher, span.getParentId(), "Unexpected parent id"); |
164 | 173 | assertSpanTags(span.getMeta()); |
165 | 174 | assertSpanMetrics(span.getMetrics()); |
166 | 175 | assertSpanMetaStruct(span.getMetaStruct()); |
@@ -190,17 +199,4 @@ private void assertSpanMetaStruct(Map<String, Object> metaStruct) { |
190 | 199 | (Matcher<Object>) entry.getValue(), metaStruct.get(key), "Unexpected meta_struct '" + key + "'"); |
191 | 200 | } |
192 | 201 | } |
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 | | - } |
206 | 202 | } |
0 commit comments