Skip to content

Commit

Permalink
Merge branch 'main' into cbindgen-version
Browse files Browse the repository at this point in the history
  • Loading branch information
p-datadog authored Jul 1, 2024
2 parents 0867fc8 + f66cd95 commit ffa9f74
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
18 changes: 17 additions & 1 deletion ddcommon/src/entity_id/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

//! Extract the entity id and container id
//! Extract the entity id, container id and external env
//!
//! The container id can be extracted from `/proc/self/group`
//!
//! The entity id is one of:
//! - `cid:<container id>` if available
//! - `in:<cgroup node inode>` if container id is not available (e.g. when using cgroupV2)
//!
//! The external env is an environment variable provided by the admission controller.
//!
//! # References
//! - [DataDog/dd-trace-go](https://github.com/DataDog/dd-trace-go/blob/v1/internal/container.go)
//! - [Qard/container-info](https://github.com/Qard/container-info/blob/master/index.js)
Expand Down Expand Up @@ -49,6 +51,11 @@
//! 1:name=systemd:/ecs/8cd79a803caf4d2aa945152e934a5c00/8cd79a803caf4d2aa945152e934a5c00-1053176469
//! ```

use crate::config::parse_env;
use lazy_static::lazy_static;

const EXTERNAL_ENV_ENVIRONMENT_VARIABLE: &str = "DD_EXTERNAL_ENV";

/// Unix specific module allowing the use of unix specific functions
#[cfg(unix)]
mod unix;
Expand Down Expand Up @@ -77,6 +84,15 @@ pub fn get_entity_id() -> Option<&'static str> {
}
}

/// Returns the `DD_EXTERNAL_ENV` if available as an env variable
pub fn get_external_env() -> Option<&'static str> {
lazy_static! {
static ref DD_EXTERNAL_ENV: Option<String> =
parse_env::str_not_empty(EXTERNAL_ENV_ENVIRONMENT_VARIABLE);
}
DD_EXTERNAL_ENV.as_deref()
}

/// Set the path to cgroup file to mock it during tests
/// # Safety
/// Must not be called in multi-threaded contexts
Expand Down
8 changes: 4 additions & 4 deletions ddcommon/src/entity_id/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn compute_entity_id(
) -> Option<String> {
container_id::extract_container_id(cgroup_path)
.ok()
.map(|container_id| format!("cid-{container_id}"))
.map(|container_id| format!("ci-{container_id}"))
.or(
cgroup_inode::get_cgroup_inode(base_controller, cgroup_path, cgroup_mount_path)
.map(|inode| format!("in-{inode}")),
Expand Down Expand Up @@ -119,8 +119,8 @@ mod tests {

lazy_static! {
static ref IN_REGEX: Regex = Regex::new(r"in-\d+").unwrap();
static ref CID_REGEX: Regex =
Regex::new(&format!(r"cid-{}", container_id::CONTAINER_REGEX.as_str())).unwrap();
static ref CI_REGEX: Regex =
Regex::new(&format!(r"ci-{}", container_id::CONTAINER_REGEX.as_str())).unwrap();
}

/// The following test can only be run in isolation because of caching behaviour introduced
Expand Down Expand Up @@ -161,7 +161,7 @@ mod tests {

#[test]
fn test_entity_id_for_container_id() {
test_entity_id("cgroup.docker", Some(&CID_REGEX))
test_entity_id("cgroup.docker", Some(&CI_REGEX))
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions ddcommon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod header {
use hyper::{header::HeaderName, http::HeaderValue};
pub const DATADOG_CONTAINER_ID: HeaderName = HeaderName::from_static("datadog-container-id");
pub const DATADOG_ENTITY_ID: HeaderName = HeaderName::from_static("datadog-entity-id");
pub const DATADOG_EXTERNAL_ENV: HeaderName = HeaderName::from_static("datadog-external-env");
pub const DATADOG_API_KEY: HeaderName = HeaderName::from_static("dd-api-key");
pub const APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json");
}
Expand Down Expand Up @@ -146,6 +147,11 @@ impl Endpoint {
builder = builder.header(header::DATADOG_ENTITY_ID, entity_id);
}

// Add the External Env header if available
if let Some(external_env) = entity_id::get_external_env() {
builder = builder.header(header::DATADOG_EXTERNAL_ENV, external_env);
}

Ok(builder)
}
}

0 comments on commit ffa9f74

Please sign in to comment.