opentelemetry_resource_detectors/
os.rs

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