Skip to content

Commit cdcc58c

Browse files
authored
Update the Prometetheus metrics library (#6473)
Signed-off-by: Fabian Stäber <[email protected]>
1 parent a1c72d1 commit cdcc58c

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

dependencyManagement/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ val DEPENDENCIES = listOf(
7070
"io.opentelemetry.proto:opentelemetry-proto:1.2.0-alpha",
7171
"io.opentracing:opentracing-api:0.33.0",
7272
"io.opentracing:opentracing-noop:0.33.0",
73-
"io.prometheus:prometheus-metrics-exporter-httpserver:1.2.1",
73+
"io.prometheus:prometheus-metrics-exporter-httpserver:1.3.1",
7474
"junit:junit:4.13.2",
7575
"nl.jqno.equalsverifier:equalsverifier:3.16.1",
7676
"org.awaitility:awaitility:4.2.1",

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,7 @@ private static MetricMetadata convertMetadata(MetricData metricData) {
537537
String help = metricData.getDescription();
538538
Unit unit = PrometheusUnitsHelper.convertUnit(metricData.getUnit());
539539
if (unit != null && !name.endsWith(unit.toString())) {
540-
// Need to re-sanitize metric name since unit may contain illegal characters
541-
name = sanitizeMetricName(name + "_" + unit);
540+
name = name + "_" + unit;
542541
}
543542
// Repeated __ are not allowed according to spec, although this is allowed in prometheus
544543
while (name.contains("__")) {

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusUnitsHelper.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.exporter.prometheus;
77

8+
import io.prometheus.metrics.model.snapshots.PrometheusNaming;
89
import io.prometheus.metrics.model.snapshots.Unit;
910
import java.util.Map;
1011
import java.util.concurrent.ConcurrentHashMap;
@@ -86,11 +87,22 @@ static Unit convertUnit(String otelUnit) {
8687
String part1 = pluralNames.getOrDefault(parts[0], parts[0]).trim();
8788
String part2 = singularNames.getOrDefault(parts[1], parts[1]).trim();
8889
if (part1.isEmpty()) {
89-
return new Unit("per_" + part2);
90+
return unitOrNull("per_" + part2);
9091
} else {
91-
return new Unit(part1 + "_per_" + part2);
92+
return unitOrNull(part1 + "_per_" + part2);
9293
}
9394
}
94-
return new Unit(otelUnit);
95+
return unitOrNull(otelUnit);
96+
}
97+
98+
@Nullable
99+
private static Unit unitOrNull(String name) {
100+
try {
101+
return new Unit(PrometheusNaming.sanitizeUnitName(name));
102+
} catch (IllegalArgumentException e) {
103+
// This happens if the name cannot be converted to a valid Prometheus unit name,
104+
// for example if name is "total".
105+
return null;
106+
}
95107
}
96108
}

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverterTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ private static Stream<Arguments> metricMetadataArgs() {
283283
// if metric name ends with unit the unit is omitted - order matters
284284
Arguments.of(
285285
createSampleMetricData("metric_total_hertz", "hertz_total", MetricDataType.LONG_SUM),
286-
"metric_total_hertz_hertz_total counter",
287-
"metric_total_hertz_hertz_total description",
288-
"metric_total_hertz_hertz_total"),
286+
"metric_total_hertz_total counter",
287+
"metric_total_hertz_total description",
288+
"metric_total_hertz_total"),
289289
// metric name cannot start with a number
290290
Arguments.of(
291291
createSampleMetricData("2_metric_name", "By", MetricDataType.SUMMARY),

0 commit comments

Comments
 (0)