@@ -343,8 +343,22 @@ impl BackupCmd {
343
343
344
344
#[ cfg( feature = "prometheus" ) ]
345
345
if config. global . is_prometheus_configured ( ) {
346
- if let Err ( err) = publish_metrics ( & snap, self . prometheus_job , self . prometheus_labels ) {
347
- warn ! ( "error pushing prometheus metrics: {err}" ) ;
346
+ #[ cfg( feature = "prometheus" ) ]
347
+ {
348
+ // Merge global prometheus labels
349
+ conflate:: btreemap:: append_or_ignore (
350
+ & mut self . prometheus_labels ,
351
+ config. global . prometheus_labels . clone ( ) ,
352
+ ) ;
353
+ if let Err ( err) =
354
+ publish_metrics ( & snap, self . prometheus_job , self . prometheus_labels )
355
+ {
356
+ warn ! ( "error pushing prometheus metrics: {err}" ) ;
357
+ }
358
+ }
359
+ #[ cfg( not( feature = "prometheus" ) ) ]
360
+ {
361
+ warn ! ( "not pushing prometheus metrics - this rustic version is compiled without prometheus support" ) ;
348
362
}
349
363
}
350
364
@@ -357,7 +371,7 @@ impl BackupCmd {
357
371
fn publish_metrics (
358
372
snap : & SnapshotFile ,
359
373
job_name : Option < String > ,
360
- extra_labels : BTreeMap < String , String > ,
374
+ mut labels : BTreeMap < String , String > ,
361
375
) -> Result < ( ) > {
362
376
use prometheus:: register_gauge;
363
377
@@ -498,12 +512,18 @@ fn publish_metrics(
498
512
. context ( "registering prometheus gauge" ) ?;
499
513
metric_total_duration. set ( summary. total_duration ) ;
500
514
501
- let mut labels = Vec :: new ( ) ;
502
- labels. push ( ( "paths" . to_owned ( ) , format ! ( "{}" , snap. paths) ) ) ;
503
- labels. push ( ( "hostname" . to_owned ( ) , snap. hostname . clone ( ) ) ) ;
504
- labels. push ( ( "snapshot_label" . to_string ( ) , snap. label . clone ( ) ) ) ;
505
- labels. push ( ( "tags" . to_string ( ) , format ! ( "{}" , snap. tags) ) ) ;
506
- labels. extend ( extra_labels) ;
515
+ _ = labels
516
+ . entry ( "paths" . to_string ( ) )
517
+ . or_insert_with ( || format ! ( "{}" , snap. paths) ) ;
518
+ _ = labels
519
+ . entry ( "hostname" . to_owned ( ) )
520
+ . or_insert_with ( || snap. hostname . clone ( ) ) ;
521
+ _ = labels
522
+ . entry ( "snapshot_label" . to_string ( ) )
523
+ . or_insert_with ( || snap. label . clone ( ) ) ;
524
+ _ = labels
525
+ . entry ( "tags" . to_string ( ) )
526
+ . or_insert_with ( || format ! ( "{}" , snap. tags) ) ;
507
527
508
528
let job_name = job_name. as_deref ( ) . unwrap_or ( "rustic_backup" ) ;
509
529
RUSTIC_APP . config ( ) . global . push_metrics ( job_name, labels)
0 commit comments