简体   繁体   English

如何在 Kafka 中访问消费者的特定指标

[英]How to access a specific metric from a consumer in Kafka

i am working with a java producer and consumer, i am trying to visualize my consumer lag (i have it lagging in purpose) from my java application.我正在使用 java 生产者和消费者,我试图从我的 java 应用程序中可视化我的消费者滞后(我故意滞后)。 I want to log that lag as a stdout.我想将该滞后记录为标准输出。 I have tried to use JMX but i havent been able to use it in the java application.我曾尝试使用 JMX,但无法在 java 应用程序中使用它。 I have used this (just a snippet, the set up of the consumer is done in the application)我用过这个(只是一个片段,消费者的设置是在应用程序中完成的)

private KafkaConsumer<String, String> consumer

Map<MetricName, ? extends Metric> metrics = consumer.metrics();
System.out.println(metrics);

But it only gives me the description of the metrics but not the value.但它只给了我对指标的描述,而不是价值。 I see in my Kafka CLI and it shows the right lag for that consumer.我在我的 Kafka CLI 中看到,它显示了该消费者的正确延迟。

First identify which metrics you are interested in using the docs or printing all metrics like in your question.首先确定您对使用文档或打印问题中的所有指标感兴趣的指标。

Then find the metrics from the Consumer.然后从消费者那里找到指标。 For example, if you are interested in request-latency-avg :例如,如果您对request-latency-avg感兴趣:

// Find the metrics you are interested in
Metric requestLatencyAvgMetric = null;
for (Entry<MetricName, ? extends Metric> entry : consumer.metrics().entrySet()) {
    if ("request-latency-avg".equals(entry.getKey().name())) {
        requestLatencyAvgMetric = entry.getValue();
    }
}

Then you can retrieve the value when you need it using:然后,您可以在需要时使用以下方法检索该值:

requestLatencyAvgMetric.metricValue()

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

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