-
Notifications
You must be signed in to change notification settings - Fork 103
ref(transactions): Ensure that every span's parent exists #4661
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
Changes from 8 commits
f71cd1c
f39e1b7
2a04451
9b6bf2b
3415750
c5ef664
93c6781
14cf904
2dfc6a1
a079f43
8704a7c
5bf7fda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,164 @@ | ||||||||||||||
//! Span tree normalization. | ||||||||||||||
use std::collections::BTreeSet; | ||||||||||||||
use std::mem; | ||||||||||||||
|
||||||||||||||
use relay_event_schema::protocol::{Event, TraceContext}; | ||||||||||||||
use relay_protocol::Error; | ||||||||||||||
|
||||||||||||||
/// Enforce that every span has a valid parent. If any `parent_span_id` is pointing nowhere, the | ||||||||||||||
/// span is re-parented onto the root span. | ||||||||||||||
/// | ||||||||||||||
/// This is to avoid any nasty surprises in the span buffer specifically. Other readers of spans | ||||||||||||||
/// (such as the frontend's tree component) were already able to deal with detached spans. | ||||||||||||||
pub fn fix_trees(event: &mut Event) { | ||||||||||||||
|
||||||||||||||
let Some(ref mut spans) = event.spans.value_mut() else { | ||||||||||||||
return; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
let Some(contexts) = event.contexts.value_mut() else { | ||||||||||||||
return; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
let Some(trace_context) = contexts.get_mut::<TraceContext>() else { | ||||||||||||||
return; | ||||||||||||||
}; | ||||||||||||||
untitaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
let Some(root_span_id) = trace_context.span_id.value() else { | ||||||||||||||
return; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
let valid_span_ids = spans | ||||||||||||||
.iter() | ||||||||||||||
.filter_map(|span| span.value()) | ||||||||||||||
.filter_map(|span| span.span_id.value()) | ||||||||||||||
.chain(Some(root_span_id)) | ||||||||||||||
.cloned() | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really dislike this. In this expression we access It would be better to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either this or we serialize like in relay/relay-base-schema/src/project.rs Lines 85 to 90 in cbf72c2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tried to change this but too much stuff has to be adjusted to make it work since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, part of this would be refactoring all usage to remove access to the field. |
||||||||||||||
.collect::<BTreeSet<_>>(); | ||||||||||||||
|
||||||||||||||
for span in spans { | ||||||||||||||
let Some(span) = span.value_mut() else { | ||||||||||||||
continue; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
let Some(parent_span_id) = span.parent_span_id.value_mut() else { | ||||||||||||||
continue; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
if valid_span_ids.contains(&*parent_span_id) { | ||||||||||||||
continue; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
let invalid_parent = mem::replace(parent_span_id, root_span_id.clone()); | ||||||||||||||
let meta = span.parent_span_id.meta_mut(); | ||||||||||||||
meta.add_error(Error::invalid("span ID does not exist")); | ||||||||||||||
meta.set_original_value(Some(invalid_parent)); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg(test)] | ||||||||||||||
mod tests { | ||||||||||||||
use relay_protocol::FromValue; | ||||||||||||||
use relay_protocol::SerializableAnnotated; | ||||||||||||||
|
||||||||||||||
use super::*; | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn basic() { | ||||||||||||||
let mut data = Event::from_value( | ||||||||||||||
serde_json::json!({ | ||||||||||||||
"type": "transaction", | ||||||||||||||
"start_timestamp": 1609455600, | ||||||||||||||
"end_timestamp": 1609455605, | ||||||||||||||
"contexts": { | ||||||||||||||
"trace": { | ||||||||||||||
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2", | ||||||||||||||
"span_id": "aaaaaaaaaaaaaaaa", | ||||||||||||||
"parent_span_id": "ffffffffffffffff", | ||||||||||||||
} | ||||||||||||||
}, | ||||||||||||||
"spans": [ | ||||||||||||||
{ | ||||||||||||||
"span_id": "bbbbbbbbbbbbbbbb", | ||||||||||||||
"parent_span_id": "aaaaaaaaaaaaaaaa" | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
"span_id": "bbbbbbbbbbbbbbbb", | ||||||||||||||
"parent_span_id": "dddddddddddddddd", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
"span_id": "bbbbbbbbbbbbbbbb", | ||||||||||||||
"parent_span_id": "eeeeeeeeeeeeeeee", | ||||||||||||||
}, | ||||||||||||||
], | ||||||||||||||
}) | ||||||||||||||
.into(), | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
fix_trees(data.value_mut().as_mut().unwrap()); | ||||||||||||||
|
||||||||||||||
insta::assert_json_snapshot!(SerializableAnnotated(&data), @r###" | ||||||||||||||
{ | ||||||||||||||
"type": "transaction", | ||||||||||||||
"start_timestamp": 1609455600.0, | ||||||||||||||
"contexts": { | ||||||||||||||
"trace": { | ||||||||||||||
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2", | ||||||||||||||
"span_id": "aaaaaaaaaaaaaaaa", | ||||||||||||||
"parent_span_id": "ffffffffffffffff", | ||||||||||||||
"type": "trace" | ||||||||||||||
} | ||||||||||||||
}, | ||||||||||||||
"spans": [ | ||||||||||||||
{ | ||||||||||||||
"span_id": "bbbbbbbbbbbbbbbb", | ||||||||||||||
"parent_span_id": "aaaaaaaaaaaaaaaa" | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
"span_id": "bbbbbbbbbbbbbbbb", | ||||||||||||||
"parent_span_id": "aaaaaaaaaaaaaaaa" | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
"span_id": "bbbbbbbbbbbbbbbb", | ||||||||||||||
"parent_span_id": "aaaaaaaaaaaaaaaa" | ||||||||||||||
} | ||||||||||||||
], | ||||||||||||||
"end_timestamp": 1609455605, | ||||||||||||||
"_meta": { | ||||||||||||||
"spans": { | ||||||||||||||
"1": { | ||||||||||||||
"parent_span_id": { | ||||||||||||||
"": { | ||||||||||||||
"err": [ | ||||||||||||||
[ | ||||||||||||||
"invalid_data", | ||||||||||||||
{ | ||||||||||||||
"reason": "span ID does not exist" | ||||||||||||||
} | ||||||||||||||
] | ||||||||||||||
], | ||||||||||||||
"val": "dddddddddddddddd" | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
}, | ||||||||||||||
"2": { | ||||||||||||||
"parent_span_id": { | ||||||||||||||
"": { | ||||||||||||||
"err": [ | ||||||||||||||
[ | ||||||||||||||
"invalid_data", | ||||||||||||||
{ | ||||||||||||||
"reason": "span ID does not exist" | ||||||||||||||
} | ||||||||||||||
] | ||||||||||||||
], | ||||||||||||||
"val": "eeeeeeeeeeeeeeee" | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
"###); | ||||||||||||||
} | ||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we not put this inside the conditional block below?