Skip to content

Commit 418fbc8

Browse files
committed
high-level api for tracer metadata storage on non-Linux
1 parent 9875e6e commit 418fbc8

2 files changed

Lines changed: 60 additions & 12 deletions

File tree

libdd-library-config-ffi/src/tracer_metadata.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::os::raw::{c_char, c_int};
1111
/// C-compatible representation of an anonymous file handle
1212
#[repr(C)]
1313
pub struct TracerMemfdHandle {
14-
/// File descriptor (relevant only on Linux)
14+
/// File descriptor on Linux; `-1` on other platforms.
1515
pub fd: c_int,
1616
}
1717

@@ -102,21 +102,17 @@ pub unsafe extern "C" fn ddog_tracer_metadata_set(
102102
}
103103
}
104104

105-
/// Serializes the `TracerMetadata` into a platform-specific memory handle (e.g., memfd on Linux).
106-
/// This function also attempts to publish the tracer metadata as an OTel process context
107-
/// separately, but will ignore resulting errors.
105+
/// Stores the `TracerMetadata` using the platform's supported mechanisms. Linux serializes the
106+
/// metadata into a memfd and attempts to publish it as an OTel process context. Other platforms
107+
/// publish only the OTel process context.
108108
///
109109
/// # Safety
110110
/// - `ptr` must be a valid, non-null pointer to a `TracerMetadata`.
111111
///
112112
/// # Returns
113113
/// - On Linux: a `TracerMemfdHandle` containing a raw file descriptor to a memory file.
114-
/// - On unsupported platforms: an error.
114+
/// - On other platforms: a `TracerMemfdHandle` with `fd` set to `-1`.
115115
/// - On failure: propagates any internal errors from the metadata storage process.
116-
///
117-
/// # Platform Support
118-
/// This function currently only supports Linux via `memfd`. On other platforms,
119-
/// it will return an error.
120116
#[no_mangle]
121117
pub unsafe extern "C" fn ddog_tracer_metadata_store(
122118
ptr: *mut TracerMetadata,
@@ -141,8 +137,23 @@ pub unsafe extern "C" fn ddog_tracer_metadata_store(
141137
})
142138
}
143139
#[cfg(not(target_os = "linux"))]
144-
Ok(_) => Err(anyhow::anyhow!("Unsupported platform")),
140+
Ok(_) => Ok(TracerMemfdHandle { fd: -1 }),
145141
Err(err) => Err(err),
146142
};
147143
result.into()
148144
}
145+
146+
#[cfg(all(test, not(target_os = "linux")))]
147+
mod tests {
148+
use super::{ddog_tracer_metadata_store, TracerMetadata};
149+
150+
#[test]
151+
fn store_returns_success_without_a_file_descriptor() {
152+
let mut metadata = TracerMetadata::default();
153+
154+
let handle = unsafe { ddog_tracer_metadata_store(&mut metadata) }.unwrap();
155+
assert_eq!(handle.fd, -1);
156+
157+
libdd_library_config::otel_process_ctx::unpublish().expect("unpublish should succeed");
158+
}
159+
}

libdd-library-config/src/tracer_metadata.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,23 @@ mod linux {
255255

256256
#[cfg(not(target_os = "linux"))]
257257
mod other {
258+
/// Publishes tracer metadata as an OTel process context.
258259
pub fn store_tracer_metadata(
259-
_data: &super::TracerMetadata,
260+
data: &super::TracerMetadata,
260261
) -> anyhow::Result<super::AnonymousFileHandle> {
261-
Ok(super::AnonymousFileHandle::Other(()))
262+
#[cfg(feature = "process-context-writer")]
263+
{
264+
crate::otel_process_ctx::publish(&data.to_otel_process_ctx())?;
265+
Ok(super::AnonymousFileHandle::Other(()))
266+
}
267+
268+
#[cfg(not(feature = "process-context-writer"))]
269+
{
270+
let _ = data;
271+
Err(anyhow::anyhow!(
272+
"storing tracer metadata requires the process-context-writer feature"
273+
))
274+
}
262275
}
263276
}
264277

@@ -309,6 +322,30 @@ mod tests {
309322
assert!(find_extra_attr(&ctx, "threadlocal.attribute_key_map").is_none());
310323
}
311324

325+
#[cfg(all(
326+
not(target_os = "linux"),
327+
feature = "process-context-reader",
328+
feature = "process-context-writer"
329+
))]
330+
#[test]
331+
#[serial_test::serial]
332+
fn store_tracer_metadata_publishes_otel_process_context() {
333+
let metadata = TracerMetadata {
334+
tracer_language: "python".to_owned(),
335+
tracer_version: "1.2.3".to_owned(),
336+
hostname: "test-host".to_owned(),
337+
..Default::default()
338+
};
339+
let expected = metadata.to_otel_process_ctx();
340+
341+
store_tracer_metadata(&metadata).expect("metadata storage should succeed");
342+
let reader = crate::otel_process_ctx::ProcessContextSelfReader::new()
343+
.expect("reader creation should succeed");
344+
assert_eq!(reader.read().expect("read should succeed"), expected);
345+
346+
crate::otel_process_ctx::unpublish().expect("unpublish should succeed");
347+
}
348+
312349
#[cfg(feature = "otel-thread-ctx")]
313350
#[test]
314351
fn threadlocal_attrs_present_with_correct_values() {

0 commit comments

Comments
 (0)