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

Update the Prometetheus metrics library #6473

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading