11package datadog .trace .common .sampling ;
22
33import datadog .trace .api .config .TracerConfig ;
4- import datadog .trace .api .sampling .PrioritySampling ;
54import datadog .trace .api .sampling .SamplingMechanism ;
65import datadog .trace .api .sampling .SamplingRule ;
76import 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