@@ -9,7 +9,6 @@ use super::gauge::Gauge;
99use super :: label_set:: LabelSet ;
1010use super :: metric:: { Metric , MetricName } ;
1111use super :: prometheus:: PrometheusSerializable ;
12- use super :: sample:: Sample ;
1312use crate :: sample_collection:: SampleCollection ;
1413
1514#[ derive( Debug , Clone , Default , PartialEq ) ]
@@ -160,11 +159,11 @@ impl MetricKindCollection<Counter> {
160159 ///
161160 /// # Panics
162161 ///
163- /// Panics if the metric name already exists in the collection .
162+ /// Panics if the metric does not exist and it could not be created .
164163 pub fn increment ( & mut self , name : & MetricName , labels : & LabelSet , time : DurationSinceUnixEpoch ) {
165164 self . crate_metric_if_does_not_exist ( name) ;
166165
167- let metric = self . metrics . get_mut ( name) . unwrap ( ) ;
166+ let metric = self . metrics . get_mut ( name) . expect ( "Counter metric should exist" ) ;
168167
169168 metric. increment ( labels, time) ;
170169 }
@@ -179,29 +178,19 @@ impl MetricKindCollection<Counter> {
179178}
180179
181180impl MetricKindCollection < Gauge > {
181+ /// Sets the gauge for the given metric name and labels.
182+ ///
183+ /// If the metric name does not exist, it will be created.
184+ ///
182185 /// # Panics
183186 ///
184- /// Panics if the metric name already exists in the collection .
187+ /// Panics if the metric does not exist and it could not be created .
185188 pub fn set ( & mut self , name : & MetricName , labels : & LabelSet , value : f64 , time : DurationSinceUnixEpoch ) {
186189 self . crate_metric_if_does_not_exist ( name) ;
187190
188- let metric = self . metrics . get_mut ( name) . unwrap ( ) ;
191+ let metric = self . metrics . get_mut ( name) . expect ( "Gauge metric should exist" ) ;
189192
190- // Use entry API to handle existing or new sample
191- match metric. sample_collection . samples . entry ( labels. clone ( ) ) {
192- std:: collections:: hash_map:: Entry :: Occupied ( mut entry) => {
193- let sample = entry. get_mut ( ) ;
194- sample. value . set ( value) ;
195- sample. update_at = time;
196- }
197- std:: collections:: hash_map:: Entry :: Vacant ( entry) => {
198- entry. insert ( Sample {
199- value : Gauge :: new ( value) ,
200- update_at : time,
201- labels : labels. clone ( ) ,
202- } ) ;
203- }
204- }
193+ metric. set ( labels, value, time) ;
205194 }
206195
207196 #[ must_use]
0 commit comments