diff --git a/ddcommon/src/container_id.rs b/ddcommon/src/container_id.rs index ec20d38ac6..b116d0ef43 100644 --- a/ddcommon/src/container_id.rs +++ b/ddcommon/src/container_id.rs @@ -8,6 +8,7 @@ use std::fmt; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::Path; +use std::path::PathBuf; /* Extract container id from /proc/self/group @@ -37,7 +38,10 @@ Following environments are supported: `1:name=systemd:/ecs/8cd79a803caf4d2aa945152e934a5c00/8cd79a803caf4d2aa945152e934a5c00-1053176469` */ -const CGROUP_PATH: &str = "/proc/self/cgroup"; +const DEFAULT_CGROUP_PATH: &str = "/proc/self/cgroup"; + +/// stores overridable cgroup path - used in end-to-end testing to "stub" cgroup values +static mut TESTING_CGROUP_PATH: Option = None; const UUID_SOURCE: &str = r"[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}"; @@ -85,10 +89,27 @@ fn extract_container_id(filepath: &Path) -> Result PathBuf { + // Safety: we assume set_cgroup_file is not called when it shouldn't + if let Some(path) = unsafe { TESTING_CGROUP_PATH.as_ref() } { + Path::new(path.as_str()).into() + } else { + Path::new(DEFAULT_CGROUP_PATH).into() + } +} + pub fn get_container_id() -> Option<&'static str> { // cache container id in a static to avoid recomputing it at each call + lazy_static! { - static ref CONTAINER_ID: Option = extract_container_id(Path::new(CGROUP_PATH)).ok(); + static ref CONTAINER_ID: Option = + extract_container_id(get_cgroup_path().as_path()).ok(); } CONTAINER_ID.as_deref() } diff --git a/ddtelemetry-ffi/Cargo.toml b/ddtelemetry-ffi/Cargo.toml index 085e3c5802..ce5838150c 100644 --- a/ddtelemetry-ffi/Cargo.toml +++ b/ddtelemetry-ffi/Cargo.toml @@ -7,9 +7,7 @@ version = "0.9.1" edition = "2021" [lib] -# LTO is ignored if "lib" is added as crate type -# cf. https://github.com/rust-lang/rust/issues/51009 -crate-type = ["staticlib", "cdylib"] +crate-type = ["lib", "staticlib", "cdylib"] [dependencies] ddtelemetry = { path = "../ddtelemetry", version = "0.9.0" } diff --git a/profiling-ffi/cbindgen.toml b/profiling-ffi/cbindgen.toml index 475d5aa784..2c967de757 100644 --- a/profiling-ffi/cbindgen.toml +++ b/profiling-ffi/cbindgen.toml @@ -11,7 +11,7 @@ style = "both" no_includes = true sys_includes = ["stdbool.h", "stddef.h", "stdint.h"] -includes = ["datadog/common.h"] +includes = ["common.h"] [export] prefix = "ddog_"