@@ -7,7 +7,6 @@ use prometheus_client::encoding::EncodeLabelSet;
7
7
use prometheus_client:: metrics:: counter:: Counter ;
8
8
use prometheus_client:: metrics:: family:: Family ;
9
9
use prometheus_client:: metrics:: gauge:: Gauge ;
10
- use prometheus_client:: metrics:: info:: Info ;
11
10
use smol:: lock:: Mutex ;
12
11
13
12
use super :: cpu:: { CpuUsage , LoadAverages } ;
@@ -414,22 +413,33 @@ impl Metric for SystemdUnitStateCollector {
414
413
#[ derive( Debug , Default ) ]
415
414
#[ allow( clippy:: type_complexity) ]
416
415
pub struct NodeInfoCollector {
417
- metric : std :: sync :: Mutex < Option < Info < Vec < ( & ' static str , String ) > > > > ,
416
+ metric : Family < NodeInfoLabels , Gauge > ,
418
417
uname : Mutex < Option < NodeInfo > > ,
419
418
}
420
419
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
+
421
430
impl Metric for NodeInfoCollector {
422
431
fn init ( & self , _options : hashbrown:: HashMap < String , String > ) -> DynFuture < ' _ , Result < ( ) > > {
423
432
Box :: pin ( async move {
424
433
let info = NodeInfo :: new ( ) ?;
425
434
426
435
{
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 ) ;
433
443
}
434
444
435
445
{
@@ -443,14 +453,9 @@ impl Metric for NodeInfoCollector {
443
453
444
454
fn register ( & self , registry : & mut prometheus_client:: registry:: Registry ) {
445
455
registry. register (
446
- // The `_info` is automatically appended.
447
- "litemon_node" ,
456
+ "litemon_node_info" ,
448
457
"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 ( ) ,
454
459
)
455
460
}
456
461
0 commit comments