Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Kafka metrics at the topic level in Java?

I am trying to get Kafka metrics at the topic level in Java. The kafka Docs show that I can get them using JMX but I am not sure how to do this.

The docs show the MBean as being kafka.producer:type=producer-topic-metrics,client-id="{client-id}",topic="{topic}" I am unsure of where to put the actual metric name that I am after, but have attempted it as such: "kafka.consumer:type=consumer-fetch-manager-metrics,client-id=\"1\",topic=\"%s\",name=\"bytes-consumed-rate\""

I am also unsure of how to actually use the bean and where to specify the kafka cluster information.

Thus far I've got this going for me... But the bean isn't actually doing anything because I dont know how to actually execute it.

        String metric = String.format("kafka.server:name=BytesInPerSec,topic=%s,type=BrokerTopicMetrics", topicName);
        try{
            ObjectName objectName = new ObjectName(metric);
            String s = objectName + " ^L";
            LOGGER.info(s);
        }
        catch(MalformedObjectNameException e){
            LOGGER.error("Malformed Object Exception: ", e);
        }
        return null;

Ideally I want to get out kafka metrics at the topic level as an object. Any help would be greatly appreciated.

like image 845
Austin Brinegar Avatar asked Oct 22 '25 18:10

Austin Brinegar


1 Answers

All the clients (Producer, Consumer, AdminClient) have methods called metrics() that exposes all the JMX metrics.

For example, for the Producer, see metrics() that returns a Map of all existing metrics and their values.

like image 69
Mickael Maison Avatar answered Oct 24 '25 06:10

Mickael Maison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!