Skip to content

Instantly share code, notes, and snippets.

@esemeniuc
Created June 14, 2024 20:22
Show Gist options
  • Save esemeniuc/ecccbd8b9f544a85e362f60084c83bbf to your computer and use it in GitHub Desktop.
Save esemeniuc/ecccbd8b9f544a85e362f60084c83bbf to your computer and use it in GitHub Desktop.
traceparent header parser
/// See https://w3c.github.io/trace-context-binary/#de-serialization-of-traceparent
fn parse_traceparent(bytes: &[u8]) -> Result<SpanContext, &'static str> {
if bytes.len() != 29 {
return Err("Invalid traceparent length. Must be 29 bytes.");
}
if bytes[0] != 0 {
return Err("Unsupported traceparent version.");
}
if bytes[1] != 0 {
return Err("Unsupported trace id");
}
let trace_id = TraceId::from_slice(&bytes[2..18]);
if bytes[18] != 1 {
return Err("Unsupported parent id");
}
let parent_id = SpanId::from_slice(&bytes[19..27]);
if bytes[27] != 2 {
return Err("Unsupported trace flags");
}
let trace_flags = TraceFlags::from_u8(bytes[28]);
Ok(SpanContext::new(trace_id, parent_id, trace_flags, true,TraceState::default()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment