fix(relay): Correctly serialize UUID without hypens#4673
fix(relay): Correctly serialize UUID without hypens#4673iambriccardo merged 33 commits intomasterfrom
Conversation
relay-sampling/src/evaluation.rs
Outdated
| fn pseudo_random_from_uuid(id: Uuid) -> f64 { | ||
| let big_seed = id.as_u128(); | ||
| let mut generator = Pcg32::new((big_seed >> 64) as u64, big_seed as u64); | ||
| fn pseudo_random_from_seed(seed: u128) -> f64 { |
There was a problem hiding this comment.
Decided to just remove our dependence from the Uuid and simply use u128.
There was a problem hiding this comment.
This can stay as uuid now, since the trace id just wraps one now? That should make sure we don't accidentally mess up the seed
There was a problem hiding this comment.
Yeah, I was thinking of keeping it since it makes the whole thing depend on a much simpler piece of code, but if you feel like reducing the surface for possible errors, I can revert.
| /// - The trace ID string is not exactly 32 characters | ||
| /// | ||
| /// Otherwise, it returns the trace ID as an u128 integer. | ||
| pub fn as_u128(&self) -> u128 { |
There was a problem hiding this comment.
This conversion assumes that for each byte we can take out a hex value from 0-15. This is not super efficient because we could technically represent the same data with 16 bytes instead of 32, but for the sake of simplicity (given the Empty, IntoValue and ProcessValue), we will keep the type as a String. We might want to make this more efficient in the future.
There was a problem hiding this comment.
As discussed, probably worth switching all of this now to [u8; 16] like EventId
| let jsons = [ | ||
| r#"{ | ||
| "trace_id": "00000000-0000-0000-0000-000000000000", | ||
| "trace_id": "67e5504410b1426f9247bb680e5fe0c8", |
There was a problem hiding this comment.
Haven't taken a full look, so maybe it's there already, it would be good if we keep at least one test with the old format.
There was a problem hiding this comment.
A trace id is not valid if it's made up of only zeros: https://www.w3.org/TR/trace-context/#trace-id.
There was a problem hiding this comment.
I will keep some tests with the -.
relay-monitors/src/lib.rs
Outdated
| #[serde(default, serialize_with = "uuid_simple")] | ||
| pub check_in_id: Uuid, | ||
| #[serde(default)] | ||
| pub check_in_id: TraceId, |
There was a problem hiding this comment.
I don't know if we can change that, check ins may actually use a uuid here.
There was a problem hiding this comment.
Isn't out format compatible with a UUID? But yeah, I should use a different type name, it was my bad for keeping TraceId.
I am also fine to keep Uuid.
There was a problem hiding this comment.
Oh it was not even serialized with hyphens before, right? Then it's safe to switch, nice. Maybe just make it a type alias to CheckInId, that should be fine.
There was a problem hiding this comment.
Yeah, this is why it had the simple serializer. Because the simple variant has no -.
| /// - The trace ID string is not exactly 32 characters | ||
| /// | ||
| /// Otherwise, it returns the trace ID as an u128 integer. | ||
| pub fn as_u128(&self) -> u128 { |
There was a problem hiding this comment.
As discussed, probably worth switching all of this now to [u8; 16] like EventId
| fn test_untrimmable_fields() { | ||
| let original_description = "a".repeat(819163); | ||
| let original_trace_id = TraceId("b".repeat(48)); | ||
| let original_trace_id = TraceId::parse_str(&"b".repeat(32)).unwrap(); |
There was a problem hiding this comment.
We have to make this valid, otherwise it will not compile.
relay-spans/src/span.rs
Outdated
| )), | ||
| timestamp: Timestamp(end_timestamp).into(), | ||
| trace_id: TraceId(trace_id).into(), | ||
| trace_id: TraceId::parse_str_annotated(&trace_id), |
There was a problem hiding this comment.
I decided to implement annotated in case of errors instead of using Result, so that we can have in the payload the errors.
My only worry is what will happen upstream in case no trace_id is set.
| contexts.add(TraceContext { | ||
| trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())), | ||
| trace_id: Annotated::new( | ||
| TraceId::parse_str("3c79f60c11214eb38604f4ae0781bfb2").unwrap(), |
There was a problem hiding this comment.
| TraceId::parse_str("3c79f60c11214eb38604f4ae0781bfb2").unwrap(), | |
| "3c79f60c11214eb38604f4ae0781bfb2".parse().unwrap(), |
Since it implements FromStr
There was a problem hiding this comment.
Is there a particular reason to change the ID itself here?
| match value { | ||
| Annotated(Some(Value::String(mut value)), mut meta) => { | ||
| if !is_hex_string(&value, 32) || value.bytes().all(|x| x == b'0') { | ||
| Annotated(Some(Value::String(value)), mut meta) => match FromStr::from_str(&value) { |
There was a problem hiding this comment.
Usually you'd just value.parse() here, instead of going through the trait
relay-sampling/src/evaluation.rs
Outdated
| fn pseudo_random_from_uuid(id: Uuid) -> f64 { | ||
| let big_seed = id.as_u128(); | ||
| let mut generator = Pcg32::new((big_seed >> 64) as u64, big_seed as u64); | ||
| fn pseudo_random_from_seed(seed: u128) -> f64 { |
There was a problem hiding this comment.
This can stay as uuid now, since the trace id just wraps one now? That should make sure we don't accidentally mess up the seed
| contexts.add(TraceContext { | ||
| trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())), | ||
| trace_id: Annotated::new( | ||
| TraceId::parse_str("3c79f60c11214eb38604f4ae0781bfb2").unwrap(), |
There was a problem hiding this comment.
Is there a particular reason to change the ID itself here?
| ), | ||
| trace_id: TraceId( | ||
| "ff62a8b040f340bda5d830223def1d81", | ||
| ff62a8b0-40f3-40bd-a5d8-30223def1d81, |
There was a problem hiding this comment.
Why are -s being introduced in these snapshots?
There was a problem hiding this comment.
This is because we are now using a Uuid whose debug/display impl renders with -. This is the drawback of using the Uuid type instead of a custom type that does the validation.
There was a problem hiding this comment.
We could overwrite the debug impl, I think I like that, then the snapshots would be matching.
There was a problem hiding this comment.
Yep, I will do this! Good idea, so that it keeps being consistent, should we also use the ""? Might be unnecessary but idk
There was a problem hiding this comment.
Oh, duh, I missed that this is Debug, not serialization. Still, I would also agree with overriding the Debug impl.
There was a problem hiding this comment.
TraceId(abcd...) should be fine, don't really see a need for the " but nbd either way
|
@loewenheim that was my bad, it was messed up during a codebase wide rename. |
This PR fixes a problem where the
trace_idwas serialized differently in the_dscand other parts of the event payload. The issue stemmed from the fact thatUuidis, by default, serialized with hyphens.The solution is to use the
TraceIdtype across the DSC and other parts of the code (relay-monitors) to ensure consistent implementation. The newTraceIdis also using aUuidinside even though the format is a subset of a uuid v4.