-
Notifications
You must be signed in to change notification settings - Fork 103
feat(server): parametrize dsc transaction #3141
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e91e05c
wip
TBS1996 2b09097
wip
TBS1996 cf01241
wip
TBS1996 25ce461
wip
TBS1996 2867b66
nits
TBS1996 11add50
nits
TBS1996 f5e6676
Merge branch 'master' into tor/traceparam
TBS1996 8cc42c3
wip
TBS1996 0ecdd0d
rm swap
TBS1996 5d36bf4
Apply suggestions from code review
TBS1996 5b6e54e
use root state
TBS1996 99b167c
respect annotated removal
TBS1996 22e5ed9
add parametrize unit test
TBS1996 cef6fc3
cl
TBS1996 b4a98f1
add integration test
TBS1996 9cc49f1
tst
TBS1996 dc2aa51
af
TBS1996 6752463
af
TBS1996 85d22a0
wip
TBS1996 b6badd3
wip
TBS1996 5afe6b8
nits
TBS1996 deaafcd
fix
TBS1996 e023acb
Merge branch 'master' into tor/traceparam
TBS1996 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
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 |
---|---|---|
|
@@ -19,6 +19,59 @@ pub struct TransactionNameConfig<'r> { | |
pub rules: &'r [TransactionNameRule], | ||
} | ||
|
||
/// Apply parametrization to transaction. | ||
pub fn normalize_transaction_name( | ||
transaction: &mut Annotated<String>, | ||
rules: &[TransactionNameRule], | ||
) { | ||
// Normalize transaction names for URLs and Sanitized transaction sources. | ||
// This in addition to renaming rules can catch some high cardinality parts. | ||
scrub_identifiers(transaction); | ||
|
||
// Apply rules discovered by the transaction clusterer in sentry. | ||
if !rules.is_empty() { | ||
apply_transaction_rename_rules(transaction, rules); | ||
} | ||
} | ||
|
||
/// Applies the rule if any found to the transaction name. | ||
/// | ||
/// It find the first rule matching the criteria: | ||
/// - source matchining the one provided in the rule sorce | ||
/// - rule hasn't epired yet | ||
/// - glob pattern matches the transaction name | ||
/// | ||
/// Note: we add `/` at the end of the transaction name if there isn't one, to make sure that | ||
/// patterns like `/<something>/*/**` where we have `**` at the end are a match. | ||
pub fn apply_transaction_rename_rules( | ||
transaction: &mut Annotated<String>, | ||
rules: &[TransactionNameRule], | ||
) { | ||
let _ = processor::apply(transaction, |transaction, meta| { | ||
let result = rules.iter().find_map(|rule| { | ||
rule.match_and_apply(Cow::Borrowed(transaction)) | ||
.map(|applied_result| (rule.pattern.compiled().pattern(), applied_result)) | ||
}); | ||
|
||
if let Some((rule, result)) = result { | ||
if *transaction != result { | ||
// If another rule was applied before, we don't want to | ||
// rename the transaction name to keep the original one. | ||
// We do want to continue adding remarks though, in | ||
// order to keep track of all rules applied. | ||
if meta.original_value().is_none() { | ||
meta.set_original_value(Some(transaction.clone())); | ||
} | ||
// add also the rule which was applied to the transaction name | ||
meta.add_remark(Remark::new(RemarkType::Substituted, rule)); | ||
*transaction = result; | ||
} | ||
} | ||
|
||
Ok(()) | ||
}); | ||
} | ||
|
||
/// Rejects transactions based on required fields. | ||
#[derive(Debug, Default)] | ||
pub struct TransactionsProcessor<'r> { | ||
|
@@ -31,41 +84,6 @@ impl<'r> TransactionsProcessor<'r> { | |
Self { name_config } | ||
} | ||
|
||
/// Applies the rule if any found to the transaction name. | ||
/// | ||
/// It find the first rule matching the criteria: | ||
/// - source matchining the one provided in the rule sorce | ||
/// - rule hasn't epired yet | ||
/// - glob pattern matches the transaction name | ||
/// | ||
/// Note: we add `/` at the end of the transaction name if there isn't one, to make sure that | ||
/// patterns like `/<something>/*/**` where we have `**` at the end are a match. | ||
pub fn apply_transaction_rename_rule(&self, transaction: &mut Annotated<String>) { | ||
let _ = processor::apply(transaction, |transaction, meta| { | ||
let result = self.name_config.rules.iter().find_map(|rule| { | ||
rule.match_and_apply(Cow::Borrowed(transaction)) | ||
.map(|applied_result| (rule.pattern.compiled().pattern(), applied_result)) | ||
}); | ||
|
||
if let Some((rule, result)) = result { | ||
if *transaction != result { | ||
// If another rule was applied before, we don't want to | ||
// rename the transaction name to keep the original one. | ||
// We do want to continue adding remarks though, in | ||
// order to keep track of all rules applied. | ||
if meta.original_value().is_none() { | ||
meta.set_original_value(Some(transaction.clone())); | ||
} | ||
// add also the rule which was applied to the transaction name | ||
meta.add_remark(Remark::new(RemarkType::Substituted, rule)); | ||
*transaction = result; | ||
} | ||
} | ||
|
||
Ok(()) | ||
}); | ||
} | ||
|
||
/// Returns `true` if the given transaction name should be treated as a URL. | ||
/// | ||
/// We treat a transaction as URL if one of the following conditions apply: | ||
|
@@ -89,14 +107,7 @@ impl<'r> TransactionsProcessor<'r> { | |
|
||
fn normalize_transaction_name(&self, event: &mut Event) { | ||
if self.treat_transaction_as_url(event) { | ||
TBS1996 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Normalize transaction names for URLs and Sanitized transaction sources. | ||
// This in addition to renaming rules can catch some high cardinality parts. | ||
scrub_identifiers(&mut event.transaction); | ||
|
||
// Apply rules discovered by the transaction clusterer in sentry. | ||
if !self.name_config.rules.is_empty() { | ||
self.apply_transaction_rename_rule(&mut event.transaction); | ||
} | ||
normalize_transaction_name(&mut event.transaction, self.name_config.rules); | ||
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. extracted for re-use with dsc |
||
|
||
// Always mark URL transactions as sanitized, even if no modification were made by | ||
// clusterer rules or regex matchers. This has the consequence that the transaction name | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -348,6 +348,72 @@ def test_it_tags_error(mini_sentry, relay, expected_sampled, sample_rate): | |
assert evt_id == event_id | ||
|
||
|
||
def test_sample_on_parametrized_root_transaction(mini_sentry, relay): | ||
""" | ||
Tests that dynamic sampling on traces operate on | ||
the parametrized version of the root transaction. | ||
""" | ||
project_id = 42 | ||
relay = relay(mini_sentry, _outcomes_enabled_config()) | ||
|
||
# The pattern used to parametrize the transaction | ||
pattern = "/auth/login/*/**" | ||
# How the original transaction is in the DSC. | ||
original_transaction = "/auth/login/test/" | ||
# What the transaction is transformed into, which the dynamic sampling rules should respect. | ||
parametrized_transaction = "/auth/login/*/" | ||
|
||
config = mini_sentry.add_basic_project_config(project_id) | ||
config["config"]["transactionMetrics"] = {"version": 1} | ||
|
||
sampling_config = mini_sentry.add_basic_project_config(43) | ||
sampling_public_key = sampling_config["publicKeys"][0]["publicKey"] | ||
sampling_config["config"]["txNameRules"] = [ | ||
{ | ||
"pattern": pattern, | ||
"expiry": "3022-11-30T00:00:00.000000Z", | ||
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. note: test will fail 998 years from now |
||
"redaction": {"method": "replace", "substitution": "*"}, | ||
} | ||
] | ||
|
||
ds = sampling_config["config"].setdefault("sampling", {}) | ||
ds.setdefault("version", 2) | ||
|
||
sampling_rule = { | ||
"samplingValue": {"type": "sampleRate", "value": 0.0}, | ||
"type": "trace", | ||
"condition": { | ||
"op": "and", | ||
"inner": [ | ||
{ | ||
"op": "eq", | ||
"name": "trace.transaction", | ||
"value": parametrized_transaction, | ||
"options": { | ||
"ignoreCase": True, | ||
}, | ||
} | ||
], | ||
}, | ||
"id": 1, | ||
} | ||
|
||
rules = ds.setdefault("rules", [sampling_rule]) | ||
rules.append(sampling_rule) | ||
|
||
envelope = Envelope() | ||
transaction_event, trace_id, _ = _create_transaction_item() | ||
envelope.add_transaction(transaction_event) | ||
_add_trace_info( | ||
envelope, trace_id, sampling_public_key, transaction=original_transaction | ||
) | ||
|
||
relay.send_envelope(project_id, envelope) | ||
|
||
outcome = mini_sentry.captured_outcomes.get(timeout=2) | ||
assert outcome["outcomes"][0]["reason"] == "Sampled:1" | ||
|
||
|
||
def test_it_keeps_events(mini_sentry, relay): | ||
""" | ||
Tests that when sampling is set to 100% for the trace context project the events are kept | ||
|
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.
unchanged, just moved