Skip to content

Commit 782f9db

Browse files
authored
cheaper to generate random primitives than byte arrays (#1091)
1 parent b253945 commit 782f9db

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

opentelemetry-api/src/trace/span_context.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ impl TraceId {
120120
}
121121
}
122122

123-
impl From<[u8; 16]> for TraceId {
124-
fn from(bytes: [u8; 16]) -> Self {
125-
TraceId::from_bytes(bytes)
123+
impl From<u128> for TraceId {
124+
fn from(value: u128) -> Self {
125+
TraceId(value)
126126
}
127127
}
128128

@@ -181,9 +181,9 @@ impl SpanId {
181181
}
182182
}
183183

184-
impl From<[u8; 8]> for SpanId {
185-
fn from(bytes: [u8; 8]) -> Self {
186-
SpanId::from_bytes(bytes)
184+
impl From<u64> for SpanId {
185+
fn from(value: u64) -> Self {
186+
SpanId(value)
187187
}
188188
}
189189

opentelemetry-contrib/src/trace/propagator/binary/binary_propagator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl BinaryFormat for BinaryPropagator {
6969
}
7070

7171
let span_context = SpanContext::new(
72-
TraceId::from(trace_id),
73-
SpanId::from(span_id),
72+
TraceId::from_bytes(trace_id),
73+
SpanId::from_bytes(span_id),
7474
TraceFlags::new(trace_flags),
7575
true,
7676
// TODO traceparent and tracestate should both begin with a 0 byte, figure out how to differentiate

opentelemetry-datadog/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,14 @@ mod propagator {
201201
fn extract_trace_id(&self, trace_id: &str) -> Result<TraceId, ExtractError> {
202202
trace_id
203203
.parse::<u64>()
204-
.map(|id| TraceId::from((id as u128).to_be_bytes()))
204+
.map(|id| TraceId::from(id as u128))
205205
.map_err(|_| ExtractError::TraceId)
206206
}
207207

208208
fn extract_span_id(&self, span_id: &str) -> Result<SpanId, ExtractError> {
209209
span_id
210210
.parse::<u64>()
211-
.map(|id| SpanId::from(id.to_be_bytes()))
211+
.map(SpanId::from)
212212
.map_err(|_| ExtractError::SpanId)
213213
}
214214

opentelemetry-sdk/src/trace/id_generator/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ pub struct RandomIdGenerator {
2525

2626
impl IdGenerator for RandomIdGenerator {
2727
fn new_trace_id(&self) -> TraceId {
28-
CURRENT_RNG.with(|rng| TraceId::from(rng.borrow_mut().gen::<[u8; 16]>()))
28+
CURRENT_RNG.with(|rng| TraceId::from(rng.borrow_mut().gen::<u128>()))
2929
}
3030

3131
fn new_span_id(&self) -> SpanId {
32-
CURRENT_RNG.with(|rng| SpanId::from(rng.borrow_mut().gen::<[u8; 8]>()))
32+
CURRENT_RNG.with(|rng| SpanId::from(rng.borrow_mut().gen::<u64>()))
3333
}
3434
}
3535

opentelemetry-sdk/src/trace/sampler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ mod tests {
326326
None
327327
};
328328

329-
let trace_id = TraceId::from(rng.gen::<[u8; 16]>());
329+
let trace_id = TraceId::from(rng.gen::<u128>());
330330
if sampler
331331
.should_sample(
332332
parent_context.as_ref(),

0 commit comments

Comments
 (0)