From fb2e18ef018aaadc7e2a526124b673cade87f0a6 Mon Sep 17 00:00:00 2001 From: Zachary Groves <32471391+ZStriker19@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:09:48 -0400 Subject: [PATCH] fix(tracing): do not incorrectly match on None with ?* (#13100) Fixes https://github.com/DataDog/dd-trace-py/issues/12775 ## 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) (cherry picked from commit ea64efc698bb158da34e6ed1aaad670485987eca) --- ddtrace/_trace/sampling_rule.py | 15 +++++++-------- ...ct_sampler_match_on_none-c6e04cc4dfdf9007.yaml | 5 +++++ tests/tracer/test_sampler.py | 10 ++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/fix_incorrect_sampler_match_on_none-c6e04cc4dfdf9007.yaml diff --git a/ddtrace/_trace/sampling_rule.py b/ddtrace/_trace/sampling_rule.py index 532a0b71f51..cc8667b6bad 100644 --- a/ddtrace/_trace/sampling_rule.py +++ b/ddtrace/_trace/sampling_rule.py @@ -154,26 +154,25 @@ def check_tags(self, meta, metrics): tag_match = False for tag_key in self._tag_value_matchers.keys(): value = meta.get(tag_key) - tag_match = self._tag_value_matchers[tag_key].match(str(value)) - # If the value doesn't match in meta, check the metrics - if tag_match is False: + # it's because we're not checking metrics first before continuing + if value is None: value = metrics.get(tag_key) + if value is None: + continue # Floats: Matching floating point values with a non-zero decimal part is not supported. # For floating point values with a non-zero decimal part, any all * pattern always returns true. # Other patterns always return false. if isinstance(value, float): if not value.is_integer(): - if self._tag_value_matchers[tag_key].pattern == "*": + if all(c == "*" for c in self._tag_value_matchers[tag_key].pattern): tag_match = True + continue else: return False - continue else: value = int(value) - tag_match = self._tag_value_matchers[tag_key].match(str(value)) - else: - continue + tag_match = self._tag_value_matchers[tag_key].match(str(value)) # if we don't match with all specified tags for a rule, it's not a match if tag_match is False: return False diff --git a/releasenotes/notes/fix_incorrect_sampler_match_on_none-c6e04cc4dfdf9007.yaml b/releasenotes/notes/fix_incorrect_sampler_match_on_none-c6e04cc4dfdf9007.yaml new file mode 100644 index 00000000000..65e5687d6e1 --- /dev/null +++ b/releasenotes/notes/fix_incorrect_sampler_match_on_none-c6e04cc4dfdf9007.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + fix(tracer):Fixed a bug in the sampling rule matcher where the pattern ``?*`` was not being matched correctly + for ``DD_TRACE_SAMPLING_RULES`` tags, due to it matching on spans with no tag matching the specified key. \ No newline at end of file diff --git a/tests/tracer/test_sampler.py b/tests/tracer/test_sampler.py index 813dc1be439..64beed7ecda 100644 --- a/tests/tracer/test_sampler.py +++ b/tests/tracer/test_sampler.py @@ -932,6 +932,16 @@ def sample(self, span): 0, None, ), + ( # Regression test for https://github.com/DataDog/dd-trace-py/issues/12775 + # We should not match None values with ?* patterns. + DatadogSampler( + rules=[SamplingRule(sample_rate=0, name="?*", resource="?*", service="?*", tags={"key": "?*"})], + ), + AUTO_KEEP, + SamplingMechanism.DEFAULT, + 0, + None, + ), ], ) def test_datadog_sampler_sample_rules(sampler, sampling_priority, sampling_mechanism, rule, limit, dummy_tracer):