Search code examples
apache-kafkainfluxdbjmxtrans

Adding a custom tag based on topicName (wildcard) via using JmxTrans to send Kafka JMX to influxDb


Basically what i wanted to achieve was to get MessageInPerSec metric for all the topic in kafka and to add the custom tag as topicName in the influx db so as to query based on the topic not based on the 'ObjDomain' definition, below are my JmxTrans configuration, (Note using the wildcard for the topic as to fetch the data MessageInPerSec JMX attribute for all the topic)

{
  "servers": [
    {
      "port": "9581",
      "host": "192.168.43.78",
      "alias": "kafka-metric",
      "queries": [
        {
          "outputWriters": [
            {
              "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
              "url": "http://192.168.43.78:8086/",
              "database": "kafka",
              "username": "admin",
              "password": "root"
            }
          ],
          "obj": "kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec,topic=*",
          "attr": [
            "Count",
            "MeanRate",
            "OneMinuteRate",
            "FiveMinuteRate",
            "FifteenMinuteRate"
          ],
          "resultAlias": "newTopic"
        }
      ],
      "numQueryThreads": 2
    }
  ]
}

which yields a result in the Influx DB as follow

[name=newTopic, time=1589425526087, tags={attributeName=FifteenMinuteRate,
 className=com.yammer.metrics.reporting.JmxReporter$Meter, objDomain=kafka.server,
 typeName=type=BrokerTopicMetrics,name=MessagesInPerSec,topic=backblaze_smart}, 
precision=MILLISECONDS, fields={FifteenMinuteRate=1362.9446063537794, _jmx_port=9581
}]

and create tag with whole objDomain spefcified in the config, but i wanted to have topic as a seperate tag that is something as follow

[name=newTopic, time=1589425526087, tags={attributeName=FifteenMinuteRate,
 className=com.yammer.metrics.reporting.JmxReporter$Meter, objDomain=kafka.server,
  topic=backblaze_smart,
 typeName=type=BrokerTopicMetrics,name=MessagesInPerSec,topic=backblaze_smart}, 
precision=MILLISECONDS, fields={FifteenMinuteRate=1362.9446063537794, _jmx_port=9581
}]

was not able to find any adequate documentation for the same on how to use the wildcard value of topic as a separate tag using jmxtrans and writing it to the InfluxDB.


Solution

  • You just need to add the following additional properties for Influx output writer. Just make sure you are using the latest version of jmxtrans release. The docs are here: https://github.com/jmxtrans/jmxtrans/wiki/InfluxDBWriter

    "typeNames": ["topic"],
    "typeNamesAsTags": "true"

    I have listed your config with the above modifications.

    {
      "servers": [
        {
          "port": "9581",
          "host": "192.168.43.78",
          "alias": "kafka-metric",
          "queries": [
            {
              "outputWriters": [
                {
                  "@class": "com.googlecode.jmxtrans.model.output.InfluxDbWriterFactory",
                  "url": "http://192.168.43.78:8086/",
                  "database": "kafka",
                  "username": "admin",
                  "password": "root",
                  "typeNames": ["topic"],
                  "typeNamesAsTags": "true"
                }
              ],
              "obj": "kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec,topic=*",
              "attr": [
                "Count",
                "MeanRate",
                "OneMinuteRate",
                "FiveMinuteRate",
                "FifteenMinuteRate"
              ],
              "resultAlias": "newTopic"
            }
          ],
          "numQueryThreads": 2
        }
      ]
    }