Austin Brinegar
Austin Brinegar

Reputation: 73

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.

Upvotes: 4

Views: 2104

Answers (1)

Mickael Maison
Mickael Maison

Reputation: 26865

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.

Upvotes: 3

Related Questions