Skip to content

Commit

Permalink
Allow setting cgroup path for integration testing purposes (#64)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
pawelchcki and bwoebi authored Oct 19, 2022
1 parent 41b3573 commit c0f46e4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
25 changes: 23 additions & 2 deletions ddcommon/src/container_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String> = 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}";
Expand Down Expand Up @@ -85,10 +89,27 @@ fn extract_container_id(filepath: &Path) -> Result<String, Box<dyn std::error::E
Err(ContainerIdNotFoundError.into())
}

/// # Safety
/// Must not be called in multi-threaded contexts
pub unsafe fn set_cgroup_file(file: String) {
TESTING_CGROUP_PATH = Some(file)
}

fn get_cgroup_path() -> 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<String> = extract_container_id(Path::new(CGROUP_PATH)).ok();
static ref CONTAINER_ID: Option<String> =
extract_container_id(get_cgroup_path().as_path()).ok();
}
CONTAINER_ID.as_deref()
}
Expand Down
4 changes: 1 addition & 3 deletions ddtelemetry-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion profiling-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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_"
Expand Down

0 comments on commit c0f46e4

Please sign in to comment.