简体   繁体   English

尝试以编程方式通过jmx访问kafka指标时出错

[英]error trying to access kafka metrics through jmx programmatically

I just started with a simple example to read the jmx metrics and used the simple values of HeapMemoryUsage, CPUTime etc to get the feel for it. 我只是从一个简单的示例开始,以读取jmx指标,并使用HeapMemoryUsage,CPUTime等简单的值来了解它。 I need to try and access the kafka server / consumer metrics specifically lag which I can see is visible as a key in the jconsole app under FetcherLagMetrics-ConsumerLag. 我需要尝试访问kafka服务器/消费者指标,特别是滞后,我可以看到滞后在fetcherLagMetrics-ConsumerLag下的jconsole应用中可见。 But programmaticaly I get the following error: 但是以编程方式我得到以下错误:

javax.management.InstanceNotFoundException: kafka.consumer:type=consumer- 
  fetch-manager-metrics

This tells me that the consumer-fetch-manager metrics is the issue as it is not present even in the jconsole. 这告诉我,使用者获取管理器指标是个问题,因为即使在jconsole中也不存在。 I changed it to the following & still the same issue: 我将其更改为以下&仍然是相同的问题:

consumerBean = jmxCon.getMBeanServerConnection().getAttribute(new 
   ObjectName("kafka.server:type=FetcherLagMetrics"),"ConsumerLag");
            cd = (CompositeData) consumerBean;

The code trying to access these values is as follows: 尝试访问这些值的代码如下:

        jmxCon.getMBeanServerConnection().invoke(new 
         ObjectName("java.lang:type=Memory"), "gc", null, null);

        for (int i = 0; i < 100; i++) {

            //get an instance of the HeapMemoryUsage Mbean
            memoryMbean = jmxCon.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=Memory"), "HeapMemoryUsage");
            cd = (CompositeData) memoryMbean;

            //get an instance of the OperatingSystem Mbean
            osMbean = jmxCon.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=OperatingSystem"),"ProcessCpuTime");

            //get an instance of the kafka metrics Mbean
            consumerBean = jmxCon.getMBeanServerConnection().getAttribute(new ObjectName("kafka.consumer:type=consumer-fetch-manager-metrics"),"MaxLag");
            cd = (CompositeData) consumerBean;

            consumerBeanII = jmxCon.getMBeanServerConnection().getAttribute(new ObjectName("kafka.server:type=FetcherLagMetrics,name=ConsumerLag"),"Lag");


            System.out.println("Used memory: " + " " + cd.get("MaxLag") + " Used cpu: " + consumerBean); //print memory usage

            tempMemory = tempMemory + Long.parseLong(cd.get("used").toString());
            Thread.sleep(1000); //delay for one second

        }

It fails at line consumerBean = ......Could someone please explain or provide the correct way to access the kafka metrics using jmx /JMI implementation? 它无法在consumerBean = =一行上失败。有人可以解释或提供使用jmx / JMI实现访问kafka指标的正确方法吗?

I'm not sure your version of Kafka, but when I look at mine (v1.1.0) using JConsole there is no such bean for consumer lag that you describe. 我不确定您使用的Kafka版本是什么,但是当我使用JConsole查看我的(v1.1.0)时,您描述的消费者延迟并没有这样的bean。 Therefore I think it is expected that your JMX query will fail. 因此,我认为您的JMX查询将会失败。

Instead, I believe this kind of information was moved to the Kafka Admin API interface. 相反,我相信这类信息已移至Kafka Admin API界面。 So you must use this now to get lag settings, if you don't want to use kafka-consumer-groups.sh command-line utility. 因此,如果您不想使用kafka-consumer-groups.sh命令行实用程序,则必须立即使用它来获取滞后设置。

For us we only need these stats for monitoring, so we are using https://github.com/danielqsj/kafka_exporter to obtain the info for Prometheus. 对于我们来说,我们只需要这些统计信息即可进行监视,因此我们使用https://github.com/danielqsj/kafka_exporter来获取Prometheus的信息。

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

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