Skip to content

Commit

Permalink
Add client sample rate as a measurement on spans
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-sentry committed Jul 30, 2024
1 parent 24726ce commit 330b93a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 38 additions & 5 deletions relay-event-normalization/src/normalize/span/tag_extraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -194,6 +191,7 @@ pub fn extract_span_tags(event: &Event, spans: &mut [Annotated<Span>], 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");
Expand All @@ -218,6 +216,17 @@ pub fn extract_span_tags(event: &Event, spans: &mut [Annotated<Span>], 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);
}
}
Expand Down Expand Up @@ -794,6 +803,25 @@ fn value_to_f64(val: Option<&Value>) -> Option<f64> {
}
}

fn extract_shared_measurements(event: &Event) -> Measurements {
let mut measurements = Measurements::default();

if let Some(trace_context) = event.context::<TraceContext>() {
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 {
Expand Down Expand Up @@ -1623,6 +1651,11 @@ LIMIT 1
fn test_ai_extraction() {
let json = r#"
{
"contexts": {
"trace": {
"client_sample_rate": 0.1
}
},
"spans": [
{
"timestamp": 1694732408.3145,
Expand Down

0 comments on commit 330b93a

Please sign in to comment.