Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(sampling): simplify sampling (#12581)
## 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