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 {
4949 // One can not use [`TypedMetric`] directly, as associated constants are not
5050 // object safe and thus can not be used with dynamic dispatching.
5151 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+ }
5263}
5364
5465impl EncodeMetric for Box < dyn EncodeMetric > {
Original file line number Diff line number Diff line change @@ -377,6 +377,10 @@ where
377377 fn metric_type ( & self ) -> MetricType {
378378 M :: TYPE
379379 }
380+
381+ fn is_empty ( & self ) -> bool {
382+ self . metrics . read ( ) . is_empty ( )
383+ }
380384}
381385
382386#[ cfg( test) ]
Original file line number Diff line number Diff line change @@ -287,7 +287,7 @@ impl Registry {
287287 }
288288
289289 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 ( ) ) {
291291 let mut descriptor_encoder =
292292 encoder. with_prefix_and_labels ( self . prefix . as_ref ( ) , & self . labels ) ;
293293 let metric_encoder = descriptor_encoder. encode_descriptor (
You can’t perform that action at this time.
0 commit comments