Skip to content

Commit d7b35ed

Browse files
authored
internal: only set _dd.limit_psr when rate limit rejects
According to the original spec we only need to actually set this tag when the rate limiter rejects a span, not every time it is used. Moving this to only be set on rejections will reduce overhead of setting the tag when it isn't necessary.
1 parent 23827ed commit d7b35ed

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

ddtrace/sampler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,12 @@ def sample(self, span):
335335

336336
# Ensure all allowed traces adhere to the global rate limit
337337
allowed = self.limiter.is_allowed(span.start_ns)
338-
# Always set the sample rate metric whether it was allowed or not
339-
# DEV: Setting this allows us to properly compute metrics and debug the
340-
# various sample rates that are getting applied to this span
341-
span.set_metric(SAMPLING_LIMIT_DECISION, self.limiter.effective_rate)
342338
if not allowed:
343339
self._set_priority(span, USER_REJECT)
340+
# We only need to set the rate limit metric if the limiter is rejecting the span
341+
# DEV: Setting this allows us to properly compute metrics and debug the
342+
# various sample rates that are getting applied to this span
343+
span.set_metric(SAMPLING_LIMIT_DECISION, self.limiter.effective_rate)
344344
return False
345345

346346
# We made it by all of checks, sample this trace

tests/snapshots/tests.integration.test_integration_snapshots.test_sampling.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"runtime-id": "364c9bd490824db6960feef75bb5f197"
1111
},
1212
"metrics": {
13-
"_dd.limit_psr": 1.0,
1413
"_dd.rule_psr": 1,
1514
"_dd.top_level": 1,
1615
"_dd.tracer_kr": 1.0,
@@ -42,7 +41,6 @@
4241
"runtime-id": "364c9bd490824db6960feef75bb5f197"
4342
},
4443
"metrics": {
45-
"_dd.limit_psr": 1.0,
4644
"_dd.rule_psr": 1,
4745
"_dd.top_level": 1,
4846
"_dd.tracer_kr": 1.0,
@@ -105,7 +103,6 @@
105103
"runtime-id": "364c9bd490824db6960feef75bb5f197"
106104
},
107105
"metrics": {
108-
"_dd.limit_psr": 1.0,
109106
"_dd.rule_psr": 1.0,
110107
"_dd.top_level": 1,
111108
"_dd.tracer_kr": 1.0,
@@ -168,7 +165,6 @@
168165
"runtime-id": "364c9bd490824db6960feef75bb5f197"
169166
},
170167
"metrics": {
171-
"_dd.limit_psr": 1.0,
172168
"_dd.rule_psr": 1.0,
173169
"_dd.top_level": 1,
174170
"_dd.tracer_kr": 1.0,

tests/tracer/test_sampler.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ def sample(self, span):
751751
),
752752
USER_KEEP,
753753
1.0,
754-
1.0,
754+
None,
755755
),
756756
(
757757
DatadogSampler(
@@ -764,7 +764,7 @@ def sample(self, span):
764764
),
765765
USER_KEEP,
766766
0.5,
767-
1.0,
767+
None,
768768
),
769769
(
770770
DatadogSampler(
@@ -777,7 +777,7 @@ def sample(self, span):
777777
),
778778
USER_KEEP,
779779
0.5,
780-
1.0,
780+
None,
781781
),
782782
(
783783
DatadogSampler(
@@ -813,6 +813,15 @@ def sample(self, span):
813813
0,
814814
None,
815815
),
816+
(
817+
DatadogSampler(
818+
default_sample_rate=1.0,
819+
rate_limit=0,
820+
),
821+
AUTO_REJECT,
822+
1.0,
823+
0.0,
824+
),
816825
],
817826
)
818827
def test_datadog_sampler_sample_rules(sampler, sampling_priority, rule, limit, dummy_tracer):
@@ -843,7 +852,7 @@ def test_datadog_sampler_tracer(dummy_tracer):
843852
spans = dummy_tracer.pop()
844853
assert len(spans) == 1, "Span should have been sampled and written"
845854
assert spans[0].get_metric(SAMPLING_PRIORITY_KEY) is USER_KEEP
846-
assert_sampling_decision_tags(spans[0], rule=1.0, limit=1.0)
855+
assert_sampling_decision_tags(spans[0], rule=1.0, limit=None)
847856

848857

849858
def test_datadog_sampler_tracer_rate_limited(dummy_tracer):
@@ -888,7 +897,7 @@ def test_datadog_sampler_tracer_child(dummy_tracer):
888897
spans = dummy_tracer.pop()
889898
assert len(spans) == 2, "Trace should have been sampled and written"
890899
assert spans[0].get_metric(SAMPLING_PRIORITY_KEY) is USER_KEEP
891-
assert_sampling_decision_tags(spans[0], rule=1.0, limit=1.0)
900+
assert_sampling_decision_tags(spans[0], rule=1.0, limit=None)
892901
assert_sampling_decision_tags(spans[1], agent=None, rule=None, limit=None)
893902

894903

@@ -904,7 +913,7 @@ def test_datadog_sampler_tracer_start_span(dummy_tracer):
904913
spans = dummy_tracer.pop()
905914
assert len(spans) == 1, "Span should have been sampled and written"
906915
assert spans[0].get_metric(SAMPLING_PRIORITY_KEY) is USER_KEEP
907-
assert_sampling_decision_tags(spans[0], rule=1.0, limit=1.0)
916+
assert_sampling_decision_tags(spans[0], rule=1.0, limit=None)
908917

909918

910919
def test_datadog_sampler_update_rate_by_service_sample_rates(dummy_tracer):

0 commit comments

Comments
 (0)