Skip to content

Commit

Permalink
Send client sample rate as a span measurement (#3872)
Browse files Browse the repository at this point in the history
This will be consumed by snuba spans consumer
  • Loading branch information
colin-sentry authored Jul 30, 2024
1 parent 24726ce commit 72b5d37
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 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
52 changes: 48 additions & 4 deletions relay-event-normalization/src/normalize/span/tag_extraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ 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, OsContext, ProfileContext, Span, Timestamp,
TraceContext,
AppContext, BrowserContext, Event, Measurement, Measurements, OsContext, ProfileContext, Span,
Timestamp, TraceContext,
};
use relay_protocol::{Annotated, Value};
use relay_protocol::{Annotated, Empty, Value};
use sqlparser::ast::Visit;
use sqlparser::ast::{ObjectName, Visitor};
use url::Url;
Expand Down Expand Up @@ -194,6 +194,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 +219,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(), val.clone());
}),
None => span
.measurements
.set_value(Some(shared_measurements.clone())),
}
}

extract_measurements(span, is_mobile);
}
}
Expand Down Expand Up @@ -794,6 +806,27 @@ 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::Fraction(FractionUnit::Ratio).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 +1656,11 @@ LIMIT 1
fn test_ai_extraction() {
let json = r#"
{
"contexts": {
"trace": {
"client_sample_rate": 0.1
}
},
"spans": [
{
"timestamp": 1694732408.3145,
Expand Down Expand Up @@ -1683,6 +1721,12 @@ LIMIT 1
value: 300.0,
unit: None,
},
"client_sample_rate": Measurement {
value: 0.1,
unit: Fraction(
Ratio,
),
},
},
)
"###);
Expand Down

0 comments on commit 72b5d37

Please sign in to comment.