1- use crate :: description:: { DescriptionEntry , DescriptionTable } ;
21use crate :: instruments:: { OtelCounter , OtelGauge , OtelHistogram } ;
2+ use crate :: metadata:: { MetricDescription , MetricMetadata } ;
33use metrics:: { Key , KeyName } ;
44use metrics_util:: registry:: Storage ;
55use metrics_util:: MetricKind ;
66use opentelemetry:: metrics:: { AsyncInstrumentBuilder , HistogramBuilder , Meter } ;
77use opentelemetry:: KeyValue ;
8- use std:: collections:: HashMap ;
9- use std:: sync:: { Arc , PoisonError , RwLock } ;
8+ use std:: sync:: Arc ;
109
1110pub struct OtelMetricStorage {
1211 meter : Meter ,
13- description_table : Arc < DescriptionTable > ,
14- histogram_bounds : Arc < RwLock < HashMap < KeyName , Vec < f64 > > > > ,
12+ metadata : MetricMetadata ,
1513}
1614
1715impl OtelMetricStorage {
18- pub fn new ( meter : Meter , description_table : Arc < DescriptionTable > , histogram_bounds : Arc < RwLock < HashMap < KeyName , Vec < f64 > > > > ) -> Self {
19- Self {
20- meter,
21- description_table,
22- histogram_bounds,
23- }
16+ pub fn new ( meter : Meter , metadata : MetricMetadata ) -> Self {
17+ Self { meter, metadata }
2418 }
2519
2620 fn get_attributes ( key : & Key ) -> Vec < KeyValue > {
@@ -29,28 +23,27 @@ impl OtelMetricStorage {
2923 . collect ( )
3024 }
3125
32- fn with_description_entry < ' a , I , M > (
33- description_entry : & DescriptionEntry ,
26+ fn with_description < ' a , I , M > (
27+ description : & MetricDescription ,
3428 builder : AsyncInstrumentBuilder < ' a , I , M > ,
3529 ) -> AsyncInstrumentBuilder < ' a , I , M > {
36- // let builder = builder.with_description(self.description.to_string());
37- match description_entry. unit ( ) {
30+ match description. unit ( ) {
3831 Some ( unit) => builder
39- . with_description ( description_entry . description ( ) . to_string ( ) )
32+ . with_description ( description . description ( ) . to_string ( ) )
4033 . with_unit ( unit. as_canonical_label ( ) ) ,
41- None => builder. with_description ( description_entry . description ( ) . to_string ( ) ) ,
34+ None => builder. with_description ( description . description ( ) . to_string ( ) ) ,
4235 }
4336 }
44- fn with_description_entry_histogram < ' a , T > (
45- description_entry : & DescriptionEntry ,
37+
38+ fn with_description_histogram < ' a , T > (
39+ description : & MetricDescription ,
4640 builder : HistogramBuilder < ' a , T > ,
4741 ) -> HistogramBuilder < ' a , T > {
48- // let builder = builder.with_description(self.description.to_string());
49- match description_entry. unit ( ) {
42+ match description. unit ( ) {
5043 Some ( unit) => builder
51- . with_description ( description_entry . description ( ) . to_string ( ) )
44+ . with_description ( description . description ( ) . to_string ( ) )
5245 . with_unit ( unit. as_canonical_label ( ) ) ,
53- None => builder. with_description ( description_entry . description ( ) . to_string ( ) ) ,
46+ None => builder. with_description ( description . description ( ) . to_string ( ) ) ,
5447 }
5548 }
5649}
@@ -62,11 +55,11 @@ impl Storage<Key> for OtelMetricStorage {
6255
6356 fn counter ( & self , key : & Key ) -> Self :: Counter {
6457 let builder = self . meter . u64_observable_counter ( key. name ( ) . to_string ( ) ) ;
65- let description = self
66- . description_table
67- . get_describe ( KeyName :: from ( key . name ( ) . to_string ( ) ) , MetricKind :: Counter ) ;
68- let builder = if let Some ( description ) = description {
69- Self :: with_description_entry ( & description, builder)
58+ let key_name = KeyName :: from ( key . name ( ) . to_string ( ) ) ;
59+ let builder = if let Some ( description ) =
60+ self . metadata . get_description ( & key_name , MetricKind :: Counter )
61+ {
62+ Self :: with_description ( & description, builder)
7063 } else {
7164 builder
7265 } ;
@@ -76,11 +69,11 @@ impl Storage<Key> for OtelMetricStorage {
7669
7770 fn gauge ( & self , key : & Key ) -> Self :: Gauge {
7871 let builder = self . meter . f64_observable_gauge ( key. name ( ) . to_string ( ) ) ;
79- let description = self
80- . description_table
81- . get_describe ( KeyName :: from ( key . name ( ) . to_string ( ) ) , MetricKind :: Gauge ) ;
82- let builder = if let Some ( description ) = description {
83- Self :: with_description_entry ( & description, builder)
72+ let key_name = KeyName :: from ( key . name ( ) . to_string ( ) ) ;
73+ let builder = if let Some ( description ) =
74+ self . metadata . get_description ( & key_name , MetricKind :: Gauge )
75+ {
76+ Self :: with_description ( & description, builder)
8477 } else {
8578 builder
8679 } ;
@@ -90,26 +83,23 @@ impl Storage<Key> for OtelMetricStorage {
9083
9184 fn histogram ( & self , key : & Key ) -> Self :: Histogram {
9285 let builder = self . meter . f64_histogram ( key. name ( ) . to_string ( ) ) ;
93- let description = self
94- . description_table
95- . get_describe ( KeyName :: from ( key. name ( ) . to_string ( ) ) , MetricKind :: Histogram ) ;
96- let builder = if let Some ( description) = description {
97- Self :: with_description_entry_histogram ( & description, builder)
86+ let key_name = KeyName :: from ( key. name ( ) . to_string ( ) ) ;
87+
88+ let builder = if let Some ( description) =
89+ self . metadata . get_description ( & key_name, MetricKind :: Histogram )
90+ {
91+ Self :: with_description_histogram ( & description, builder)
9892 } else {
9993 builder
10094 } ;
101-
95+
10296 // Apply histogram bounds if they exist
103- let key_name = KeyName :: from ( key. name ( ) . to_string ( ) ) ;
104- let builder = {
105- let bounds_map = self . histogram_bounds . read ( ) . unwrap_or_else ( PoisonError :: into_inner) ; ;
106- if let Some ( bounds) = bounds_map. get ( & key_name) {
107- builder. with_boundaries ( bounds. clone ( ) )
108- } else {
109- builder
110- }
97+ let builder = if let Some ( bounds) = self . metadata . get_histogram_bounds ( & key_name) {
98+ builder. with_boundaries ( bounds)
99+ } else {
100+ builder
111101 } ;
112-
102+
113103 let attributes = Self :: get_attributes ( key) ;
114104 Arc :: new ( OtelHistogram :: new ( builder. build ( ) , attributes) )
115105 }
0 commit comments