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):