如何使用spring Boot 2将Kafka度量暴露给/actuator/metrics

xxe27gdn  于 2022-12-18  发布在  Spring
关注(0)|答案(4)|浏览(152)

我看了一会儿,似乎没有找到答案。我正在使用Sping Boot 2,Spring Kafka 2.1.4,我想在spring boot actuator的/metrics端点中看到Kafka消费者指标。我不明白的是-我应该自己实现暴露,还是在boot 2中开箱即用?
如果我想自己实现这一点,最好的方法是什么?

rt4zxlrg

rt4zxlrg1#

micromicron v1.1.0的目标是MeterBinderKafkaConsumerMetrics实现,这应该会暴露你所寻找的Kafka消费者指标。
来源参考:
https://github.com/micrometer-metrics/micrometer/blob/master/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/kafka/KafkaConsumerMetrics.java

bz4sfanl

bz4sfanl2#

添加此配置适合我:

spring.jmx.enabled=true
nimxete2

nimxete23#

你是对的,没有现成的,你别无选择,除非你实现自己的MeterBinder。对于Apache Kafka Consumer指标本身,你应该注入一个KafkaListenerEndpointRegistry,调用它的getListenerContainers(),并使用它们的metrics()绑定到提供的MeterRegistry
当您想到一些东西时,请随时将解决方案贡献给 Boot 。

uqzxnwby

uqzxnwby4#

这适用于我(适用于Spring Boot 2.3.0.RELEASE及更高版本):

@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'

指标将在/actuator/prometheus/kafka_consumer_<metric-name>上提供。

相关问题