@@ -158,31 +158,24 @@ impl<T> MetricKindCollection<T> {
158158}
159159
160160impl MetricKindCollection < Counter > {
161+ /// Increments the counter for the given metric name and labels.
162+ ///
163+ /// If the metric name does not exist, it will be created.
164+ ///
161165 /// # Panics
162166 ///
163167 /// Panics if the metric name already exists in the collection.
164168 pub fn increment ( & mut self , name : & MetricName , labels : & LabelSet , time : DurationSinceUnixEpoch ) {
165- // Create the metric if it doesn't exist
166169 if !self . metrics . contains_key ( name) {
167- let new_metric = Metric :: new ( name. clone ( ) , SampleCollection { samples : HashMap :: new ( ) } ) ;
168- self . metrics . insert ( name. clone ( ) , new_metric) ;
170+ self . metrics . insert (
171+ name. clone ( ) ,
172+ Metric :: new ( name. clone ( ) , SampleCollection { samples : HashMap :: new ( ) } ) ,
173+ ) ;
169174 }
170175
171176 let metric = self . metrics . get_mut ( name) . unwrap ( ) ;
172177
173- // Use entry API to handle existing or new sample
174- let sample = metric
175- . sample_collection
176- . samples
177- . entry ( labels. clone ( ) )
178- . or_insert_with ( || Sample {
179- value : Counter :: new ( 0 ) ,
180- update_at : time,
181- labels : labels. clone ( ) ,
182- } ) ;
183-
184- sample. value . increment ( 1 ) ;
185- sample. update_at = time;
178+ metric. increment ( labels, time) ;
186179 }
187180
188181 #[ must_use]
@@ -242,17 +235,33 @@ mod tests {
242235 use crate :: metrics:: sample:: Sample ;
243236
244237 #[ test]
245- fn test_increase_counter_in_metric_collection ( ) {
238+ fn it_should_increase_a_preexistent_counter ( ) {
246239 let time = DurationSinceUnixEpoch :: from_secs ( 1_743_552_000 ) ;
247240 let label_set: LabelSet = ( LabelName :: new ( "label_name" ) , LabelValue :: new ( "value" ) ) . into ( ) ;
248241
249- let mut metric_collection = MetricCollection {
250- counters : MetricKindCollection :: new ( vec ! [ Metric :: new(
242+ let mut metric_collection = MetricCollection :: new (
243+ MetricKindCollection :: new ( vec ! [ Metric :: new(
251244 MetricName :: new( "test_counter" ) ,
252245 SampleCollection :: new( vec![ Sample :: new( Counter :: new( 0 ) , time, label_set. clone( ) ) ] ) ,
253246 ) ] ) ,
254- gauges : MetricKindCollection :: new ( vec ! [ ] ) ,
255- } ;
247+ MetricKindCollection :: new ( vec ! [ ] ) ,
248+ ) ;
249+
250+ metric_collection. increase_counter ( & MetricName :: new ( "test_counter" ) , & label_set, time) ;
251+ metric_collection. increase_counter ( & MetricName :: new ( "test_counter" ) , & label_set, time) ;
252+
253+ assert_eq ! (
254+ metric_collection. get_counter_value( & MetricName :: new( "test_counter" ) , & label_set) ,
255+ 2
256+ ) ;
257+ }
258+
259+ #[ test]
260+ fn it_should_automatically_create_a_counter_when_increasing_if_it_does_not_exist ( ) {
261+ let time = DurationSinceUnixEpoch :: from_secs ( 1_743_552_000 ) ;
262+ let label_set: LabelSet = ( LabelName :: new ( "label_name" ) , LabelValue :: new ( "value" ) ) . into ( ) ;
263+
264+ let mut metric_collection = MetricCollection :: new ( MetricKindCollection :: new ( vec ! [ ] ) , MetricKindCollection :: new ( vec ! [ ] ) ) ;
256265
257266 metric_collection. increase_counter ( & MetricName :: new ( "test_counter" ) , & label_set, time) ;
258267 metric_collection. increase_counter ( & MetricName :: new ( "test_counter" ) , & label_set, time) ;
0 commit comments