♻️ refactor: extract metrics monitor function and add it to ptth_server

main
_ 2020-12-20 17:32:13 -06:00
parent f335644b03
commit bcc673b74b
3 changed files with 52 additions and 38 deletions

View File

@ -100,44 +100,8 @@ async fn main () -> Result <(), anyhow::Error> {
let metrics_interval = Arc::new (ArcSwap::default ()); let metrics_interval = Arc::new (ArcSwap::default ());
let interval_writer = Arc::clone (&metrics_interval); let interval_writer = Arc::clone (&metrics_interval);
tokio::spawn (async move { tokio::spawn (async move {
use std::time::Duration; file_server::metrics::Interval::monitor (interval_writer).await;
use uom::si::ratio::percent;
let mut interval = tokio::time::interval (Duration::from_secs (60));
let mut counter = 0_u64;
let mut next_10_time = counter;
let mut metrics_at_last_10: Arc <Option <metrics::Interval>> = Arc::new (None);
loop {
interval.tick ().await;
let new_interval_metrics = match file_server::metrics::Interval::new ().await {
Err (e) => {
error! ("Failed to update interval metrics: {:?}", e);
continue;
},
Ok (x) => x,
};
let new_interval_metrics = Arc::new (Some (new_interval_metrics));
if counter >= next_10_time {
if let (Some (old), Some (new)) = (&*metrics_at_last_10, &*new_interval_metrics) {
let diff = new.cpu_usage.clone () - old.cpu_usage.clone ();
trace! ("CPU usage: {}%", diff.get::<percent> ());
}
next_10_time += 1;
metrics_at_last_10 = new_interval_metrics.clone ();
}
interval_writer.store (new_interval_metrics);
counter += 1;
//trace! ("interval metrics 1");
}
}); });
let state = Arc::new (State { let state = Arc::new (State {

View File

@ -1,5 +1,10 @@
use std::sync::Arc;
use arc_swap::ArcSwap;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use tracing::debug; use tracing::{
debug, error, trace,
};
use ulid::Ulid; use ulid::Ulid;
// Metrics are named for when they're updated: // Metrics are named for when they're updated:
@ -96,6 +101,46 @@ impl Interval {
Ok (x) Ok (x)
} }
pub async fn monitor (interval_writer: Arc <ArcSwap <Option <Interval>>>)
{
use std::time::Duration;
use uom::si::ratio::percent;
let mut interval = tokio::time::interval (Duration::from_secs (4));
let mut counter = 0_u64;
let mut next_10_time = counter;
let mut metrics_at_last_10: Arc <Option <Interval>> = Arc::new (None);
loop {
interval.tick ().await;
let new_interval_metrics = match Interval::new ().await {
Err (e) => {
error! ("Failed to update interval metrics: {:?}", e);
continue;
},
Ok (x) => x,
};
let new_interval_metrics = Arc::new (Some (new_interval_metrics));
if counter >= next_10_time {
if let (Some (old), Some (new)) = (&*metrics_at_last_10, &*new_interval_metrics) {
let diff = new.cpu_usage.clone () - old.cpu_usage.clone ();
trace! ("CPU usage: {}%", diff.get::<percent> ());
}
next_10_time += 1;
metrics_at_last_10 = new_interval_metrics.clone ();
}
interval_writer.store (new_interval_metrics);
counter += 1;
//trace! ("interval metrics 1");
}
}
} }
fn get_machine_id () -> Option <String> { fn get_machine_id () -> Option <String> {

View File

@ -203,6 +203,11 @@ pub async fn run_server (
let metrics_startup = file_server::metrics::Startup::new (config_file.name); let metrics_startup = file_server::metrics::Startup::new (config_file.name);
let metrics_interval = Arc::new (ArcSwap::default ()); let metrics_interval = Arc::new (ArcSwap::default ());
let interval_writer = Arc::clone (&metrics_interval);
tokio::spawn (async move {
file_server::metrics::Interval::monitor (interval_writer).await;
});
let state = Arc::new (State { let state = Arc::new (State {
file_server: file_server::State { file_server: file_server::State {
config: file_server::Config { config: file_server::Config {