1
- use crate :: description:: { DescriptionEntry , DescriptionTable } ;
2
1
use crate :: instruments:: { OtelCounter , OtelGauge , OtelHistogram } ;
2
+ use crate :: metadata:: { MetricDescription , MetricMetadata } ;
3
3
use metrics:: { Key , KeyName } ;
4
4
use metrics_util:: registry:: Storage ;
5
5
use metrics_util:: MetricKind ;
6
6
use opentelemetry:: metrics:: { AsyncInstrumentBuilder , HistogramBuilder , Meter } ;
7
7
use opentelemetry:: KeyValue ;
8
- use std:: collections:: HashMap ;
9
- use std:: sync:: { Arc , PoisonError , RwLock } ;
8
+ use std:: sync:: Arc ;
10
9
11
10
pub struct OtelMetricStorage {
12
11
meter : Meter ,
13
- description_table : Arc < DescriptionTable > ,
14
- histogram_bounds : Arc < RwLock < HashMap < KeyName , Vec < f64 > > > > ,
12
+ metadata : MetricMetadata ,
15
13
}
16
14
17
15
impl 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 }
24
18
}
25
19
26
20
fn get_attributes ( key : & Key ) -> Vec < KeyValue > {
@@ -29,28 +23,27 @@ impl OtelMetricStorage {
29
23
. collect ( )
30
24
}
31
25
32
- fn with_description_entry < ' a , I , M > (
33
- description_entry : & DescriptionEntry ,
26
+ fn with_description < ' a , I , M > (
27
+ description : & MetricDescription ,
34
28
builder : AsyncInstrumentBuilder < ' a , I , M > ,
35
29
) -> AsyncInstrumentBuilder < ' a , I , M > {
36
- // let builder = builder.with_description(self.description.to_string());
37
- match description_entry. unit ( ) {
30
+ match description. unit ( ) {
38
31
Some ( unit) => builder
39
- . with_description ( description_entry . description ( ) . to_string ( ) )
32
+ . with_description ( description . description ( ) . to_string ( ) )
40
33
. 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 ( ) ) ,
42
35
}
43
36
}
44
- fn with_description_entry_histogram < ' a , T > (
45
- description_entry : & DescriptionEntry ,
37
+
38
+ fn with_description_histogram < ' a , T > (
39
+ description : & MetricDescription ,
46
40
builder : HistogramBuilder < ' a , T > ,
47
41
) -> HistogramBuilder < ' a , T > {
48
- // let builder = builder.with_description(self.description.to_string());
49
- match description_entry. unit ( ) {
42
+ match description. unit ( ) {
50
43
Some ( unit) => builder
51
- . with_description ( description_entry . description ( ) . to_string ( ) )
44
+ . with_description ( description . description ( ) . to_string ( ) )
52
45
. 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 ( ) ) ,
54
47
}
55
48
}
56
49
}
@@ -62,11 +55,11 @@ impl Storage<Key> for OtelMetricStorage {
62
55
63
56
fn counter ( & self , key : & Key ) -> Self :: Counter {
64
57
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)
70
63
} else {
71
64
builder
72
65
} ;
@@ -76,11 +69,11 @@ impl Storage<Key> for OtelMetricStorage {
76
69
77
70
fn gauge ( & self , key : & Key ) -> Self :: Gauge {
78
71
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)
84
77
} else {
85
78
builder
86
79
} ;
@@ -90,26 +83,23 @@ impl Storage<Key> for OtelMetricStorage {
90
83
91
84
fn histogram ( & self , key : & Key ) -> Self :: Histogram {
92
85
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)
98
92
} else {
99
93
builder
100
94
} ;
101
-
95
+
102
96
// 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
111
101
} ;
112
-
102
+
113
103
let attributes = Self :: get_attributes ( key) ;
114
104
Arc :: new ( OtelHistogram :: new ( builder. build ( ) , attributes) )
115
105
}
0 commit comments