Skip to content

Commit 0e92b93

Browse files
authored
Update Prometheus client to 1.5.0 (#8080)
1 parent 2572978 commit 0e92b93

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

dependencyManagement/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val jmhVersion = "1.37"
1515
val mockitoVersion = "4.11.0"
1616
val slf4jVersion = "2.0.17"
1717
val opencensusVersion = "0.31.1"
18-
val prometheusServerVersion = "1.3.10"
18+
val prometheusServerVersion = "1.5.0"
1919
val armeriaVersion = "1.36.0"
2020
val junitVersion = "5.14.3"
2121
val okhttpVersion = "5.3.2"

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

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

66
package io.opentelemetry.exporter.prometheus;
77

8+
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;
89
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.sanitizeLabelName;
910
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.sanitizeMetricName;
1011
import static java.util.Objects.requireNonNull;
@@ -474,7 +475,7 @@ private Labels convertAttributes(
474475
attributes.forEach(
475476
(key, value) ->
476477
labelNameToValue.put(
477-
sanitizeLabelName(key.getKey()), toLabelValue(key.getType(), value)));
478+
convertLabelName(key.getKey()), toLabelValue(key.getType(), value)));
478479

479480
for (int i = 0; i < additionalAttributes.length; i += 2) {
480481
labelNameToValue.putIfAbsent(
@@ -504,7 +505,7 @@ private Labels convertAttributes(
504505
Object attributeValue = resourceAttributes.get(attributeKey);
505506
if (attributeValue != null) {
506507
labelNameToValue.putIfAbsent(
507-
sanitizeLabelName(attributeKey.getKey()), attributeValue.toString());
508+
convertLabelName(attributeKey.getKey()), attributeValue.toString());
508509
}
509510
}
510511
}
@@ -544,14 +545,24 @@ private List<AttributeKey<?>> filterAllowedResourceAttributeKeys(@Nullable Resou
544545
return allowedAttributeKeys;
545546
}
546547

548+
/**
549+
* Convert an attribute key to a legacy Prometheus label name. {@code prometheusName} converts
550+
* non-standard characters (dots, dashes, etc.) to underscores, and {@code sanitizeLabelName}
551+
* strips invalid leading prefixes.
552+
*/
553+
private static String convertLabelName(String key) {
554+
return sanitizeLabelName(prometheusName(key));
555+
}
556+
547557
private static MetricMetadata convertMetadata(MetricData metricData) {
548-
String name = sanitizeMetricName(metricData.getName());
558+
String name = sanitizeMetricName(prometheusName(metricData.getName()));
549559
String help = metricData.getDescription();
550560
Unit unit = PrometheusUnitsHelper.convertUnit(metricData.getUnit());
551561
if (unit != null && !name.endsWith(unit.toString())) {
552562
name = name + "_" + unit;
553563
}
554-
// Repeated __ are not allowed according to spec, although this is allowed in prometheus
564+
// Repeated __ are discouraged according to spec, although this is allowed in prometheus, see
565+
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/compatibility/prometheus_and_openmetrics.md#metric-metadata-1
555566
while (name.contains("__")) {
556567
name = name.replace("__", "_");
557568
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import io.opentelemetry.sdk.resources.Resource;
4747
import io.prometheus.metrics.exporter.httpserver.HTTPServer;
4848
import io.prometheus.metrics.exporter.httpserver.MetricsHandler;
49-
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_31_1.Metrics;
49+
import io.prometheus.metrics.expositionformats.generated.Metrics;
5050
import io.prometheus.metrics.model.registry.PrometheusRegistry;
5151
import java.io.ByteArrayInputStream;
5252
import java.io.IOException;
@@ -590,6 +590,7 @@ public Result authenticate(HttpExchange exchange) {
590590
* the protobuf java bindings, and assert against the string representation.
591591
*/
592592
@Test
593+
@SuppressWarnings("NonCanonicalType") // stable Metrics class extends versioned protobuf class
593594
void histogramDefaultBase2ExponentialHistogram() throws IOException {
594595
PrometheusHttpServer prometheusServer =
595596
PrometheusHttpServer.builder()

0 commit comments

Comments
 (0)