use opentelemetry::trace::{SpanId, TraceId};
use rand::{rngs, Rng, SeedableRng};
use std::cell::RefCell;
use std::fmt;
pub trait IdGenerator: Send + Sync + fmt::Debug {
fn new_trace_id(&self) -> TraceId;
fn new_span_id(&self) -> SpanId;
}
#[derive(Clone, Debug, Default)]
pub struct RandomIdGenerator {
_private: (),
}
impl IdGenerator for RandomIdGenerator {
fn new_trace_id(&self) -> TraceId {
CURRENT_RNG.with(|rng| TraceId::from(rng.borrow_mut().gen::<u128>()))
}
fn new_span_id(&self) -> SpanId {
CURRENT_RNG.with(|rng| SpanId::from(rng.borrow_mut().gen::<u64>()))
}
}
thread_local! {
static CURRENT_RNG: RefCell<rngs::SmallRng> = RefCell::new(rngs::SmallRng::from_entropy());
}