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>
.
MeterBinder
to expose these metrics. Plus, you can create spring bootautoconfigure and starter modules
to simplify the usage. You can find an example (ready to use) here: github.com/luiztoscano/spring-boot-kmetrics Hope it helps. – Forbidding