-
Notifications
You must be signed in to change notification settings - Fork 7
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
Merged
mimaison
merged 5 commits into
strimzi:main
from
OwenCorrigan76:KafkaPrometheusMetricsReporter_performance_improvements
Aug 16, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf86d91
move label filtering into Reporter
OwenCorrigan76 95afd33
reviewing comments and fixing issues
OwenCorrigan76 bf14478
further review comments
OwenCorrigan76 333a307
more review comments
OwenCorrigan76 8bc61a8
fix mistakes
OwenCorrigan76 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(prefix, metric.metricName()); | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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