@@ -72,6 +72,8 @@ impl MetricCollection {
7272 self . counters . get_value ( name, label_set)
7373 }
7474
75+ /// Increases the counter for the given metric name and labels.
76+ ///
7577 /// # Errors
7678 ///
7779 /// Return an error if a metrics of a different type with the same name
@@ -93,6 +95,30 @@ impl MetricCollection {
9395 Ok ( ( ) )
9496 }
9597
98+ /// Sets the counter for the given metric name and labels.
99+ ///
100+ /// # Errors
101+ ///
102+ /// Return an error if a metrics of a different type with the same name
103+ /// already exists.
104+ pub fn set_counter (
105+ & mut self ,
106+ name : & MetricName ,
107+ label_set : & LabelSet ,
108+ value : u64 ,
109+ time : DurationSinceUnixEpoch ,
110+ ) -> Result < ( ) , Error > {
111+ if self . gauges . metrics . contains_key ( name) {
112+ return Err ( Error :: MetricNameCollisionAdding {
113+ metric_name : name. clone ( ) ,
114+ } ) ;
115+ }
116+
117+ self . counters . absolute ( name, label_set, value, time) ;
118+
119+ Ok ( ( ) )
120+ }
121+
96122 pub fn ensure_counter_exists ( & mut self , name : & MetricName ) {
97123 self . counters . ensure_metric_exists ( name) ;
98124 }
@@ -361,7 +387,7 @@ impl MetricKindCollection<Counter> {
361387 ///
362388 /// # Panics
363389 ///
364- /// Panics if the metric does not exist and it could not be created .
390+ /// Panics if the metric does not exist.
365391 pub fn increment ( & mut self , name : & MetricName , label_set : & LabelSet , time : DurationSinceUnixEpoch ) {
366392 self . ensure_metric_exists ( name) ;
367393
@@ -370,6 +396,21 @@ impl MetricKindCollection<Counter> {
370396 metric. increment ( label_set, time) ;
371397 }
372398
399+ /// Sets the counter to an absolute value for the given metric name and labels.
400+ ///
401+ /// If the metric name does not exist, it will be created.
402+ ///
403+ /// # Panics
404+ ///
405+ /// Panics if the metric does not exist.
406+ pub fn absolute ( & mut self , name : & MetricName , label_set : & LabelSet , value : u64 , time : DurationSinceUnixEpoch ) {
407+ self . ensure_metric_exists ( name) ;
408+
409+ let metric = self . metrics . get_mut ( name) . expect ( "Counter metric should exist" ) ;
410+
411+ metric. absolute ( label_set, value, time) ;
412+ }
413+
373414 #[ must_use]
374415 pub fn get_value ( & self , name : & MetricName , label_set : & LabelSet ) -> Option < Counter > {
375416 self . metrics
0 commit comments