Skip to content

Commit fbdbd49

Browse files
committed
feat: add uptime metric
1 parent 87c5aa5 commit fbdbd49

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/collector.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use smol::lock::RwLock;
1212

1313
use crate::config::UserConfig;
1414
use crate::metrics::collector::{
15-
CpuStatsCollector, DiskStatsCollector, FilesystemStatsCollector, MemoryStatsCollector,
16-
NetworkStatsCollector, NodeInfoCollector, PressureCollector, SystemdUnitStateCollector,
15+
CpuStatsCollector, DiskStatsCollector, FilesystemStatsCollector, MemoryStatsCollector, NetworkStatsCollector, NodeInfoCollector, NodeUptimeCollector, PressureCollector, SystemdUnitStateCollector
1716
};
1817
use crate::metrics::Metric;
1918

@@ -54,6 +53,10 @@ impl Collector {
5453
let collector = Box::new(NodeInfoCollector::new()?);
5554
inner.metrics.push(collector);
5655
}
56+
{
57+
let collector = Box::new(NodeUptimeCollector::default());
58+
inner.metrics.push(collector);
59+
}
5760

5861
if metrics.cpu_seconds.enabled || metrics.loadavg.enabled {
5962
// TODO: Fix disabling of CPU seconds.

src/metrics/collector.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,6 @@ struct NodeInfoLabels {
411411
hostname: String,
412412
/// CPU architecture
413413
arch: String,
414-
/// Uptime as seconds
415-
uptime: String,
416414
}
417415

418416
impl NodeInfoCollector {
@@ -422,7 +420,6 @@ impl NodeInfoCollector {
422420
let labels = NodeInfoLabels {
423421
hostname: info.hostname.clone(),
424422
arch: info.arch.clone(),
425-
uptime: format!("{}", info.uptime.as_secs()),
426423
};
427424
let metric = Family::<NodeInfoLabels, Gauge>::default();
428425
metric.get_or_create(&labels).set(1);
@@ -572,3 +569,26 @@ impl Metric for DiskStatsCollector {
572569
})
573570
}
574571
}
572+
573+
#[derive(Debug, Default)]
574+
pub struct NodeUptimeCollector {
575+
metric: Gauge<u64, AtomicU64>,
576+
}
577+
578+
impl Metric for NodeUptimeCollector {
579+
fn register(&self, registry: &mut prometheus_client::registry::Registry) {
580+
registry.register(
581+
"litemon_node_uptime",
582+
"Uptime in seconds",
583+
self.metric.clone(),
584+
);
585+
}
586+
587+
fn collect(&self) -> DynFuture<'_, Result<()>> {
588+
Box::pin(async move {
589+
let info = NodeInfo::new()?;
590+
self.metric.set(info.uptime.as_secs());
591+
Ok(())
592+
})
593+
}
594+
}

0 commit comments

Comments
 (0)