Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(worker/monitor): add metrics collector #6

Merged
merged 24 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5de7f17
feat(worker/monitor): Free memory
YuriRDev Mar 19, 2024
45fc37b
Merge branch 'main' into feat(worker/monitor)--added-metrics-collector
YuriRDev Mar 19, 2024
6df70b5
feat(worker/monitor): Get free memory method
Mar 19, 2024
2d6dbea
feat(worker/monitor)-resolved merge conflict
YuriRDev Mar 21, 2024
7df30c2
feat(worker/monitor) Resolved nitpicks
YuriRDev Mar 21, 2024
37e037a
feat(worker/monitor) - resolved nightly
YuriRDev Mar 21, 2024
f4f302d
feat(worker/monitor) - LF
YuriRDev Mar 21, 2024
f15169f
feat(worker/monitor) moved to LF
YuriRDev Mar 21, 2024
05b545f
feat(worker/monitor) Modev to LF - For sure
YuriRDev Mar 21, 2024
e4fff15
feat(worker/monitor) Removed git attributes
YuriRDev Mar 21, 2024
dd9e9de
feat(worker/monitor) - Average cpu usage
YuriRDev Mar 22, 2024
5011cbb
feat(worker/monitor) - Return metrics without multiple instanciations
YuriRDev Mar 23, 2024
d76f336
feat(worker/monitor) - Fixed get_metrics instantiations
YuriRDev Mar 23, 2024
d3c11e0
Added metrics
YuriRDev Mar 23, 2024
81ffcf8
feat(worker/monitor) - Memory in MiB
YuriRDev Mar 24, 2024
e96a43b
feat(worker/monitor): Changed collected metrics
lemosep Mar 24, 2024
245e7f9
feat(worker/monitor): Implemented monitor
lemosep Apr 2, 2024
0d872af
feat(worker/monitor): Fixing clippy issues
lemosep Apr 2, 2024
0c6ac0b
feat(worker/monitor): Fixing formatting & clippy
lemosep Apr 2, 2024
dd91ca7
feat(worker/monitor): Resolving code review issues
lemosep Apr 5, 2024
3bd894b
feat(worker/monitor): Resolving clippy & formatting
lemosep Apr 5, 2024
0783679
feat(worker/monitor): Fixing CR issues
lemosep Apr 6, 2024
668c82b
Merge branch 'main' into feat(worker/monitor)--added-metrics-collector
lemosep Apr 6, 2024
710e8fe
feat(worker/monitor): Fixing CR issues
lemosep Apr 11, 2024
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
1,421 changes: 1,355 additions & 66 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ chrono = { version = "0.4", default-features = false, features = [
"std",
"serde",
] }
eyre = "0.6.12"
reqwest = { version = "0.12.1" , features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sysinfo = "0.30.7"
tokio = { version = "1.36.0", features = ["full"] }
lffg marked this conversation as resolved.
Show resolved Hide resolved
uuid = { version = "1", features = ["serde", "v4"] }

[workspace.lints.clippy]
all = "warn"
pedantic = "warn"
wildcard_imports = "allow"
module_name_repetitions = "allow"
cast_precision_loss = "allow"
3 changes: 2 additions & 1 deletion proto/src/common/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub enum NodeKind {

#[derive(Debug, Serialize, Deserialize)]
pub struct Metrics {
pub cpu_usage: f64,
/// Collection of CPU usages.
pub cpu_usage: Vec<f64>,
lemosep marked this conversation as resolved.
Show resolved Hide resolved
/// The total memory, in MiB.
pub mem_total_mib: f64,
/// The used memory, in MiB.
Expand Down
7 changes: 7 additions & 0 deletions worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ edition.workspace = true
workspace = true

[dependencies]
eyre.workspace = true
proto.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
lffg marked this conversation as resolved.
Show resolved Hide resolved
sysinfo.workspace = true
tokio.workspace = true
25 changes: 23 additions & 2 deletions worker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
fn main() {
println!("Hello, world!");
mod monitor;
use std::{thread::sleep, time::Duration};

use eyre::Result;
use monitor::{MetricsCollector, MetricsSender};

/// `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 monitor = MetricsSender::new(AGT_MGR_REQUEST_URL)?;
lemosep marked this conversation as resolved.
Show resolved Hide resolved
let mut metrics_report: MetricsCollector = MetricsCollector::new();

loop {
let metrics = metrics_report.get_metrics();
monitor.send_request(&metrics).await?;
sleep(Duration::from_millis(REPORT_INTERVAL_IN_MILLIS));
lffg marked this conversation as resolved.
Show resolved Hide resolved
}
}
67 changes: 67 additions & 0 deletions worker/src/monitor/metrics_collector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::{thread::sleep, time::Duration};

use proto::common::node::Metrics;
use sysinfo::System;

const CPU_DELAY_IN_MILLIS: u64 = 500;

/// Struct containing system's information such as CPU and Memory.
lemosep marked this conversation as resolved.
Show resolved Hide resolved
pub struct MetricsCollector {
system: System,
}
impl MetricsCollector {
/// Instantiates a new `MetricsCollector`.
///
/// Responsable for collecting metrics about the System, such as
lemosep marked this conversation as resolved.
Show resolved Hide resolved
/// CPU Percentage Usage, Free Memory Space.
pub fn new() -> Self {
Self {
system: System::new_all(),
}
}

/// Get the `cpu_usage` and `free_memory` metrics from the current system.
///
/// When calling this method, will sleep the thread for
/// `CPU_DELAY_IN_MILLIS`.
pub fn get_metrics(&mut self) -> Metrics {
Metrics {
cpu_usage: self.get_cpu_usage(),
mem_total_mib: self.get_total_memory(),
mem_used_mib: self.get_used_memory(),
}
}

fn get_cpu_usage(&mut self) -> Vec<f64> {
lemosep marked this conversation as resolved.
Show resolved Hide resolved
self.system.refresh_cpu();
sleep(Duration::from_millis(CPU_DELAY_IN_MILLIS));
lffg marked this conversation as resolved.
Show resolved Hide resolved
self.system.refresh_cpu();

let all_cpus_usages: Vec<f64> = self
lemosep marked this conversation as resolved.
Show resolved Hide resolved
.system
.cpus()
.iter()
.map(|cpu| f64::from(cpu.cpu_usage()))
.collect();
lffg marked this conversation as resolved.
Show resolved Hide resolved

all_cpus_usages
}

fn get_total_memory(&mut self) -> f64 {
(self.get_total_memory_as_byte() / 1024 / 1024) as f64
}

fn get_total_memory_as_byte(&mut self) -> u64 {
self.system.refresh_memory();

self.system.total_memory()
}

fn get_used_memory(&mut self) -> f64 {
(self.get_used_memory_as_byte() / 1024 / 1024) as f64
}

fn get_used_memory_as_byte(&mut self) -> u64 {
self.system.used_memory()
}
}
34 changes: 34 additions & 0 deletions worker/src/monitor/metrics_sender.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use eyre::Result;
use proto::common::node::Metrics;
use reqwest::{Client, Url};

/// A connection pool that sends the collected `Metrics` to the `http`component.
pub struct MetricsSender {
client: Client,
base_url: String,
}

impl MetricsSender {
/// Instantiates a new `MetricsSender`.
///
/// Sends requests containing a JSON with the collected metrics
lemosep marked this conversation as resolved.
Show resolved Hide resolved
/// to the `http` component from `ctl` node.
pub fn new(url: &str) -> Result<Self> {
let client = Client::builder().build()?;

let base_url = Url::parse(url)?.to_string();

Ok(Self { client, base_url })
}

/// Sends a POST request containing `Metrics` to the `http` component.
pub async fn send_request(&mut self, metrics: &Metrics) -> Result<()> {
self.client
.post(&self.base_url)
.json(metrics)
.send()
.await?;

Ok(())
}
}
5 changes: 5 additions & 0 deletions worker/src/monitor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod metrics_collector;
mod metrics_sender;

pub use metrics_collector::*;
pub use metrics_sender::*;