File tree Expand file tree Collapse file tree 3 files changed +16
-1
lines changed Expand file tree Collapse file tree 3 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,17 @@ pub trait EncodeMetric {
49
49
// One can not use [`TypedMetric`] directly, as associated constants are not
50
50
// object safe and thus can not be used with dynamic dispatching.
51
51
fn metric_type ( & self ) -> MetricType ;
52
+
53
+ /// Check if the metric is empty.
54
+ ///
55
+ /// An empty metric is a metric that has no data to encode, and thus should not have any
56
+ /// descriptor in the final output.
57
+ ///
58
+ /// By default, this returns `false`, ensuring the metric and its description is always
59
+ /// encoded.
60
+ fn is_empty ( & self ) -> bool {
61
+ false
62
+ }
52
63
}
53
64
54
65
impl EncodeMetric for Box < dyn EncodeMetric > {
Original file line number Diff line number Diff line change @@ -377,6 +377,10 @@ where
377
377
fn metric_type ( & self ) -> MetricType {
378
378
M :: TYPE
379
379
}
380
+
381
+ fn is_empty ( & self ) -> bool {
382
+ self . metrics . read ( ) . is_empty ( )
383
+ }
380
384
}
381
385
382
386
#[ cfg( test) ]
Original file line number Diff line number Diff line change @@ -287,7 +287,7 @@ impl Registry {
287
287
}
288
288
289
289
pub ( crate ) fn encode ( & self , encoder : & mut DescriptorEncoder ) -> Result < ( ) , std:: fmt:: Error > {
290
- for ( descriptor, metric) in self . metrics . iter ( ) {
290
+ for ( descriptor, metric) in self . metrics . iter ( ) . filter ( | ( _ , m ) | !m . is_empty ( ) ) {
291
291
let mut descriptor_encoder =
292
292
encoder. with_prefix_and_labels ( self . prefix . as_ref ( ) , & self . labels ) ;
293
293
let metric_encoder = descriptor_encoder. encode_descriptor (
You can’t perform that action at this time.
0 commit comments