Skip to content

Commit aedbe1b

Browse files
committed
Merge remote-tracking branch 'origin/dougqh/sampling-provenance' into mcculls/handle-remote-sampling-rules_MERGE
2 parents 1b78963 + 1a97821 commit aedbe1b

File tree

5 files changed

+160
-107
lines changed

5 files changed

+160
-107
lines changed

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

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

3+
import datadog.trace.api.sampling.SamplingMechanism;
34
import datadog.trace.core.CoreSpan;
45
import datadog.trace.core.util.Matcher;
56
import datadog.trace.core.util.Matchers;
@@ -10,9 +11,16 @@
1011

1112
public abstract class RateSamplingRule {
1213
private final RateSampler sampler;
14+
private final byte mechanism;
1315

14-
public RateSamplingRule(final RateSampler sampler) {
16+
public RateSamplingRule(final RateSampler sampler, byte mechanism) {
1517
this.sampler = sampler;
18+
this.mechanism = mechanism;
19+
}
20+
21+
@Deprecated
22+
public RateSamplingRule(final RateSampler sampler) {
23+
this(sampler, SamplingMechanism.LOCAL_USER_RULE);
1624
}
1725

1826
public abstract <T extends CoreSpan<T>> boolean matches(T span);
@@ -25,10 +33,13 @@ public RateSampler getSampler() {
2533
return sampler;
2634
}
2735

28-
public static class AlwaysMatchesSamplingRule extends RateSamplingRule {
36+
public final byte getMechanism() {
37+
return mechanism;
38+
}
2939

30-
public AlwaysMatchesSamplingRule(final RateSampler sampler) {
31-
super(sampler);
40+
public static class AlwaysMatchesSamplingRule extends RateSamplingRule {
41+
public AlwaysMatchesSamplingRule(final RateSampler sampler, byte samplingMechanism) {
42+
super(sampler, samplingMechanism);
3243
}
3344

3445
@Override
@@ -87,8 +98,10 @@ public TraceSamplingRule(
8798
final String operationGlob,
8899
final String resourceGlob,
89100
final Map<String, String> tags,
90-
final RateSampler sampler) {
91-
super(sampler);
101+
final RateSampler sampler,
102+
final byte samplingMechanism) {
103+
super(sampler, samplingMechanism);
104+
92105
serviceMatcher = Matchers.compileGlob(serviceGlob);
93106
operationMatcher = Matchers.compileGlob(operationGlob);
94107
resourceMatcher = Matchers.compileGlob(resourceGlob);

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

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

33
import datadog.trace.api.config.TracerConfig;
4-
import datadog.trace.api.sampling.PrioritySampling;
54
import datadog.trace.api.sampling.SamplingMechanism;
65
import datadog.trace.api.sampling.SamplingRule;
76
import datadog.trace.core.CoreSpan;
@@ -68,7 +67,8 @@ public static RuleBasedTraceSampler build(
6867
rule.getName(),
6968
rule.getResource(),
7069
rule.getTags(),
71-
new DeterministicSampler.TraceSampler(rule.getSampleRate()));
70+
new DeterministicSampler.TraceSampler(rule.getSampleRate()),
71+
samplingMechanism(rule.getProvenance()));
7272
samplingRules.add(samplingRule);
7373
}
7474
} else {
@@ -102,16 +102,31 @@ public static RuleBasedTraceSampler build(
102102
}
103103
}
104104

105+
// Per spec, defaultRate is treated as "rule". Arguably a defaultRate set via RC should be
106+
// remote rule,
107+
// but that's not currenlty part of the spec.
105108
if (defaultRate != null) {
106109
final RateSamplingRule samplingRule =
107110
new RateSamplingRule.AlwaysMatchesSamplingRule(
108-
new DeterministicSampler.TraceSampler(defaultRate));
111+
new DeterministicSampler.TraceSampler(defaultRate),
112+
SamplingMechanism.LOCAL_USER_RULE);
109113
samplingRules.add(samplingRule);
110114
}
111115

112116
return new RuleBasedTraceSampler(samplingRules, rateLimit, new RateByServiceTraceSampler());
113117
}
114118

119+
private static byte samplingMechanism(SamplingRule.Provenance provenance) {
120+
switch (provenance) {
121+
case DYNAMIC:
122+
return SamplingMechanism.REMOTE_ADAPTIVE_RULE;
123+
case CUSTOMER:
124+
return SamplingMechanism.REMOTE_USER_RULE;
125+
default:
126+
return SamplingMechanism.LOCAL_USER_RULE;
127+
}
128+
}
129+
115130
@Override
116131
public <T extends CoreSpan<T>> boolean sample(final T span) {
117132
return true;
@@ -137,21 +152,21 @@ public <T extends CoreSpan<T>> void setSamplingPriority(final T span) {
137152
PrioritySampling.USER_KEEP,
138153
SAMPLING_RULE_RATE,
139154
matchedRule.getSampler().getSampleRate(),
140-
SamplingMechanism.RULE);
155+
matchedRule.getMechanism());
141156
} else {
142157
span.setSamplingPriority(
143158
PrioritySampling.USER_DROP,
144159
SAMPLING_RULE_RATE,
145160
matchedRule.getSampler().getSampleRate(),
146-
SamplingMechanism.RULE);
161+
matchedRule.getMechanism());
147162
}
148163
span.setMetric(SAMPLING_LIMIT_RATE, rateLimit);
149164
} else {
150165
span.setSamplingPriority(
151166
PrioritySampling.USER_DROP,
152167
SAMPLING_RULE_RATE,
153168
matchedRule.getSampler().getSampleRate(),
154-
SamplingMechanism.RULE);
169+
matchedRule.getMechanism());
155170
}
156171
}
157172
}

0 commit comments

Comments
 (0)