-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added k8s ResourceDetector #122
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"), | ||
]) | ||
); | ||
}, | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this exported from
lib.rs
?