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;
#[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(target_os = "linux")]
#[cfg(test)]
mod tests {
use crate::sdk::resource::{ProcessResourceDetector, ResourceDetector};
use std::time::Duration;
#[test]
fn test_processor_resource_detector() {
let resource = ProcessResourceDetector.detect(Duration::from_secs(0));
assert_eq!(resource.len(), 2); }
}