Search code examples
spring-bootapache-kafkaapache-kafka-streams

How can i enable kafka metrics in Kafka streams spring boot app


Prometheus end point is working but not getting kafka metrics in that

I added the micrometer-prometheus dependency and spring.jmx.enabled: true and management configuration is needed

 <!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-registry-prometheus -->
 <dependency>     
    <groupId>io.micrometer</groupId>     
    <artifactId>micrometer-registry-prometheus</artifactId>    
    <version>1.12.5</version> 
 </dependency>

I tried adding the KafkaStreamsMetrics but getting null pointer when trying to get the streams from streams builder bean.


Solution

  • StreamsBuilderFactoryBean can return the KafkaStreams once the streams state changes to Created, here is the sample to set KafkaStreamsMetrics using a state listener.

       streamsBuilderFactoryBean.setStateListener((newState, oldState) - > {
           if (oldState.name().equals(KafkaStreams.State.CREATED.name())) {
               new KafkaStreamsMetrics(streamsBuilderFactoryBean.getKafkaStreams()).bindTo(meterRegistry);
           }
       })