opentelemetry_spanprocessor_any/sdk/trace/id_generator/
mod.rs

1//! Id Generator
2pub(super) mod aws;
3
4use crate::trace::{SpanId, TraceId};
5use rand::{rngs, Rng};
6use std::cell::RefCell;
7
8/// Default [`crate::trace::IdGenerator`] implementation.
9/// Generates Trace and Span ids using a random number generator.
10#[derive(Clone, Debug, Default)]
11pub struct IdGenerator {
12    _private: (),
13}
14
15impl crate::trace::IdGenerator for IdGenerator {
16    /// Generate new `TraceId` using thread local rng
17    fn new_trace_id(&self) -> TraceId {
18        CURRENT_RNG.with(|rng| TraceId::from(rng.borrow_mut().gen::<[u8; 16]>()))
19    }
20
21    /// Generate new `SpanId` using thread local rng
22    fn new_span_id(&self) -> SpanId {
23        CURRENT_RNG.with(|rng| SpanId::from(rng.borrow_mut().gen::<[u8; 8]>()))
24    }
25}
26
27thread_local! {
28    /// Store random number generator for each thread
29    static CURRENT_RNG: RefCell<rngs::ThreadRng> = RefCell::new(rngs::ThreadRng::default());
30}