-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be31dcb
commit fe3b916
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
]) | ||
); | ||
}, | ||
); | ||
} | ||
} |