Skip to content

Commit da5e7c3

Browse files
mtoffl01devflow.devflow-routing-intake
andauthored
Use ParentBasedAlwaysOnSampler by default when OTLP traces export is enabled (#11225)
Introduce isTraceOtlpExporterEnabled branching logic for Sampler.java, plus related tests Include regression test for UNSET spans in otlp mode Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent e6cac64 commit da5e7c3

2 files changed

Lines changed: 116 additions & 1 deletion

File tree

dd-trace-core/src/main/java/datadog/trace/common/sampling/Sampler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public static Sampler forConfig(final Config config, final TraceConfig traceConf
8080
log.error("Invalid sampler configuration. Using AllSampler", e);
8181
sampler = new AllSampler();
8282
}
83-
// TODO: if OTLP trace export enabled, select ParentBasedAlwaysOnSampler here
8483
} else if (config.isPrioritySamplingEnabled()) {
8584
if (KEEP.equalsIgnoreCase(config.getPrioritySamplingForce())) {
8685
log.debug("Force Sampling Priority to: SAMPLER_KEEP.");
@@ -90,9 +89,19 @@ public static Sampler forConfig(final Config config, final TraceConfig traceConf
9089
log.debug("Force Sampling Priority to: SAMPLER_DROP.");
9190
sampler =
9291
new ForcePrioritySampler(PrioritySampling.SAMPLER_DROP, SamplingMechanism.DEFAULT);
92+
} else if (config.isTraceOtlpExporterEnabled()) {
93+
// RateByServiceTraceSampler relies on the Datadog Agent for rate updates.
94+
log.debug(
95+
"OTLP traces export enabled. Using ParentBasedAlwaysOnSampler instead of RateByServiceTraceSampler.");
96+
sampler = new ParentBasedAlwaysOnSampler();
9397
} else {
9498
sampler = new RateByServiceTraceSampler();
9599
}
100+
} else if (config.isTraceOtlpExporterEnabled()) {
101+
// AllSampler does not emit a sampling priority; OTLP export requires one.
102+
log.debug(
103+
"OTLP traces export enabled. Using ParentBasedAlwaysOnSampler instead of AllSampler.");
104+
sampler = new ParentBasedAlwaysOnSampler();
96105
} else {
97106
sampler = new AllSampler();
98107
}

dd-trace-core/src/test/groovy/datadog/trace/common/sampling/SamplerTest.groovy

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package datadog.trace.common.sampling
22

33
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
48
import datadog.trace.test.util.DDSpecification
59

610
class SamplerTest extends DDSpecification{
@@ -66,4 +70,106 @@ class SamplerTest extends DDSpecification{
6670
then:
6771
!(sampler instanceof AsmStandaloneSampler)
6872
}
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+
}
69175
}

0 commit comments

Comments
 (0)