Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions relay-cabi/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ pub unsafe extern "C" fn relay_store_normalizer_normalize_event(
performance_issues_spans: Default::default(),
derive_trace_id: Default::default(),
dsc: None,
sampling_project_id: None,
};
normalize_event(&mut event, &normalization_config);

Expand Down
40 changes: 36 additions & 4 deletions relay-event-normalization/src/eap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::borrow::Cow;
use std::net::IpAddr;

use chrono::{DateTime, Utc};
use relay_base_schema::project::ProjectId;
use relay_common::time::UnixTimestamp;
use relay_conventions::attributes::*;
use relay_conventions::{AttributeInfo, WriteBehavior};
Expand Down Expand Up @@ -355,6 +356,7 @@ pub fn normalize_dsc(
attributes: &mut Annotated<Attributes>,
is_segment: &Annotated<bool>,
dsc: Option<&DynamicSamplingContext>,
project_id: Option<ProjectId>,
) {
let Some(dsc) = dsc else { return };

Expand All @@ -369,6 +371,9 @@ pub fn normalize_dsc(
if let Some(transaction) = &dsc.transaction {
attributes.insert(SENTRY__DSC__TRANSACTION, transaction.clone());
}
if let Some(project_id) = project_id {
attributes.insert(SENTRY__DSC__PROJECT_ID, project_id.value() as i64);
Comment thread
elramen marked this conversation as resolved.
Outdated
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
}

if is_segment.value().is_some_and(|is_segment| *is_segment) {
attributes.insert(SENTRY__DSC__PUBLIC_KEY, dsc.public_key.to_string());
Expand Down Expand Up @@ -752,17 +757,26 @@ mod tests {
#[test]
fn test_normalize_dsc_child_span_no_dsc() {
let mut attributes = Annotated::empty();
normalize_dsc(&mut attributes, &Annotated::new(false), None);
normalize_dsc(&mut attributes, &Annotated::new(false), None, None);
assert!(attributes.value().is_none());
}

#[test]
fn test_normalize_dsc_child_span_no_transaction() {
let mut attributes = Annotated::empty();
let dsc = mock_dsc(None);
normalize_dsc(&mut attributes, &Annotated::new(false), Some(&dsc));
normalize_dsc(
&mut attributes,
&Annotated::new(false),
Some(&dsc),
Some(ProjectId::new(42)),
);
assert_annotated_snapshot!(attributes, @r#"
{
"sentry.dsc.project_id": {
"type": "integer",
"value": 42
},
"sentry.dsc.trace_id": {
"type": "string",
"value": "67e5504410b1426f9247bb680e5fe0c8"
Expand All @@ -775,9 +789,18 @@ mod tests {
fn test_normalize_dsc_child_span() {
let mut attributes = Annotated::empty();
let dsc = mock_dsc(Some("/some/endpoint"));
normalize_dsc(&mut attributes, &Annotated::new(false), Some(&dsc));
normalize_dsc(
&mut attributes,
&Annotated::new(false),
Some(&dsc),
Some(ProjectId::new(42)),
);
assert_annotated_snapshot!(attributes, @r#"
{
"sentry.dsc.project_id": {
"type": "integer",
"value": 42
},
"sentry.dsc.trace_id": {
"type": "string",
"value": "67e5504410b1426f9247bb680e5fe0c8"
Expand All @@ -794,9 +817,18 @@ mod tests {
fn test_normalize_dsc_segment() {
let mut attributes = Annotated::empty();
let dsc = mock_dsc(Some("/some/endpoint"));
normalize_dsc(&mut attributes, &Annotated::new(true), Some(&dsc));
normalize_dsc(
&mut attributes,
&Annotated::new(true),
Some(&dsc),
Some(ProjectId::new(42)),
);
assert_annotated_snapshot!(attributes, @r#"
{
"sentry.dsc.project_id": {
"type": "integer",
"value": 42
},
"sentry.dsc.public_key": {
"type": "string",
"value": "12345678901234567890123456789012"
Expand Down
6 changes: 5 additions & 1 deletion relay-event-normalization/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ pub struct NormalizationConfig<'a> {

/// Dynamic sampling context
pub dsc: Option<&'a DynamicSamplingContext>,

/// The identifier of the project where the trace originated.
pub sampling_project_id: Option<u64>,
}

impl Default for NormalizationConfig<'_> {
Expand Down Expand Up @@ -215,6 +218,7 @@ impl Default for NormalizationConfig<'_> {
performance_issues_spans: Default::default(),
derive_trace_id: Default::default(),
dsc: None,
sampling_project_id: Default::default(),
}
}
}
Expand Down Expand Up @@ -347,7 +351,7 @@ fn normalize(event: &mut Event, meta: &mut Meta, config: &NormalizationConfig) {

if config.normalize_spans && event.ty.value() == Some(&EventType::Transaction) {
span::normalize_app_start_spans(event);
span::normalize_dsc_for_event_spans(event, config.dsc);
span::normalize_dsc_for_event_spans(event, config.dsc, config.sampling_project_id);
Comment thread
elramen marked this conversation as resolved.
Outdated
span::exclusive_time::compute_span_exclusive_time(event);
}

Expand Down
19 changes: 15 additions & 4 deletions relay-event-normalization/src/normalize/span/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Span normalization logic.

use regex::Regex;
use relay_conventions::attributes::{SENTRY__DSC__TRACE_ID, SENTRY__DSC__TRANSACTION};
use relay_conventions::attributes::*;
use relay_event_schema::protocol::{Event, SpanData, TraceContext};
use relay_protocol::{Annotated, Value};
use relay_sampling::DynamicSamplingContext;
Expand Down Expand Up @@ -61,14 +61,18 @@ pub fn normalize_app_start_spans(event: &mut Event) {
///
/// If `sentry.dsc.trace_id` is already present in a span's `data`, the function does nothing for
/// that span.
pub fn normalize_dsc_for_event_spans(event: &mut Event, dsc: Option<&DynamicSamplingContext>) {
pub fn normalize_dsc_for_event_spans(
event: &mut Event,
dsc: Option<&DynamicSamplingContext>,
project_id: Option<u64>,
) {
if let Some(ctx) = event.context_mut::<TraceContext>() {
normalize_dsc_for_span_data(&mut ctx.data, dsc);
normalize_dsc_for_span_data(&mut ctx.data, dsc, project_id);
}
if let Some(spans) = event.spans.value_mut() {
for span in spans {
if let Some(span) = span.value_mut() {
normalize_dsc_for_span_data(&mut span.data, dsc);
normalize_dsc_for_span_data(&mut span.data, dsc, project_id);
}
}
}
Expand All @@ -80,6 +84,7 @@ pub fn normalize_dsc_for_event_spans(event: &mut Event, dsc: Option<&DynamicSamp
pub fn normalize_dsc_for_span_data(
span_data: &mut Annotated<SpanData>,
dsc: Option<&DynamicSamplingContext>,
project_id: Option<u64>,
) {
Comment thread
elramen marked this conversation as resolved.
let Some(dsc) = dsc else { return };

Expand All @@ -98,4 +103,10 @@ pub fn normalize_dsc_for_span_data(
Annotated::new(Value::String(transaction.clone())),
);
}
if let Some(project_id) = project_id {
data.other.insert(
SENTRY__DSC__PROJECT_ID.to_owned(),
Annotated::new(Value::U64(project_id)),
);
}
Comment thread
elramen marked this conversation as resolved.
Outdated
}
7 changes: 6 additions & 1 deletion relay-server/src/processing/legacy_spans/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::services::processor::ProcessingError;
use chrono::{DateTime, Utc};
use relay_base_schema::project::ProjectId;
use relay_event_normalization::span::{self, ai};
use relay_event_normalization::{
BorrowedSpanOpDefaults, ClientHints, CombinedMeasurementsConfig, FromUserAgentInfo,
Expand Down Expand Up @@ -58,6 +59,8 @@ pub struct NormalizeSpanConfig<'a> {
pub span_op_defaults: BorrowedSpanOpDefaults<'a>,
/// Dynamic sampling context from the envelope headers.
pub dsc: Option<&'a DynamicSamplingContext>,
/// Project ID of the project that started the trace.
pub sampling_project_id: Option<ProjectId>,
}

fn set_segment_attributes(span: &mut Annotated<Span>) {
Expand Down Expand Up @@ -112,6 +115,7 @@ pub fn normalize(
geo_lookup,
span_op_defaults,
dsc,
sampling_project_id,
} = config;

set_segment_attributes(annotated_span);
Expand Down Expand Up @@ -211,7 +215,7 @@ pub fn normalize(

normalize_performance_score(span, *performance_score);

span::normalize_dsc_for_span_data(&mut span.data, *dsc);
span::normalize_dsc_for_span_data(&mut span.data, *dsc, sampling_project_id.map(|p| p.value()));

ai::enrich_ai_span(span, *ai_model_metadata);

Expand Down Expand Up @@ -555,6 +559,7 @@ mod tests {
geo_lookup: &GEO_LOOKUP,
span_op_defaults: Default::default(),
dsc: None,
sampling_project_id: None,
}
}

Expand Down
1 change: 1 addition & 0 deletions relay-server/src/processing/legacy_spans/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn normalize(
geo_lookup,
span_op_defaults: ctx.global_config.span_op_defaults.borrow(),
dsc: dsc.as_ref(),
sampling_project_id: ctx.sampling_project_info.and_then(|p| p.project_id),
};

spans.retain(
Expand Down
7 changes: 6 additions & 1 deletion relay-server/src/processing/spans/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,12 @@ fn normalize_span(
}
eap::normalize_user_agent(&mut span.attributes, client_ua_info);
eap::normalize_user_geo(&mut span.attributes, |ip| geo_lookup.lookup(ip));
eap::normalize_dsc(&mut span.attributes, &span.is_segment, dsc);
eap::normalize_dsc(
&mut span.attributes,
&span.is_segment,
dsc,
ctx.sampling_project_info.and_then(|p| p.project_id),
);
if ctx.is_processing() {
eap::normalize_ai(&mut span.attributes, duration, model_metdata);
}
Expand Down
3 changes: 3 additions & 0 deletions relay-server/src/processing/utils/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ pub fn normalize(
.has_feature(Feature::PerformanceIssuesSpans),
derive_trace_id: project_info.has_feature(Feature::AddDefaultTraceID),
dsc: headers.dsc(),
sampling_project_id: ctx
.sampling_project_info
.and_then(|p| p.project_id.map(|p| p.value())),
};

metric!(timer(RelayTimers::EventProcessingNormalization), {
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ def test_ai_spans_example_transaction(
"type": "string",
"value": "generateText weather-chat",
},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -539,6 +540,7 @@ def test_ai_spans_example_transaction(
"type": "string",
"value": "generate_text gpt-4o",
},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -639,6 +641,7 @@ def test_ai_spans_example_transaction(
"value": "POST " "https://api.openai.com/v1/responses",
},
"sentry.domain": {"type": "string", "value": "*.openai.com"},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -735,6 +738,7 @@ def test_ai_spans_example_transaction(
"type": "string",
"value": "execute_tool getWeather",
},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -798,6 +802,7 @@ def test_ai_spans_example_transaction(
"value": "GET " "https://wttr.in/San%20Francisco",
},
"sentry.domain": {"type": "string", "value": "wttr.in"},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -887,6 +892,7 @@ def test_ai_spans_example_transaction(
"type": "string",
"value": "execute_tool getWeather",
},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -950,6 +956,7 @@ def test_ai_spans_example_transaction(
"value": "GET https://wttr.in/London",
},
"sentry.domain": {"type": "string", "value": "wttr.in"},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -1069,6 +1076,7 @@ def test_ai_spans_example_transaction(
"type": "string",
"value": "generate_text gpt-4o",
},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -1166,6 +1174,7 @@ def test_ai_spans_example_transaction(
"value": "POST " "https://api.openai.com/v1/responses",
},
"sentry.domain": {"type": "string", "value": "*.openai.com"},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down Expand Up @@ -1244,6 +1253,7 @@ def test_ai_spans_example_transaction(
"gen_ai.usage.output_tokens": {"type": "integer", "value": 65},
"gen_ai.usage.total_tokens": {"type": "double", "value": 310.0},
"sentry.description": {"type": "string", "value": "main"},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a9351cd574f092f6acad48e250981f11",
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_spans.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def test_span_extraction(
},
"sentry.is_remote": {"type": "boolean", "value": False},
"sentry.segment.id": {"type": "string", "value": "968cff94913ebb07"},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
"sentry.dsc.trace_id": {
"type": "string",
"value": "a0fa8803753e40fd8124b21eeb2986b5",
Expand Down Expand Up @@ -232,6 +233,7 @@ def test_span_extraction(
"sentry.user.id": {"type": "string", "value": user_id},
"sentry.user.ip": {"type": "string", "value": "192.168.0.1"},
"sentry.user": {"type": "string", "value": f"id:{user_id}"},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "a0fa8803753e40fd8124b21eeb2986b5",
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/test_spans_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def test_lcp_span(
"type": "string",
"value": "/insights/projects/",
},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "d3d20f000885466b8c8f947c9b92b8d3",
Expand Down Expand Up @@ -445,6 +446,7 @@ def test_cls_span(
"type": "string",
"value": "/insights/projects/",
},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "d3d20f000885466b8c8f947c9b92b8d3",
Expand Down Expand Up @@ -631,6 +633,7 @@ def test_inp_span(
"type": "string",
"value": "/insights/projects/",
},
"sentry.dsc.project_id": {"type": "integer", "value": 42},
"sentry.dsc.trace_id": {
"type": "string",
"value": "d3d20f000885466b8c8f947c9b92b8d3",
Expand Down
Loading
Loading