|
| 1 | +package datadog.trace.common.sampling; |
| 2 | + |
| 3 | +import static datadog.trace.api.TracePropagationStyle.DATADOG; |
| 4 | +import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_DROP; |
| 5 | +import static datadog.trace.api.sampling.PrioritySampling.SAMPLER_KEEP; |
| 6 | +import static datadog.trace.api.sampling.PrioritySampling.USER_DROP; |
| 7 | +import static datadog.trace.api.sampling.PrioritySampling.USER_KEEP; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 11 | + |
| 12 | +import datadog.trace.api.DDTraceId; |
| 13 | +import datadog.trace.common.writer.ListWriter; |
| 14 | +import datadog.trace.common.writer.RemoteResponseListener; |
| 15 | +import datadog.trace.core.CoreTracer; |
| 16 | +import datadog.trace.core.DDSpan; |
| 17 | +import datadog.trace.core.propagation.ExtractedContext; |
| 18 | +import datadog.trace.core.propagation.PropagationTags; |
| 19 | +import java.util.stream.Stream; |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.Arguments; |
| 24 | +import org.junit.jupiter.params.provider.MethodSource; |
| 25 | + |
| 26 | +class ParentBasedAlwaysOnSamplerTest { |
| 27 | + |
| 28 | + private final ListWriter writer = new ListWriter(); |
| 29 | + private CoreTracer tracer; |
| 30 | + |
| 31 | + @AfterEach |
| 32 | + void tearDown() { |
| 33 | + if (tracer != null) { |
| 34 | + tracer.close(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private CoreTracer buildTracer(ParentBasedAlwaysOnSampler sampler) { |
| 39 | + tracer = CoreTracer.builder().writer(writer).sampler(sampler).build(); |
| 40 | + return tracer; |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void alwaysSamplesSpans() { |
| 45 | + ParentBasedAlwaysOnSampler sampler = new ParentBasedAlwaysOnSampler(); |
| 46 | + CoreTracer tracer = buildTracer(sampler); |
| 47 | + |
| 48 | + DDSpan span = (DDSpan) tracer.buildSpan("test").start(); |
| 49 | + try { |
| 50 | + assertTrue(sampler.sample(span)); |
| 51 | + } finally { |
| 52 | + span.finish(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void setsSamplingPriorityToSamplerKeep() { |
| 58 | + ParentBasedAlwaysOnSampler sampler = new ParentBasedAlwaysOnSampler(); |
| 59 | + CoreTracer tracer = buildTracer(sampler); |
| 60 | + |
| 61 | + DDSpan span = (DDSpan) tracer.buildSpan("test").start(); |
| 62 | + try { |
| 63 | + sampler.setSamplingPriority(span); |
| 64 | + assertEquals(SAMPLER_KEEP, span.getSamplingPriority()); |
| 65 | + } finally { |
| 66 | + span.finish(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void childSpanInheritsSamplingPriorityFromLocalParent() { |
| 72 | + ParentBasedAlwaysOnSampler sampler = new ParentBasedAlwaysOnSampler(); |
| 73 | + CoreTracer tracer = buildTracer(sampler); |
| 74 | + |
| 75 | + DDSpan rootSpan = (DDSpan) tracer.buildSpan("root").start(); |
| 76 | + sampler.setSamplingPriority(rootSpan); |
| 77 | + DDSpan childSpan = (DDSpan) tracer.buildSpan("child").asChildOf(rootSpan.context()).start(); |
| 78 | + try { |
| 79 | + assertEquals(SAMPLER_KEEP, rootSpan.getSamplingPriority()); |
| 80 | + assertEquals(SAMPLER_KEEP, childSpan.getSamplingPriority()); |
| 81 | + } finally { |
| 82 | + childSpan.finish(); |
| 83 | + rootSpan.finish(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + static Stream<Arguments> childSpanInheritsSamplingDecisionFromRemoteParentArguments() { |
| 88 | + return Stream.of( |
| 89 | + Arguments.arguments("sampler keep", SAMPLER_KEEP), |
| 90 | + Arguments.arguments("sampler drop", SAMPLER_DROP), |
| 91 | + Arguments.arguments("user keep", USER_KEEP), |
| 92 | + Arguments.arguments("user drop", USER_DROP)); |
| 93 | + } |
| 94 | + |
| 95 | + @ParameterizedTest(name = "child span inherits sampling decision from remote parent: {0}") |
| 96 | + @MethodSource("childSpanInheritsSamplingDecisionFromRemoteParentArguments") |
| 97 | + void childSpanInheritsSamplingDecisionFromRemoteParent(String scenario, int parentPriority) { |
| 98 | + ParentBasedAlwaysOnSampler sampler = new ParentBasedAlwaysOnSampler(); |
| 99 | + CoreTracer tracer = buildTracer(sampler); |
| 100 | + |
| 101 | + ExtractedContext extractedContext = |
| 102 | + new ExtractedContext( |
| 103 | + DDTraceId.ONE, 2, parentPriority, null, PropagationTags.factory().empty(), DATADOG); |
| 104 | + |
| 105 | + DDSpan span = (DDSpan) tracer.buildSpan("child").asChildOf(extractedContext).start(); |
| 106 | + try { |
| 107 | + assertEquals(parentPriority, span.getSamplingPriority()); |
| 108 | + } finally { |
| 109 | + span.finish(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void isNotARemoteResponseListener() { |
| 115 | + ParentBasedAlwaysOnSampler sampler = new ParentBasedAlwaysOnSampler(); |
| 116 | + assertFalse(sampler instanceof RemoteResponseListener); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void implementsPrioritySampler() { |
| 121 | + ParentBasedAlwaysOnSampler sampler = new ParentBasedAlwaysOnSampler(); |
| 122 | + assertTrue(sampler instanceof PrioritySampler); |
| 123 | + } |
| 124 | +} |
0 commit comments