|
1 | 1 | package datadog.trace.common.sampling |
2 | 2 |
|
3 | 3 | import datadog.trace.api.Config |
| 4 | +import datadog.trace.api.sampling.PrioritySampling |
| 5 | +import datadog.trace.common.writer.ListWriter |
| 6 | +import datadog.trace.core.CoreTracer |
| 7 | +import datadog.trace.core.DDSpan |
4 | 8 | import datadog.trace.test.util.DDSpecification |
5 | 9 |
|
6 | 10 | class SamplerTest extends DDSpecification{ |
@@ -66,4 +70,106 @@ class SamplerTest extends DDSpecification{ |
66 | 70 | then: |
67 | 71 | !(sampler instanceof AsmStandaloneSampler) |
68 | 72 | } |
| 73 | + |
| 74 | + void "test that ParentBasedAlwaysOnSampler replaces AllSampler when OTLP traces export is enabled and priority sampling is disabled"() { |
| 75 | + setup: |
| 76 | + System.setProperty("dd.trace.otel.exporter", "otlp") |
| 77 | + System.setProperty("dd.priority.sampling", "false") |
| 78 | + Config config = new Config() |
| 79 | + |
| 80 | + when: |
| 81 | + Sampler sampler = Sampler.Builder.forConfig(config, null) |
| 82 | + |
| 83 | + then: |
| 84 | + sampler instanceof ParentBasedAlwaysOnSampler |
| 85 | + } |
| 86 | + |
| 87 | + void "test that AllSampler is selected when OTLP traces export is disabled and priority sampling is disabled"() { |
| 88 | + setup: |
| 89 | + System.setProperty("dd.priority.sampling", "false") |
| 90 | + Config config = new Config() |
| 91 | + |
| 92 | + when: |
| 93 | + Sampler sampler = Sampler.Builder.forConfig(config, null) |
| 94 | + |
| 95 | + then: |
| 96 | + sampler instanceof AllSampler |
| 97 | + !(sampler instanceof ParentBasedAlwaysOnSampler) |
| 98 | + } |
| 99 | + |
| 100 | + void "test that trace sampling rules are respected when OTLP traces export is enabled"() { |
| 101 | + setup: |
| 102 | + System.setProperty("dd.trace.otel.exporter", "otlp") |
| 103 | + System.setProperty("dd.trace.sample.rate", "0.5") |
| 104 | + Config config = new Config() |
| 105 | + |
| 106 | + when: |
| 107 | + Sampler sampler = Sampler.Builder.forConfig(config, null) |
| 108 | + |
| 109 | + then: |
| 110 | + sampler instanceof RuleBasedTraceSampler |
| 111 | + !(sampler instanceof ParentBasedAlwaysOnSampler) |
| 112 | + } |
| 113 | + |
| 114 | + void "test that ParentBasedAlwaysOnSampler replaces RateByServiceTraceSampler when OTLP traces export is enabled with default priority sampling"() { |
| 115 | + setup: |
| 116 | + System.setProperty("dd.trace.otel.exporter", "otlp") |
| 117 | + Config config = new Config() |
| 118 | + |
| 119 | + when: |
| 120 | + Sampler sampler = Sampler.Builder.forConfig(config, null) |
| 121 | + |
| 122 | + then: |
| 123 | + sampler instanceof ParentBasedAlwaysOnSampler |
| 124 | + !(sampler instanceof RateByServiceTraceSampler) |
| 125 | + } |
| 126 | + |
| 127 | + void "test that ForcePrioritySampler is respected when OTLP traces export is enabled and priority sampling is forced to keep"() { |
| 128 | + setup: |
| 129 | + System.setProperty("dd.trace.otel.exporter", "otlp") |
| 130 | + System.setProperty("dd.priority.sampling.force", "keep") |
| 131 | + Config config = new Config() |
| 132 | + |
| 133 | + when: |
| 134 | + Sampler sampler = Sampler.Builder.forConfig(config, null) |
| 135 | + |
| 136 | + then: |
| 137 | + sampler instanceof ForcePrioritySampler |
| 138 | + !(sampler instanceof ParentBasedAlwaysOnSampler) |
| 139 | + } |
| 140 | + |
| 141 | + void "test that ForcePrioritySampler is respected when OTLP traces export is enabled and priority sampling is forced to drop"() { |
| 142 | + setup: |
| 143 | + System.setProperty("dd.trace.otel.exporter", "otlp") |
| 144 | + System.setProperty("dd.priority.sampling.force", "drop") |
| 145 | + Config config = new Config() |
| 146 | + |
| 147 | + when: |
| 148 | + Sampler sampler = Sampler.Builder.forConfig(config, null) |
| 149 | + |
| 150 | + then: |
| 151 | + sampler instanceof ForcePrioritySampler |
| 152 | + !(sampler instanceof ParentBasedAlwaysOnSampler) |
| 153 | + } |
| 154 | + |
| 155 | + void "test that spans built with OTLP traces export enabled and priority sampling disabled have a non-UNSET sampling priority"() { |
| 156 | + setup: |
| 157 | + System.setProperty("dd.trace.otel.exporter", "otlp") |
| 158 | + System.setProperty("dd.priority.sampling", "false") |
| 159 | + Config config = new Config() |
| 160 | + Sampler sampler = Sampler.Builder.forConfig(config, null) |
| 161 | + CoreTracer tracer = CoreTracer.builder().writer(new ListWriter()).sampler(sampler).build() |
| 162 | + |
| 163 | + when: |
| 164 | + DDSpan span = (DDSpan) tracer.buildSpan("test").start() |
| 165 | + ((PrioritySampler) sampler).setSamplingPriority(span) |
| 166 | + |
| 167 | + then: |
| 168 | + span.getSamplingPriority() != null |
| 169 | + span.getSamplingPriority() == PrioritySampling.SAMPLER_KEEP |
| 170 | + |
| 171 | + cleanup: |
| 172 | + span.finish() |
| 173 | + tracer.close() |
| 174 | + } |
69 | 175 | } |
0 commit comments