From 330b93a35b411de3ab04f6a70d475fcc2f2a4c0d Mon Sep 17 00:00:00 2001 From: Colin Chartier Date: Tue, 30 Jul 2024 12:24:50 -0400 Subject: [PATCH 1/5] Add client sample rate as a measurement on spans --- CHANGELOG.md | 2 + .../src/normalize/span/tag_extraction.rs | 43 ++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 482b8084d5..3bd86a9957 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ **Internal**: - Add `EnvelopeStack` and `SQLiteEnvelopeStack` to manage envelopes on disk. ([#3855](https://github.com/getsentry/relay/pull/3855)) +- Add `client_sample_rate` to spans, pulled from the trace context ([#3872](https://github.com/getsentry/relay/pull/3872)). + ## 24.7.1 diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index 7e987ea400..5f90c757cb 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -9,11 +9,8 @@ use std::ops::ControlFlow; use once_cell::sync::Lazy; use regex::Regex; use relay_base_schema::metrics::{DurationUnit, InformationUnit, MetricUnit}; -use relay_event_schema::protocol::{ - AppContext, BrowserContext, Event, Measurement, OsContext, ProfileContext, Span, Timestamp, - TraceContext, -}; -use relay_protocol::{Annotated, Value}; +use relay_event_schema::protocol::{AppContext, BrowserContext, Event, Measurement, Measurements, OsContext, ProfileContext, Span, Timestamp, TraceContext}; +use relay_protocol::{Annotated, Empty, Value}; use sqlparser::ast::Visit; use sqlparser::ast::{ObjectName, Visitor}; use url::Url; @@ -194,6 +191,7 @@ pub fn extract_span_tags(event: &Event, spans: &mut [Annotated], max_tag_v // TODO: To prevent differences between metrics and payloads, we should not extract tags here // when they have already been extracted by a downstream relay. let shared_tags = extract_shared_tags(event); + let shared_measurements = extract_shared_measurements(event); let is_mobile = shared_tags .get(&SpanTagKey::Mobile) .is_some_and(|v| v.as_str() == "true"); @@ -218,6 +216,17 @@ pub fn extract_span_tags(event: &Event, spans: &mut [Annotated], max_tag_v .collect(), ); + if !shared_measurements.is_empty() { + match span.measurements.value_mut() { + Some(left) => { + shared_measurements.iter().for_each(|(key, val)| { + left.insert(key.clone().into(), val.clone().into()); + }) + } + None => span.measurements.set_value(Some(shared_measurements.clone())) + } + } + extract_measurements(span, is_mobile); } } @@ -794,6 +803,25 @@ fn value_to_f64(val: Option<&Value>) -> Option { } } +fn extract_shared_measurements(event: &Event) -> Measurements { + let mut measurements = Measurements::default(); + + if let Some(trace_context) = event.context::() { + if let Some(client_sample_rate) = trace_context.client_sample_rate.value() { + if *client_sample_rate > 0. { + measurements + .insert("client_sample_rate".into(), Measurement{ + value: (*client_sample_rate).into(), + unit: MetricUnit::None.into(), + }.into()); + } + } + } + + measurements +} + + /// Copies specific numeric values from span data to span measurements. pub fn extract_measurements(span: &mut Span, is_mobile: bool) { let Some(span_op) = span.op.as_str() else { @@ -1623,6 +1651,11 @@ LIMIT 1 fn test_ai_extraction() { let json = r#" { + "contexts": { + "trace": { + "client_sample_rate": 0.1 + } + }, "spans": [ { "timestamp": 1694732408.3145, From 5aef5897883efadd7b821934bdec3ac2f3b303ea Mon Sep 17 00:00:00 2001 From: Colin Chartier Date: Tue, 30 Jul 2024 12:28:10 -0400 Subject: [PATCH 2/5] Lint issue --- .../src/normalize/span/tag_extraction.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index 5f90c757cb..124ca1211f 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -220,7 +220,7 @@ pub fn extract_span_tags(event: &Event, spans: &mut [Annotated], max_tag_v match span.measurements.value_mut() { Some(left) => { shared_measurements.iter().for_each(|(key, val)| { - left.insert(key.clone().into(), val.clone().into()); + left.insert(key.clone(), val.clone()); }) } None => span.measurements.set_value(Some(shared_measurements.clone())) @@ -1716,6 +1716,10 @@ LIMIT 1 value: 300.0, unit: None, }, + "client_sample_rate": Measurement { + value: 0.1, + unit: None, + }, }, ) "###); From 525b2cc3e0f79093b90b90e9179f8aa99d153583 Mon Sep 17 00:00:00 2001 From: Colin Chartier Date: Tue, 30 Jul 2024 12:32:48 -0400 Subject: [PATCH 3/5] lint again --- .../src/normalize/span/tag_extraction.rs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index 124ca1211f..32dc2ada3b 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -9,7 +9,10 @@ use std::ops::ControlFlow; use once_cell::sync::Lazy; use regex::Regex; use relay_base_schema::metrics::{DurationUnit, InformationUnit, MetricUnit}; -use relay_event_schema::protocol::{AppContext, BrowserContext, Event, Measurement, Measurements, OsContext, ProfileContext, Span, Timestamp, TraceContext}; +use relay_event_schema::protocol::{ + AppContext, BrowserContext, Event, Measurement, Measurements, OsContext, ProfileContext, Span, + Timestamp, TraceContext, +}; use relay_protocol::{Annotated, Empty, Value}; use sqlparser::ast::Visit; use sqlparser::ast::{ObjectName, Visitor}; @@ -218,12 +221,12 @@ pub fn extract_span_tags(event: &Event, spans: &mut [Annotated], max_tag_v if !shared_measurements.is_empty() { match span.measurements.value_mut() { - Some(left) => { - shared_measurements.iter().for_each(|(key, val)| { - left.insert(key.clone(), val.clone()); - }) - } - None => span.measurements.set_value(Some(shared_measurements.clone())) + Some(left) => shared_measurements.iter().for_each(|(key, val)| { + left.insert(key.clone(), val.clone()); + }), + None => span + .measurements + .set_value(Some(shared_measurements.clone())), } } @@ -809,11 +812,14 @@ fn extract_shared_measurements(event: &Event) -> Measurements { if let Some(trace_context) = event.context::() { if let Some(client_sample_rate) = trace_context.client_sample_rate.value() { if *client_sample_rate > 0. { - measurements - .insert("client_sample_rate".into(), Measurement{ + measurements.insert( + "client_sample_rate".into(), + Measurement { value: (*client_sample_rate).into(), unit: MetricUnit::None.into(), - }.into()); + } + .into(), + ); } } } @@ -821,7 +827,6 @@ fn extract_shared_measurements(event: &Event) -> Measurements { measurements } - /// Copies specific numeric values from span data to span measurements. pub fn extract_measurements(span: &mut Span, is_mobile: bool) { let Some(span_op) = span.op.as_str() else { From a1d6b4c37b238d6db3fbca2f49b599e629cdda82 Mon Sep 17 00:00:00 2001 From: Colin Chartier Date: Tue, 30 Jul 2024 14:18:43 -0400 Subject: [PATCH 4/5] Ratio for unit --- .../src/normalize/span/tag_extraction.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index 32dc2ada3b..63e8f68823 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -8,7 +8,7 @@ use std::ops::ControlFlow; use once_cell::sync::Lazy; use regex::Regex; -use relay_base_schema::metrics::{DurationUnit, InformationUnit, MetricUnit}; +use relay_base_schema::metrics::{DurationUnit, FractionUnit, InformationUnit, MetricUnit}; use relay_event_schema::protocol::{ AppContext, BrowserContext, Event, Measurement, Measurements, OsContext, ProfileContext, Span, Timestamp, TraceContext, @@ -816,7 +816,7 @@ fn extract_shared_measurements(event: &Event) -> Measurements { "client_sample_rate".into(), Measurement { value: (*client_sample_rate).into(), - unit: MetricUnit::None.into(), + unit: MetricUnit::Fraction(FractionUnit::Ratio).into(), } .into(), ); From 6f6301fae828db67561ba065b7cbf92f8c25b824 Mon Sep 17 00:00:00 2001 From: Colin Chartier Date: Tue, 30 Jul 2024 14:21:41 -0400 Subject: [PATCH 5/5] Fix snapshot --- .../src/normalize/span/tag_extraction.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/relay-event-normalization/src/normalize/span/tag_extraction.rs b/relay-event-normalization/src/normalize/span/tag_extraction.rs index 63e8f68823..ef3e1e0676 100644 --- a/relay-event-normalization/src/normalize/span/tag_extraction.rs +++ b/relay-event-normalization/src/normalize/span/tag_extraction.rs @@ -1723,7 +1723,9 @@ LIMIT 1 }, "client_sample_rate": Measurement { value: 0.1, - unit: None, + unit: Fraction( + Ratio, + ), }, }, )