Skip to content

Avoid encoding descriptor of empty family #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ pub trait EncodeMetric {
// One can not use [`TypedMetric`] directly, as associated constants are not
// object safe and thus can not be used with dynamic dispatching.
fn metric_type(&self) -> MetricType;

/// Check if the metric is empty.
///
/// An empty metric is a metric that has no data to encode, and thus should not have any
/// descriptor in the final output.
///
/// By default, this returns `false`, ensuring the metric and its description is always
/// encoded.
fn is_empty(&self) -> bool {
false
}
}

impl EncodeMetric for Box<dyn EncodeMetric> {
Expand Down
4 changes: 4 additions & 0 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ where
fn metric_type(&self) -> MetricType {
M::TYPE
}

fn is_empty(&self) -> bool {
self.metrics.read().is_empty()
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl Registry {
}

pub(crate) fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error> {
for (descriptor, metric) in self.metrics.iter() {
for (descriptor, metric) in self.metrics.iter().filter(|(_, m)| !m.is_empty()) {
let mut descriptor_encoder =
encoder.with_prefix_and_labels(self.prefix.as_ref(), &self.labels);
let metric_encoder = descriptor_encoder.encode_descriptor(
Expand Down