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

Drop delta metrics in prometheus exporter #5062

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.exporter.prometheus;

import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.LongPointData;
import io.opentelemetry.sdk.metrics.data.MetricData;
Expand All @@ -31,15 +30,13 @@ static PrometheusType forMetric(MetricData metric) {
return GAUGE;
case LONG_SUM:
SumData<LongPointData> longSumData = metric.getLongSumData();
if (longSumData.isMonotonic()
&& longSumData.getAggregationTemporality() == AggregationTemporality.CUMULATIVE) {
if (longSumData.isMonotonic()) {
return COUNTER;
}
return GAUGE;
case DOUBLE_SUM:
SumData<DoublePointData> doubleSumData = metric.getDoubleSumData();
if (doubleSumData.isMonotonic()
&& doubleSumData.getAggregationTemporality() == AggregationTemporality.CUMULATIVE) {
if (doubleSumData.isMonotonic()) {
return COUNTER;
}
return GAUGE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoubleExemplarData;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.ExemplarData;
Expand Down Expand Up @@ -104,6 +105,10 @@ final void write(Collection<MetricData> metrics, OutputStream output) throws IOE
metrics.stream()
// Not supported in specification yet.
.filter(metric -> metric.getType() != MetricDataType.EXPONENTIAL_HISTOGRAM)
// PrometheusHttpServer#getAggregationTemporality specifies cumulative temporality for
// all instruments, but non-SDK MetricProducers may not conform. We drop delta
// temporality metrics to avoid the complexity of stateful transformation to cumulative.
.filter(metric -> !isDeltaTemporality(metric))
.filter(metric -> metricNameFilter.test(metricName(metric)))
.collect(
Collectors.groupingBy(
Expand Down Expand Up @@ -209,6 +214,26 @@ private void write(MetricData metric, Writer writer) throws IOException {
}
}

private static boolean isDeltaTemporality(MetricData metricData) {
switch (metricData.getType()) {
case LONG_GAUGE:
case DOUBLE_GAUGE:
case SUMMARY:
return false;
case LONG_SUM:
return metricData.getLongSumData().getAggregationTemporality()
== AggregationTemporality.DELTA;
case DOUBLE_SUM:
return metricData.getDoubleSumData().getAggregationTemporality()
== AggregationTemporality.DELTA;
case HISTOGRAM:
return metricData.getHistogramData().getAggregationTemporality()
== AggregationTemporality.DELTA;
default:
}
throw new IllegalArgumentException("Can't happen");
}

private static void writeResource(Resource resource, Writer writer) throws IOException {
writer.write("# TYPE target info\n");
writer.write("# HELP target Target metadata\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class SerializerTest {
1633950672000000000L,
Attributes.of(TYPE, "nmcds"),
5))));
private static final MetricData MONOTONIC_DELTA_DOUBLE_SUM =
private static final MetricData DELTA_DOUBLE_SUM =
ImmutableMetricData.createDoubleSum(
Resource.create(Attributes.of(stringKey("kr"), "vr")),
InstrumentationScopeInfo.builder("full")
Expand All @@ -97,25 +97,6 @@ class SerializerTest {
1633950672000000000L,
Attributes.of(TYPE, "mdds"),
5))));
private static final MetricData NON_MONOTONIC_DELTA_DOUBLE_SUM =
ImmutableMetricData.createDoubleSum(
Resource.create(Attributes.of(stringKey("kr"), "vr")),
InstrumentationScopeInfo.builder("full")
.setVersion("version")
.setAttributes(Attributes.of(stringKey("ks"), "vs"))
.build(),
"instrument.name",
"unused",
"1",
ImmutableSumData.create(
/* isMonotonic= */ false,
AggregationTemporality.DELTA,
Collections.singletonList(
ImmutableDoublePointData.create(
1633947011000000000L,
1633950672000000000L,
Attributes.of(TYPE, "nmdds"),
5))));
private static final MetricData MONOTONIC_CUMULATIVE_LONG_SUM =
ImmutableMetricData.createLongSum(
Resource.create(Attributes.of(stringKey("kr"), "vr")),
Expand Down Expand Up @@ -154,7 +135,7 @@ class SerializerTest {
1633950672000000000L,
Attributes.of(TYPE, "nmcls"),
5))));
private static final MetricData MONOTONIC_DELTA_LONG_SUM =
private static final MetricData DELTA_LONG_SUM =
ImmutableMetricData.createLongSum(
Resource.create(Attributes.of(stringKey("kr"), "vr")),
InstrumentationScopeInfo.builder("full")
Expand All @@ -173,25 +154,6 @@ class SerializerTest {
1633950672000000000L,
Attributes.of(TYPE, "mdls"),
5))));
private static final MetricData NON_MONOTONIC_DELTA_LONG_SUM =
ImmutableMetricData.createLongSum(
Resource.create(Attributes.of(stringKey("kr"), "vr")),
InstrumentationScopeInfo.builder("full")
.setVersion("version")
.setAttributes(Attributes.of(stringKey("ks"), "vs"))
.build(),
"instrument.name",
"unused",
"1",
ImmutableSumData.create(
/* isMonotonic= */ false,
AggregationTemporality.DELTA,
Collections.singletonList(
ImmutableLongPointData.create(
1633947011000000000L,
1633950672000000000L,
Attributes.of(TYPE, "nmdls"),
5))));

private static final MetricData DOUBLE_GAUGE =
ImmutableMetricData.createDoubleGauge(
Expand Down Expand Up @@ -242,7 +204,7 @@ class SerializerTest {
Arrays.asList(
ImmutableValueAtQuantile.create(0.9, 0.1),
ImmutableValueAtQuantile.create(0.99, 0.3))))));
private static final MetricData HISTOGRAM_NO_ATTRIBUTES =
private static final MetricData DELTA_HISTOGRAM =
ImmutableMetricData.createDoubleHistogram(
Resource.create(Attributes.of(stringKey("kr"), "vr")),
InstrumentationScopeInfo.builder("full")
Expand All @@ -254,6 +216,29 @@ class SerializerTest {
"1",
ImmutableHistogramData.create(
AggregationTemporality.DELTA,
Collections.singletonList(
ImmutableHistogramPointData.create(
1633947011000000000L,
1633950672000000000L,
Attributes.empty(),
1.0,
null,
null,
Collections.emptyList(),
Collections.singletonList(2L),
Collections.emptyList()))));
private static final MetricData CUMULATIVE_HISTOGRAM_NO_ATTRIBUTES =
ImmutableMetricData.createDoubleHistogram(
Resource.create(Attributes.of(stringKey("kr"), "vr")),
InstrumentationScopeInfo.builder("full")
.setVersion("version")
.setAttributes(Attributes.of(stringKey("ks"), "vs"))
.build(),
"instrument.name",
"unused",
"1",
ImmutableHistogramData.create(
AggregationTemporality.CUMULATIVE,
Collections.singletonList(
ImmutableHistogramPointData.create(
1633947011000000000L,
Expand All @@ -274,7 +259,7 @@ class SerializerTest {
TraceFlags.getDefault(),
TraceState.getDefault()),
/* value= */ 4))))));
private static final MetricData HISTOGRAM_SINGLE_ATTRIBUTE =
private static final MetricData CUMULATIVE_HISTOGRAM_SINGLE_ATTRIBUTE =
ImmutableMetricData.createDoubleHistogram(
Resource.create(Attributes.of(stringKey("kr"), "vr")),
InstrumentationScopeInfo.builder("full")
Expand All @@ -285,7 +270,7 @@ class SerializerTest {
"unused",
"1",
ImmutableHistogramData.create(
AggregationTemporality.DELTA,
AggregationTemporality.CUMULATIVE,
Collections.singletonList(
ImmutableHistogramPointData.create(
1633947011000000000L,
Expand Down Expand Up @@ -348,17 +333,16 @@ void prometheus004() {
serialize004(
MONOTONIC_CUMULATIVE_DOUBLE_SUM,
NON_MONOTONIC_CUMULATIVE_DOUBLE_SUM,
MONOTONIC_DELTA_DOUBLE_SUM,
NON_MONOTONIC_DELTA_DOUBLE_SUM,
DELTA_DOUBLE_SUM, // Deltas are dropped
MONOTONIC_CUMULATIVE_LONG_SUM,
NON_MONOTONIC_CUMULATIVE_LONG_SUM,
MONOTONIC_DELTA_LONG_SUM,
NON_MONOTONIC_DELTA_LONG_SUM,
DELTA_LONG_SUM, // Deltas are dropped
DOUBLE_GAUGE,
LONG_GAUGE,
SUMMARY,
HISTOGRAM_NO_ATTRIBUTES,
HISTOGRAM_SINGLE_ATTRIBUTE,
DELTA_HISTOGRAM, // Deltas are dropped
CUMULATIVE_HISTOGRAM_NO_ATTRIBUTES,
CUMULATIVE_HISTOGRAM_SINGLE_ATTRIBUTE,
DOUBLE_GAUGE_NO_ATTRIBUTES,
DOUBLE_GAUGE_MULTIPLE_ATTRIBUTES))
.isEqualTo(
Expand All @@ -375,11 +359,7 @@ void prometheus004() {
+ "# TYPE instrument_name gauge\n"
+ "# HELP instrument_name description\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmcds\"} 5.0 1633950672000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mdds\"} 5.0 1633950672000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmdds\"} 5.0 1633950672000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmcls\"} 5.0 1633950672000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mdls\"} 5.0 1633950672000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmdls\"} 5.0 1633950672000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"dg\"} 5.0 1633950672000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"lg\"} 5.0 1633950672000\n"
+ "instrument_name_count{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"s\"} 5.0 1633950672000\n"
Expand All @@ -402,16 +382,16 @@ void openMetrics() {
serializeOpenMetrics(
MONOTONIC_CUMULATIVE_DOUBLE_SUM,
NON_MONOTONIC_CUMULATIVE_DOUBLE_SUM,
MONOTONIC_DELTA_DOUBLE_SUM,
NON_MONOTONIC_DELTA_DOUBLE_SUM,
DELTA_DOUBLE_SUM, // Deltas are dropped
MONOTONIC_CUMULATIVE_LONG_SUM,
NON_MONOTONIC_CUMULATIVE_LONG_SUM,
MONOTONIC_DELTA_LONG_SUM,
NON_MONOTONIC_DELTA_LONG_SUM,
DELTA_LONG_SUM, // Deltas are dropped
DOUBLE_GAUGE,
LONG_GAUGE,
SUMMARY,
HISTOGRAM_SINGLE_ATTRIBUTE,
DELTA_HISTOGRAM, // Deltas are dropped
CUMULATIVE_HISTOGRAM_NO_ATTRIBUTES,
CUMULATIVE_HISTOGRAM_SINGLE_ATTRIBUTE,
DOUBLE_GAUGE_NO_ATTRIBUTES,
DOUBLE_GAUGE_MULTIPLE_ATTRIBUTES))
.isEqualTo(
Expand All @@ -425,18 +405,17 @@ void openMetrics() {
+ "# HELP instrument_name description\n"
+ "instrument_name_total{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mcds\"} 5.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmcds\"} 5.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mdds\"} 5.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmdds\"} 5.0 1633950672.000\n"
+ "instrument_name_total{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mcls\"} 5.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmcls\"} 5.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"mdls\"} 5.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"nmdls\"} 5.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"dg\"} 5.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"lg\"} 5.0 1633950672.000\n"
+ "instrument_name_count{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"s\"} 5.0 1633950672.000\n"
+ "instrument_name_sum{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"s\"} 7.0 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"s\",quantile=\"0.9\"} 0.1 1633950672.000\n"
+ "instrument_name{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"s\",quantile=\"0.99\"} 0.3 1633950672.000\n"
+ "instrument_name_count{otel_scope_name=\"full\",otel_scope_version=\"version\"} 2.0 1633950672.000\n"
+ "instrument_name_sum{otel_scope_name=\"full\",otel_scope_version=\"version\"} 1.0 1633950672.000\n"
+ "instrument_name_bucket{otel_scope_name=\"full\",otel_scope_version=\"version\",le=\"+Inf\"} 2.0 1633950672.000 # {span_id=\"0000000000000002\",trace_id=\"00000000000000000000000000000001\"} 4.0 0.001\n"
+ "instrument_name_count{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"hs\"} 2.0 1633950672.000\n"
+ "instrument_name_sum{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"hs\"} 1.0 1633950672.000\n"
+ "instrument_name_bucket{otel_scope_name=\"full\",otel_scope_version=\"version\",type=\"hs\",le=\"+Inf\"} 2.0 1633950672.000 # {span_id=\"0000000000000002\",trace_id=\"00000000000000000000000000000001\"} 4.0 0.001\n"
Expand Down