Skip to content

Commit

Permalink
Added k8s ResourceDetector (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardChukwu authored Oct 27, 2024
1 parent be31dcb commit fe3b916
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
24 changes: 24 additions & 0 deletions opentelemetry-resource-detectors/src/k8s.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use opentelemetry::sdk::resource::{Resource, ResourceDetector};
use opentelemetry_semantic_conventions::resource;
use std::env;
use std::time::Duration;

/// A resource detector for Kubernetes environment variables.
pub struct K8sResourceDetector;

impl ResourceDetector for K8sResourceDetector {
/// Detect Kubernetes-related environment variables and return a Resource.
fn detect(&self, _timeout: Duration) -> Resource {
// Attempt to read Kubernetes-specific environment variables.
let pod_name = env::var("K8S_POD_NAME").unwrap_or_else(|_| "unknown_pod".to_string());
let namespace_name = env::var("K8S_NAMESPACE_NAME").unwrap_or_else(|_| "unknown_namespace".to_string());
let node_name = env::var("K8S_NODE_NAME").unwrap_or_else(|_| "unknown_node".to_string());

// Create a Resource with Kubernetes attributes.
Resource::new(vec![
resource::K8S_POD_NAME.string(pod_name),
resource::K8S_NAMESPACE_NAME.string(namespace_name),
resource::K8S_NODE_NAME.string(node_name),
])
}
}
58 changes: 58 additions & 0 deletions opentelemetry-resource-detectors/src/k8stest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
use temp_env;

#[test]
fn test_k8s_resource_detector_with_env_vars() {
// Temporarily set environment variables for the test
temp_env::with_vars(
[
("K8S_POD_NAME", Some("test-pod")),
("K8S_NAMESPACE_NAME", Some("test-namespace")),
("K8S_NODE_NAME", Some("test-node")),
],
|| {
// Create the K8sResourceDetector
let detector = K8sResourceDetector::new();
// Use the detector to fetch the resources
let resource = detector.detect(Duration::from_secs(5));

// Assert that the detected resource attributes match the expected values
assert_eq!(
resource,
Resource::new(vec![
KeyValue::new("k8s.pod.name", "test-pod"),
KeyValue::new("k8s.namespace.name", "test-namespace"),
KeyValue::new("k8s.node.name", "test-node"),
])
);
},
);
}

#[test]
fn test_k8s_resource_detector_with_missing_env_vars() {
// Temporarily set only one environment variable to test defaults
temp_env::with_vars(
[("K8S_POD_NAME", Some("test-pod"))],
|| {
// Create the K8sResourceDetector
let detector = K8sResourceDetector::new();
// Use the detector to fetch the resources
let resource = detector.detect(Duration::from_secs(5));

// Assert that missing values use the default "unknown" values
assert_eq!(
resource,
Resource::new(vec![
KeyValue::new("k8s.pod.name", "test-pod"),
KeyValue::new("k8s.namespace.name", "unknown_namespace"),
KeyValue::new("k8s.node.name", "unknown_node"),
])
);
},
);
}
}

0 comments on commit fe3b916

Please sign in to comment.