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 Prometheus text format serializers. #4178

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -98,7 +98,7 @@ static Collector.Type toMetricFamilyType(MetricData metricData) {
return Collector.Type.UNKNOWN;
}

private static final Function<String, String> sanitizer = new LabelNameSanitizer();
static final Function<String, String> sanitizer = new LabelNameSanitizer();

// Converts a list of points from MetricData to a list of Prometheus Samples.
static List<Sample> toSamples(
Expand Down Expand Up @@ -277,7 +277,7 @@ private static int estimateNumSamples(int numPoints, MetricDataType type) {
return numPoints;
}

private static Collection<? extends PointData> getPoints(MetricData metricData) {
static Collection<? extends PointData> getPoints(MetricData metricData) {
switch (metricData.getType()) {
case DOUBLE_GAUGE:
return metricData.getDoubleGaugeData().getPoints();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.prometheus;

import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoubleSumData;
import io.opentelemetry.sdk.metrics.data.LongSumData;
import io.opentelemetry.sdk.metrics.data.MetricData;

// Four types we use are same in prometheus and openmetrics format
enum PrometheusType {
GAUGE("gauge"),
COUNTER("counter"),
SUMMARY("summary"),
HISTOGRAM("histogram");

private final String typeString;

PrometheusType(String typeString) {
this.typeString = typeString;
}

static PrometheusType forMetric(MetricData metric) {
switch (metric.getType()) {
case LONG_GAUGE:
case DOUBLE_GAUGE:
return GAUGE;
case LONG_SUM:
LongSumData longSumData = metric.getLongSumData();
if (longSumData.isMonotonic()
&& longSumData.getAggregationTemporality() == AggregationTemporality.CUMULATIVE) {
return COUNTER;
}
return GAUGE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for a prometheus collector which prefers cumulative temporality to get non-monotonic, delta, sum data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe after we added preferred aggregation support it isn't possible when using our SDK. It could be possible if someone used the exporter without our SDK though (creating arbitrary MetricData).

case DOUBLE_SUM:
DoubleSumData doubleSumData = metric.getDoubleSumData();
if (doubleSumData.isMonotonic()
&& doubleSumData.getAggregationTemporality() == AggregationTemporality.CUMULATIVE) {
return COUNTER;
}
return GAUGE;
case SUMMARY:
return SUMMARY;
case HISTOGRAM:
case EXPONENTIAL_HISTOGRAM:
return HISTOGRAM;
}
throw new IllegalArgumentException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cause the entire prometheus export to crash, or is this handled in the exporter later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not wired up to an exporter yet so I believe that's a decision that is yet to be made.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can never be thrown when the BOM is used, if it did happen to be thrown then the export would fail. I've seen various linkage exceptions related to unaligned dependencies and think it's mostly not possible to use OTel without alignment so leant towards just making this an exception instead of log / fallback, but either approach would be fine here.

"Unsupported metric type, this generally indicates version misalignment "
+ "among opentelemetry dependencies. Please make sure to use opentelemetry-bom.");
}

String getTypeString() {
return typeString;
}
}
Loading