Skip to content

Commit

Permalink
Update the Prometetheus metrics library (#6473)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <fabian@fstab.de>
  • Loading branch information
fstab committed May 28, 2024
1 parent a1c72d1 commit cdcc58c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ val DEPENDENCIES = listOf(
"io.opentelemetry.proto:opentelemetry-proto:1.2.0-alpha",
"io.opentracing:opentracing-api:0.33.0",
"io.opentracing:opentracing-noop:0.33.0",
"io.prometheus:prometheus-metrics-exporter-httpserver:1.2.1",
"io.prometheus:prometheus-metrics-exporter-httpserver:1.3.1",
"junit:junit:4.13.2",
"nl.jqno.equalsverifier:equalsverifier:3.16.1",
"org.awaitility:awaitility:4.2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,7 @@ private static MetricMetadata convertMetadata(MetricData metricData) {
String help = metricData.getDescription();
Unit unit = PrometheusUnitsHelper.convertUnit(metricData.getUnit());
if (unit != null && !name.endsWith(unit.toString())) {
// Need to re-sanitize metric name since unit may contain illegal characters
name = sanitizeMetricName(name + "_" + unit);
name = name + "_" + unit;
}
// Repeated __ are not allowed according to spec, although this is allowed in prometheus
while (name.contains("__")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.exporter.prometheus;

import io.prometheus.metrics.model.snapshots.PrometheusNaming;
import io.prometheus.metrics.model.snapshots.Unit;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -86,11 +87,22 @@ static Unit convertUnit(String otelUnit) {
String part1 = pluralNames.getOrDefault(parts[0], parts[0]).trim();
String part2 = singularNames.getOrDefault(parts[1], parts[1]).trim();
if (part1.isEmpty()) {
return new Unit("per_" + part2);
return unitOrNull("per_" + part2);
} else {
return new Unit(part1 + "_per_" + part2);
return unitOrNull(part1 + "_per_" + part2);
}
}
return new Unit(otelUnit);
return unitOrNull(otelUnit);
}

@Nullable
private static Unit unitOrNull(String name) {
try {
return new Unit(PrometheusNaming.sanitizeUnitName(name));
} catch (IllegalArgumentException e) {
// This happens if the name cannot be converted to a valid Prometheus unit name,
// for example if name is "total".
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ private static Stream<Arguments> metricMetadataArgs() {
// if metric name ends with unit the unit is omitted - order matters
Arguments.of(
createSampleMetricData("metric_total_hertz", "hertz_total", MetricDataType.LONG_SUM),
"metric_total_hertz_hertz_total counter",
"metric_total_hertz_hertz_total description",
"metric_total_hertz_hertz_total"),
"metric_total_hertz_total counter",
"metric_total_hertz_total description",
"metric_total_hertz_total"),
// metric name cannot start with a number
Arguments.of(
createSampleMetricData("2_metric_name", "By", MetricDataType.SUMMARY),
Expand Down

0 comments on commit cdcc58c

Please sign in to comment.