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

Apply metric filtering when adding and removing metrics instead of in the collect method #36

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ public void setPrefix(String prefix) {
* Adds a Kafka metric to be collected.
*
* @param metric The Kafka metric to add.
* The metric name is sanitized and converted to lower case using the PrometheusNaming convention.
*/
public void addMetric(KafkaMetric metric) {
metrics.put(metric.metricName(), metric);
String prometheusMetricName = PrometheusNaming
.sanitizeMetricName(prefix + '_' + metric.metricName().group() + '_' + metric.metricName().name())
.toLowerCase(Locale.ROOT);
if (config.isAllowed(prometheusMetricName)) {
metrics.put(metric.metricName(), metric);
}
}

/**
Expand All @@ -89,10 +95,6 @@ public MetricSnapshots collect() {
KafkaMetric kafkaMetric = entry.getValue();

String prometheusMetricName = metricName(metricName);
if (!config.isAllowed(prometheusMetricName)) {
LOG.trace("Ignoring metric {} as it does not match the allowlist", prometheusMetricName);
continue;
}
Labels labels = labelsFromTags(metricName.tags(), metricName.name());
LOG.debug("Collecting metric {} with the following labels: {}", prometheusMetricName, labels);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class KafkaMetricsCollectorTest {

Expand Down Expand Up @@ -133,6 +134,27 @@ public void testMetricName() {
assertEquals("kafka_server_kafka_network_name", metricName);
}

@Test
public void testAddMetricFiltersMetrics() {
Map<String, String> props = new HashMap<>();
props.put(PrometheusMetricsReporterConfig.ALLOWLIST_CONFIG, "kafka_server_group_name");
PrometheusMetricsReporterConfig config = new PrometheusMetricsReporterConfig(props, new PrometheusRegistry());
System.out.println(config);
KafkaMetricsCollector collector = new KafkaMetricsCollector(config);
collector.setPrefix("kafka.server");

// Create two metrics, one that matches the allowlist and one that does not
KafkaMetric allowedMetric = buildMetric("name", "group_name", 1.0);
KafkaMetric disallowedMetric = buildMetric("name", "other", 1.0);

// Add the metrics
collector.addMetric(allowedMetric);
collector.addMetric(disallowedMetric);

assertEquals("kafka_server_group_name_name", collector.metricName(allowedMetric.metricName()));
assertNotEquals("kafka_server_group_name_name", collector.metricName(disallowedMetric.metricName()));
}

private void assertGaugeSnapshot(MetricSnapshot snapshot, double expectedValue, Labels expectedLabels) {
assertInstanceOf(GaugeSnapshot.class, snapshot);
GaugeSnapshot gaugeSnapshot = (GaugeSnapshot) snapshot;
Expand Down