Skip to content

Commit d35e0bb

Browse files
fix(sampling): always use root span for sampling decision on partial flushes (backport #14213 to 3.11) (#14227)
Ensure local root span is used to make sampling decisions when partial flushing is enabled ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --------- Co-authored-by: Vítor De Araújo <[email protected]>
1 parent 8c15d39 commit d35e0bb

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

ddtrace/_trace/processor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def process_trace(self, trace: List[Span]) -> Optional[List[Span]]:
157157

158158
# only trace sample if we haven't already sampled
159159
if root_ctx and root_ctx.sampling_priority is None:
160-
self.sampler.sample(trace[0])
160+
self.sampler.sample(trace[0]._local_root)
161161
# When stats computation is enabled in the tracer then we can
162162
# safely drop the traces.
163163
if self._compute_stats_enabled and not self.apm_opt_out:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: Fix inconsistent trace sampling during partial flush (traces >300 spans). Now correctly applies sampling rules to the root span instead of a random payload span.
5+
Since traces are sampled only once, using the wrong span could bypass sampling rules and cause the agent to apply default rate limits. Fixes regression introduced in v2.8.0.

tests/tracer/test_sampler.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,38 @@ def test_rate_limit_without_sampling_rules_warning():
549549
assert config._trace_rate_limit == 2
550550

551551

552+
@pytest.mark.subprocess(
553+
env={
554+
"DD_TRACE_PARTIAL_FLUSH_ENABLED": "true",
555+
"DD_TRACE_PARTIAL_FLUSH_MIN_SPANS": "5",
556+
"DD_TRACE_SAMPLING_RULES": '[{"sample_rate":0, "name":"root_span"}, {"sample_rate":1, "name":"child_span1"}]',
557+
},
558+
)
559+
def test_partial_flush_with_sampling_rules():
560+
"""
561+
Detects a bug where the local root span is not used to make sampling decisions when a trace is partially flushed.
562+
"""
563+
from ddtrace import tracer
564+
565+
with tracer.trace("root_span") as root_span:
566+
with tracer.trace("child_span1") as child_span1:
567+
for _ in range(4):
568+
with tracer.trace("span"):
569+
pass
570+
571+
with tracer.trace("child_span2") as child_span2:
572+
for _ in range(4):
573+
with tracer.trace("span"):
574+
pass
575+
576+
assert root_span.get_metric("_dd.rule_psr") == 0, repr(root_span)
577+
assert child_span1.get_metric("_dd.py.partial_flush") == 5, repr(child_span1)
578+
assert child_span2.get_metric("_dd.py.partial_flush") == 5, repr(child_span2)
579+
580+
for span in (root_span, child_span1, child_span2):
581+
assert span.context.sampling_priority == -1, repr(span)
582+
583+
552584
def test_datadog_sampler_init():
553585
sampler = DatadogSampler()
554586
assert sampler.rules == [], "DatadogSampler initialized with no arguments should hold no rules"

0 commit comments

Comments
 (0)