From 74c506a01863f28fe6e0afc21a27125368da25f6 Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Thu, 13 Oct 2022 18:44:52 +0200 Subject: [PATCH 1/4] Make it compile in ddtrace --- ddcommon/src/container_id.rs | 12 ++++++++++-- ddtelemetry-ffi/Cargo.toml | 2 +- ddtelemetry-ffi/cbindgen.toml | 2 +- profiling-ffi/cbindgen.toml | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ddcommon/src/container_id.rs b/ddcommon/src/container_id.rs index ec20d38ac6..a32099cdaf 100644 --- a/ddcommon/src/container_id.rs +++ b/ddcommon/src/container_id.rs @@ -37,7 +37,9 @@ 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"; + +static mut 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 +87,16 @@ fn extract_container_id(filepath: &Path) -> Result 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(); + // Safety: we assume set_cgroup_file is not called when it shouldn't + static ref CONTAINER_ID: Option = unsafe { extract_container_id(Path::new(CGROUP_PATH.as_ref().map(String::as_str).unwrap_or(&DEFAULT_CGROUP_PATH))).ok() }; } CONTAINER_ID.as_deref() } diff --git a/ddtelemetry-ffi/Cargo.toml b/ddtelemetry-ffi/Cargo.toml index 085e3c5802..cb0c344921 100644 --- a/ddtelemetry-ffi/Cargo.toml +++ b/ddtelemetry-ffi/Cargo.toml @@ -9,7 +9,7 @@ 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/ddtelemetry-ffi/cbindgen.toml b/ddtelemetry-ffi/cbindgen.toml index 96b42d3f37..b31efcb280 100644 --- a/ddtelemetry-ffi/cbindgen.toml +++ b/ddtelemetry-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_" 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_" From 7f874628f5c9b6ae959cac35af50c65c59edec89 Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Mon, 17 Oct 2022 13:18:04 +0200 Subject: [PATCH 2/4] revert changes to cbindgen.toml --- ddtelemetry-ffi/cbindgen.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddtelemetry-ffi/cbindgen.toml b/ddtelemetry-ffi/cbindgen.toml index b31efcb280..96b42d3f37 100644 --- a/ddtelemetry-ffi/cbindgen.toml +++ b/ddtelemetry-ffi/cbindgen.toml @@ -11,7 +11,7 @@ style = "both" no_includes = true sys_includes = ["stdbool.h", "stddef.h", "stdint.h"] -includes = ["common.h"] +includes = ["datadog/common.h"] [export] prefix = "ddog_" From 6dcd5bd96672bfb182e771dae59b77b63065f499 Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Mon, 17 Oct 2022 13:43:45 +0200 Subject: [PATCH 3/4] extract get_cgroup_path into its own fn --- ddcommon/src/container_id.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/ddcommon/src/container_id.rs b/ddcommon/src/container_id.rs index a32099cdaf..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 @@ -39,7 +40,8 @@ Following environments are supported: const DEFAULT_CGROUP_PATH: &str = "/proc/self/cgroup"; -static mut CGROUP_PATH: Option = None; +/// 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}"; @@ -87,16 +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! { - // Safety: we assume set_cgroup_file is not called when it shouldn't - static ref CONTAINER_ID: Option = unsafe { extract_container_id(Path::new(CGROUP_PATH.as_ref().map(String::as_str).unwrap_or(&DEFAULT_CGROUP_PATH))).ok() }; + static ref CONTAINER_ID: Option = + extract_container_id(get_cgroup_path().as_path()).ok(); } CONTAINER_ID.as_deref() } From 254ab9aa32f96c85b6bc552875766b47dbd3b0c8 Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Tue, 18 Oct 2022 19:44:42 +0200 Subject: [PATCH 4/4] remove comment --- ddtelemetry-ffi/Cargo.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/ddtelemetry-ffi/Cargo.toml b/ddtelemetry-ffi/Cargo.toml index cb0c344921..ce5838150c 100644 --- a/ddtelemetry-ffi/Cargo.toml +++ b/ddtelemetry-ffi/Cargo.toml @@ -7,8 +7,6 @@ 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 = ["lib", "staticlib", "cdylib"] [dependencies]