@@ -21,8 +21,9 @@ pub(crate) struct Inner {
21
21
pub recency : Recency < Key > ,
22
22
pub distributions : RwLock < HashMap < String , IndexMap < Vec < String > , Distribution > > > ,
23
23
pub distribution_builder : DistributionBuilder ,
24
- pub descriptions : RwLock < HashMap < String , SharedString > > ,
24
+ pub descriptions : RwLock < HashMap < String , ( SharedString , Option < Unit > ) > > ,
25
25
pub global_labels : IndexMap < String , String > ,
26
+ pub enable_unit_suffix : bool ,
26
27
}
27
28
28
29
impl Inner {
@@ -116,33 +117,52 @@ impl Inner {
116
117
let descriptions = self . descriptions . read ( ) . unwrap_or_else ( PoisonError :: into_inner) ;
117
118
118
119
for ( name, mut by_labels) in counters. drain ( ) {
119
- if let Some ( desc ) = descriptions. get ( name. as_str ( ) ) {
120
+ let unit = descriptions. get ( name. as_str ( ) ) . and_then ( | ( desc , unit ) | {
120
121
write_help_line ( & mut output, name. as_str ( ) , desc) ;
121
- }
122
+ * unit
123
+ } ) ;
122
124
123
125
write_type_line ( & mut output, name. as_str ( ) , "counter" ) ;
124
126
for ( labels, value) in by_labels. drain ( ) {
125
- write_metric_line :: < & str , u64 > ( & mut output, & name, None , & labels, None , value) ;
127
+ write_metric_line :: < & str , u64 > (
128
+ & mut output,
129
+ & name,
130
+ None ,
131
+ & labels,
132
+ None ,
133
+ value,
134
+ unit. filter ( |_| self . enable_unit_suffix ) ,
135
+ ) ;
126
136
}
127
137
output. push ( '\n' ) ;
128
138
}
129
139
130
140
for ( name, mut by_labels) in gauges. drain ( ) {
131
- if let Some ( desc ) = descriptions. get ( name. as_str ( ) ) {
141
+ let unit = descriptions. get ( name. as_str ( ) ) . and_then ( | ( desc , unit ) | {
132
142
write_help_line ( & mut output, name. as_str ( ) , desc) ;
133
- }
143
+ * unit
144
+ } ) ;
134
145
135
146
write_type_line ( & mut output, name. as_str ( ) , "gauge" ) ;
136
147
for ( labels, value) in by_labels. drain ( ) {
137
- write_metric_line :: < & str , f64 > ( & mut output, & name, None , & labels, None , value) ;
148
+ write_metric_line :: < & str , f64 > (
149
+ & mut output,
150
+ & name,
151
+ None ,
152
+ & labels,
153
+ None ,
154
+ value,
155
+ unit. filter ( |_| self . enable_unit_suffix ) ,
156
+ ) ;
138
157
}
139
158
output. push ( '\n' ) ;
140
159
}
141
160
142
161
for ( name, mut by_labels) in distributions. drain ( ) {
143
- if let Some ( desc ) = descriptions. get ( name. as_str ( ) ) {
162
+ let unit = descriptions. get ( name. as_str ( ) ) . and_then ( | ( desc , unit ) | {
144
163
write_help_line ( & mut output, name. as_str ( ) , desc) ;
145
- }
164
+ * unit
165
+ } ) ;
146
166
147
167
let distribution_type = self . distribution_builder . get_distribution_type ( name. as_str ( ) ) ;
148
168
write_type_line ( & mut output, name. as_str ( ) , distribution_type) ;
@@ -159,6 +179,7 @@ impl Inner {
159
179
& labels,
160
180
Some ( ( "quantile" , quantile. value ( ) ) ) ,
161
181
value,
182
+ unit. filter ( |_| self . enable_unit_suffix ) ,
162
183
) ;
163
184
}
164
185
@@ -173,6 +194,7 @@ impl Inner {
173
194
& labels,
174
195
Some ( ( "le" , le) ) ,
175
196
count,
197
+ unit. filter ( |_| self . enable_unit_suffix ) ,
176
198
) ;
177
199
}
178
200
write_metric_line (
@@ -182,20 +204,30 @@ impl Inner {
182
204
& labels,
183
205
Some ( ( "le" , "+Inf" ) ) ,
184
206
histogram. count ( ) ,
207
+ unit. filter ( |_| self . enable_unit_suffix ) ,
185
208
) ;
186
209
187
210
( histogram. sum ( ) , histogram. count ( ) )
188
211
}
189
212
} ;
190
213
191
- write_metric_line :: < & str , f64 > ( & mut output, & name, Some ( "sum" ) , & labels, None , sum) ;
214
+ write_metric_line :: < & str , f64 > (
215
+ & mut output,
216
+ & name,
217
+ Some ( "sum" ) ,
218
+ & labels,
219
+ None ,
220
+ sum,
221
+ unit,
222
+ ) ;
192
223
write_metric_line :: < & str , u64 > (
193
224
& mut output,
194
225
& name,
195
226
Some ( "count" ) ,
196
227
& labels,
197
228
None ,
198
229
count,
230
+ unit,
199
231
) ;
200
232
}
201
233
@@ -226,11 +258,16 @@ impl PrometheusRecorder {
226
258
PrometheusHandle { inner : self . inner . clone ( ) }
227
259
}
228
260
229
- fn add_description_if_missing ( & self , key_name : & KeyName , description : SharedString ) {
261
+ fn add_description_if_missing (
262
+ & self ,
263
+ key_name : & KeyName ,
264
+ description : SharedString ,
265
+ unit : Option < Unit > ,
266
+ ) {
230
267
let sanitized = sanitize_metric_name ( key_name. as_str ( ) ) ;
231
268
let mut descriptions =
232
269
self . inner . descriptions . write ( ) . unwrap_or_else ( PoisonError :: into_inner) ;
233
- descriptions. entry ( sanitized) . or_insert ( description) ;
270
+ descriptions. entry ( sanitized) . or_insert ( ( description, unit ) ) ;
234
271
}
235
272
}
236
273
@@ -241,21 +278,16 @@ impl From<Inner> for PrometheusRecorder {
241
278
}
242
279
243
280
impl Recorder for PrometheusRecorder {
244
- fn describe_counter ( & self , key_name : KeyName , _unit : Option < Unit > , description : SharedString ) {
245
- self . add_description_if_missing ( & key_name, description) ;
281
+ fn describe_counter ( & self , key_name : KeyName , unit : Option < Unit > , description : SharedString ) {
282
+ self . add_description_if_missing ( & key_name, description, unit ) ;
246
283
}
247
284
248
- fn describe_gauge ( & self , key_name : KeyName , _unit : Option < Unit > , description : SharedString ) {
249
- self . add_description_if_missing ( & key_name, description) ;
285
+ fn describe_gauge ( & self , key_name : KeyName , unit : Option < Unit > , description : SharedString ) {
286
+ self . add_description_if_missing ( & key_name, description, unit ) ;
250
287
}
251
288
252
- fn describe_histogram (
253
- & self ,
254
- key_name : KeyName ,
255
- _unit : Option < Unit > ,
256
- description : SharedString ,
257
- ) {
258
- self . add_description_if_missing ( & key_name, description) ;
289
+ fn describe_histogram ( & self , key_name : KeyName , unit : Option < Unit > , description : SharedString ) {
290
+ self . add_description_if_missing ( & key_name, description, unit) ;
259
291
}
260
292
261
293
fn register_counter ( & self , key : & Key , _metadata : & Metadata < ' _ > ) -> Counter {
0 commit comments