From 5cb870690a4fb5af7c8dfed4b042af67188ba0c1 Mon Sep 17 00:00:00 2001 From: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com> Date: Tue, 3 Sep 2024 14:52:18 +0900 Subject: [PATCH] Rename uptime description for Prometheus in rename filter Prometheus prescribes what the description of the uptime metric should be[1], and while it does not enforce this on the server, the pushgateway complains. [1] https://prometheus.io/docs/instrumenting/writing_clientlibs/#process-metrics Closes gh-5290 --- .../prometheusmetrics/PrometheusRenameFilter.java | 5 +++++ .../prometheusmetrics/PrometheusRenameFilterTest.java | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusRenameFilter.java b/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusRenameFilter.java index 8f791fa882..e79ef5e7dd 100644 --- a/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusRenameFilter.java +++ b/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusRenameFilter.java @@ -16,6 +16,7 @@ package io.micrometer.prometheusmetrics; import io.micrometer.core.instrument.Meter; +import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.config.MeterFilter; import java.util.HashMap; @@ -39,6 +40,10 @@ public class PrometheusRenameFilter implements MeterFilter { @Override public Meter.Id map(Meter.Id id) { + if (id.getName().equals("process.start.time")) { + return new Meter.Id(id.getName(), Tags.of(id.getTagsAsIterable()), id.getBaseUnit(), + "Start time of the process since unix epoch in seconds.", id.getType()); + } String convertedName = MICROMETER_TO_PROMETHEUS_NAMES.get(id.getName()); return convertedName == null ? id : id.withName(convertedName); } diff --git a/implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusRenameFilterTest.java b/implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusRenameFilterTest.java index 7a8cb67bc5..697e0c7f7c 100644 --- a/implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusRenameFilterTest.java +++ b/implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusRenameFilterTest.java @@ -15,9 +15,11 @@ */ package io.micrometer.prometheusmetrics; +import io.micrometer.core.Issue; import io.micrometer.core.instrument.Gauge; import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.binder.system.UptimeMetrics; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import org.junit.jupiter.api.Test; @@ -49,4 +51,13 @@ void doesChangeApplicableMeter() { assertThat(actual.getName()).isEqualTo("process.open.fds"); } + @Test + @Issue("#5290") + void processStartTimeDescriptionMatchesPrometheusHelpString() { + registry.config().meterFilter(filter); + new UptimeMetrics().bindTo(registry); + assertThat(registry.get("process.start.time").meter().getId().getDescription()) + .isEqualTo("Start time of the process since unix epoch in seconds."); + } + }