From cf5f18acf26e392aac363cdffc8aa011301e126b Mon Sep 17 00:00:00 2001 From: Edward Gou Date: Tue, 9 Jul 2024 14:50:17 -0400 Subject: [PATCH 1/2] Stop checking profiles after the first match --- relay-event-normalization/src/event.rs | 275 ++++++++++++++++++++++++- 1 file changed, 273 insertions(+), 2 deletions(-) diff --git a/relay-event-normalization/src/event.rs b/relay-event-normalization/src/event.rs index c3655bbab4..26b3b9036b 100644 --- a/relay-event-normalization/src/event.rs +++ b/relay-event-normalization/src/event.rs @@ -946,6 +946,7 @@ pub fn normalize_performance_score( ); } } + break; // Stop after the first matching profile. } } } @@ -3300,8 +3301,8 @@ mod tests { { "measurement": "inp", "weight": 1.0, - "p10": 0.1, - "p50": 0.25 + "p10": 100, + "p50": 2500 }, ], "condition": { @@ -3348,6 +3349,276 @@ mod tests { "###); } + #[test] + fn test_computed_performance_score_uses_first_matching_profile() { + let json = r#" + { + "type": "transaction", + "timestamp": "2021-04-26T08:00:05+0100", + "start_timestamp": "2021-04-26T08:00:00+0100", + "measurements": { + "a": {"value": 213, "unit": "millisecond"}, + "b": {"value": 213, "unit": "millisecond"} + }, + "contexts": { + "browser": { + "name": "Chrome", + "version": "120.1.1", + "type": "browser" + } + } + } + "#; + + let mut event = Annotated::::from_json(json).unwrap().0.unwrap(); + + let performance_score: PerformanceScoreConfig = serde_json::from_value(json!({ + "profiles": [ + { + "name": "Desktop", + "scoreComponents": [ + { + "measurement": "a", + "weight": 0.15, + "p10": 900, + "p50": 1600, + }, + { + "measurement": "b", + "weight": 0.30, + "p10": 1200, + "p50": 2400, + "optional": true + }, + { + "measurement": "c", + "weight": 0.55, + "p10": 1200, + "p50": 2400, + "optional": true + }, + ], + "condition": { + "op":"eq", + "name": "event.contexts.browser.name", + "value": "Chrome" + } + }, + { + "name": "Default", + "scoreComponents": [ + { + "measurement": "a", + "weight": 0.15, + "p10": 100, + "p50": 200, + }, + { + "measurement": "b", + "weight": 0.30, + "p10": 100, + "p50": 200, + "optional": true + }, + { + "measurement": "c", + "weight": 0.55, + "p10": 100, + "p50": 200, + "optional": true + }, + ], + "condition": { + "op": "and", + "inner": [], + } + } + ] + })) + .unwrap(); + + normalize_performance_score(&mut event, Some(&performance_score)); + + insta::assert_ron_snapshot!(SerializableAnnotated(&Annotated::new(event)), {}, @r###" + { + "type": "transaction", + "timestamp": 1619420405.0, + "start_timestamp": 1619420400.0, + "contexts": { + "browser": { + "name": "Chrome", + "version": "120.1.1", + "type": "browser", + }, + }, + "measurements": { + "a": { + "value": 213.0, + "unit": "millisecond", + }, + "b": { + "value": 213.0, + "unit": "millisecond", + }, + "score.a": { + "value": 0.33333215313291975, + "unit": "ratio", + }, + "score.b": { + "value": 0.66666415149198, + "unit": "ratio", + }, + "score.total": { + "value": 0.9999963046248997, + "unit": "ratio", + }, + "score.weight.a": { + "value": 0.33333333333333337, + "unit": "ratio", + }, + "score.weight.b": { + "value": 0.6666666666666667, + "unit": "ratio", + }, + "score.weight.c": { + "value": 0.0, + "unit": "ratio", + }, + }, + } + "###); + } + + #[test] + fn test_computed_performance_score_falls_back_to_default_profile() { + let json = r#" + { + "type": "transaction", + "timestamp": "2021-04-26T08:00:05+0100", + "start_timestamp": "2021-04-26T08:00:00+0100", + "measurements": { + "a": {"value": 213, "unit": "millisecond"}, + "b": {"value": 213, "unit": "millisecond"} + }, + "contexts": {} + } + "#; + + let mut event = Annotated::::from_json(json).unwrap().0.unwrap(); + + let performance_score: PerformanceScoreConfig = serde_json::from_value(json!({ + "profiles": [ + { + "name": "Desktop", + "scoreComponents": [ + { + "measurement": "a", + "weight": 0.15, + "p10": 900, + "p50": 1600, + "optional": true + }, + { + "measurement": "b", + "weight": 0.30, + "p10": 1200, + "p50": 2400, + "optional": true + }, + { + "measurement": "c", + "weight": 0.55, + "p10": 1200, + "p50": 2400, + "optional": true + }, + ], + "condition": { + "op":"eq", + "name": "event.contexts.browser.name", + "value": "Chrome" + } + }, + { + "name": "Default", + "scoreComponents": [ + { + "measurement": "a", + "weight": 0.15, + "p10": 100, + "p50": 200, + "optional": true + }, + { + "measurement": "b", + "weight": 0.30, + "p10": 100, + "p50": 200, + "optional": true + }, + { + "measurement": "c", + "weight": 0.55, + "p10": 100, + "p50": 200, + "optional": true + }, + ], + "condition": { + "op": "and", + "inner": [], + } + } + ] + })) + .unwrap(); + + normalize_performance_score(&mut event, Some(&performance_score)); + + insta::assert_ron_snapshot!(SerializableAnnotated(&Annotated::new(event)), {}, @r###" + { + "type": "transaction", + "timestamp": 1619420405.0, + "start_timestamp": 1619420400.0, + "contexts": {}, + "measurements": { + "a": { + "value": 213.0, + "unit": "millisecond", + }, + "b": { + "value": 213.0, + "unit": "millisecond", + }, + "score.a": { + "value": 0.15121816827413334, + "unit": "ratio", + }, + "score.b": { + "value": 0.3024363365482667, + "unit": "ratio", + }, + "score.total": { + "value": 0.4536545048224, + "unit": "ratio", + }, + "score.weight.a": { + "value": 0.33333333333333337, + "unit": "ratio", + }, + "score.weight.b": { + "value": 0.6666666666666667, + "unit": "ratio", + }, + "score.weight.c": { + "value": 0.0, + "unit": "ratio", + }, + }, + } + "###); + } + #[test] fn test_normalization_removes_reprocessing_context() { let json = r#"{ From 52745363fea35d74bcbf7d0b014c8cef119fe939 Mon Sep 17 00:00:00 2001 From: Edward Gou Date: Tue, 9 Jul 2024 15:26:10 -0400 Subject: [PATCH 2/2] undo --- relay-event-normalization/src/event.rs | 65 +++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/relay-event-normalization/src/event.rs b/relay-event-normalization/src/event.rs index 26b3b9036b..86b0398128 100644 --- a/relay-event-normalization/src/event.rs +++ b/relay-event-normalization/src/event.rs @@ -3301,8 +3301,8 @@ mod tests { { "measurement": "inp", "weight": 1.0, - "p10": 100, - "p50": 2500 + "p10": 0.1, + "p50": 0.25 }, ], "condition": { @@ -3374,6 +3374,36 @@ mod tests { let performance_score: PerformanceScoreConfig = serde_json::from_value(json!({ "profiles": [ + { + "name": "Mobile", + "scoreComponents": [ + { + "measurement": "a", + "weight": 0.15, + "p10": 100, + "p50": 200, + }, + { + "measurement": "b", + "weight": 0.30, + "p10": 100, + "p50": 200, + "optional": true + }, + { + "measurement": "c", + "weight": 0.55, + "p10": 100, + "p50": 200, + "optional": true + }, + ], + "condition": { + "op":"eq", + "name": "event.contexts.browser.name", + "value": "Chrome Mobile" + } + }, { "name": "Desktop", "scoreComponents": [ @@ -3508,6 +3538,37 @@ mod tests { let performance_score: PerformanceScoreConfig = serde_json::from_value(json!({ "profiles": [ + { + "name": "Mobile", + "scoreComponents": [ + { + "measurement": "a", + "weight": 0.15, + "p10": 900, + "p50": 1600, + "optional": true + }, + { + "measurement": "b", + "weight": 0.30, + "p10": 1200, + "p50": 2400, + "optional": true + }, + { + "measurement": "c", + "weight": 0.55, + "p10": 1200, + "p50": 2400, + "optional": true + }, + ], + "condition": { + "op":"eq", + "name": "event.contexts.browser.name", + "value": "Chrome Mobile" + } + }, { "name": "Desktop", "scoreComponents": [