-
Notifications
You must be signed in to change notification settings - Fork 171
Add _dd.p.ksr propagated tag for Knuth sampling rate #3701
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
Draft
bm1549
wants to merge
5
commits into
master
Choose a base branch
from
brian.marks/add-ksr-tag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+120
−0
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8637533
Add _dd.p.ksr propagated tag for Knuth sampling rate
bm1549 7821296
fix: only set _dd.p.ksr for explicit agent rates, not default mechanism
bm1549 ce49c53
fix: add _dd.p.ksr to expected output in sampling_rules.phpt
bm1549 d3d2938
fix: propagate _dd.p.ksr to inferred span and ignore in SpanChecker
bm1549 db13664
perf: skip ksr tag update when value is unchanged
bm1549 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #include <json/json.h> | ||
|
|
||
| #include "../configuration.h" | ||
| #include "../tracer_tag_propagation/tracer_tag_propagation.h" | ||
|
|
||
| #include "../limiter/limiter.h" | ||
| #include "ddshared.h" | ||
|
|
@@ -62,6 +63,24 @@ static void dd_update_decision_maker_tag(ddtrace_root_span_data *root_span, | |
| } | ||
| } | ||
|
|
||
| static void dd_update_knuth_sampling_rate_tag(ddtrace_root_span_data *root_span, double sample_rate) { | ||
| zend_array *meta = ddtrace_property_array(&root_span->property_meta); | ||
|
|
||
| char buf[32]; | ||
| snprintf(buf, sizeof(buf), "%.6g", sample_rate); | ||
|
|
||
| // Skip update if already set to the same value | ||
| zval *existing = zend_hash_str_find(meta, ZEND_STRL("_dd.p.ksr")); | ||
| if (existing && Z_TYPE_P(existing) == IS_STRING && strcmp(Z_STRVAL_P(existing), buf) == 0) { | ||
| return; | ||
| } | ||
|
|
||
| zval ksr; | ||
| ZVAL_STRING(&ksr, buf); | ||
| zend_hash_str_update(meta, ZEND_STRL("_dd.p.ksr"), &ksr); | ||
| zend_hash_str_add_empty_element(ddtrace_property_array(&root_span->property_propagated_tags), ZEND_STRL("_dd.p.ksr")); | ||
| } | ||
|
|
||
| static bool dd_check_sampling_rule(zend_array *rule, ddtrace_span_data *span) { | ||
| zval *service = &span->property_service; | ||
| zval *resource = &span->property_resource; | ||
|
|
@@ -320,6 +339,7 @@ static void dd_decide_on_sampling(ddtrace_root_span_data *span) { | |
| zend_hash_str_del(metrics, ZEND_STRL("_dd.rule_psr")); | ||
| } else { | ||
| zend_hash_str_update(metrics, ZEND_STRL("_dd.rule_psr"), &sample_rate_zv); | ||
| dd_update_knuth_sampling_rate_tag(span, sample_rate); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that the _dd.p.ksr is not deleted where the psr is deleted? |
||
| } | ||
|
|
||
| zend_hash_str_del(metrics, ZEND_STRL("_dd.agent_psr")); | ||
|
|
@@ -329,6 +349,9 @@ static void dd_decide_on_sampling(ddtrace_root_span_data *span) { | |
| priority = sampling && !limited ? PRIORITY_SAMPLING_AUTO_KEEP : PRIORITY_SAMPLING_AUTO_REJECT; | ||
|
|
||
| zend_hash_str_update(metrics, ZEND_STRL("_dd.agent_psr"), &sample_rate_zv); | ||
| if (mechanism == DD_MECHANISM_AGENT_RATE) { | ||
| dd_update_knuth_sampling_rate_tag(span, sample_rate); | ||
| } | ||
| } | ||
|
|
||
| if (limited) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/ext/priority_sampling/025-ksr-tag-rule-sampling.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --TEST-- | ||
| _dd.p.ksr propagated tag is set for rule-based sampling | ||
| --ENV-- | ||
| DD_TRACE_SAMPLING_RULES=[{"sample_rate": 0.3}] | ||
| DD_TRACE_GENERATE_ROOT_SPAN=1 | ||
| --FILE-- | ||
| <?php | ||
| $root = \DDTrace\root_span(); | ||
|
|
||
| \DDTrace\get_priority_sampling(); | ||
|
|
||
| if ($root->metrics["_dd.rule_psr"] == 0.3) { | ||
| echo "Rule OK\n"; | ||
| } else { | ||
| var_dump($root->metrics); | ||
| } | ||
|
|
||
| echo "_dd.p.ksr = ", isset($root->meta["_dd.p.ksr"]) ? $root->meta["_dd.p.ksr"] : "-", "\n"; | ||
| echo "_dd.p.dm = ", isset($root->meta["_dd.p.dm"]) ? $root->meta["_dd.p.dm"] : "-", "\n"; | ||
| ?> | ||
| --EXPECTREGEX-- | ||
| Rule OK | ||
| _dd.p.ksr = 0.3 | ||
| _dd.p.dm = (-3|-) |
23 changes: 23 additions & 0 deletions
23
tests/ext/priority_sampling/026-ksr-tag-default-sampling.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --TEST-- | ||
| _dd.p.ksr propagated tag is NOT set for default sampling (only for explicit agent rates) | ||
| --ENV-- | ||
| DD_TRACE_GENERATE_ROOT_SPAN=1 | ||
| --FILE-- | ||
| <?php | ||
| $root = \DDTrace\root_span(); | ||
|
|
||
| \DDTrace\get_priority_sampling(); | ||
|
|
||
| if ($root->metrics["_dd.agent_psr"] === 1.0) { | ||
| echo "Agent PSR OK\n"; | ||
| } else { | ||
| echo "Agent PSR missing\n"; | ||
| } | ||
|
|
||
| echo "_dd.p.ksr = ", isset($root->meta["_dd.p.ksr"]) ? $root->meta["_dd.p.ksr"] : "not set", "\n"; | ||
| echo "_dd.p.dm = {$root->meta["_dd.p.dm"]}\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| Agent PSR OK | ||
| _dd.p.ksr = not set | ||
| _dd.p.dm = -0 |
25 changes: 25 additions & 0 deletions
25
tests/ext/priority_sampling/027-ksr-tag-not-set-manual.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --TEST-- | ||
| _dd.p.ksr propagated tag is NOT set for manual sampling | ||
| --ENV-- | ||
| DD_TRACE_SAMPLE_RATE=1 | ||
| DD_TRACE_GENERATE_ROOT_SPAN=1 | ||
| --FILE-- | ||
| <?php | ||
| $root = \DDTrace\root_span(); | ||
| $root->meta["manual.keep"] = true; | ||
|
|
||
| \DDTrace\get_priority_sampling(); | ||
|
|
||
| if (!isset($root->metrics["_dd.rule_psr"])) { | ||
| echo "No rule_psr OK\n"; | ||
| } else { | ||
| echo "rule_psr unexpectedly set\n"; | ||
| } | ||
|
|
||
| echo "_dd.p.ksr = ", isset($root->meta["_dd.p.ksr"]) ? $root->meta["_dd.p.ksr"] : "-", "\n"; | ||
| echo "_dd.p.dm = {$root->meta["_dd.p.dm"]}\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| No rule_psr OK | ||
| _dd.p.ksr = - | ||
| _dd.p.dm = -4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --TEST-- | ||
| _dd.p.ksr propagated tag formats rate with up to 6 significant digits and no trailing zeros | ||
| --ENV-- | ||
| DD_TRACE_SAMPLING_RULES=[{"sample_rate": 0.7654321}] | ||
| DD_TRACE_GENERATE_ROOT_SPAN=1 | ||
| --FILE-- | ||
| <?php | ||
| $root = \DDTrace\root_span(); | ||
|
|
||
| \DDTrace\get_priority_sampling(); | ||
|
|
||
| echo "_dd.p.ksr = ", isset($root->meta["_dd.p.ksr"]) ? $root->meta["_dd.p.ksr"] : "-", "\n"; | ||
| // Verify it's a string in meta, not metrics | ||
| echo "is_string = ", is_string($root->meta["_dd.p.ksr"] ?? null) ? "true" : "false", "\n"; | ||
| ?> | ||
| --EXPECTREGEX-- | ||
| _dd.p.ksr = 0.765432 | ||
| is_string = true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant header inclusion.