@@ -10,6 +10,7 @@ use super::label::LabelSet;
1010use super :: metric:: { Metric , MetricName } ;
1111use super :: prometheus:: PrometheusSerializable ;
1212use crate :: metric:: description:: MetricDescription ;
13+ use crate :: sample_collection:: SampleCollection ;
1314use crate :: unit:: Unit ;
1415use crate :: METRICS_TARGET ;
1516
@@ -56,12 +57,12 @@ impl MetricCollection {
5657
5758 // Counter-specific methods
5859
59- pub fn describe_counter ( & mut self , name : & MetricName , opt_unit : Option < Unit > , opt_description : Option < & MetricDescription > ) {
60+ pub fn describe_counter ( & mut self , name : & MetricName , opt_unit : Option < Unit > , opt_description : Option < MetricDescription > ) {
6061 tracing:: info!( target: METRICS_TARGET , type = "counter" , name = name. to_string( ) , unit = ?opt_unit, description = ?opt_description) ;
6162
62- let metric = Metric :: < Counter > :: without_samples ( name. clone ( ) ) ;
63+ let metric = Metric :: < Counter > :: new ( name. clone ( ) , opt_unit , opt_description , SampleCollection :: default ( ) ) ;
6364
64- self . counters . ensure_metric_exists ( metric) ;
65+ self . counters . insert ( metric) ;
6566 }
6667
6768 #[ must_use]
@@ -123,12 +124,12 @@ impl MetricCollection {
123124
124125 // Gauge-specific methods
125126
126- pub fn describe_gauge ( & mut self , name : & MetricName , opt_unit : Option < Unit > , opt_description : Option < & MetricDescription > ) {
127+ pub fn describe_gauge ( & mut self , name : & MetricName , opt_unit : Option < Unit > , opt_description : Option < MetricDescription > ) {
127128 tracing:: info!( target: METRICS_TARGET , type = "gauge" , name = name. to_string( ) , unit = ?opt_unit, description = ?opt_description) ;
128129
129- let metric = Metric :: < Gauge > :: without_samples ( name. clone ( ) ) ;
130+ let metric = Metric :: < Gauge > :: new ( name. clone ( ) , opt_unit , opt_description , SampleCollection :: default ( ) ) ;
130131
131- self . gauges . ensure_metric_exists ( metric) ;
132+ self . gauges . insert ( metric) ;
132133 }
133134
134135 #[ must_use]
@@ -333,11 +334,15 @@ impl<T> MetricKindCollection<T> {
333334 self . metrics . keys ( )
334335 }
335336
336- pub fn ensure_metric_exists ( & mut self , metric : Metric < T > ) {
337+ pub fn insert_if_absent ( & mut self , metric : Metric < T > ) {
337338 if !self . metrics . contains_key ( metric. name ( ) ) {
338- self . metrics . insert ( metric . name ( ) . clone ( ) , metric) ;
339+ self . insert ( metric) ;
339340 }
340341 }
342+
343+ pub fn insert ( & mut self , metric : Metric < T > ) {
344+ self . metrics . insert ( metric. name ( ) . clone ( ) , metric) ;
345+ }
341346}
342347
343348impl < T : Clone > MetricKindCollection < T > {
@@ -377,9 +382,9 @@ impl MetricKindCollection<Counter> {
377382 ///
378383 /// Panics if the metric does not exist.
379384 pub fn increment ( & mut self , name : & MetricName , label_set : & LabelSet , time : DurationSinceUnixEpoch ) {
380- let metric = Metric :: < Counter > :: without_samples ( name. clone ( ) ) ;
385+ let metric = Metric :: < Counter > :: new_empty_with_name ( name. clone ( ) ) ;
381386
382- self . ensure_metric_exists ( metric) ;
387+ self . insert_if_absent ( metric) ;
383388
384389 let metric = self . metrics . get_mut ( name) . expect ( "Counter metric should exist" ) ;
385390
@@ -394,9 +399,9 @@ impl MetricKindCollection<Counter> {
394399 ///
395400 /// Panics if the metric does not exist.
396401 pub fn absolute ( & mut self , name : & MetricName , label_set : & LabelSet , value : u64 , time : DurationSinceUnixEpoch ) {
397- let metric = Metric :: < Counter > :: without_samples ( name. clone ( ) ) ;
402+ let metric = Metric :: < Counter > :: new_empty_with_name ( name. clone ( ) ) ;
398403
399- self . ensure_metric_exists ( metric) ;
404+ self . insert_if_absent ( metric) ;
400405
401406 let metric = self . metrics . get_mut ( name) . expect ( "Counter metric should exist" ) ;
402407
@@ -421,9 +426,9 @@ impl MetricKindCollection<Gauge> {
421426 ///
422427 /// Panics if the metric does not exist and it could not be created.
423428 pub fn set ( & mut self , name : & MetricName , label_set : & LabelSet , value : f64 , time : DurationSinceUnixEpoch ) {
424- let metric = Metric :: < Gauge > :: without_samples ( name. clone ( ) ) ;
429+ let metric = Metric :: < Gauge > :: new_empty_with_name ( name. clone ( ) ) ;
425430
426- self . ensure_metric_exists ( metric) ;
431+ self . insert_if_absent ( metric) ;
427432
428433 let metric = self . metrics . get_mut ( name) . expect ( "Gauge metric should exist" ) ;
429434
@@ -438,9 +443,9 @@ impl MetricKindCollection<Gauge> {
438443 ///
439444 /// Panics if the metric does not exist and it could not be created.
440445 pub fn increment ( & mut self , name : & MetricName , label_set : & LabelSet , time : DurationSinceUnixEpoch ) {
441- let metric = Metric :: < Gauge > :: without_samples ( name. clone ( ) ) ;
446+ let metric = Metric :: < Gauge > :: new_empty_with_name ( name. clone ( ) ) ;
442447
443- self . ensure_metric_exists ( metric) ;
448+ self . insert_if_absent ( metric) ;
444449
445450 let metric = self . metrics . get_mut ( name) . expect ( "Gauge metric should exist" ) ;
446451
@@ -455,9 +460,9 @@ impl MetricKindCollection<Gauge> {
455460 ///
456461 /// Panics if the metric does not exist and it could not be created.
457462 pub fn decrement ( & mut self , name : & MetricName , label_set : & LabelSet , time : DurationSinceUnixEpoch ) {
458- let metric = Metric :: < Gauge > :: without_samples ( name. clone ( ) ) ;
463+ let metric = Metric :: < Gauge > :: new_empty_with_name ( name. clone ( ) ) ;
459464
460- self . ensure_metric_exists ( metric) ;
465+ self . insert_if_absent ( metric) ;
461466
462467 let metric = self . metrics . get_mut ( name) . expect ( "Gauge metric should exist" ) ;
463468
@@ -523,11 +528,15 @@ mod tests {
523528 MetricCollection :: new (
524529 MetricKindCollection :: new ( vec ! [ Metric :: new(
525530 metric_name!( "http_tracker_core_announce_requests_received_total" ) ,
531+ None ,
532+ None ,
526533 SampleCollection :: new( vec![ Sample :: new( Counter :: new( 1 ) , time, label_set_1. clone( ) ) ] ) . unwrap( ) ,
527534 ) ] )
528535 . unwrap ( ) ,
529536 MetricKindCollection :: new ( vec ! [ Metric :: new(
530537 metric_name!( "udp_tracker_server_performance_avg_announce_processing_time_ns" ) ,
538+ None ,
539+ None ,
531540 SampleCollection :: new( vec![ Sample :: new( Gauge :: new( 1.0 ) , time, label_set_1. clone( ) ) ] ) . unwrap( ) ,
532541 ) ] )
533542 . unwrap ( ) ,
@@ -541,6 +550,8 @@ mod tests {
541550 {
542551 "type":"counter",
543552 "name":"http_tracker_core_announce_requests_received_total",
553+ "unit": null,
554+ "description": null,
544555 "samples":[
545556 {
546557 "value":1,
@@ -565,6 +576,8 @@ mod tests {
565576 {
566577 "type":"gauge",
567578 "name":"udp_tracker_server_performance_avg_announce_processing_time_ns",
579+ "unit": null,
580+ "description": null,
568581 "samples":[
569582 {
570583 "value":1.0,
@@ -603,10 +616,20 @@ mod tests {
603616
604617 #[ test]
605618 fn it_should_not_allow_duplicate_names_across_types ( ) {
606- let counters =
607- MetricKindCollection :: new ( vec ! [ Metric :: new( metric_name!( "test_metric" ) , SampleCollection :: default ( ) ) ] ) . unwrap ( ) ;
608- let gauges =
609- MetricKindCollection :: new ( vec ! [ Metric :: new( metric_name!( "test_metric" ) , SampleCollection :: default ( ) ) ] ) . unwrap ( ) ;
619+ let counters = MetricKindCollection :: new ( vec ! [ Metric :: new(
620+ metric_name!( "test_metric" ) ,
621+ None ,
622+ None ,
623+ SampleCollection :: default ( ) ,
624+ ) ] )
625+ . unwrap ( ) ;
626+ let gauges = MetricKindCollection :: new ( vec ! [ Metric :: new(
627+ metric_name!( "test_metric" ) ,
628+ None ,
629+ None ,
630+ SampleCollection :: default ( ) ,
631+ ) ] )
632+ . unwrap ( ) ;
610633
611634 assert ! ( MetricCollection :: new( counters, gauges) . is_err( ) ) ;
612635 }
@@ -699,6 +722,8 @@ mod tests {
699722 let metric_collection = MetricCollection :: new (
700723 MetricKindCollection :: new ( vec ! [ Metric :: new(
701724 metric_name!( "http_tracker_core_announce_requests_received_total" ) ,
725+ None ,
726+ None ,
702727 SampleCollection :: new( vec![
703728 Sample :: new( Counter :: new( 1 ) , time, label_set_1. clone( ) ) ,
704729 Sample :: new( Counter :: new( 2 ) , time, label_set_2. clone( ) ) ,
@@ -730,11 +755,11 @@ mod tests {
730755 let mut counters = MetricKindCollection :: default ( ) ;
731756 let mut gauges = MetricKindCollection :: default ( ) ;
732757
733- let counter = Metric :: < Counter > :: without_samples ( metric_name ! ( "test_counter" ) ) ;
734- counters. ensure_metric_exists ( counter) ;
758+ let counter = Metric :: < Counter > :: new_empty_with_name ( metric_name ! ( "test_counter" ) ) ;
759+ counters. insert_if_absent ( counter) ;
735760
736- let gauge = Metric :: < Gauge > :: without_samples ( metric_name ! ( "test_gauge" ) ) ;
737- gauges. ensure_metric_exists ( gauge) ;
761+ let gauge = Metric :: < Gauge > :: new_empty_with_name ( metric_name ! ( "test_gauge" ) ) ;
762+ gauges. insert_if_absent ( gauge) ;
738763
739764 let metric_collection = MetricCollection :: new ( counters, gauges) . unwrap ( ) ;
740765
@@ -760,6 +785,8 @@ mod tests {
760785 let mut metric_collection = MetricCollection :: new (
761786 MetricKindCollection :: new ( vec ! [ Metric :: new(
762787 metric_name!( "test_counter" ) ,
788+ None ,
789+ None ,
763790 SampleCollection :: new( vec![ Sample :: new( Counter :: new( 0 ) , time, label_set. clone( ) ) ] ) . unwrap( ) ,
764791 ) ] )
765792 . unwrap ( ) ,
@@ -819,10 +846,14 @@ mod tests {
819846 let result = MetricKindCollection :: new ( vec ! [
820847 Metric :: new(
821848 metric_name!( "test_counter" ) ,
849+ None ,
850+ None ,
822851 SampleCollection :: new( vec![ Sample :: new( Counter :: new( 0 ) , time, label_set. clone( ) ) ] ) . unwrap( ) ,
823852 ) ,
824853 Metric :: new(
825854 metric_name!( "test_counter" ) ,
855+ None ,
856+ None ,
826857 SampleCollection :: new( vec![ Sample :: new( Counter :: new( 0 ) , time, label_set. clone( ) ) ] ) . unwrap( ) ,
827858 ) ,
828859 ] ) ;
@@ -849,6 +880,8 @@ mod tests {
849880 MetricKindCollection :: default ( ) ,
850881 MetricKindCollection :: new ( vec ! [ Metric :: new(
851882 metric_name!( "test_gauge" ) ,
883+ None ,
884+ None ,
852885 SampleCollection :: new( vec![ Sample :: new( Gauge :: new( 0.0 ) , time, label_set. clone( ) ) ] ) . unwrap( ) ,
853886 ) ] )
854887 . unwrap ( ) ,
@@ -901,10 +934,14 @@ mod tests {
901934 let result = MetricKindCollection :: new ( vec ! [
902935 Metric :: new(
903936 metric_name!( "test_gauge" ) ,
937+ None ,
938+ None ,
904939 SampleCollection :: new( vec![ Sample :: new( Gauge :: new( 0.0 ) , time, label_set. clone( ) ) ] ) . unwrap( ) ,
905940 ) ,
906941 Metric :: new(
907942 metric_name!( "test_gauge" ) ,
943+ None ,
944+ None ,
908945 SampleCollection :: new( vec![ Sample :: new( Gauge :: new( 0.0 ) , time, label_set. clone( ) ) ] ) . unwrap( ) ,
909946 ) ,
910947 ] ) ;
0 commit comments