diff --git a/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Serializer.java b/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Serializer.java index 5053f9aedbd..193bc622383 100644 --- a/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Serializer.java +++ b/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Serializer.java @@ -513,7 +513,7 @@ String contentType() { @Override String headerName(String name, PrometheusType type) { - if (type == PrometheusType.COUNTER) { + if (type == PrometheusType.COUNTER && !name.endsWith("_total")) { return name + "_total"; } return name; @@ -652,7 +652,7 @@ static Collection getPoints(MetricData metricData) { private static String metricName(String rawMetricName, PrometheusType type) { String name = NameSanitizer.INSTANCE.apply(rawMetricName); - if (type == PrometheusType.COUNTER) { + if (type == PrometheusType.COUNTER && !name.endsWith("_total")) { name = name + "_total"; } return name; diff --git a/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/SerializerTest.java b/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/SerializerTest.java index 07db0ae02ae..df1f8416e16 100644 --- a/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/SerializerTest.java +++ b/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/SerializerTest.java @@ -59,6 +59,26 @@ class SerializerTest { 1633950672000000000L, Attributes.of(TYPE, "mcds"), 5)))); + + private static final MetricData MONOTONIC_CUMULATIVE_DOUBLE_SUM_WITH_SUFFIX_TOTAL = + ImmutableMetricData.createDoubleSum( + Resource.create(Attributes.of(stringKey("kr"), "vr")), + InstrumentationScopeInfo.builder("full") + .setVersion("version") + .setAttributes(Attributes.of(stringKey("ks"), "vs")) + .build(), + "monotonic.cumulative.double.sum.suffix.total", + "description", + "1", + ImmutableSumData.create( + /* isMonotonic= */ true, + AggregationTemporality.CUMULATIVE, + Collections.singletonList( + ImmutableDoublePointData.create( + 1633947011000000000L, + 1633950672000000000L, + Attributes.of(TYPE, "mcds"), + 5)))); private static final MetricData NON_MONOTONIC_CUMULATIVE_DOUBLE_SUM = ImmutableMetricData.createDoubleSum( Resource.create(Attributes.of(stringKey("kr"), "vr")), @@ -338,6 +358,7 @@ void prometheus004() { assertThat( serialize004( MONOTONIC_CUMULATIVE_DOUBLE_SUM, + MONOTONIC_CUMULATIVE_DOUBLE_SUM_WITH_SUFFIX_TOTAL, NON_MONOTONIC_CUMULATIVE_DOUBLE_SUM, DELTA_DOUBLE_SUM, // Deltas are dropped MONOTONIC_CUMULATIVE_LONG_SUM, @@ -361,6 +382,9 @@ void prometheus004() { + "# TYPE monotonic_cumulative_double_sum_total counter\n" + "# HELP monotonic_cumulative_double_sum_total description\n" + "monotonic_cumulative_double_sum_total{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mcds\"} 5.0 1633950672000\n" + + "# TYPE monotonic_cumulative_double_sum_suffix_total counter\n" + + "# HELP monotonic_cumulative_double_sum_suffix_total description\n" + + "monotonic_cumulative_double_sum_suffix_total{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mcds\"} 5.0 1633950672000\n" + "# TYPE non_monotonic_cumulative_double_sum gauge\n" + "# HELP non_monotonic_cumulative_double_sum description\n" + "non_monotonic_cumulative_double_sum{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmcds\"} 5.0 1633950672000\n" @@ -405,6 +429,7 @@ void openMetrics() { assertThat( serializeOpenMetrics( MONOTONIC_CUMULATIVE_DOUBLE_SUM, + MONOTONIC_CUMULATIVE_DOUBLE_SUM_WITH_SUFFIX_TOTAL, NON_MONOTONIC_CUMULATIVE_DOUBLE_SUM, DELTA_DOUBLE_SUM, // Deltas are dropped MONOTONIC_CUMULATIVE_LONG_SUM, @@ -428,6 +453,9 @@ void openMetrics() { + "# TYPE monotonic_cumulative_double_sum counter\n" + "# HELP monotonic_cumulative_double_sum description\n" + "monotonic_cumulative_double_sum_total{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mcds\"} 5.0 1633950672.000\n" + + "# TYPE monotonic_cumulative_double_sum_suffix_total counter\n" + + "# HELP monotonic_cumulative_double_sum_suffix_total description\n" + + "monotonic_cumulative_double_sum_suffix_total{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mcds\"} 5.0 1633950672.000\n" + "# TYPE non_monotonic_cumulative_double_sum gauge\n" + "# HELP non_monotonic_cumulative_double_sum description\n" + "non_monotonic_cumulative_double_sum{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmcds\"} 5.0 1633950672.000\n"