Measurement and Tags are lost after parsing MQTT payload with xpath_json

Hello Community,

Thank you for your efforts !

This is my first experience with Telegraf and InfluxDB and I need your help.

I’m parsing the JSON payload sent from mqtt broker, the fieldset and timestamp are OK
while the measurement and Tags are replaced with unexpected values:

My MQTT Broker is sending the following json

{
    "fields": {
        "cache_size": 16384.0,
        "core_id": 0,
        "cpu_cores": 8,
        "vendor_id": "GenuineIntel"
    },
    "measurement": "cpuinfo",
    "tags": {
        "architecture": "x86_64",
        "cpuinfo_name": "proc0",
        "os": "Debian"
    },
    "time": "1706804130618257510"
}

The Telegraf config:

[[inputs.mqtt_consumer]]
  data_format = "xpath_json"
  servers = [
    "tcp://mosquitto.mosquitto-mqtt.svc.cluster.local:1883"
  ]
  topics = [
    "vvp.v1.detailed.metrics/#"
  ]
  [[inputs.mqtt_consumer.xpath]]
     metric_name = "measurement"
  [[inputs.mqtt_consumer.xpath]]
     metric_selection = "/measurement"
  [[inputs.mqtt_consumer.xpath]]
     timestamp = "/time"
  [[inputs.mqtt_consumer.xpath]]
     timestamp_format = "unix"
  [[inputs.mqtt_consumer.xpath]]
     field_selection = "/fields/*"
  [[inputs.mqtt_consumer.xpath]]
     tag_selection = "/tags/*"

Is there any thing missing ?

Thank you

Hi,

Is there any thing missing ?

You are actually defining too much :wink: Try this:

[[inputs.mqtt_consumer]]
  servers = [
    "tcp://mosquitto.mosquitto-mqtt.svc.cluster.local:1883"
  ]
  topics = [
    "vvp.v1.detailed.metrics/#"
  ]
  data_format = "xpath_json"
  [[inputs.mqtt_consumer.xpath]]
    metric_name = "/measurement"
    timestamp = "/time"
    timestamp_format = "unix"
    field_selection = "/fields/*"
    tag_selection = "/tags/*"

You don’t need to define a table for every single item.

Thank you @jpowers, It is working :slight_smile: after defining only one table for all items.

But Still having the topic in the line protocol :

How can I remove it ?

See the tagexclude config option.

Thank a lot @jpowers !

I added the tagexclude and it’s fixed !

[[inputs.mqtt_consumer]]
  data_format = "xpath_json"
  servers = [
    "tcp://mosquitto.mosquitto-mqtt.svc.cluster.local:1883"
  ]
  topics = [
    "vvp.v1.detailed.metrics/#"
  ]
  tagexclude = ["topic"]
  [[inputs.mqtt_consumer.xpath]]
     metric_name = "measurement"
     timestamp = "/time"
     timestamp_format = "unix"
     field_selection = "/fields/*"
     tag_selection = "/tags/*"

[[inputs.internal]]
  collect_memstats = false