Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(event-schema): Copy root span data when converting from events #3790

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
20 changes: 18 additions & 2 deletions relay-event-schema/src/protocol/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use relay_protocol::{Annotated, Empty, FromValue, Getter, IntoValue, Object, Val

use crate::processor::ProcessValue;
use crate::protocol::{
EventId, JsonLenientString, LenientString, Measurements, MetricsSummary, OperationType,
OriginType, SpanId, SpanStatus, ThreadId, Timestamp, TraceId,
Data as TraceData, EventId, JsonLenientString, LenientString, Measurements, MetricsSummary,
OperationType, OriginType, SpanId, SpanStatus, ThreadId, Timestamp, TraceId,
};

#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
Expand Down Expand Up @@ -454,6 +454,22 @@ impl Getter for SpanData {
}
}

impl From<TraceData> for SpanData {
fn from(trace_data: TraceData) -> Self {
FromValue::from_value(Annotated::new(trace_data.into_value()))
.into_value()
.unwrap_or_default()
}
}

impl From<SpanData> for TraceData {
fn from(span_data: SpanData) -> Self {
FromValue::from_value(Annotated::new(span_data.into_value()))
.into_value()
.unwrap_or_default()
}
}

#[cfg(test)]
mod tests {
use crate::protocol::Measurement;
Expand Down
31 changes: 29 additions & 2 deletions relay-event-schema/src/protocol/span/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ macro_rules! map_fields {
// This macro call implements a bidirectional mapping between transaction event and segment spans,
// allowing users to call both `Event::from(&span)` and `Span::from(&event)`.
map_fields!(
// Data must go first to ensure it doesn't overwrite more specific fields
span.data <=> event.contexts.trace.data,
span._metrics_summary <=> event._metrics_summary,
span.description <=> event.transaction,
span.data.segment_name <=> event.transaction,
Expand Down Expand Up @@ -188,6 +190,7 @@ map_fields!(
#[cfg(test)]
mod tests {
use relay_protocol::Annotated;
use similar_asserts::assert_eq;

use crate::protocol::{SpanData, SpanId};

Expand All @@ -214,7 +217,10 @@ mod tests {
"op": "myop",
"status": "ok",
"exclusive_time": 123.4,
"parent_span_id": "FA90FDEAD5F74051"
"parent_span_id": "FA90FDEAD5F74051",
"data": {
"custom_attribute": 42
}
}
},
"_metrics_summary": {
Expand Down Expand Up @@ -320,7 +326,13 @@ mod tests {
user_agent_original: ~,
url_full: ~,
client_address: ~,
other: {},
other: {
"custom_attribute": I64(
42,
),
"previousRoute": ~,
"route": ~,
},
},
sentry_tags: ~,
received: ~,
Expand Down Expand Up @@ -360,6 +372,21 @@ mod tests {
let event_id = event_from_span.id.value_mut().take().unwrap();
assert_eq!(&event_id.to_string(), "0000000000000000fa90fdead5f74052");

// Before comparing, remove any additional data that was injected into trace data during
// span conversion. Note that the keys are renamed on the `SpanData` struct and mostly start
// with `sentry.`.
let trace = event_from_span.context_mut::<TraceContext>().unwrap();
let trace_data = trace.data.value_mut().as_mut().unwrap();

trace_data.other.retain(|k, v| {
if v.value().is_none() || k.starts_with("sentry.") {
return false;
}

// Seems to be a special case
k != "browser.name"
});

assert_eq!(event, event_from_span);
}

Expand Down
Loading