@@ -107,6 +107,7 @@ pub struct PrometheusClientLayerBuilder {
107
107
entries_rate_buckets : Vec < f64 > ,
108
108
duration_seconds_buckets : Vec < f64 > ,
109
109
ttfb_buckets : Vec < f64 > ,
110
+ disable_label_root : bool ,
110
111
}
111
112
112
113
impl Default for PrometheusClientLayerBuilder {
@@ -118,6 +119,7 @@ impl Default for PrometheusClientLayerBuilder {
118
119
entries_rate_buckets : observe:: DEFAULT_ENTRIES_RATE_BUCKETS . to_vec ( ) ,
119
120
duration_seconds_buckets : observe:: DEFAULT_DURATION_SECONDS_BUCKETS . to_vec ( ) ,
120
121
ttfb_buckets : observe:: DEFAULT_TTFB_BUCKETS . to_vec ( ) ,
122
+ disable_label_root : false ,
121
123
}
122
124
}
123
125
}
@@ -171,6 +173,13 @@ impl PrometheusClientLayerBuilder {
171
173
self
172
174
}
173
175
176
+ /// The 'root' label might have risks of being high cardinality, users can choose to disable it
177
+ /// when they found it's not useful for their metrics.
178
+ pub fn disable_label_root ( mut self , disable : bool ) -> Self {
179
+ self . disable_label_root = disable;
180
+ self
181
+ }
182
+
174
183
/// Register the metrics into the registry and return a [`PrometheusClientLayer`].
175
184
///
176
185
/// # Examples
@@ -359,6 +368,8 @@ impl PrometheusClientLayerBuilder {
359
368
http_response_duration_seconds,
360
369
http_connection_errors_total,
361
370
http_status_errors_total,
371
+
372
+ disable_label_root : self . disable_label_root ,
362
373
} ,
363
374
}
364
375
}
@@ -395,11 +406,16 @@ pub struct PrometheusClientInterceptor {
395
406
http_response_duration_seconds : Family < OperationLabels , Histogram , HistogramConstructor > ,
396
407
http_connection_errors_total : Family < OperationLabels , Counter > ,
397
408
http_status_errors_total : Family < OperationLabels , Counter > ,
409
+
410
+ disable_label_root : bool ,
398
411
}
399
412
400
413
impl observe:: MetricsIntercept for PrometheusClientInterceptor {
401
414
fn observe ( & self , labels : observe:: MetricLabels , value : observe:: MetricValue ) {
402
- let labels = OperationLabels ( labels) ;
415
+ let labels = OperationLabels {
416
+ labels,
417
+ disable_label_root : self . disable_label_root ,
418
+ } ;
403
419
match value {
404
420
observe:: MetricValue :: OperationBytes ( v) => self
405
421
. operation_bytes
@@ -473,19 +489,25 @@ impl observe::MetricsIntercept for PrometheusClientInterceptor {
473
489
}
474
490
475
491
#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
476
- struct OperationLabels ( observe:: MetricLabels ) ;
492
+ struct OperationLabels {
493
+ labels : observe:: MetricLabels ,
494
+ disable_label_root : bool ,
495
+ }
477
496
478
497
impl EncodeLabelSet for OperationLabels {
479
498
fn encode ( & self , mut encoder : LabelSetEncoder ) -> Result < ( ) , fmt:: Error > {
480
- ( observe:: LABEL_SCHEME , self . 0 . scheme . into_static ( ) ) . encode ( encoder. encode_label ( ) ) ?;
481
- ( observe:: LABEL_NAMESPACE , self . 0 . namespace . as_ref ( ) ) . encode ( encoder. encode_label ( ) ) ?;
482
- ( observe:: LABEL_ROOT , self . 0 . root . as_ref ( ) ) . encode ( encoder. encode_label ( ) ) ?;
483
- ( observe:: LABEL_OPERATION , self . 0 . operation ) . encode ( encoder. encode_label ( ) ) ?;
499
+ ( observe:: LABEL_SCHEME , self . labels . scheme . into_static ( ) ) . encode ( encoder. encode_label ( ) ) ?;
500
+ ( observe:: LABEL_NAMESPACE , self . labels . namespace . as_ref ( ) )
501
+ . encode ( encoder. encode_label ( ) ) ?;
502
+ if !self . disable_label_root {
503
+ ( observe:: LABEL_ROOT , self . labels . root . as_ref ( ) ) . encode ( encoder. encode_label ( ) ) ?;
504
+ }
505
+ ( observe:: LABEL_OPERATION , self . labels . operation ) . encode ( encoder. encode_label ( ) ) ?;
484
506
485
- if let Some ( error) = & self . 0 . error {
507
+ if let Some ( error) = & self . labels . error {
486
508
( observe:: LABEL_ERROR , error. into_static ( ) ) . encode ( encoder. encode_label ( ) ) ?;
487
509
}
488
- if let Some ( code) = & self . 0 . status_code {
510
+ if let Some ( code) = & self . labels . status_code {
489
511
( observe:: LABEL_STATUS_CODE , code. as_str ( ) ) . encode ( encoder. encode_label ( ) ) ?;
490
512
}
491
513
Ok ( ( ) )
0 commit comments