Skip to content

Commit 5bd6446

Browse files
committed
fix: use gauge for the _info metrics
The `info` OpenMetrics type is not supported by some versions of Prometheus.
1 parent 47911a1 commit 5bd6446

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/metrics/collector.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use prometheus_client::encoding::EncodeLabelSet;
77
use prometheus_client::metrics::counter::Counter;
88
use prometheus_client::metrics::family::Family;
99
use prometheus_client::metrics::gauge::Gauge;
10-
use prometheus_client::metrics::info::Info;
1110
use smol::lock::Mutex;
1211

1312
use super::cpu::{CpuUsage, LoadAverages};
@@ -414,22 +413,33 @@ impl Metric for SystemdUnitStateCollector {
414413
#[derive(Debug, Default)]
415414
#[allow(clippy::type_complexity)]
416415
pub struct NodeInfoCollector {
417-
metric: std::sync::Mutex<Option<Info<Vec<(&'static str, String)>>>>,
416+
metric: Family<NodeInfoLabels, Gauge>,
418417
uname: Mutex<Option<NodeInfo>>,
419418
}
420419

420+
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
421+
struct NodeInfoLabels {
422+
/// Hostname
423+
hostname: String,
424+
/// CPU architecture
425+
arch: String,
426+
/// Uptime as seconds
427+
uptime: String,
428+
}
429+
421430
impl Metric for NodeInfoCollector {
422431
fn init(&self, _options: hashbrown::HashMap<String, String>) -> DynFuture<'_, Result<()>> {
423432
Box::pin(async move {
424433
let info = NodeInfo::new()?;
425434

426435
{
427-
let mut lock = self.metric.lock().expect("not poisoned");
428-
*lock = Some(Info::new(vec![
429-
("hostname", info.hostname.clone()),
430-
("arch", info.arch.clone()),
431-
("uptime", format!("{}", info.uptime.as_secs())),
432-
]))
436+
let labels = NodeInfoLabels {
437+
hostname: info.hostname.clone(),
438+
arch: info.arch.clone(),
439+
uptime: format!("{}", info.uptime.as_secs()),
440+
};
441+
let metric = self.metric.get_or_create(&labels);
442+
metric.set(1);
433443
}
434444

435445
{
@@ -443,14 +453,9 @@ impl Metric for NodeInfoCollector {
443453

444454
fn register(&self, registry: &mut prometheus_client::registry::Registry) {
445455
registry.register(
446-
// The `_info` is automatically appended.
447-
"litemon_node",
456+
"litemon_node_info",
448457
"System information about the node",
449-
self.metric
450-
.lock()
451-
.expect("not poisoned")
452-
.take()
453-
.expect("must be initialized"),
458+
self.metric.clone(),
454459
)
455460
}
456461

0 commit comments

Comments
 (0)