Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,10 @@ pub struct VerifyConfig {
pub blocklist: HashSet<String>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct VarlinkConfig {
pub enabled: bool,
}
impl Default for VarlinkConfig {
fn default() -> Self {
VarlinkConfig { enabled: false }
}
}

/// Config struct
/// Each section represents an ini file section
Expand Down
29 changes: 23 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,29 @@ pub async fn stat_collector(
// Run service collectors if there are services listed in config
if config.units.enabled {
if config.varlink.enabled {
join_set.spawn(crate::varlink_units::update_unit_stats(
Arc::clone(&config),
sdc.clone(),
locked_machine_stats.clone(),
crate::varlink_units::METRICS_SOCKET_PATH.to_string(),
));
let config_clone = Arc::clone(&config);
let sdc_clone = sdc.clone();
let stats_clone = locked_machine_stats.clone();
let socket_path = crate::varlink_units::METRICS_SOCKET_PATH.to_string();
join_set.spawn(async move {
match crate::varlink_units::update_unit_stats(
Arc::clone(&config_clone),
stats_clone.clone(),
socket_path,
)
.await
{
Ok(()) => Ok(()),
Err(err) => {
warn!(
"Varlink units stats failed, falling back to D-Bus: {:?}",
err
);
crate::units::update_unit_stats(config_clone, sdc_clone, stats_clone)
.await
Comment on lines +192 to +193
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you calling crate::units::update_unit_stats here and a couple of lines below. Maybe merge them.

pseudo code:

if (varlink_enabled) {
  call_varlink()
  if success
     return

  log("Fallback to DBus")
}

call_dbus()

}
}
});
} else {
join_set.spawn(crate::units::update_unit_stats(
Arc::clone(&config),
Expand Down
30 changes: 23 additions & 7 deletions src/machines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use thiserror::Error;
use tokio::sync::RwLock;
use tracing::{debug, error};
use tracing::{debug, error, warn};

use crate::MachineStats;
use crate::MonitordStats;
Expand Down Expand Up @@ -103,17 +103,33 @@ pub async fn update_machines_stats(

if config.units.enabled {
if config.varlink.enabled {
let config_clone = Arc::clone(&config);
let sdc_clone = sdc.clone();
let stats_clone = locked_machine_stats.clone();
let container_socket_path = format!(
"/proc/{}/root{}",
leader_pid,
crate::varlink_units::METRICS_SOCKET_PATH
);
join_set.spawn(crate::varlink_units::update_unit_stats(
Arc::clone(&config),
sdc.clone(),
locked_machine_stats.clone(),
container_socket_path,
));
join_set.spawn(async move {
match crate::varlink_units::update_unit_stats(
Arc::clone(&config_clone),
stats_clone.clone(),
container_socket_path,
)
.await
{
Ok(()) => Ok(()),
Err(err) => {
warn!(
"Varlink units stats failed, falling back to D-Bus: {:?}",
err
);
crate::units::update_unit_stats(config_clone, sdc_clone, stats_clone)
.await
}
}
});
} else {
join_set.spawn(crate::units::update_unit_stats(
Arc::clone(&config),
Expand Down
42 changes: 25 additions & 17 deletions src/varlink/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,14 @@ impl ListOutput {
self.object.as_deref().unwrap_or("").to_string()
}

/// Returns the string value or default_value if not present
pub fn value_as_string<'a>(&'a self, default_value: &'a str) -> &'a str {
self.value.as_str().unwrap_or(default_value)
/// Returns the string value if present
pub fn value_as_string(&self) -> Option<&str> {
self.value.as_str()
}
Comment on lines +66 to 68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still, value is a mandatory field in a metric. I think you should be able to remove Option<>. Maybe you can validate metric structure before parsing it


/// Returns the int value as u64 or default_value if not present
pub fn value_as_int(&self, default_value: u64) -> u64 {
self.value
.as_i64()
.filter(|v| *v >= 0)
.map(|v| v as u64)
.unwrap_or(default_value)
/// Returns the int value as i64 if present
pub fn value_as_int(&self) -> Option<i64> {
self.value.as_i64()
}

/// Returns the bool value if present
Expand Down Expand Up @@ -296,7 +292,7 @@ mod tests {
fields: None,
};

assert_eq!(output.value_as_string("unknown"), "active");
assert_eq!(output.value_as_string(), Some("active"));
}

#[test]
Expand All @@ -308,7 +304,7 @@ mod tests {
fields: None,
};

assert_eq!(output.value_as_string("unknown"), "unknown");
assert_eq!(output.value_as_string(), None);
}

#[test]
Expand All @@ -320,7 +316,7 @@ mod tests {
fields: None,
};

assert_eq!(output.value_as_string("default"), "");
assert_eq!(output.value_as_string(), Some(""));
}

#[test]
Expand All @@ -332,7 +328,7 @@ mod tests {
fields: None,
};

assert_eq!(output.value_as_int(0), 42);
assert_eq!(output.value_as_int(), Some(42));
}

#[test]
Expand All @@ -344,7 +340,7 @@ mod tests {
fields: None,
};

assert_eq!(output.value_as_int(0), 0);
assert_eq!(output.value_as_int(), None);
}

#[test]
Expand All @@ -356,7 +352,19 @@ mod tests {
fields: None,
};

assert_eq!(output.value_as_int(0), 0);
assert_eq!(output.value_as_int(), Some(0));
}

#[test]
fn test_value_as_int_negative() {
let output = ListOutput {
name: "test.metric".to_string(),
value: serde_json::json!(-5),
object: None,
fields: None,
};

assert_eq!(output.value_as_int(), Some(-5));
}

#[test]
Expand All @@ -368,7 +376,7 @@ mod tests {
fields: None,
};

assert_eq!(output.value_as_int(0), 9999999999);
assert_eq!(output.value_as_int(), Some(9999999999));
}

#[test]
Expand Down
Loading
Loading