Skip to content

Commit 6743428

Browse files
github-actions[bot]ZStriker19brettlangdon
authored
fix(tracing): do not incorrectly match on None with ?* [backport 2.21] (#13279)
Backport ea64efc from #13100 to 2.21. Fixes #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) Co-authored-by: Zachary Groves <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 646b292 commit 6743428

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

ddtrace/_trace/sampling_rule.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,25 @@ def check_tags(self, meta, metrics):
154154
tag_match = False
155155
for tag_key in self._tag_value_matchers.keys():
156156
value = meta.get(tag_key)
157-
tag_match = self._tag_value_matchers[tag_key].match(str(value))
158-
# If the value doesn't match in meta, check the metrics
159-
if tag_match is False:
157+
# it's because we're not checking metrics first before continuing
158+
if value is None:
160159
value = metrics.get(tag_key)
160+
if value is None:
161+
continue
161162
# Floats: Matching floating point values with a non-zero decimal part is not supported.
162163
# For floating point values with a non-zero decimal part, any all * pattern always returns true.
163164
# Other patterns always return false.
164165
if isinstance(value, float):
165166
if not value.is_integer():
166-
if self._tag_value_matchers[tag_key].pattern == "*":
167+
if all(c == "*" for c in self._tag_value_matchers[tag_key].pattern):
167168
tag_match = True
169+
continue
168170
else:
169171
return False
170-
continue
171172
else:
172173
value = int(value)
173174

174-
tag_match = self._tag_value_matchers[tag_key].match(str(value))
175-
else:
176-
continue
175+
tag_match = self._tag_value_matchers[tag_key].match(str(value))
177176
# if we don't match with all specified tags for a rule, it's not a match
178177
if tag_match is False:
179178
return False
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
fix(tracer):Fixed a bug in the sampling rule matcher where the pattern ``?*`` was not being matched correctly
5+
for ``DD_TRACE_SAMPLING_RULES`` tags, due to it matching on spans with no tag matching the specified key.

tests/tracer/test_sampler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,16 @@ def sample(self, span):
932932
0,
933933
None,
934934
),
935+
( # Regression test for https://github.com/DataDog/dd-trace-py/issues/12775
936+
# We should not match None values with ?* patterns.
937+
DatadogSampler(
938+
rules=[SamplingRule(sample_rate=0, name="?*", resource="?*", service="?*", tags={"key": "?*"})],
939+
),
940+
AUTO_KEEP,
941+
SamplingMechanism.DEFAULT,
942+
0,
943+
None,
944+
),
935945
],
936946
)
937947
def test_datadog_sampler_sample_rules(sampler, sampling_priority, sampling_mechanism, rule, limit, dummy_tracer):

0 commit comments

Comments
 (0)