Skip to content

Commit

Permalink
Use Prometheus naming when matching metrics with the allowlist (strim…
Browse files Browse the repository at this point in the history
…zi#35)

Signed-off-by: Mickael Maison <[email protected]>
  • Loading branch information
mimaison authored Aug 6, 2024
1 parent f6f1f42 commit 0e6da16
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> tags, String metricName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0e6da16

Please sign in to comment.