Skip to content

fix(tracing): do not incorrectly match on None with ?* [backport 2.21] #13279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions ddtrace/_trace/sampling_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Found too many nested ifs within this condition (...read more)

Too many nested loops make the code hard to read and understand. Simplify your code by removing nesting levels and separate code in small units.

View in Datadog  Leave us feedback  Documentation

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions tests/tracer/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down