Skip to content

Commit e4ca73c

Browse files
committed
revisions part 2
1 parent eec9836 commit e4ca73c

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

libdd-trace-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ path = "benches/main.rs"
1919

2020
[dependencies]
2121
anyhow = "1.0"
22+
base64 = "0.22"
2223
hyper = { workspace = true, optional = true, default-features = false }
2324
"http" = "1"
2425
"http-body" = "1"

libdd-trace-utils/src/otlp_encoder/json_types.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//! dependency for that purpose:
1818
//! <https://github.com/open-telemetry/opentelemetry-rust/tree/opentelemetry-proto-0.28.0/opentelemetry-proto>
1919
20+
use base64::{engine::general_purpose::STANDARD, Engine as _};
2021
use serde::{Serialize, Serializer};
2122

2223
/// Top-level OTLP trace export request (ExportTraceServiceRequest).
@@ -127,14 +128,19 @@ pub enum AnyValue {
127128
#[serde(serialize_with = "serialize_int_value_as_string")]
128129
IntValue(i64),
129130
DoubleValue(f64),
130-
BytesValue(String),
131+
#[serde(serialize_with = "serialize_bytes_as_base64")]
132+
BytesValue(Vec<u8>),
131133
ArrayValue(ArrayValue),
132134
}
133135

134136
fn serialize_int_value_as_string<S: Serializer>(v: &i64, s: S) -> Result<S::Ok, S::Error> {
135137
s.serialize_str(&v.to_string())
136138
}
137139

140+
fn serialize_bytes_as_base64<S: Serializer>(v: &[u8], s: S) -> Result<S::Ok, S::Error> {
141+
s.serialize_str(&STANDARD.encode(v))
142+
}
143+
138144
/// OTLP array value — wraps a list of [`AnyValue`] items.
139145
#[derive(Debug, Serialize)]
140146
pub struct ArrayValue {

libdd-trace-utils/src/otlp_encoder/mapper.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,43 @@ mod tests {
408408
let rate = rate_kv["value"]["doubleValue"].as_f64().unwrap();
409409
assert!((rate - std::f64::consts::PI).abs() < 1e-9);
410410
}
411+
412+
#[test]
413+
fn test_128bit_trace_id_from_dd_p_tid() {
414+
// When "_dd.p.tid" is present it supplies the high 64 bits of the trace ID.
415+
// Low 64 bits come from span.trace_id; the two are concatenated to form a 128-bit hex ID.
416+
let resource_info = OtlpResourceInfo::default();
417+
let mut span: Span<BytesData> = Span {
418+
trace_id: 0xD269B633813FC60C_u128, // low 64 bits
419+
span_id: 1,
420+
name: libdd_tinybytes::BytesString::from_static("s"),
421+
start: 0,
422+
duration: 1,
423+
..Default::default()
424+
};
425+
span.meta.insert(
426+
"_dd.p.tid".into(),
427+
libdd_tinybytes::BytesString::from_static("5b8efff798038103"),
428+
);
429+
let req = map_traces_to_otlp(vec![vec![span]], &resource_info);
430+
let otlp_span = &req.resource_spans[0].scope_spans[0].spans[0];
431+
assert_eq!(otlp_span.trace_id, "5b8efff798038103d269b633813fc60c");
432+
}
433+
434+
#[test]
435+
fn test_128bit_trace_id_without_dd_p_tid() {
436+
// When "_dd.p.tid" is absent the high 64 bits default to zero.
437+
let resource_info = OtlpResourceInfo::default();
438+
let span: Span<BytesData> = Span {
439+
trace_id: 0xD269B633813FC60C_u128,
440+
span_id: 1,
441+
name: libdd_tinybytes::BytesString::from_static("s"),
442+
start: 0,
443+
duration: 1,
444+
..Default::default()
445+
};
446+
let req = map_traces_to_otlp(vec![vec![span]], &resource_info);
447+
let otlp_span = &req.resource_spans[0].scope_spans[0].spans[0];
448+
assert_eq!(otlp_span.trace_id, "0000000000000000d269b633813fc60c");
449+
}
411450
}

0 commit comments

Comments
 (0)