Michael Azimov
Michael Azimov

Reputation: 131

How to expose kafka metrics to /actuator/metrics with spring boot 2

I was looking a while and didn't seem to find the answer. I'm using Spring boot 2, Spring Kafka 2.1.4 and I want to see the kafka consumer metrics in the /metrics endpoint of spring boot actuator. What I don't understand is - should I implenemt the exposure myself or is this comes out-of-box in boot 2 ?

If I to implement this my self, what is the best way to do it?

Upvotes: 12

Views: 23766

Answers (4)

jumping_monkey
jumping_monkey

Reputation: 7865

This works for me(For Spring Boot 2.3.0.RELEASE and above):

@Autowired
private MeterRegistry meterRegistry;

@Bean
public ConsumerFactory<Object, Object> consumerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer.class);
    props.put(ErrorHandlingDeserializer.KEY_DESERIALIZER_CLASS, JsonDeserializer.class);
    props.put(ErrorHandlingDeserializer.VALUE_DESERIALIZER_CLASS, JsonDeserializer.class);

    //More code here

    DefaultKafkaConsumerFactory<Object,Object> cf = new DefaultKafkaConsumerFactory<>(props);
    cf.addListener(new MicrometerConsumerListener<>(meterRegistry));
        
    return new DefaultKafkaConsumerFactory<>(props);
}

build.gradle
implementation 'io.micrometer:micrometer-registry-prometheus'

Metrics will be available at /actuator/prometheus/kafka_consumer_<metric-name>.

Upvotes: 2

ThinkAlone
ThinkAlone

Reputation: 51

add this config works for me:

spring.jmx.enabled=true

Upvotes: 2

Derek Slife
Derek Slife

Reputation: 22496

Targeted for micrometer v1.1.0 is the KafkaConsumerMetrics implementation of MeterBinder. This should expose the kafka consumer metrics you're looking for.

Source Reference:

https://github.com/micrometer-metrics/micrometer/blob/master/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/kafka/KafkaConsumerMetrics.java

Upvotes: 5

Artem Bilan
Artem Bilan

Reputation: 121560

You are right, there is no out-of-the-box one. You don't have choice unless to implement your own MeterBinder. For the Apache Kafka Consumer metrics per se, you should inject a KafkaListenerEndpointRegistry and call its getListenerContainers() and use their metrics() to bind to the provided MeterRegistry.

When you come up with something, feel free to contribute the solution back to Spring Boot.

Upvotes: 1

Related Questions