From 0d7cb01154babbeea9bcd7986787b59b43f4f9a9 Mon Sep 17 00:00:00 2001 From: Bing Han Date: Thu, 20 Oct 2022 16:11:12 +0800 Subject: [PATCH] Fix prometheus name duplicate _total suffix Signed-off-by: Bing Han --- CHANGELOG.md | 1 + exporters/prometheus/exporter.go | 8 ++++---- exporters/prometheus/exporter_test.go | 8 ++++++++ exporters/prometheus/testdata/counter.txt | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index faae85f292b..48d6e4db884 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The `go.opentelemetry.io/otel/exporters/prometheus` exporter now correctly adds `_total` suffixes to counter metrics. (#3360) - The `go.opentelemetry.io/otel/exporters/prometheus` exporter now adds a unit suffix to metric names. This can be disabled using the `WithoutUnits()` option added to that package. (#3352) +- The `go.opentelemetry.io/otel/exporters/prometheus` exporter fixes duplicated `_total` suffixes. (#3369) ## [1.11.0/0.32.3] 2022-10-12 diff --git a/exporters/prometheus/exporter.go b/exporters/prometheus/exporter.go index 95c7ef93bb8..4eeab23179f 100644 --- a/exporters/prometheus/exporter.go +++ b/exporters/prometheus/exporter.go @@ -203,11 +203,11 @@ func getSumMetricData[N int64 | float64](sum metricdata.Sum[N], m metricdata.Met valueType = prometheus.GaugeValue } dataPoints := make([]*metricData, 0, len(sum.DataPoints)) + if sum.IsMonotonic { + // Add _total suffix for counters + name += counterSuffix + } for _, dp := range sum.DataPoints { - if sum.IsMonotonic { - // Add _total suffix for counters - name += counterSuffix - } keys, values := getAttrs(dp.Attributes) desc := prometheus.NewDesc(name, m.Description, keys, nil) md := &metricData{ diff --git a/exporters/prometheus/exporter_test.go b/exporters/prometheus/exporter_test.go index 0aa0b3d34d6..27441e74ea1 100644 --- a/exporters/prometheus/exporter_test.go +++ b/exporters/prometheus/exporter_test.go @@ -62,6 +62,14 @@ func TestPrometheusExporter(t *testing.T) { counter.Add(ctx, 5, attrs...) counter.Add(ctx, 10.3, attrs...) counter.Add(ctx, 9, attrs...) + + attrs2 := []attribute.KeyValue{ + attribute.Key("A").String("D"), + attribute.Key("C").String("B"), + attribute.Key("E").Bool(true), + attribute.Key("F").Int(42), + } + counter.Add(ctx, 5, attrs2...) }, }, { diff --git a/exporters/prometheus/testdata/counter.txt b/exporters/prometheus/testdata/counter.txt index e8d7f654440..5da63dd144a 100755 --- a/exporters/prometheus/testdata/counter.txt +++ b/exporters/prometheus/testdata/counter.txt @@ -1,6 +1,7 @@ # HELP foo_milliseconds_total a simple counter # TYPE foo_milliseconds_total counter foo_milliseconds_total{A="B",C="D",E="true",F="42"} 24.3 +foo_milliseconds_total{A="D",C="B",E="true",F="42"} 5 # HELP target_info Target metadata # TYPE target_info gauge target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1