Skip to content
Open
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
24 changes: 23 additions & 1 deletion crates/mofa-monitoring/src/dashboard/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ use sysinfo::{Pid, System};
use tokio::sync::RwLock;
use tracing::{debug, info};

fn thread_count_from_tasks(tasks: Option<&std::collections::HashSet<Pid>>) -> u32 {
tasks
.map(|set| u32::try_from(set.len()).unwrap_or(u32::MAX))
.unwrap_or(0)
}

/// Metric type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MetricType {
Expand Down Expand Up @@ -598,7 +604,7 @@ impl MetricsCollector {
(
p.cpu_usage() as f64,
p.memory(),
p.tasks().iter().count() as u32,
thread_count_from_tasks(p.tasks()),
)
})
.unwrap_or((0.0, 0, 0));
Expand Down Expand Up @@ -753,6 +759,7 @@ impl MetricsCollector {
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;

#[tokio::test]
async fn test_counter() {
Expand Down Expand Up @@ -832,4 +839,19 @@ mod tests {
assert_eq!(snapshot.agents.len(), 1);
assert_eq!(snapshot.agents[0].agent_id, "agent-1");
}

#[test]
fn test_thread_count_from_tasks_none_returns_zero() {
assert_eq!(thread_count_from_tasks(None), 0);
}

#[test]
fn test_thread_count_from_tasks_counts_inner_set() {
let mut tasks = HashSet::new();
tasks.insert(Pid::from_u32(1));
tasks.insert(Pid::from_u32(2));
tasks.insert(Pid::from_u32(3));

assert_eq!(thread_count_from_tasks(Some(&tasks)), 3);
}
}
Loading