Skip to content
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

Add units to prometheus metric lines #535

Open
wants to merge 1 commit into
base: main
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
15 changes: 14 additions & 1 deletion metrics-exporter-prometheus/src/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Helpers for rendering metrics in the Prometheus exposition format.

use indexmap::IndexMap;
use metrics::Key;
use metrics::{Key, Unit};

/// Breaks a key into the name and label components, with optional default labels.
///
Expand Down Expand Up @@ -64,6 +64,7 @@ pub fn write_metric_line<T, T2>(
labels: &[String],
additional_label: Option<(&'static str, T)>,
value: T2,
unit: Option<Unit>,
) where
T: std::fmt::Display,
T2: std::fmt::Display,
Expand All @@ -74,6 +75,18 @@ pub fn write_metric_line<T, T2>(
buffer.push_str(suffix);
}

match unit {
Some(Unit::Count) | None => {}
Some(Unit::Percent) => {
buffer.push('_');
buffer.push_str("ratio");
}
Some(unit) => {
buffer.push('_');
buffer.push_str(unit.as_str());
}
}

if !labels.is_empty() || additional_label.is_some() {
buffer.push('{');

Expand Down
77 changes: 54 additions & 23 deletions metrics-exporter-prometheus/src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) struct Inner {
pub recency: Recency<Key>,
pub distributions: RwLock<HashMap<String, IndexMap<Vec<String>, Distribution>>>,
pub distribution_builder: DistributionBuilder,
pub descriptions: RwLock<HashMap<String, SharedString>>,
pub descriptions: RwLock<HashMap<String, (SharedString, Option<Unit>)>>,
pub global_labels: IndexMap<String, String>,
}

Expand Down Expand Up @@ -116,33 +116,52 @@ impl Inner {
let descriptions = self.descriptions.read().unwrap_or_else(PoisonError::into_inner);

for (name, mut by_labels) in counters.drain() {
if let Some(desc) = descriptions.get(name.as_str()) {
let unit = descriptions.get(name.as_str()).and_then(|(desc, unit)| {
write_help_line(&mut output, name.as_str(), desc);
}
*unit
});

write_type_line(&mut output, name.as_str(), "counter");
for (labels, value) in by_labels.drain() {
write_metric_line::<&str, u64>(&mut output, &name, None, &labels, None, value);
write_metric_line::<&str, u64>(
&mut output,
&name,
None,
&labels,
None,
value,
unit,
);
}
output.push('\n');
}

for (name, mut by_labels) in gauges.drain() {
if let Some(desc) = descriptions.get(name.as_str()) {
let unit = descriptions.get(name.as_str()).and_then(|(desc, unit)| {
write_help_line(&mut output, name.as_str(), desc);
}
*unit
});

write_type_line(&mut output, name.as_str(), "gauge");
for (labels, value) in by_labels.drain() {
write_metric_line::<&str, f64>(&mut output, &name, None, &labels, None, value);
write_metric_line::<&str, f64>(
&mut output,
&name,
None,
&labels,
None,
value,
unit,
);
}
output.push('\n');
}

for (name, mut by_labels) in distributions.drain() {
if let Some(desc) = descriptions.get(name.as_str()) {
let unit = descriptions.get(name.as_str()).and_then(|(desc, unit)| {
write_help_line(&mut output, name.as_str(), desc);
}
*unit
});

let distribution_type = self.distribution_builder.get_distribution_type(name.as_str());
write_type_line(&mut output, name.as_str(), distribution_type);
Expand All @@ -159,6 +178,7 @@ impl Inner {
&labels,
Some(("quantile", quantile.value())),
value,
unit,
);
}

Expand All @@ -173,6 +193,7 @@ impl Inner {
&labels,
Some(("le", le)),
count,
unit,
);
}
write_metric_line(
Expand All @@ -182,20 +203,30 @@ impl Inner {
&labels,
Some(("le", "+Inf")),
histogram.count(),
unit,
);

(histogram.sum(), histogram.count())
}
};

write_metric_line::<&str, f64>(&mut output, &name, Some("sum"), &labels, None, sum);
write_metric_line::<&str, f64>(
&mut output,
&name,
Some("sum"),
&labels,
None,
sum,
unit,
);
write_metric_line::<&str, u64>(
&mut output,
&name,
Some("count"),
&labels,
None,
count,
unit,
);
}

Expand Down Expand Up @@ -226,11 +257,16 @@ impl PrometheusRecorder {
PrometheusHandle { inner: self.inner.clone() }
}

fn add_description_if_missing(&self, key_name: &KeyName, description: SharedString) {
fn add_description_if_missing(
&self,
key_name: &KeyName,
description: SharedString,
unit: Option<Unit>,
) {
let sanitized = sanitize_metric_name(key_name.as_str());
let mut descriptions =
self.inner.descriptions.write().unwrap_or_else(PoisonError::into_inner);
descriptions.entry(sanitized).or_insert(description);
descriptions.entry(sanitized).or_insert((description, unit));
}
}

Expand All @@ -241,21 +277,16 @@ impl From<Inner> for PrometheusRecorder {
}

impl Recorder for PrometheusRecorder {
fn describe_counter(&self, key_name: KeyName, _unit: Option<Unit>, description: SharedString) {
self.add_description_if_missing(&key_name, description);
fn describe_counter(&self, key_name: KeyName, unit: Option<Unit>, description: SharedString) {
self.add_description_if_missing(&key_name, description, unit);
}

fn describe_gauge(&self, key_name: KeyName, _unit: Option<Unit>, description: SharedString) {
self.add_description_if_missing(&key_name, description);
fn describe_gauge(&self, key_name: KeyName, unit: Option<Unit>, description: SharedString) {
self.add_description_if_missing(&key_name, description, unit);
}

fn describe_histogram(
&self,
key_name: KeyName,
_unit: Option<Unit>,
description: SharedString,
) {
self.add_description_if_missing(&key_name, description);
fn describe_histogram(&self, key_name: KeyName, unit: Option<Unit>, description: SharedString) {
self.add_description_if_missing(&key_name, description, unit);
}

fn register_counter(&self, key: &Key, _metadata: &Metadata<'_>) -> Counter {
Expand Down