-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add more resource detectors (#573)
- Loading branch information
Showing
12 changed files
with
132 additions
and
22 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
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
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
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
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
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
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
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
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
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
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,46 @@ | ||
//! OS resource detector | ||
//! | ||
//! Detect the runtime operating system type. | ||
use crate::sdk::resource::ResourceDetector; | ||
use crate::sdk::Resource; | ||
use crate::KeyValue; | ||
use std::env::consts::OS; | ||
use std::time::Duration; | ||
|
||
/// Detect runtime operating system information. | ||
/// | ||
/// This detector uses Rust's [`OS constant`] to detect the operating system type and | ||
/// maps the result to the supported value defined in [`OpenTelemetry spec`]. | ||
/// | ||
/// [`OS constant`]: https://doc.rust-lang.org/std/env/consts/constant.OS.html | ||
/// [`OpenTelemetry spec`]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/os.md | ||
#[derive(Debug)] | ||
pub struct OsResourceDetector; | ||
|
||
impl ResourceDetector for OsResourceDetector { | ||
fn detect(&self, _timeout: Duration) -> Resource { | ||
Resource::new(vec![KeyValue::new("os.type", OS)]) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sdk::resource::os::OsResourceDetector; | ||
use crate::sdk::resource::ResourceDetector; | ||
use crate::Key; | ||
use std::time::Duration; | ||
|
||
#[cfg(target_os = "linux")] | ||
#[test] | ||
fn test_os_resource_detector() { | ||
let resource = OsResourceDetector.detect(Duration::from_secs(0)); | ||
assert_eq!( | ||
resource | ||
.iter() | ||
.0 | ||
.find(|(k, _v)| **k == Key::from_static_str("os.type")) | ||
.map(|(_k, v)| v.to_string()), | ||
Some("linux".to_string()) | ||
); | ||
} | ||
} |
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,51 @@ | ||
//! Process resource detector | ||
//! | ||
//! Detect process related information like pid, executable name. | ||
use crate::sdk::resource::ResourceDetector; | ||
use crate::sdk::Resource; | ||
use crate::{Array, KeyValue, Value}; | ||
use std::borrow::Cow; | ||
use std::env::args_os; | ||
use std::process::id; | ||
use std::time::Duration; | ||
|
||
/// Detect process information. | ||
/// | ||
/// This resource detector returns the following information: | ||
/// | ||
/// - process command line arguments(`process.command_args`), the full command arguments of this | ||
/// application. | ||
/// - OS assigned process id(`process.pid`). | ||
#[derive(Debug)] | ||
pub struct ProcessResourceDetector; | ||
|
||
impl ResourceDetector for ProcessResourceDetector { | ||
fn detect(&self, _timeout: Duration) -> Resource { | ||
let arguments = args_os(); | ||
let cmd_arg_val = arguments | ||
.into_iter() | ||
.map(|arg| Cow::from(arg.to_string_lossy().into_owned())) | ||
.collect::<Vec<Cow<'_, str>>>(); | ||
Resource::new(vec![ | ||
KeyValue::new( | ||
"process.command_args", | ||
Value::Array(Array::String(cmd_arg_val)), | ||
), | ||
KeyValue::new("process.pid", id() as i64), | ||
]) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sdk::resource::{ProcessResourceDetector, ResourceDetector}; | ||
use std::time::Duration; | ||
|
||
#[cfg(target_os = "linux")] | ||
#[test] | ||
fn test_processor_resource_detector() { | ||
let resource = ProcessResourceDetector.detect(Duration::from_secs(0)); | ||
assert_eq!(resource.len(), 2); // we cannot assert on the values because it changes along with runtime. | ||
} | ||
} |