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

ref(protocol): Represent SpanId and TraceId as bytes #3535

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion relay-event-normalization/src/clock_drift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion relay-event-normalization/src/normalize/breakdowns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ mod tests {
description: Annotated::new("desc".to_owned()),
op: Annotated::new(op_name),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74052".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
status: Annotated::new(SpanStatus::Ok),
..Default::default()
})
Expand Down
10 changes: 5 additions & 5 deletions relay-event-normalization/src/normalize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand Down Expand Up @@ -1341,7 +1341,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand All @@ -1355,7 +1355,7 @@ mod tests {
Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().into(),
),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),

..Default::default()
})]),
Expand Down Expand Up @@ -1409,7 +1409,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand All @@ -1423,7 +1423,7 @@ mod tests {
Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().into(),
),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),

..Default::default()
})]),
Expand Down
56 changes: 28 additions & 28 deletions relay-event-normalization/src/normalize/span/exclusive_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ mod tests {
assert_eq!(
extract_span_exclusive_times(&event),
HashMap::from_iter([
(&SpanId("aaaaaaaaaaaaaaaa".to_string()), 1123.0),
(&SpanId("bbbbbbbbbbbbbbbb".to_string()), 3000.0),
(&SpanId("cccccccccccccccc".to_string()), 2500.0),
(&SpanId("dddddddddddddddd".to_string()), 1877.0)
(&SpanId(0xaaaaaaaaaaaaaaaa), 1123.0),
(&SpanId(0xbbbbbbbbbbbbbbbb), 3000.0),
(&SpanId(0xcccccccccccccccc), 2500.0),
(&SpanId(0xdddddddddddddddd), 1877.0)
]),
);
}
Expand Down Expand Up @@ -299,10 +299,10 @@ mod tests {
assert_eq!(
extract_span_exclusive_times(&event),
HashMap::from_iter([
(&SpanId("aaaaaaaaaaaaaaaa".to_string()), 4000.0),
(&SpanId("bbbbbbbbbbbbbbbb".to_string()), 400.0),
(&SpanId("cccccccccccccccc".to_string()), 400.0),
(&SpanId("dddddddddddddddd".to_string()), 200.0),
(&SpanId(0xaaaaaaaaaaaaaaaa), 4000.0),
(&SpanId(0xbbbbbbbbbbbbbbbb), 400.0),
(&SpanId(0xcccccccccccccccc), 400.0),
(&SpanId(0xdddddddddddddddd), 200.0),
])
);
}
Expand Down Expand Up @@ -346,10 +346,10 @@ mod tests {
assert_eq!(
extract_span_exclusive_times(&event),
HashMap::from_iter([
(&SpanId("aaaaaaaaaaaaaaaa".to_string()), 4000.0),
(&SpanId("bbbbbbbbbbbbbbbb".to_string()), 400.0),
(&SpanId("cccccccccccccccc".to_string()), 400.0),
(&SpanId("dddddddddddddddd".to_string()), 400.0),
(&SpanId(0xaaaaaaaaaaaaaaaa), 4000.0),
(&SpanId(0xbbbbbbbbbbbbbbbb), 400.0),
(&SpanId(0xcccccccccccccccc), 400.0),
(&SpanId(0xdddddddddddddddd), 400.0),
])
);
}
Expand Down Expand Up @@ -393,10 +393,10 @@ mod tests {
assert_eq!(
extract_span_exclusive_times(&event),
HashMap::from_iter([
(&SpanId("aaaaaaaaaaaaaaaa".to_string()), 4000.0),
(&SpanId("bbbbbbbbbbbbbbbb".to_string()), 1000.0),
(&SpanId("cccccccccccccccc".to_string()), 400.0),
(&SpanId("dddddddddddddddd".to_string()), 400.0),
(&SpanId(0xaaaaaaaaaaaaaaaa), 4000.0),
(&SpanId(0xbbbbbbbbbbbbbbbb), 1000.0),
(&SpanId(0xcccccccccccccccc), 400.0),
(&SpanId(0xdddddddddddddddd), 400.0),
])
);
}
Expand Down Expand Up @@ -440,10 +440,10 @@ mod tests {
assert_eq!(
extract_span_exclusive_times(&event),
HashMap::from_iter([
(&SpanId("aaaaaaaaaaaaaaaa".to_string()), 4000.0),
(&SpanId("bbbbbbbbbbbbbbbb".to_string()), 200.0),
(&SpanId("cccccccccccccccc".to_string()), 600.0),
(&SpanId("dddddddddddddddd".to_string()), 600.0),
(&SpanId(0xaaaaaaaaaaaaaaaa), 4000.0),
(&SpanId(0xbbbbbbbbbbbbbbbb), 200.0),
(&SpanId(0xcccccccccccccccc), 600.0),
(&SpanId(0xdddddddddddddddd), 600.0),
])
);
}
Expand Down Expand Up @@ -487,10 +487,10 @@ mod tests {
assert_eq!(
extract_span_exclusive_times(&event),
HashMap::from_iter([
(&SpanId("aaaaaaaaaaaaaaaa".to_string()), 4000.0),
(&SpanId("bbbbbbbbbbbbbbbb".to_string()), 0.0),
(&SpanId("cccccccccccccccc".to_string()), 800.0),
(&SpanId("dddddddddddddddd".to_string()), 800.0),
(&SpanId(0xaaaaaaaaaaaaaaaa), 4000.0),
(&SpanId(0xbbbbbbbbbbbbbbbb), 0.0),
(&SpanId(0xcccccccccccccccc), 800.0),
(&SpanId(0xdddddddddddddddd), 800.0),
])
);
}
Expand Down Expand Up @@ -536,10 +536,10 @@ mod tests {
assert_eq!(
extract_span_exclusive_times(&event),
HashMap::from_iter([
(&SpanId("aaaaaaaaaaaaaaaa".to_string()), 4000.0),
(&SpanId("bbbbbbbbbbbbbbbb".to_string()), 600.0),
(&SpanId("cccccccccccccccc".to_string()), 400.0),
(&SpanId("dddddddddddddddd".to_string()), 400.0),
(&SpanId(0xaaaaaaaaaaaaaaaa), 4000.0),
(&SpanId(0xbbbbbbbbbbbbbbbb), 600.0),
(&SpanId(0xcccccccccccccccc), 400.0),
(&SpanId(0xdddddddddddddddd), 400.0),
])
);
}
Expand Down
14 changes: 7 additions & 7 deletions relay-event-normalization/src/transactions/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand All @@ -430,7 +430,7 @@ mod tests {
start_timestamp: Annotated::new(start.into()),
timestamp: Annotated::new(end.into()),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("db.statement".to_owned()),
..Default::default()
})]),
Expand All @@ -452,7 +452,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
..Default::default()
});
Annotated::new(contexts)
Expand Down Expand Up @@ -487,7 +487,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand Down Expand Up @@ -517,7 +517,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand Down Expand Up @@ -570,7 +570,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand All @@ -584,7 +584,7 @@ mod tests {
Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap().into(),
),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),

..Default::default()
})]),
Expand Down
12 changes: 6 additions & 6 deletions relay-event-normalization/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand All @@ -387,7 +387,7 @@ mod tests {
start_timestamp: Annotated::new(start.into()),
timestamp: Annotated::new(end.into()),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("db.statement".to_owned()),
..Default::default()
})]),
Expand Down Expand Up @@ -582,7 +582,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand Down Expand Up @@ -612,7 +612,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand Down Expand Up @@ -647,7 +647,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand Down Expand Up @@ -685,7 +685,7 @@ mod tests {
let mut contexts = Contexts::new();
contexts.add(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http.server".to_owned()),
..Default::default()
});
Expand Down
33 changes: 14 additions & 19 deletions relay-event-schema/src/protocol/contexts/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,37 @@ impl AsRef<str> for TraceId {
/// A 16-character hex string as described in the W3C trace context spec.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Empty, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct SpanId(pub String);
pub struct SpanId(pub u64);

relay_common::impl_str_serde!(SpanId, "a span identifier");

impl FromStr for SpanId {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(SpanId(s.to_string()))
Ok(SpanId(
u64::from_str_radix(s, 16).map_err(|_| Error::invalid("invalid span id"))?,
))
}
}

impl fmt::Display for SpanId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
write!(f, "{:x}", self.0)
}
}

impl FromValue for SpanId {
fn from_value(value: Annotated<Value>) -> Annotated<Self> {
match value {
Annotated(Some(Value::String(value)), mut meta) => {
if !is_hex_string(&value, 16) || value.bytes().all(|x| x == b'0') {
meta.add_error(Error::invalid("not a valid span id"));
Annotated(Some(Value::String(value)), mut meta) => match SpanId::from_str(&value) {
Ok(value) => Annotated(Some(value), meta),
Err(e) => {
meta.add_error(e);
meta.set_original_value(Some(value));
Annotated(None, meta)
} else {
Annotated(Some(SpanId(value.to_ascii_lowercase())), meta)
}
}
},
Annotated(None, meta) => Annotated(None, meta),
Annotated(Some(value), mut meta) => {
meta.add_error(Error::expected("span id"));
Expand All @@ -84,12 +85,6 @@ impl FromValue for SpanId {
}
}

impl AsRef<str> for SpanId {
fn as_ref(&self) -> &str {
&self.0
}
}

fn is_hex_string(string: &str, len: usize) -> bool {
string.len() == len && string.bytes().all(|b| b.is_ascii_hexdigit())
}
Expand Down Expand Up @@ -296,8 +291,8 @@ mod tests {
}"#;
let context = Annotated::new(Context::Trace(Box::new(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74052".into())),
parent_span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
parent_span_id: Annotated::new(SpanId(0xfa90fdead5f74053)),
op: Annotated::new("http".into()),
status: Annotated::new(SpanStatus::Ok),
exclusive_time: Annotated::new(0.0),
Expand Down Expand Up @@ -349,7 +344,7 @@ mod tests {
}"#;
let context = Annotated::new(Context::Trace(Box::new(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74052".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
..Default::default()
})));

Expand All @@ -368,7 +363,7 @@ mod tests {
}"#;
let context = Annotated::new(Context::Trace(Box::new(TraceContext {
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74052".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
data: Annotated::new(Data {
route: Annotated::new(Route {
name: Annotated::new("HomeRoute".into()),
Expand Down
6 changes: 3 additions & 3 deletions relay-event-schema/src/protocol/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ impl Getter for Span {
"exclusive_time" => self.exclusive_time.value()?.into(),
"description" => self.description.as_str()?.into(),
"op" => self.op.as_str()?.into(),
"span_id" => self.span_id.as_str()?.into(),
"parent_span_id" => self.parent_span_id.as_str()?.into(),
// "span_id" => self.span_id.value()?.to_string().as_deref().into(),
// "parent_span_id" => self.parent_span_id.as_str()?.into(),
"trace_id" => self.trace_id.as_str()?.into(),
"status" => self.status.as_str()?.into(),
"origin" => self.origin.as_str()?.into(),
Expand Down Expand Up @@ -436,7 +436,7 @@ mod tests {
description: Annotated::new("desc".to_owned()),
op: Annotated::new("operation".to_owned()),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74052".into())),
span_id: Annotated::new(SpanId(0xfa90fdead5f74052)),
status: Annotated::new(SpanStatus::Ok),
origin: Annotated::new("auto.http".to_owned()),
measurements: Annotated::new(Measurements(measurements)),
Expand Down
Loading
Loading