From c172a9e39fdc3554da9802e7d3bef0d3eda49f2c Mon Sep 17 00:00:00 2001 From: Mickael Maison Date: Tue, 6 Aug 2024 16:31:23 +0200 Subject: [PATCH] Use Prometheus naming when matching metrics with the allowlist Signed-off-by: Mickael Maison --- .../strimzi/kafka/metrics/KafkaMetricsCollector.java | 6 +++--- .../strimzi/kafka/metrics/YammerMetricsCollector.java | 6 +++--- .../kafka/metrics/KafkaMetricsCollectorTest.java | 11 +++++++++++ .../kafka/metrics/YammerMetricsCollectorTest.java | 6 ++++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/strimzi/kafka/metrics/KafkaMetricsCollector.java b/src/main/java/io/strimzi/kafka/metrics/KafkaMetricsCollector.java index b8ab59f..0071d2e 100644 --- a/src/main/java/io/strimzi/kafka/metrics/KafkaMetricsCollector.java +++ b/src/main/java/io/strimzi/kafka/metrics/KafkaMetricsCollector.java @@ -116,10 +116,10 @@ public MetricSnapshots collect() { return new MetricSnapshots(snapshots); } - private String metricName(MetricName metricName) { - return PrometheusNaming + String metricName(MetricName metricName) { + return PrometheusNaming.prometheusName(PrometheusNaming .sanitizeMetricName(prefix + '_' + metricName.group() + '_' + metricName.name()) - .toLowerCase(Locale.ROOT); + .toLowerCase(Locale.ROOT)); } static Labels labelsFromTags(Map tags, String metricName) { diff --git a/src/main/java/io/strimzi/kafka/metrics/YammerMetricsCollector.java b/src/main/java/io/strimzi/kafka/metrics/YammerMetricsCollector.java index 4ea7c4c..53ef3e7 100644 --- a/src/main/java/io/strimzi/kafka/metrics/YammerMetricsCollector.java +++ b/src/main/java/io/strimzi/kafka/metrics/YammerMetricsCollector.java @@ -132,12 +132,12 @@ public MetricSnapshots collect() { return new MetricSnapshots(snapshots); } - private static String metricName(MetricName metricName) { - return PrometheusNaming.sanitizeMetricName( + static String metricName(MetricName metricName) { + return PrometheusNaming.prometheusName(PrometheusNaming.sanitizeMetricName( "kafka_server_" + metricName.getGroup() + '_' + metricName.getType() + '_' + - metricName.getName()).toLowerCase(Locale.ROOT); + metricName.getName()).toLowerCase(Locale.ROOT)); } static Labels labelsFromScope(String scope, String metricName) { diff --git a/src/test/java/io/strimzi/kafka/metrics/KafkaMetricsCollectorTest.java b/src/test/java/io/strimzi/kafka/metrics/KafkaMetricsCollectorTest.java index 45cb29d..6486ba4 100644 --- a/src/test/java/io/strimzi/kafka/metrics/KafkaMetricsCollectorTest.java +++ b/src/test/java/io/strimzi/kafka/metrics/KafkaMetricsCollectorTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -122,6 +123,16 @@ public void testLabelsFromTags() { assertEquals(1, labels.size()); } + @Test + public void testMetricName() { + PrometheusMetricsReporterConfig config = new PrometheusMetricsReporterConfig(Collections.emptyMap(), new PrometheusRegistry()); + KafkaMetricsCollector collector = new KafkaMetricsCollector(config); + collector.setPrefix("kafka.server"); + + String metricName = collector.metricName(new MetricName("NaMe", "KafKa.neTwork", "", Collections.emptyMap())); + assertEquals("kafka_server_kafka_network_name", metricName); + } + private void assertGaugeSnapshot(MetricSnapshot snapshot, double expectedValue, Labels expectedLabels) { assertInstanceOf(GaugeSnapshot.class, snapshot); GaugeSnapshot gaugeSnapshot = (GaugeSnapshot) snapshot; diff --git a/src/test/java/io/strimzi/kafka/metrics/YammerMetricsCollectorTest.java b/src/test/java/io/strimzi/kafka/metrics/YammerMetricsCollectorTest.java index 74b1f4b..bf3cba6 100644 --- a/src/test/java/io/strimzi/kafka/metrics/YammerMetricsCollectorTest.java +++ b/src/test/java/io/strimzi/kafka/metrics/YammerMetricsCollectorTest.java @@ -128,6 +128,12 @@ public void testLabelsFromScope() { assertEquals(1, labels.size()); } + @Test + public void testMetricName() { + String metricName = YammerMetricsCollector.metricName(new MetricName("Kafka.Server", "Log", "NumLogSegments")); + assertEquals("kafka_server_kafka_server_log_numlogsegments", metricName); + } + public Counter newCounter(String group, String type, String name) { MetricName metricName = KafkaYammerMetrics.getMetricName(group, type, name, tagsMap); return KafkaYammerMetrics.defaultRegistry().newCounter(metricName);