Skip to content

Commit

Permalink
Add Prometheus text format serializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Feb 16, 2022
1 parent 6266b16 commit 1f49ebb
Show file tree
Hide file tree
Showing 5 changed files with 1,041 additions and 16 deletions.
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;
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(
"Unsupported metric type, this generally indicates version misalignment "
+ "among opentelemetry dependencies. Please make sure to use opentelemetry-bom.");
}

String getTypeString() {
return typeString;
}
}
Loading

0 comments on commit 1f49ebb

Please sign in to comment.