Skip to content

Commit

Permalink
feat: Add host.id resource detector (#57)
Browse files Browse the repository at this point in the history
Add the Host Resource detector retrieving the "host.id" as defined in: https://opentelemetry.io/docs/specs/semconv/resource/host/

Defined in #56
  • Loading branch information
alvarocabanas committed Apr 2, 2024
1 parent 749bbbe commit 56a6f45
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
6 changes: 6 additions & 0 deletions opentelemetry-resource-detectors/CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## vNext

### Added

- Added `HostResourceDetector` which populates "host.id" attribute. Currently only Linux is supported.

## v0.1.0

### Added
Expand Down
9 changes: 5 additions & 4 deletions opentelemetry-resource-detectors/README.md
Expand Up @@ -12,7 +12,8 @@ Community supported Resource detectors implementations for applications instrume

## Features

| Detector | Implemented Resources | Semantic Conventions |
| -------------- | --------------------------------------------------- | -------------------------- |
| ProcessResourceDetector | PROCESS_COMMAND_ARGS, PROCESS_PID | https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.x/specification/resource/semantic_conventions/process.md |
| OsResourceDetector | OS_TYPE | https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.x/specification/resource/semantic_conventions/os.md |
| Detector | Implemented Resources | OS Supported | Semantic Conventions |
|-------------------------| --------------------------------------------------- |--------------|-------------------------------------------------------------------------------------------|
| ProcessResourceDetector | PROCESS_COMMAND_ARGS, PROCESS_PID | all | https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/process.md |
| OsResourceDetector | OS_TYPE | all | https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/os.md |
| HostResourceDetector | HOST_ID | linux | https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/host.md |
73 changes: 73 additions & 0 deletions opentelemetry-resource-detectors/src/host.rs
@@ -0,0 +1,73 @@
//! HOST resource detector
//!
//! Detect the unique host ID.
use opentelemetry::KeyValue;
use opentelemetry_sdk::resource::ResourceDetector;
use opentelemetry_sdk::Resource;
use std::fs::read_to_string;
use std::path::Path;
use std::time::Duration;

/// Detect the unique host ID.
///
/// This detector looks up the host id using the sources defined
/// in the OpenTelemetry semantic conventions [`host.id from non-containerized systems`].
///
/// [`host.id from non-containerized systems`]: https://opentelemetry.io/docs/specs/semconv/resource/host/#collecting-hostid-from-non-containerized-systems
pub struct HostResourceDetector {
host_id_detect: fn() -> Option<String>,
}

impl ResourceDetector for HostResourceDetector {
fn detect(&self, _timeout: Duration) -> Resource {
(self.host_id_detect)()
.map(|host_id| {
Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::HOST_ID,
host_id,
)])
})
.unwrap_or(Resource::new(vec![]))
}
}

#[cfg(target_os = "linux")]
fn host_id_detect() -> Option<String> {
let machine_id_path = Path::new("/etc/machine-id");
let dbus_machine_id_path = Path::new("/var/lib/dbus/machine-id");
read_to_string(machine_id_path)
.or_else(|_| read_to_string(dbus_machine_id_path))
.ok()
}

// TODO: Implement non-linux platforms
#[cfg(not(target_os = "linux"))]
fn host_id_detect() -> Option<String> {
None
}

impl Default for HostResourceDetector {
fn default() -> Self {
Self { host_id_detect }
}
}

#[cfg(target_os = "linux")]
#[cfg(test)]
mod tests {
use super::HostResourceDetector;
use opentelemetry::Key;
use opentelemetry_sdk::resource::ResourceDetector;
use std::time::Duration;

#[test]
fn test_host_resource_detector() {
let resource = HostResourceDetector::default().detect(Duration::from_secs(0));
assert_eq!(resource.len(), 1);
assert!(resource
.get(Key::from_static_str(
opentelemetry_semantic_conventions::resource::HOST_ID
))
.is_some())
}
}
3 changes: 3 additions & 0 deletions opentelemetry-resource-detectors/src/lib.rs
Expand Up @@ -5,8 +5,11 @@
//!
//! - [`OsResourceDetector`] - detect OS from runtime.
//! - [`ProcessResourceDetector`] - detect process information.
//! - [`HostResourceDetector`] - detect unique host ID.
mod host;
mod os;
mod process;

pub use host::HostResourceDetector;
pub use os::OsResourceDetector;
pub use process::ProcessResourceDetector;

0 comments on commit 56a6f45

Please sign in to comment.