Skip to content

Commit c0f46e4

Browse files
pawelchckibwoebi
andauthored
Allow setting cgroup path for integration testing purposes (#64)
* Make it compile in ddtrace * revert changes to cbindgen.toml * extract get_cgroup_path into its own fn * remove comment Co-authored-by: Bob Weinand <[email protected]>
1 parent 41b3573 commit c0f46e4

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

ddcommon/src/container_id.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::fmt;
88
use std::fs::File;
99
use std::io::{BufRead, BufReader};
1010
use std::path::Path;
11+
use std::path::PathBuf;
1112

1213
/* Extract container id from /proc/self/group
1314
@@ -37,7 +38,10 @@ Following environments are supported:
3738
`1:name=systemd:/ecs/8cd79a803caf4d2aa945152e934a5c00/8cd79a803caf4d2aa945152e934a5c00-1053176469`
3839
*/
3940

40-
const CGROUP_PATH: &str = "/proc/self/cgroup";
41+
const DEFAULT_CGROUP_PATH: &str = "/proc/self/cgroup";
42+
43+
/// stores overridable cgroup path - used in end-to-end testing to "stub" cgroup values
44+
static mut TESTING_CGROUP_PATH: Option<String> = None;
4145

4246
const UUID_SOURCE: &str =
4347
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<String, Box<dyn std::error::E
8589
Err(ContainerIdNotFoundError.into())
8690
}
8791

92+
/// # Safety
93+
/// Must not be called in multi-threaded contexts
94+
pub unsafe fn set_cgroup_file(file: String) {
95+
TESTING_CGROUP_PATH = Some(file)
96+
}
97+
98+
fn get_cgroup_path() -> PathBuf {
99+
// Safety: we assume set_cgroup_file is not called when it shouldn't
100+
if let Some(path) = unsafe { TESTING_CGROUP_PATH.as_ref() } {
101+
Path::new(path.as_str()).into()
102+
} else {
103+
Path::new(DEFAULT_CGROUP_PATH).into()
104+
}
105+
}
106+
88107
pub fn get_container_id() -> Option<&'static str> {
89108
// cache container id in a static to avoid recomputing it at each call
109+
90110
lazy_static! {
91-
static ref CONTAINER_ID: Option<String> = extract_container_id(Path::new(CGROUP_PATH)).ok();
111+
static ref CONTAINER_ID: Option<String> =
112+
extract_container_id(get_cgroup_path().as_path()).ok();
92113
}
93114
CONTAINER_ID.as_deref()
94115
}

ddtelemetry-ffi/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ version = "0.9.1"
77
edition = "2021"
88

99
[lib]
10-
# LTO is ignored if "lib" is added as crate type
11-
# cf. https://github.com/rust-lang/rust/issues/51009
12-
crate-type = ["staticlib", "cdylib"]
10+
crate-type = ["lib", "staticlib", "cdylib"]
1311

1412
[dependencies]
1513
ddtelemetry = { path = "../ddtelemetry", version = "0.9.0" }

profiling-ffi/cbindgen.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ style = "both"
1111

1212
no_includes = true
1313
sys_includes = ["stdbool.h", "stddef.h", "stdint.h"]
14-
includes = ["datadog/common.h"]
14+
includes = ["common.h"]
1515

1616
[export]
1717
prefix = "ddog_"

0 commit comments

Comments
 (0)