Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarocabanas committed Mar 27, 2024
1 parent 122703e commit adaf908
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
46 changes: 24 additions & 22 deletions opentelemetry-resource-detectors/src/host.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! HOST resource detector
//!
//! Detect the unique host ID.
use opentelemetry::KeyValue;
use opentelemetry_sdk::resource::ResourceDetector;
use opentelemetry_sdk::Resource;
use std::error::Error;

Check failure on line 7 in opentelemetry-resource-detectors/src/host.rs

View workflow job for this annotation

GitHub Actions / lint

unused import: `std::error::Error`
use std::fs::read_to_string;
use std::path::Path;
use std::time::Duration;
use opentelemetry::KeyValue;
use opentelemetry_sdk::Resource;
use opentelemetry_sdk::resource::ResourceDetector;

/// Detect the unique host ID.
///
Expand All @@ -16,25 +16,29 @@ use opentelemetry_sdk::resource::ResourceDetector;
///
/// [`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>
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![]))
(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 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()
read_to_string(machine_id_path)
.or_else(|_| read_to_string(dbus_machine_id_path))
.ok()
}

// TODO: Implement non-linux platforms
Expand All @@ -45,28 +49,26 @@ fn host_id_detect() -> Option<String> {

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

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

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

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

0 comments on commit adaf908

Please sign in to comment.