Search code examples
spring-bootapache-kafkaspring-boot-actuatorspring-kafka

Kafka MetricsReporter backed by Spring Boot Actuator


I decided to go back to my idea of integrating Kafka Metrics with Spring Boot Actuator, which I've mentioned here:

https://github.com/spring-projects/spring-boot/issues/6227

As of now I have a separate "sandbox" project with working code, which I want to merge into Spring Boot. And now I'm a bit confused. Part of my tests require powermock to Mock Kafka's "super-secured" class:

package org.apache.kafka.common.metrics;

// some imports...

public final class KafkaMetric implements Metric {
    private MetricName metricName;
    private final Object lock;
    private final Time time;
    private final Measurable measurable;
    private MetricConfig config;

    KafkaMetric(Object lock, MetricName metricName, Measurable measurable, MetricConfig config, Time time) {
        this.metricName = metricName;
        this.lock = lock;
        this.measurable = measurable;
        this.config = config;
        this.time = time;
    }

    // some good code here, but no public constructor and the class is final... of course.

}

But powermock is not used in Spring Boot.

What should I do?

  1. Add latest stable powermock - both in spring-boot-dependencies and spring-boot-actuator.
  2. Add latest stable powermock to spring-boot-actuator only - let it be a hidden secret helper.
  3. Exclude the tests which require powermock.
  4. Forget about Kafka Metrics, they're huge and scary and nobody wants them in our nice and friendly Spring Boot Actuator.

Solution

  • Having your confirmation in the comments, I'm just copying here my comment as a valid answer to close the question properly.

    You can try to use Metrics.addMetric() to create KafkaMetric instance and than react to it via your KafkaStatisticsProvider.metricChange(), which you will register in the Metrics instance before testing. Instead of Powermock, of course.

    I will try to find time next week to contribute to your actuator project that part to avoid Powermock.