Skip to content

Commit

Permalink
fix: prometheus counters should have _total suffix (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengxilong authored Jan 29, 2023
1 parent 01564af commit a82d970
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ mod sanitize;

use sanitize::sanitize;

/// Monotonic Sum metric points MUST have _total added as a suffix to the metric name
/// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.14.0/specification/metrics/data-model.md#sums-1
const MONOTONIC_COUNTER_SUFFIX: &str = "_total";

/// Create a new prometheus exporter builder.
pub fn exporter(controller: BasicController) -> ExporterBuilder {
ExporterBuilder::new(controller)
Expand Down Expand Up @@ -329,7 +333,7 @@ fn build_monotonic_counter(
m.set_counter(c);

let mut mf = prometheus::proto::MetricFamily::default();
mf.set_name(desc.name);
mf.set_name(desc.name + MONOTONIC_COUNTER_SUFFIX);
mf.set_help(desc.help);
mf.set_field_type(prometheus::proto::MetricType::COUNTER);
mf.set_metric(protobuf::RepeatedField::from_vec(vec![m]));
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-prometheus/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn free_unused_instruments() {
counter.add(&cx, 10.0, &attributes);
counter.add(&cx, 5.3, &attributes);

expected.push(r#"counter{A="B",C="D",R="V"} 15.3"#);
expected.push(r#"counter_total{A="B",C="D",R="V"} 15.3"#);
}
// Standard export
compare_export(&exporter, expected.clone());
Expand Down Expand Up @@ -67,7 +67,7 @@ fn test_add() {
counter.add(&cx, 10.0, &attributes);
counter.add(&cx, 5.3, &attributes);

expected.push(r#"counter{A="B",C="D",R="V"} 15.3"#);
expected.push(r#"counter_total{A="B",C="D",R="V"} 15.3"#);

let cb_attributes = attributes.clone();
let gauge = meter.i64_observable_gauge("intgauge").init();
Expand Down

0 comments on commit a82d970

Please sign in to comment.