opentelemetry_spanprocessor_any/sdk/resource/
os.rs

1//! OS resource detector
2//!
3//! Detect the runtime operating system type.
4use crate::sdk::resource::ResourceDetector;
5use crate::sdk::Resource;
6use crate::KeyValue;
7use std::env::consts::OS;
8use std::time::Duration;
9
10/// Detect runtime operating system information.
11///
12/// This detector uses Rust's [`OS constant`] to detect the operating system type and
13/// maps the result to the supported value defined in [`OpenTelemetry spec`].
14///
15/// [`OS constant`]: https://doc.rust-lang.org/std/env/consts/constant.OS.html
16/// [`OpenTelemetry spec`]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/os.md
17#[derive(Debug)]
18pub struct OsResourceDetector;
19
20impl ResourceDetector for OsResourceDetector {
21    fn detect(&self, _timeout: Duration) -> Resource {
22        Resource::new(vec![KeyValue::new("os.type", OS)])
23    }
24}
25
26#[cfg(target_os = "linux")]
27#[cfg(test)]
28mod tests {
29    use crate::sdk::resource::os::OsResourceDetector;
30    use crate::sdk::resource::ResourceDetector;
31    use crate::Key;
32    use std::time::Duration;
33
34    #[test]
35    fn test_os_resource_detector() {
36        let resource = OsResourceDetector.detect(Duration::from_secs(0));
37        assert_eq!(
38            resource
39                .iter()
40                .0
41                .find(|(k, _v)| **k == Key::from_static_str("os.type"))
42                .map(|(_k, v)| v.to_string()),
43            Some("linux".to_string())
44        );
45    }
46}