-
Notifications
You must be signed in to change notification settings - Fork 9
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
Move label filtering into KafkaPrometheusReporter and out of the KafkaPrometheusCollector #38
Changes from 3 commits
bf86d91
95afd33
bf14478
333a307
8bc61a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import io.prometheus.metrics.exporter.httpserver.HTTPServer; | ||
import io.prometheus.metrics.instrumentation.jvm.JvmMetrics; | ||
import io.prometheus.metrics.model.registry.PrometheusRegistry; | ||
import io.prometheus.metrics.model.snapshots.PrometheusNaming; | ||
import org.apache.kafka.common.config.ConfigException; | ||
import org.apache.kafka.common.metrics.KafkaMetric; | ||
import org.apache.kafka.common.metrics.MetricsContext; | ||
|
@@ -29,12 +30,15 @@ | |
public class KafkaPrometheusMetricsReporter implements MetricsReporter { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(KafkaPrometheusMetricsReporter.class); | ||
|
||
private final PrometheusRegistry registry; | ||
@SuppressFBWarnings({"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"}) // This field is initialized in the configure method | ||
private KafkaMetricsCollector kafkaMetricsCollector; | ||
private KafkaMetricsCollector collector; | ||
@SuppressFBWarnings({"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"}) // This field is initialized in the init method | ||
private PrometheusMetricsReporterConfig config; | ||
@SuppressFBWarnings({"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"}) // This field is initialized in the configure method | ||
private Optional<HTTPServer> httpServer; | ||
@SuppressFBWarnings({"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"}) // This field is initialized in the setPrefix method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
private String prefix; | ||
|
||
/** | ||
* Constructor | ||
|
@@ -50,8 +54,8 @@ public KafkaPrometheusMetricsReporter() { | |
|
||
@Override | ||
public void configure(Map<String, ?> map) { | ||
PrometheusMetricsReporterConfig config = new PrometheusMetricsReporterConfig(map, registry); | ||
kafkaMetricsCollector = new KafkaMetricsCollector(config); | ||
config = new PrometheusMetricsReporterConfig(map, registry); | ||
collector = new KafkaMetricsCollector(); | ||
// Add JVM metrics | ||
JvmMetrics.builder().register(registry); | ||
httpServer = config.startHttpServer(); | ||
|
@@ -60,25 +64,30 @@ public void configure(Map<String, ?> map) { | |
|
||
@Override | ||
public void init(List<KafkaMetric> metrics) { | ||
registry.register(kafkaMetricsCollector); | ||
registry.register(collector); | ||
for (KafkaMetric metric : metrics) { | ||
metricChange(metric); | ||
} | ||
} | ||
|
||
@Override | ||
public void metricChange(KafkaMetric metric) { | ||
kafkaMetricsCollector.addMetric(metric); | ||
String prometheusName = MetricWrapper.prometheusName(this.prefix, metric.metricName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
if (!config.isAllowed(prometheusName)) { | ||
LOG.trace("Ignoring metric {} as it does not match the allowlist", prometheusName); | ||
} else { | ||
MetricWrapper metricWrapper = new MetricWrapper(prometheusName, metric, metric.metricName().name()); | ||
OwenCorrigan76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
collector.addMetric(metric.metricName(), metricWrapper); | ||
} | ||
} | ||
|
||
@Override | ||
public void metricRemoval(KafkaMetric metric) { | ||
kafkaMetricsCollector.removeMetric(metric); | ||
collector.removeMetric(metric.metricName()); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
registry.unregister(kafkaMetricsCollector); | ||
registry.unregister(collector); | ||
} | ||
|
||
@Override | ||
|
@@ -97,7 +106,7 @@ public Set<String> reconfigurableConfigs() { | |
@Override | ||
public void contextChange(MetricsContext metricsContext) { | ||
String prefix = metricsContext.contextLabels().get(MetricsContext.NAMESPACE); | ||
kafkaMetricsCollector.setPrefix(prefix); | ||
this.prefix = PrometheusNaming.prometheusName(prefix); | ||
} | ||
|
||
// for testing | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
init
->configure