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,9 @@ 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) = self . metadata . get_description ( & key_name, MetricKind :: Counter ) {
60+ Self :: with_description ( & description, builder)
7061 } else {
7162 builder
7263 } ;
@@ -76,11 +67,9 @@ impl Storage<Key> for OtelMetricStorage {
7667
7768 fn gauge ( & self , key : & Key ) -> Self :: Gauge {
7869 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)
70+ let key_name = KeyName :: from ( key. name ( ) . to_string ( ) ) ;
71+ let builder = if let Some ( description) = self . metadata . get_description ( & key_name, MetricKind :: Gauge ) {
72+ Self :: with_description ( & description, builder)
8473 } else {
8574 builder
8675 } ;
@@ -90,24 +79,19 @@ impl Storage<Key> for OtelMetricStorage {
9079
9180 fn histogram ( & self , key : & Key ) -> Self :: Histogram {
9281 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)
82+ let key_name = KeyName :: from ( key. name ( ) . to_string ( ) ) ;
83+
84+ let builder = if let Some ( description) = self . metadata . get_description ( & key_name, MetricKind :: Histogram ) {
85+ Self :: with_description_histogram ( & description, builder)
9886 } else {
9987 builder
10088 } ;
10189
10290 // 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- }
91+ let builder = if let Some ( bounds) = self . metadata . get_histogram_bounds ( & key_name) {
92+ builder. with_boundaries ( bounds)
93+ } else {
94+ builder
11195 } ;
11296
11397 let attributes = Self :: get_attributes ( key) ;
0 commit comments