Skip to content

Commit

Permalink
chore(sampling): simplify sampling (#12581)
Browse files Browse the repository at this point in the history
## Description

With ddtrace v3.0 many sampling components are now internal to the
library. This allows to refactor and simplify how sampling rules and
agent service based sample rates are applied. This PR also improves the
performance of creating spans by 9%.

## Changes 
- Removes all samplers from `ddtrace._trace.sampling` except for the
`RateSampler` and `DatadogSampler`.
- These are the only two samplers that are used by ddtrace components
(all other samplers just add unnecessary complexity).
- Updates the types of `ddtrace._tracer._sampler` and
`ddtrace._tracer._user_sampler` from `BaseSampler` to `DatadogSampler`.
- The tracer is only compatible with the `DatadogSampler`, the Datadog
sampler is required to enable dynamic sampling and priority sampling.
This also fixes some typing issues and removes unnecessary type checks
and try-except blocks.
- Removes `sample_rate` field from
`ddtrace._trace.sampling.DatadogSampler`
- Moving forward tracing spans must be sampled via a sampling rule,
priority sampling (agent service based sampling), a RateLimiter or use
the default sample rate (1.0 + auto_keep).
- Cleans up the sampling rules parsing logic in
`DatadogSampler._parse_rules_from_str`
- Also fixes a subtle bug where a `NameError` is raised when we failed
to parse SamplingRules (even when `DD_TESTING_RAISE` is false).
[here](https://github.com/DataDog/dd-trace-py/blob/v3.1.0/ddtrace/_trace/sampler.py#L293)
json_rules can be undefined.
- Replaces `_PRIORITY_CATEGORY` with `_MECHANISM_TO_PRIORITIES` and
`DatadogSampler._choose_priority_category_with_rule(...) with
`DatadogSampler._get_sampling_mechanism(...)`
- Previously a sampling outcome was mapped a "priority category", the
priority category was then mapped to a sampling mechanism and then the
sampling mechanism was mapped to a sampling_priority. This indirection
is a bit unnecessary. This PR maps sampling outcomes directly to
sampling mechanisms and sampling_priority (via
`ddtrace.constants._MECHANISM_TO_PRIORITIES` map)
- Renames constants in `ddtrace.internal.constants.SamplingMechanism` to
improve clarity:
   - AUTO -> AGENT_RATE_BY_SERVICE
   - TRACE_SAMPLING_RULE -> LOCAL_USER_TRACE_SAMPLING_RULE
   - REMOTE_USER_RULE  -> REMOTE_USER_TRACE_SAMPLING_RULE
   - REMOTE_DYNAMIC_RULE  -> REMOTE_DYNAMIC_TRACE_SAMPLING_RULE
- Removes `DatadogSampler._make_sampling_decision(...)`,
`DatadogSampler._default_sampler`, and
`DatadogSampler.set_sample_rate(...)`.
   - These attributes are no longer used. 


Follow up: Remove
[ddtrace.config._trace_sample_rate](#12582)

## 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: Brett Langdon <[email protected]>
  • Loading branch information
mabdinur and brettlangdon authored Mar 4, 2025
1 parent 81ce3e5 commit 1b8923a
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 437 deletions.
4 changes: 2 additions & 2 deletions ddtrace/_trace/processor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Union

from ddtrace import config
from ddtrace._trace.sampler import BaseSampler
from ddtrace._trace.sampler import DatadogSampler
from ddtrace._trace.span import Span
from ddtrace._trace.span import _get_64_highest_order_bits_as_hex
from ddtrace._trace.span import _is_top_level
Expand Down Expand Up @@ -120,7 +120,7 @@ class TraceSamplingProcessor(TraceProcessor):
def __init__(
self,
compute_stats_enabled: bool,
sampler: BaseSampler,
sampler: DatadogSampler,
single_span_rules: List[SpanSamplingRule],
apm_opt_out: bool,
):
Expand Down
Loading

0 comments on commit 1b8923a

Please sign in to comment.