Skip to content

Commit

Permalink
resource-detectors: add support for host ID detection on macOS (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com>
  • Loading branch information
djc and cijothomas committed May 24, 2024
1 parent b30b6b3 commit 6457627
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion opentelemetry-resource-detectors/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
### Added

- Add "host.arch" attribute into the `HostResourceDetector`.
- Added `HostResourceDetector` which populates "host.id" attribute. Currently only Linux is supported.
- Added `HostResourceDetector` which populates "host.id" attribute. Currently only Linux and macOS are supported.

## v0.1.0

Expand Down
42 changes: 40 additions & 2 deletions opentelemetry-resource-detectors/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::env::consts::ARCH;
use std::fs::read_to_string;
#[cfg(target_os = "linux")]
use std::path::Path;
#[cfg(target_os = "macos")]
use std::process::Command;
use std::time::Duration;

/// Detect host information.
Expand Down Expand Up @@ -53,8 +55,26 @@ fn host_id_detect() -> Option<String> {
.ok()
}

#[cfg(target_os = "macos")]
fn host_id_detect() -> Option<String> {
let output = Command::new("ioreg")
.arg("-rd1")
.arg("-c")
.arg("IOPlatformExpertDevice")
.output()
.ok()?
.stdout;

let output = String::from_utf8(output).ok()?;
let line = output
.lines()
.find(|line| line.contains("IOPlatformUUID"))?;

Some(line.split_once('=')?.1.trim().trim_matches('"').to_owned())
}

// TODO: Implement non-linux platforms
#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn host_id_detect() -> Option<String> {
None
}
Expand All @@ -74,8 +94,26 @@ mod tests {

#[cfg(target_os = "linux")]
#[test]
fn test_host_resource_detector() {
fn test_host_resource_detector_linux() {
let resource = HostResourceDetector::default().detect(Duration::from_secs(0));
assert_eq!(resource.len(), 2);
assert!(resource
.get(Key::from_static_str(
opentelemetry_semantic_conventions::resource::HOST_ID
))
.is_some());
assert!(resource
.get(Key::from_static_str(
opentelemetry_semantic_conventions::resource::HOST_ARCH
))
.is_some())
}

#[cfg(target_os = "macos")]
#[test]
fn test_host_resource_detector_macos() {
let resource = HostResourceDetector::default().detect(Duration::from_secs(0));
dbg!(&resource);
assert_eq!(resource.len(), 2);
assert!(resource
.get(Key::from_static_str(
Expand Down

0 comments on commit 6457627

Please sign in to comment.