|
8 | 8 | import static org.junit.jupiter.api.Assertions.assertTrue; |
9 | 9 |
|
10 | 10 | import datadog.trace.api.Config; |
| 11 | +import datadog.trace.api.interceptor.MutableSpan; |
| 12 | +import datadog.trace.api.interceptor.TraceInterceptor; |
11 | 13 | import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
12 | 14 | import datadog.trace.core.CoreTracer.CoreSpanBuilder; |
13 | 15 | import datadog.trace.core.CoreTracer.ReusableSingleSpanBuilder; |
14 | 16 | import datadog.trace.core.CoreTracer.ReusableSingleSpanBuilderThreadLocalCache; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
| 20 | +import java.util.SortedSet; |
| 21 | +import java.util.TreeSet; |
15 | 22 | import org.junit.jupiter.api.Test; |
16 | 23 |
|
17 | 24 | // named CoreTracerTest2 to avoid collision with Groovy which appears to have messed up test |
@@ -179,4 +186,114 @@ public void start_not_inUse() { |
179 | 186 | ReusableSingleSpanBuilder builder = new ReusableSingleSpanBuilder(TRACER); |
180 | 187 | assertThrows(AssertionError.class, () -> builder.start()); |
181 | 188 | } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void noInterceptorsTest() { |
| 192 | + // No interceptors should return the original list to avoid allocation |
| 193 | + DDSpan span = (DDSpan) TRACER.startSpan("foo", "foo"); |
| 194 | + |
| 195 | + SpanList list = SpanList.of(span); |
| 196 | + List<DDSpan> interceptedList = |
| 197 | + CoreTracer.interceptCompleteTrace(Collections.emptySortedSet(), list); |
| 198 | + assertSame(list, interceptedList); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void interceptNoInterceptors() { |
| 203 | + // No interceptors should return the original list to avoid allocation |
| 204 | + DDSpan span = (DDSpan) TRACER.startSpan("foo", "foo"); |
| 205 | + |
| 206 | + SpanList list = SpanList.of(span); |
| 207 | + List<DDSpan> interceptedList = |
| 208 | + CoreTracer.interceptCompleteTrace(Collections.emptySortedSet(), list); |
| 209 | + assertSame(list, interceptedList); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void interceptEmptyList() { |
| 214 | + DDSpan span = (DDSpan) TRACER.startSpan("foo", "foo"); |
| 215 | + SortedSet<TraceInterceptor> interceptors = interceptors((list) -> SpanList.of(span)); |
| 216 | + |
| 217 | + SpanList list = new SpanList(0); // not using EMPTY deliberately |
| 218 | + List<DDSpan> interceptedList = CoreTracer.interceptCompleteTrace(interceptors, list); |
| 219 | + assertTrue(interceptedList.isEmpty()); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void interceptUnchanged() { |
| 224 | + SortedSet<TraceInterceptor> interceptors = interceptors((list) -> list, (list) -> list); |
| 225 | + |
| 226 | + DDSpan span = (DDSpan) TRACER.startSpan("foo", "foo"); |
| 227 | + SpanList list = SpanList.of(span); |
| 228 | + List<DDSpan> interceptedList = CoreTracer.interceptCompleteTrace(interceptors, list); |
| 229 | + assertSame(list, interceptedList); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + public void interceptNewList() { |
| 234 | + DDSpan substituteSpan = (DDSpan) TRACER.startSpan("sub", "sub"); |
| 235 | + SpanList substituteList = SpanList.of(substituteSpan); |
| 236 | + SortedSet<TraceInterceptor> interceptors = |
| 237 | + interceptors((list) -> list, (list) -> substituteList); |
| 238 | + |
| 239 | + DDSpan span = (DDSpan) TRACER.startSpan("foo", "foo"); |
| 240 | + SpanList list = SpanList.of(span); |
| 241 | + List<DDSpan> interceptedList = CoreTracer.interceptCompleteTrace(interceptors, list); |
| 242 | + assertEquals(1, interceptedList.size()); |
| 243 | + assertEquals("sub", interceptedList.get(0).getOperationName()); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void interceptAlteredList() { |
| 248 | + // This is an unlikely case and arguably not something we need to support |
| 249 | + |
| 250 | + DDSpan substituteSpan = (DDSpan) TRACER.startSpan("sub", "sub"); |
| 251 | + SortedSet<TraceInterceptor> interceptors = |
| 252 | + interceptors( |
| 253 | + (list) -> list, |
| 254 | + (list) -> { |
| 255 | + List erasedList = (List) list; |
| 256 | + erasedList.clear(); |
| 257 | + erasedList.add(substituteSpan); |
| 258 | + return erasedList; |
| 259 | + }); |
| 260 | + |
| 261 | + DDSpan span = (DDSpan) TRACER.startSpan("foo", "foo"); |
| 262 | + SpanList list = SpanList.of(span); |
| 263 | + List<DDSpan> interceptedList = CoreTracer.interceptCompleteTrace(interceptors, list); |
| 264 | + assertNotSame(interceptedList, list); |
| 265 | + assertEquals(1, interceptedList.size()); |
| 266 | + assertEquals("sub", interceptedList.get(0).getOperationName()); |
| 267 | + } |
| 268 | + |
| 269 | + static final SortedSet<TraceInterceptor> interceptors(TestInterceptor... interceptors) { |
| 270 | + TreeSet<TraceInterceptor> interceptorSet = |
| 271 | + new TreeSet<>((lhs, rhs) -> Integer.compare(lhs.priority(), rhs.priority())); |
| 272 | + for (int i = 0; i < interceptors.length; ++i) { |
| 273 | + int priority = i; |
| 274 | + TestInterceptor interceptor = interceptors[i]; |
| 275 | + |
| 276 | + interceptorSet.add( |
| 277 | + new TraceInterceptor() { |
| 278 | + @Override |
| 279 | + public int priority() { |
| 280 | + return priority; |
| 281 | + } |
| 282 | + |
| 283 | + @Override |
| 284 | + public Collection<? extends MutableSpan> onTraceComplete( |
| 285 | + Collection<? extends MutableSpan> trace) { |
| 286 | + return interceptor.onTraceComplete(trace); |
| 287 | + } |
| 288 | + }); |
| 289 | + } |
| 290 | + return interceptorSet; |
| 291 | + } |
| 292 | + |
| 293 | + // Matches TraceInterceptor but priority is implied in interceptors |
| 294 | + // Only having onTraceComplete allows this to @FunctionalInterface and a little nicer for a test |
| 295 | + @FunctionalInterface |
| 296 | + interface TestInterceptor { |
| 297 | + Collection<? extends MutableSpan> onTraceComplete(Collection<? extends MutableSpan> trace); |
| 298 | + } |
182 | 299 | } |
0 commit comments