@@ -138,15 +138,24 @@ def set_sample_rate(
138
138
# type: (...) -> None
139
139
self ._by_service_samplers [self ._key (service , env )] = RateSampler (sample_rate )
140
140
141
+ def _set_priority (self , span , priority ):
142
+ # type: (Span, int) -> None
143
+ span .context .sampling_priority = priority
144
+ span .sampled = priority > 0 # Positive priorities mean it was kept
145
+
141
146
def sample (self , span ):
142
147
# type: (Span) -> bool
143
148
tags = span .tracer .tags if span .tracer else {}
144
149
env = tags [ENV_KEY ] if ENV_KEY in tags else None
145
150
key = self ._key (span .service , env )
146
151
147
152
sampler = self ._by_service_samplers .get (key , self ._by_service_samplers [self ._default_key ])
153
+ sampled = sampler .sample (span )
154
+ priority = AUTO_KEEP if sampled else AUTO_REJECT
155
+
148
156
span .set_metric (SAMPLING_AGENT_DECISION , sampler .sample_rate )
149
- return sampler .sample (span )
157
+ self ._set_priority (span , priority )
158
+ return sampled
150
159
151
160
def update_rate_by_service_sample_rates (self , rate_by_service ):
152
161
# type: (Dict[str, float]) -> None
@@ -157,7 +166,7 @@ def update_rate_by_service_sample_rates(self, rate_by_service):
157
166
self ._by_service_samplers = new_by_service_samplers
158
167
159
168
160
- class DatadogSampler (BasePrioritySampler ):
169
+ class DatadogSampler (RateByServiceSampler ):
161
170
"""
162
171
Default sampler used by Tracer for determining if a trace should be kept or dropped.
163
172
@@ -188,7 +197,7 @@ class DatadogSampler(BasePrioritySampler):
188
197
provided. It is not used when the agent supplied sample rates are used.
189
198
"""
190
199
191
- __slots__ = ("_agent_sampler" , " limiter" , "rules" )
200
+ __slots__ = ("limiter" , "rules" )
192
201
193
202
NO_RATE_LIMIT = - 1
194
203
DEFAULT_RATE_LIMIT = 100
@@ -212,6 +221,9 @@ def __init__(
212
221
applied to them, (default: ``100``)
213
222
:type rate_limit: :obj:`int`
214
223
"""
224
+ # Use default sample rate of 1.0
225
+ super (DatadogSampler , self ).__init__ ()
226
+
215
227
if default_sample_rate is None :
216
228
sample_rate = get_env ("trace" , "sample_rate" )
217
229
if sample_rate is not None :
@@ -238,9 +250,6 @@ def __init__(
238
250
if default_sample_rate is not None :
239
251
self .rules .append (SamplingRule (sample_rate = default_sample_rate ))
240
252
241
- # If no rules are configured (or match), fallback to agent based sampling
242
- self ._agent_sampler = RateByServiceSampler ()
243
-
244
253
# Configure rate limiter
245
254
self .limiter = RateLimiter (rate_limit )
246
255
@@ -250,11 +259,12 @@ def __init__(
250
259
def default_sampler (self ):
251
260
if self .rules :
252
261
return self .rules [- 1 ]
253
- return self . _agent_sampler
262
+ return self
254
263
255
264
def __str__ (self ):
256
- return "{}(agent_sampler={!r}, limiter={!r}, rules={!r})" .format (
257
- self .__class__ .__name__ , self ._agent_sampler , self .limiter , self .rules
265
+ rates = {key : sampler .sample_rate for key , sampler in self ._by_service_samplers .items ()}
266
+ return "{}(agent_rates={!r}, limiter={!r}, rules={!r})" .format (
267
+ self .__class__ .__name__ , rates , self .limiter , self .rules
258
268
)
259
269
260
270
__repr__ = __str__
@@ -280,16 +290,6 @@ def _parse_rules_from_env_variable(self, rules):
280
290
sampling_rules .append (sampling_rule )
281
291
return sampling_rules
282
292
283
- def update_rate_by_service_sample_rates (self , sample_rates ):
284
- # type: (Dict[str, float]) -> None
285
- # Pass through the call to our RateByServiceSampler
286
- self ._agent_sampler .update_rate_by_service_sample_rates (sample_rates )
287
-
288
- def _set_priority (self , span , priority ):
289
- # type: (Span, int) -> None
290
- span .context .sampling_priority = priority
291
- span .sampled = priority > 0 # Positive priorities mean it was kept
292
-
293
293
def sample (self , span ):
294
294
# type: (Span) -> bool
295
295
"""
@@ -313,12 +313,7 @@ def sample(self, span):
313
313
break
314
314
else :
315
315
# No rules matches so use agent based sampling
316
- if self ._agent_sampler .sample (span ):
317
- self ._set_priority (span , AUTO_KEEP )
318
- return True
319
- else :
320
- self ._set_priority (span , AUTO_REJECT )
321
- return False
316
+ return super (DatadogSampler , self ).sample (span )
322
317
323
318
# DEV: This should never happen, but since the type is Optional we have to check
324
319
if not matching_rule :
0 commit comments