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,9 @@ 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) = self . metadata . get_description ( & key_name, MetricKind :: Counter ) {
60
+ Self :: with_description ( & description, builder)
70
61
} else {
71
62
builder
72
63
} ;
@@ -76,11 +67,9 @@ impl Storage<Key> for OtelMetricStorage {
76
67
77
68
fn gauge ( & self , key : & Key ) -> Self :: Gauge {
78
69
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)
84
73
} else {
85
74
builder
86
75
} ;
@@ -90,24 +79,19 @@ impl Storage<Key> for OtelMetricStorage {
90
79
91
80
fn histogram ( & self , key : & Key ) -> Self :: Histogram {
92
81
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)
98
86
} else {
99
87
builder
100
88
} ;
101
89
102
90
// 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
111
95
} ;
112
96
113
97
let attributes = Self :: get_attributes ( key) ;
0 commit comments