简体   繁体   English

我如何向普罗米修斯报告 Kafka Producer 的指标(使用 spring boot)

[英]How can I Report Kafka Producer's metrics to prometheus(using spring boot)

I am working with spring-integration for data flow from a UDP endpoint to kafka.我正在使用 spring-integration 处理从 UDP 端点到 kafka 的数据流。 I have initialized a replyingKafkaTemplate as a @Bean in the @Configuration with both consumer and producer configurations.我已经在 @Configuration 中使用消费者和生产者配置将一个 replyingKafkaTemplate 初始化为 @Bean。 When my server is up and after sending some udp requests, I can see the consumer's metrics.当我的服务器启动并发送一些 udp 请求后,我可以看到消费者的指标。 However, I cannot see the producer's metrics even after setting a jmx reporter in the Producer Configuration.但是,即使在生产者配置中设置了 jmx 报告器后,我也看不到生产者的指标。

I have tried not to set the producer metrics reporter assuming it will automatically appear as the consumer metrics did(with no extra configuration there)我尝试不设置生产者指标报告器,假设它会像消费者指标一样自动出现(那里没有额外的配置)

producer configuration生产者配置

Map<String, Object> configProps = new HashMap<>();
        configProps.put(
                ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
                bootstrapAddress);
        configProps.put(
                ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
                StringSerializer.class);
        configProps.put(
                ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
                KafkaAvroSerializer.class);
        configProps.put("schema.registry.url", "http://schema-regisry-server:8081");
        configProps.put(
                ProducerConfig.RETRIES_CONFIG,
                3);
        configProps.put(ProducerConfig.RECONNECT_BACKOFF_MS_CONFIG, 500);
        configProps.put(ProducerConfig.RECONNECT_BACKOFF_MAX_MS_CONFIG, 5000);
        configProps.put(ProducerConfig.METRIC_REPORTER_CLASSES_CONFIG, "org.apache.kafka.common.metrics.JmxReporter");
        configProps.put(ProducerConfig.METRICS_RECORDING_LEVEL_CONFIG, "INFO");

        printConfigProps(configProps);
        return new DefaultKafkaProducerFactory<>(configProps);

consumer configuration消费者配置

Map<String, Object> properties = new HashMap<>();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class);
        properties.put("schema.registry.url", "http://schema-regisry-server:8081");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "spring-integration");
        // automatically reset the offset to the earliest offset
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        return properties;

kafka template creation kafka模板创建

@Bean
    public ReplyingKafkaTemplate<String, DataModel, DataModel> replyKafkaTemplate(ProducerFactory<String, DataModel> pf, KafkaMessageListenerContainer<String, DataModel> container) {
        ReplyingKafkaTemplate<String, DataModel, DataModel> template = new ReplyingKafkaTemplate<>(pf, container);
        template.start();
        return template;
    }

Listener container creation:侦听器容器创建:

@Bean
    public KafkaMessageListenerContainer<String, DataModel> replyContainer(ConsumerFactory<String, DataModel> cf) {
        ContainerProperties containerProperties = new ContainerProperties(destinationTopic);
        containerProperties.setGroupId("test");
        return new KafkaMessageListenerContainer<>(cf, containerProperties);
    }

ConsumerFactory creation消费者工厂创建

@Bean
    public ConsumerFactory<?, ?> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

Spring Boot 2 versions prior to 2.3.0 are only exposing consumer metrics by default. 2.3.0 之前的 Spring Boot 2 版本默认仅公开消费者指标。 Spring Boot 2.3.0 (released a few weeks ago) is dependent on Micrometer 1.4, which is exposing both consumer and producer metrics by default. Spring Boot 2.3.0(几周前发布)依赖于 Micrometer 1.4,它默认公开消费者和生产者指标。 If you cant use the latest version of Spring Boot, you'll have to implement it yourself.如果你不能使用最新版本的 Spring Boot,你将不得不自己实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM