1
- use std:: sync:: Arc ;
2
- use opentelemetry:: KeyValue ;
3
- use opentelemetry:: metrics:: Meter ;
4
- use metrics:: Key ;
5
- use metrics_util:: registry:: Storage ;
1
+ use crate :: description:: { DescriptionEntry , DescriptionTable } ;
6
2
use crate :: instruments:: { OtelCounter , OtelGauge , OtelHistogram } ;
3
+ use metrics:: { Key , KeyName } ;
4
+ use metrics_util:: registry:: Storage ;
5
+ use metrics_util:: MetricKind ;
6
+ use opentelemetry:: metrics:: { AsyncInstrumentBuilder , HistogramBuilder , Meter } ;
7
+ use opentelemetry:: KeyValue ;
8
+ use std:: sync:: Arc ;
7
9
8
10
pub struct OtelMetricStorage {
9
11
meter : Meter ,
12
+ description_table : Arc < DescriptionTable > ,
10
13
}
11
14
12
15
impl OtelMetricStorage {
13
- pub fn new ( meter : Meter ) -> Self {
14
- Self { meter }
16
+ pub fn new ( meter : Meter , description_table : Arc < DescriptionTable > ) -> Self {
17
+ Self { meter, description_table }
15
18
}
16
19
17
20
fn get_attributes ( key : & Key ) -> Vec < KeyValue > {
18
21
key. labels ( )
19
22
. map ( |label| KeyValue :: new ( label. key ( ) . to_string ( ) , label. value ( ) . to_string ( ) ) )
20
23
. collect ( )
21
24
}
25
+
26
+ fn with_description_entry < ' a , I , M > (
27
+ description_entry : & DescriptionEntry ,
28
+ builder : AsyncInstrumentBuilder < ' a , I , M > ,
29
+ ) -> AsyncInstrumentBuilder < ' a , I , M > {
30
+ // let builder = builder.with_description(self.description.to_string());
31
+ match description_entry. unit ( ) {
32
+ Some ( unit) => builder
33
+ . with_description ( description_entry. description ( ) . to_string ( ) )
34
+ . with_unit ( unit. as_canonical_label ( ) ) ,
35
+ None => builder. with_description ( description_entry. description ( ) . to_string ( ) ) ,
36
+ }
37
+ }
38
+ fn with_description_entry_histogram < ' a , T > (
39
+ description_entry : & DescriptionEntry ,
40
+ builder : HistogramBuilder < ' a , T > ,
41
+ ) -> HistogramBuilder < ' a , T > {
42
+ // let builder = builder.with_description(self.description.to_string());
43
+ match description_entry. unit ( ) {
44
+ Some ( unit) => builder
45
+ . with_description ( description_entry. description ( ) . to_string ( ) )
46
+ . with_unit ( unit. as_canonical_label ( ) ) ,
47
+ None => builder. with_description ( description_entry. description ( ) . to_string ( ) ) ,
48
+ }
49
+ }
22
50
}
23
51
24
52
impl Storage < Key > for OtelMetricStorage {
@@ -27,28 +55,44 @@ impl Storage<Key> for OtelMetricStorage {
27
55
type Histogram = Arc < OtelHistogram > ;
28
56
29
57
fn counter ( & self , key : & Key ) -> Self :: Counter {
30
- let otel_counter_builder = self
31
- . meter
32
- . u64_observable_counter ( key. name ( ) . to_string ( ) ) ;
58
+ let builder = self . meter . u64_observable_counter ( key. name ( ) . to_string ( ) ) ;
59
+ let description = self
60
+ . description_table
61
+ . get_describe ( KeyName :: from ( key. name ( ) . to_string ( ) ) , MetricKind :: Counter ) ;
62
+ let builder = if let Some ( description) = description {
63
+ Self :: with_description_entry ( & description, builder)
64
+ } else {
65
+ builder
66
+ } ;
33
67
let attributes = Self :: get_attributes ( key) ;
34
- Arc :: new ( OtelCounter :: new ( otel_counter_builder , attributes) )
68
+ Arc :: new ( OtelCounter :: new ( builder , attributes) )
35
69
}
36
70
37
71
fn gauge ( & self , key : & Key ) -> Self :: Gauge {
38
- let builder = self
39
- . meter
40
- . f64_observable_gauge ( key. name ( ) . to_string ( ) ) ;
72
+ let builder = self . meter . f64_observable_gauge ( key. name ( ) . to_string ( ) ) ;
73
+ let description = self
74
+ . description_table
75
+ . get_describe ( KeyName :: from ( key. name ( ) . to_string ( ) ) , MetricKind :: Gauge ) ;
76
+ let builder = if let Some ( description) = description {
77
+ Self :: with_description_entry ( & description, builder)
78
+ } else {
79
+ builder
80
+ } ;
41
81
let attributes = Self :: get_attributes ( key) ;
42
82
Arc :: new ( OtelGauge :: new ( builder, attributes) )
43
83
}
44
84
45
85
fn histogram ( & self , key : & Key ) -> Self :: Histogram {
46
- let histogram = self
47
- . meter
48
- . f64_histogram ( key. name ( ) . to_string ( ) )
49
- . build ( ) ;
86
+ let builder = self . meter . f64_histogram ( key. name ( ) . to_string ( ) ) ;
87
+ let description = self
88
+ . description_table
89
+ . get_describe ( KeyName :: from ( key. name ( ) . to_string ( ) ) , MetricKind :: Histogram ) ;
90
+ let builder = if let Some ( description) = description {
91
+ Self :: with_description_entry_histogram ( & description, builder)
92
+ } else {
93
+ builder
94
+ } ;
50
95
let attributes = Self :: get_attributes ( key) ;
51
- Arc :: new ( OtelHistogram :: new ( histogram, attributes) )
52
-
96
+ Arc :: new ( OtelHistogram :: new ( builder. build ( ) , attributes) )
53
97
}
54
98
}
0 commit comments