Skip to content

Commit

Permalink
Introduce SerializableNonZeroU64
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Nov 20, 2024
1 parent 6b50803 commit 1fd880d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 17 additions & 2 deletions libs/telemetry/src/capturing/ng/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@ use tracing::Level;

use crate::models::{LogLevel, SpanKind, TraceSpan};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct SerializableNonZeroU64(NonZeroU64);

impl Serialize for SerializableNonZeroU64 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
// Serialize as string to preserve full u64 precision in JavaScript. Otherwise values
// larger than 2^53 - 1 will be parsed as floats on the client side, making it possible for
// IDs to collide.
self.0.to_string().serialize(serializer)
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct SpanId(NonZeroU64);
pub struct SpanId(SerializableNonZeroU64);

impl From<&tracing::span::Id> for SpanId {
fn from(id: &tracing::span::Id) -> Self {
Self(id.into_non_zero_u64())
Self(SerializableNonZeroU64(id.into_non_zero_u64()))
}
}

Expand Down
6 changes: 3 additions & 3 deletions libs/telemetry/src/capturing/ng/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ mod tests {
/// IDs (as libtest runs every test in its own thread).
fn redact_id() -> Redaction {
thread_local! {
static SPAN_ID_TO_SEQUENTIAL_ID: RefCell<HashMap<u64, u64>> = <_>::default();
static SPAN_ID_TO_SEQUENTIAL_ID: RefCell<HashMap<String, u64>> = <_>::default();
static NEXT_ID: RefCell<u64> = const { RefCell::new(1) };
}

fn redact_recursive(value: Content) -> Content {
match value {
Content::NewtypeStruct("SpanId", ref nested) => match **nested {
Content::U64(original_id) => SPAN_ID_TO_SEQUENTIAL_ID.with_borrow_mut(|map| {
let id = map.entry(original_id).or_insert_with(|| {
Content::String(ref original_id) => SPAN_ID_TO_SEQUENTIAL_ID.with_borrow_mut(|map| {
let id = map.entry(original_id.clone()).or_insert_with(|| {
NEXT_ID.with_borrow_mut(|next_id| {
let id = *next_id;
*next_id += 1;
Expand Down

0 comments on commit 1fd880d

Please sign in to comment.