Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"meta": {
"env": "test-env",
"service": "test-service",
"runtime-id": "test-runtime-id-value"
"runtime-id": "test-runtime-id-value",
"events": "[{\"time_unix_nano\":1727211691770715042,\"name\":\"test_span\",\"attributes\":{}},{\"time_unix_nano\":1727211691770716000,\"name\":\"exception\",\"attributes\":{\"exception.count\":1,\"exception.escaped\":true,\"exception.lines\":[\" File \\\"<string>\\\", line 1, in <module>\",\" File \\\"<string>\\\", line 1, in divide\"],\"exception.message\":\"Cannot divide by zero\",\"exception.version\":4.2}}]"
},
"metrics": {
"_dd_metric1": 1.0,
Expand All @@ -34,7 +35,8 @@
"meta": {
"env": "test-env",
"runtime-id": "test-runtime-id-value",
"service": "test-service"
"service": "test-service",
"_dd.span_links": "[{\"trace_id\":\"0000000000000000c151df7d6ee5e2d6\",\"span_id\":\"a3978fb9b92502a8\",\"attributes\":{\"link.name\":\"Job #123\"}},{\"trace_id\":\"527ccbd68a74d57ea918bf567eec151d\",\"span_id\":\"c08c967f0e5e7b0a\"}]"
},
"metrics": {},
"type": ""
Expand Down
14 changes: 14 additions & 0 deletions libdd-trace-utils/src/span/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ use std::{fmt, ptr};
/// from a static str and check if the string is empty.
pub trait SpanText: Debug + Eq + Hash + Borrow<str> + Serialize + Default {
fn from_static_str(value: &'static str) -> Self;

/// Copies this text into an owned [`BytesString`].
///
/// Used by the v0.5 conversion, whose shared dictionary always owns its strings so it
/// can hold both interned span text and dynamically-built JSON (span links / events).
/// The default copies the bytes; owned text types (e.g. `BytesString`) should override
/// with a cheaper reference-counted clone.
fn to_bytes_string(&self) -> BytesString {
BytesString::from(<Self as Borrow<str>>::borrow(self).to_string())
}
}

impl SpanText for &str {
Expand All @@ -36,6 +46,10 @@ impl SpanText for BytesString {
fn from_static_str(value: &'static str) -> Self {
BytesString::from_static(value)
}

fn to_bytes_string(&self) -> BytesString {
self.clone()
}
}

pub trait SpanBytes: Debug + Eq + Hash + Borrow<[u8]> + Serialize + Default {
Expand Down
20 changes: 12 additions & 8 deletions libdd-trace-utils/src/span/v05/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ use crate::span::SpanText;
#[derive(Debug, Clone)]
pub struct SharedDict<T> {
/// Map strings with their index and keep insertion order(O(1) retrieval complexity).
map: indexmap::IndexSet<T>,
pub(crate) map: indexmap::IndexMap<T, ()>,
}

impl<T: SpanText> serde::Serialize for SharedDict<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_seq(self.map.iter().map(|entry| -> &str { entry.borrow() }))
serializer.collect_seq(
self.map
.iter()
.map(|(entry, ())| -> &str { entry.borrow() }),
)
}
}

Expand All @@ -32,7 +36,7 @@ impl<T: SpanText> SharedDict<T> {
(index).try_into()
} else {
let index = self.map.len();
self.map.insert(s);
self.map.insert(s, ());
index.try_into()
}
}
Expand All @@ -43,14 +47,14 @@ impl<T: SpanText> SharedDict<T> {
}

pub fn iter(&self) -> impl Iterator<Item = &T> {
self.map.iter()
self.map.keys()
}
}

impl<T: SpanText> Default for SharedDict<T> {
fn default() -> Self {
Self {
map: indexmap::indexset! {T::default()},
map: indexmap::indexmap! {T::default() => ()},
}
}
}
Expand Down Expand Up @@ -84,8 +88,8 @@ mod tests {

assert_eq!(dict.map.len(), 3);

assert_eq!(dict.map[0].as_str(), "");
assert_eq!(dict.map[1].as_str(), "foo");
assert_eq!(dict.map[2].as_str(), "bar");
assert_eq!(dict.map.get_index(0).unwrap().0.as_str(), "");
assert_eq!(dict.map.get_index(1).unwrap().0.as_str(), "foo");
assert_eq!(dict.map.get_index(2).unwrap().0.as_str(), "bar");
}
}
Loading
Loading