spring - 从 Kafka 到 Actuator 的报告指标

标签 spring spring-boot apache-kafka kafka-consumer-api spring-kafka

我正在尝试从 kafka 获取一些指标(客户端延迟等),以供 prometheus 使用。

我的方法是编写一个简单的 springboot 应用程序,该应用程序公开 prometheus 的指标。据我了解,kafka 通过 MetricsReporter 接口(interface)向所有消费者提供指标。

所以我实现了一个应该完全做到这一点的类:

public class MonitoringIntegration implements MetricsReporter {

    @Override
    public void init(List<KafkaMetric> list) {
        System.out.println("init");
        for (KafkaMetric kafkaMetric : list) {
            System.out.println(kafkaMetric.metricName());
            System.out.println(kafkaMetric.metricValue());
        }
    }

    @Override
    public void metricChange(KafkaMetric kafkaMetric) {
        System.out.println("Metric Change");
        System.out.println(kafkaMetric.metricName());
        System.out.println(kafkaMetric.metricValue());
    }

    @Override
    public void metricRemoval(KafkaMetric kafkaMetric) {
        System.out.println("Removal");
        System.out.println(kafkaMetric.metricName());
        System.out.println(kafkaMetric.metricValue());
    }

    @Override
    public void close() {
        System.out.println("close");
    }

    @Override
    public void configure(Map<String, ?> map) {
        System.out.println("Configuring");
        System.out.println(map);
    }
}

我用一个 bean 注册了这个类:

@Configuration
public class MetricConfiguration {

    @Bean
    public ProducerFactory<?, ?> kafkaProducerFactory(KafkaProperties properties) {
        Map<String, Object> producerProperties = properties.buildProducerProperties();
        producerProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
                MonitoringIntegration.class.getName());
        return new DefaultKafkaProducerFactory<>(producerProperties);
    }

    @Bean
    public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties) {
        Map<String, Object> consumererProperties = properties.buildConsumerProperties();
        consumererProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
                MonitoringIntegration.class.getName());
        return new DefaultKafkaConsumerFactory<>(consumererProperties);
    }
}

当我启动应用程序时,一些指标将打印到 cmd,但它们具有所有默认值(0.0、无限、..),并且仅在应用程序启动后提供一次。

为什么我没有获得指标?我做错了什么?

干杯,

法比安

最佳答案

Spring Kafka 已经将 Kafka 指标公开为 JMX 指标。您不需要更新/发送指标到 Prometheus。 Prometheus 服务器将自动从应用程序的“/prometheus”端点读取。在 Spring 项目中启用带有 Prometheus 的 Spring Actuator 并配置 Prometheus 服务器以从中读取数据。

这是一个使用 Spring Boot 的很好的例子 - https://www.callicoder.com/spring-boot-actuator-metrics-monitoring-dashboard-prometheus-grafana/

MetricsReporter 不用于“报告”指标值的变化。检查文档。 (由于某种原因我找不到最新的 API)。

https://archive.apache.org/dist/kafka/0.8.2-beta/java-doc/org/apache/kafka/common/metrics/MetricsReporter.html

A plugin interface to allow things to listen as new metrics are created so they can be reported.

metricChange() 方法仅在指标发生更改时才会被调用。这就是您在应用程序启动期间看到前几个输出的原因,因为指标已创建。

关于spring - 从 Kafka 到 Actuator 的报告指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53706947/

相关文章:

windows - Kafka 无法在 Windows 上启动 - 未找到 key :\tmp\kafka-logs

java - @DataJpaTest 使用存储库保存实体,无法使用 JdbcTemplate 检索数据

Spring Data + QueryDSL空谓词+谓词链接

java - Service层异常处理

java - 当 URL 为 "+"时,JarURLConnection.connect 抛出 java.io.FileNotFoundException

java - 在 Spring Boot 中返回状态码为 202 的 HTTP 响应

ubuntu - kafka 经纪人在开始时不可用

类 FIELD 上的 java 注释不起作用

spring-boot - 运行 Spring Boot Scheduler 和 Apache Camel 时出现问题

java - Kubernetes 中的 Kafka UnknownHostException 异常