Skip to content

Commit 661dc37

Browse files
authored
ref: impl TryFrom<&[u8]> for TraceId (#4759)
This means we can read `TraceId`s from bytes directly instead of first hex encoding them and then parsing them again.
1 parent b1e73a8 commit 661dc37

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

relay-event-schema/src/protocol/contexts/trace.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use relay_protocol::{
2-
Annotated, Array, Empty, Error, ErrorKind, FromValue, IntoValue, Object, SkipSerialization,
3-
Value,
2+
Annotated, Array, Empty, Error, FromValue, IntoValue, Object, SkipSerialization, Value,
43
};
54
use serde::{Serialize, Serializer};
65
use std::fmt;
@@ -29,23 +28,25 @@ use crate::protocol::{OperationType, OriginType, SpanData, SpanLink, SpanStatus}
2928
#[derive(Clone, Copy, Default, PartialEq, Empty, ProcessValue)]
3029
pub struct TraceId(Uuid);
3130

32-
impl TraceId {
33-
pub fn parse_str(input: &str) -> Result<TraceId, Error> {
34-
Self::from_str(input)
35-
}
36-
}
37-
3831
relay_common::impl_str_serde!(TraceId, "a trace identifier");
3932

4033
impl FromStr for TraceId {
4134
type Err = Error;
4235

4336
fn from_str(s: &str) -> Result<Self, Self::Err> {
44-
Uuid::parse_str(s).map(Into::into).map_err(|_| {
45-
Error::with(ErrorKind::InvalidData, |e| {
46-
e.insert("reason", "the trace id is not valid");
47-
})
48-
})
37+
Uuid::parse_str(s)
38+
.map(Into::into)
39+
.map_err(|_| Error::invalid("the trace id is not valid"))
40+
}
41+
}
42+
43+
impl TryFrom<&[u8]> for TraceId {
44+
type Error = Error;
45+
46+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
47+
let uuid =
48+
Uuid::from_slice(value).map_err(|_| Error::invalid("the trace id is not valid"))?;
49+
Ok(Self(uuid))
4950
}
5051
}
5152

relay-ourlogs/src/ourlog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn otel_to_sentry_log(otel_log: OtelLog) -> Result<OurLog, Error> {
3636
..
3737
} = otel_log;
3838
let span_id = SpanId(hex::encode(span_id));
39-
let trace_id: TraceId = hex::encode(trace_id).parse()?;
39+
let trace_id = TraceId::try_from(trace_id.as_slice())?;
4040
let timestamp = Utc.timestamp_nanos(time_unix_nano as i64);
4141
let body = body
4242
.and_then(|v| v.value)

relay-spans/src/otel_to_sentry_v2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn otel_to_sentry_span(otel_span: OtelSpan) -> Result<SentrySpanV2, Error> {
4747
let end_timestamp = Utc.timestamp_nanos(end_time_unix_nano as i64);
4848

4949
let span_id: SpanId = hex::encode(span_id).parse()?;
50-
let trace_id: TraceId = hex::encode(trace_id).parse()?;
50+
let trace_id = TraceId::try_from(trace_id.as_slice())?;
5151
let parent_span_id = match parent_span_id.as_slice() {
5252
&[] => None,
5353
_ => Some(hex::encode(parent_span_id).parse()?),

0 commit comments

Comments
 (0)