-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(worker/monitor): Fixing CR issues
- Loading branch information
Showing
9 changed files
with
75 additions
and
1,098 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
mod monitor; | ||
use std::{thread::sleep, time::Duration}; | ||
|
||
use eyre::Result; | ||
use monitor::{MetricsCollector, MetricsSender}; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
use crate::monitor::collector::MetricsCollector; | ||
|
||
pub mod monitor; | ||
|
||
/// `clap` crate report interval placeholder | ||
/// The value 500 is for quick visualization purposes. | ||
const REPORT_INTERVAL_IN_MILLIS: u64 = 500; | ||
|
||
/// `clap` crate url placeholder | ||
const AGT_MGR_REQUEST_URL: &str = "http://localhost:8080/http/agt_mgr"; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let mut metrics_sender = MetricsSender::new(AGT_MGR_REQUEST_URL)?; | ||
let mut metrics_report: MetricsCollector = MetricsCollector::new(); | ||
|
||
loop { | ||
sleep(Duration::from_millis(REPORT_INTERVAL_IN_MILLIS)).await; | ||
let metrics = metrics_report.get_metrics(); | ||
metrics_sender.send_request(&metrics).await?; | ||
sleep(Duration::from_millis(REPORT_INTERVAL_IN_MILLIS)); | ||
println!("{metrics:#?}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use proto::common::node::Metrics; | ||
use sysinfo::System; | ||
|
||
/// Responsable for collecting metrics about the System, such as | ||
/// CPU Percentage Usage, Free Memory Space. | ||
#[derive(Default)] | ||
pub struct MetricsCollector { | ||
system: System, | ||
} | ||
|
||
impl MetricsCollector { | ||
/// Instantiates a new [`MetricsCollector`]. | ||
#[must_use] | ||
pub fn new() -> Self { | ||
MetricsCollector::default() | ||
} | ||
|
||
/// Retruns the [`Metrics`] struct for the current system. | ||
#[must_use] | ||
pub fn get_metrics(&mut self) -> Metrics { | ||
self.system.refresh_memory(); | ||
self.system.refresh_cpu(); | ||
|
||
Metrics { | ||
cpu_usage: self.get_cpu_usage(), | ||
mem_total: self.get_total_memory(), | ||
mem_used: self.get_used_memory(), | ||
} | ||
} | ||
|
||
fn get_cpu_usage(&mut self) -> f64 { | ||
let amount_cpus = self.system.cpus().len(); | ||
|
||
let sum_cpus_usages: f64 = self | ||
.system | ||
.cpus() | ||
.iter() | ||
.map(|cpu| f64::from(cpu.cpu_usage())) | ||
.sum(); | ||
|
||
sum_cpus_usages / amount_cpus as f64 | ||
} | ||
|
||
fn get_total_memory(&mut self) -> u64 { | ||
self.system.total_memory() | ||
} | ||
|
||
fn get_used_memory(&mut self) -> u64 { | ||
self.system.used_memory() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
mod metrics_collector; | ||
mod metrics_sender; | ||
|
||
pub use metrics_collector::*; | ||
pub use metrics_sender::*; | ||
pub mod collector; |