Skip to content

Commit 7dd922e

Browse files
committed
chore: relax str to [u8] for thread ctx attrs
1 parent c40f314 commit 7dd922e

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

libdd-profiling/src/otel_thread_ctx.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
//!
4141
//! let trace_id = [0u8; 16];
4242
//! let span_id = [1u8; 8];
43-
//! let attrs: &[(u8, &str)] = &[(0, "GET"), (1, "/api/v1")];
43+
//! let attrs: &[(u8, &[u8])] = &[(0, "GET"), (1, "/api/v1")];
4444
//!
4545
//! // Publish a new context and save the previously attached one (if any).
4646
//! let ctx = ThreadContext::new(trace_id, span_id, attrs);
@@ -176,7 +176,7 @@ pub mod linux {
176176
trace_id: [u8; 16],
177177
span_id: [u8; 8],
178178
local_root_span_id: [u8; 8],
179-
attrs: &[(u8, &str)],
179+
attrs: &[(u8, &[u8])],
180180
) -> Self {
181181
const { assert!(size_of::<ThreadContextRecord>() == 640) }
182182

@@ -210,16 +210,15 @@ pub mod linux {
210210
/// recovery would require us to be able to rollback to the previous attributes which would
211211
/// hurt the happy path, or leave the record in a inconsistent state. Another possibility
212212
/// would be to error out and reset the record in that situation.
213-
fn set_attrs(&mut self, local_root_span_id: [u8; 8], attributes: &[(u8, &str)]) -> bool {
213+
fn set_attrs(&mut self, local_root_span_id: [u8; 8], attributes: &[(u8, &[u8])]) -> bool {
214214
let mut fully_encoded = true;
215215

216216
self.attrs_data[0] = ROOT_SPAN_KEY_INDEX;
217217
self.attrs_data[1] = 8;
218218
self.attrs_data[2..10].copy_from_slice(local_root_span_id.as_slice());
219219
let mut offset = 10;
220220

221-
for &(key_index, val) in attributes {
222-
let val_bytes = val.as_bytes();
221+
for &(key_index, val_bytes) in attributes {
223222
let val_len = val_bytes.len();
224223
let val_len = if val_len > 255 {
225224
fully_encoded = false;
@@ -235,7 +234,7 @@ pub mod linux {
235234
}
236235

237236
self.attrs_data[offset] = key_index;
238-
// `val_len <= 255` thanks to the `min()`
237+
// `val_len <= 255` from the check above
239238
self.attrs_data[offset + 1] = val_len as u8;
240239
self.attrs_data[offset + 2..offset + 2 + val_len]
241240
.copy_from_slice(&val_bytes[..val_len]);
@@ -280,7 +279,7 @@ pub mod linux {
280279
trace_id: [u8; 16],
281280
span_id: [u8; 8],
282281
local_root_span_id: [u8; 8],
283-
attrs: &[(u8, &str)],
282+
attrs: &[(u8, &[u8])],
284283
) -> Self {
285284
Self::from(ThreadContextRecord::new(
286285
trace_id,
@@ -364,7 +363,7 @@ pub mod linux {
364363
trace_id: [u8; 16],
365364
span_id: [u8; 8],
366365
local_root_span_id: [u8; 8],
367-
attrs: &[(u8, &str)],
366+
attrs: &[(u8, &[u8])],
368367
) {
369368
let slot = get_tls_slot();
370369

@@ -482,7 +481,7 @@ pub mod linux {
482481
#[cfg_attr(miri, ignore)]
483482
fn attribute_encoding_basic() {
484483
let root_span_id = [0u8; 8];
485-
let attrs: &[(u8, &str)] = &[(1, "GET"), (2, "/api/v1")];
484+
let attrs: &[(u8, &[u8])] = &[(1, b"GET"), (2, b"/api/v1")];
486485
ThreadContext::new([0u8; 16], [0u8; 8], root_span_id, attrs).attach();
487486

488487
let ptr = read_tls_context_ptr();
@@ -513,14 +512,14 @@ pub mod linux {
513512
// Two such entries: 514 bytes, plus root_span_id: 524.
514513
// A third entry of 100 chars would need 102 bytes, bringing the total to 626 > 612, so
515514
// the third entry must be dropped.
516-
let val_a = "a".repeat(255); // 257 bytes encoded
517-
let val_b = "b".repeat(255); // 257 bytes encoded → 514 total
518-
let val_c = "c".repeat(100); // 102 bytes encoded → 626 total: must be dropped
519-
520-
let attrs: &[(u8, &str)] = &[
521-
(1, val_a.as_str()),
522-
(2, val_b.as_str()),
523-
(3, val_c.as_str()),
515+
let val_a = b"a".repeat(255); // 257 bytes encoded
516+
let val_b = b"b".repeat(255); // 257 bytes encoded → 514 total
517+
let val_c = b"c".repeat(100); // 102 bytes encoded → 626 total: must be dropped
518+
519+
let attrs: &[(u8, &[u8])] = &[
520+
(1, val_a.as_slice()),
521+
(2, val_b.as_slice()),
522+
(3, val_c.as_slice()),
524523
];
525524

526525
ThreadContext::new([0u8; 16], [0u8; 8], [0u8; 8], attrs).attach();
@@ -549,7 +548,7 @@ pub mod linux {
549548
let root_span_id2 = [0x79u8; 8];
550549

551550
// Updating before any context is attached should be equivalent to `attach()`
552-
ThreadContext::update(trace_id1, span_id1, root_span_id1, &[(0, "v1")]);
551+
ThreadContext::update(trace_id1, span_id1, root_span_id1, &[(0, b"v1")]);
553552

554553
let ptr_before = read_tls_context_ptr();
555554
assert!(!ptr_before.is_null());
@@ -564,7 +563,7 @@ pub mod linux {
564563
assert_eq!(record.attrs_data[11], 2);
565564
assert_eq!(&record.attrs_data[12..14], b"v1");
566565

567-
ThreadContext::update(trace_id2, span_id2, root_span_id2, &[(0, "v2")]);
566+
ThreadContext::update(trace_id2, span_id2, root_span_id2, &[(0, b"v2")]);
568567

569568
let ptr_after = read_tls_context_ptr();
570569
assert_eq!(
@@ -604,8 +603,8 @@ pub mod linux {
604603
#[test]
605604
#[cfg_attr(miri, ignore)]
606605
fn long_value_capped_at_255_bytes() {
607-
let long_val = "a".repeat(300);
608-
ThreadContext::new([0u8; 16], [0u8; 8], [0u8; 8], &[(0, long_val.as_str())]).attach();
606+
let long_val = b"a".repeat(300);
607+
ThreadContext::new([0u8; 16], [0u8; 8], [0u8; 8], &[(0, long_val.as_slice())]).attach();
609608

610609
let ptr = read_tls_context_ptr();
611610
assert!(!ptr.is_null());

0 commit comments

Comments
 (0)