@@ -240,27 +240,24 @@ Raises:
240
240
Count :: integer ().
241
241
observe_n (Registry , Name , LabelValues , Value , Count ) when is_integer (Value ), is_integer (Count ) ->
242
242
Key = key (Registry , Name , LabelValues ),
243
- case ets :lookup (? TABLE , Key ) of
244
- [Metric ] ->
245
- BucketPosition = calculate_histogram_bucket_position (Metric , Value ),
246
- ets :update_counter (
247
- ? TABLE ,
248
- Key ,
249
- [
250
- {? ISUM_POS , Value * Count },
251
- {? BUCKETS_START + BucketPosition , Count }
252
- ]
253
- );
254
- [] ->
243
+ try ets :lookup_element (? TABLE , Key , ? BOUNDS_POS ) of
244
+ Buckets ->
245
+ BucketPosition = prometheus_buckets :position (Buckets , Value ),
246
+ Spec = [{? ISUM_POS , Value * Count }, {? BUCKETS_START + BucketPosition , Count }],
247
+ ets :update_counter (? TABLE , Key , Spec )
248
+ catch
249
+ error :badarg ->
255
250
insert_metric (Registry , Name , LabelValues , Value , Count , fun observe_n /5 )
256
251
end ,
257
252
ok ;
258
253
observe_n (Registry , Name , LabelValues , Value , Count ) when is_float (Value ), is_integer (Count ) ->
259
254
Key = key (Registry , Name , LabelValues ),
260
- case ets :lookup (? TABLE , Key ) of
261
- [Metric ] ->
262
- fobserve_impl (Key , Metric , Value , Count );
263
- [] ->
255
+ try ets :lookup_element (? TABLE , Key , ? BOUNDS_POS ) of
256
+ Buckets ->
257
+ BucketPosition = prometheus_buckets :position (Buckets , Value ),
258
+ fobserve_impl (Key , Buckets , BucketPosition , Value , Count )
259
+ catch
260
+ error :badarg ->
264
261
insert_metric (Registry , Name , LabelValues , Value , Count , fun observe_n /5 )
265
262
end ;
266
263
observe_n (_Registry , _Name , _LabelValues , Value , Count ) when is_number (Value ) ->
@@ -438,7 +435,6 @@ Raises:
438
435
{number (), infinity | number ()} | undefined .
439
436
value (Registry , Name , LabelValues ) ->
440
437
MF = prometheus_metric :check_mf_exists (? TABLE , Registry , Name , LabelValues ),
441
-
442
438
RawValues = [
443
439
ets :lookup (? TABLE , {Registry , Name , LabelValues , Scheduler })
444
440
|| Scheduler <- schedulers_seq ()
@@ -535,11 +531,6 @@ insert_metric(Registry, Name, LabelValues, Value, Count, CB) ->
535
531
insert_placeholders (Registry , Name , LabelValues ),
536
532
CB (Registry , Name , LabelValues , Value , Count ).
537
533
538
- fobserve_impl (Key , Metric , Value , Count ) ->
539
- Buckets = metric_buckets (Metric ),
540
- BucketPos = calculate_histogram_bucket_position (Metric , Value ),
541
- fobserve_impl (Key , Buckets , BucketPos , Value , Count ).
542
-
543
534
fobserve_impl (Key , Buckets , BucketPos , Value , Count ) ->
544
535
ets :select_replace (? TABLE , generate_select_replace (Key , Buckets , BucketPos , Value , Count )).
545
536
@@ -551,33 +542,33 @@ insert_placeholders(Registry, Name, LabelValues) ->
551
542
prometheus_time :maybe_convert_to_native (DU , Bucket )
552
543
end ,
553
544
BoundCounters = lists :duplicate (length (MFBuckets ), 0 ),
545
+ Buckets = list_to_tuple (lists :map (Fun , MFBuckets )),
554
546
MetricSpec =
555
- [key (Registry , Name , LabelValues ), lists :map (Fun , MFBuckets ), 0 , 0 ] ++
556
- BoundCounters ,
547
+ [key (Registry , Name , LabelValues ), Buckets , 0 , 0 | BoundCounters ],
557
548
ets :insert_new (? TABLE , list_to_tuple (MetricSpec )).
558
549
559
- calculate_histogram_bucket_position (Metric , Value ) ->
560
- Buckets = metric_buckets (Metric ),
561
- prometheus_buckets :position (Buckets , Value ).
562
-
563
550
generate_select_replace (Key , Bounds , BucketPos , Value , Count ) ->
564
551
BoundPlaceholders = gen_query_bound_placeholders (Bounds ),
565
- HistMatch = list_to_tuple ([Key , '$2' , '$3' , '$4' ] ++ BoundPlaceholders ),
552
+ HistMatch = list_to_tuple ([Key , '$2' , '$3' , '$4' | BoundPlaceholders ] ),
566
553
BucketUpdate =
567
554
lists :sublist (BoundPlaceholders , BucketPos ) ++
568
- [{'+' , gen_query_placeholder (? BUCKETS_START + BucketPos ), Count }] ++
569
- lists :nthtail (BucketPos + 1 , BoundPlaceholders ),
570
- HistUpdate = list_to_tuple ([{Key }, '$2' , '$3' , {'+' , '$4' , Value * Count }] ++ BucketUpdate ),
555
+ [
556
+ {'+' , gen_query_placeholder (? BUCKETS_START + BucketPos ), Count }
557
+ | lists :nthtail (BucketPos + 1 , BoundPlaceholders )
558
+ ],
559
+ HistUpdate = list_to_tuple ([{Key }, '$2' , '$3' , {'+' , '$4' , Value * Count } | BucketUpdate ]),
571
560
[{HistMatch , [], [{HistUpdate }]}].
572
561
573
- buckets_seq (Buckets ) ->
574
- lists :seq (? BUCKETS_START , ? BUCKETS_START + length (Buckets ) - 1 ).
562
+ buckets_seq (Buckets ) when is_list (Buckets ) ->
563
+ lists :seq (? BUCKETS_START , ? BUCKETS_START + length (Buckets ) - 1 );
564
+ buckets_seq (Buckets ) when is_tuple (Buckets ) ->
565
+ lists :seq (? BUCKETS_START , ? BUCKETS_START + tuple_size (Buckets ) - 1 ).
575
566
576
567
generate_update_spec (Buckets ) ->
577
568
[{Index , 0 } || Index <- buckets_seq (Buckets )].
578
569
579
570
gen_query_placeholder (Index ) ->
580
- list_to_atom (" $ " ++ integer_to_list (Index )).
571
+ list_to_atom ([ $$ | integer_to_list (Index )] ).
581
572
582
573
gen_query_bound_placeholders (Buckets ) ->
583
574
[gen_query_placeholder (Index ) || Index <- buckets_seq (Buckets )].
@@ -586,9 +577,9 @@ augment_counters([Start | Counters]) ->
586
577
augment_counters (Counters , [Start ], Start ).
587
578
588
579
augment_counters ([], LAcc , _CAcc ) ->
589
- LAcc ;
580
+ lists : reverse ( LAcc ) ;
590
581
augment_counters ([Counter | Counters ], LAcc , CAcc ) ->
591
- augment_counters (Counters , LAcc ++ [CAcc + Counter ], CAcc + Counter ).
582
+ augment_counters (Counters , [CAcc + Counter | LAcc ], CAcc + Counter ).
592
583
593
584
metric_buckets (Metric ) ->
594
585
element (? BOUNDS_POS , Metric ).
@@ -599,7 +590,7 @@ reduce_buckets_counters(Metrics) ->
599
590
sub_tuple_to_list (
600
591
Metric ,
601
592
? BUCKETS_START ,
602
- ? BUCKETS_START + length (metric_buckets (Metric ))
593
+ ? BUCKETS_START + tuple_size (metric_buckets (Metric ))
603
594
)
604
595
|| Metric <- Metrics
605
596
],
@@ -629,17 +620,17 @@ create_histogram_metric(CLabels, Labels, DU, Bounds, LabelValues, [ISum, FSum |
629
620
630
621
load_all_values (Registry , Name , Bounds ) ->
631
622
BoundPlaceholders = gen_query_bound_placeholders (Bounds ),
632
- QuerySpec = [{Registry , Name , '$1' , '_' }, '_' , '$3' , '$4' ] ++ BoundPlaceholders ,
623
+ QuerySpec = [{Registry , Name , '$1' , '_' }, '_' , '$3' , '$4' | BoundPlaceholders ] ,
633
624
ets :match (? TABLE , list_to_tuple (QuerySpec )).
634
625
635
626
deregister_select (Registry , Name , Buckets ) ->
636
627
BoundCounters = lists :duplicate (length (Buckets ), '_' ),
637
- MetricSpec = [{Registry , Name , '_' , '_' }, '_' , '_' , '_' ] ++ BoundCounters ,
628
+ MetricSpec = [{Registry , Name , '_' , '_' }, '_' , '_' , '_' | BoundCounters ] ,
638
629
[{list_to_tuple (MetricSpec ), [], [true ]}].
639
630
640
631
delete_metrics (Registry , Buckets ) ->
641
632
BoundCounters = lists :duplicate (length (Buckets ), '_' ),
642
- MetricSpec = [{Registry , '_' , '_' , '_' }, '_' , '_' , '_' ] ++ BoundCounters ,
633
+ MetricSpec = [{Registry , '_' , '_' , '_' }, '_' , '_' , '_' | BoundCounters ] ,
643
634
ets :match_delete (? TABLE , list_to_tuple (MetricSpec )).
644
635
645
636
sub_tuple_to_list (Tuple , Pos , Size ) when Pos < Size ->
0 commit comments