Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable extracting tag values from MQTT topics #9995

Merged
merged 15 commits into from
Nov 23, 2021
61 changes: 60 additions & 1 deletion plugins/inputs/mqtt_consumer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The [MQTT][mqtt] consumer plugin reads from the specified MQTT topics
and creates metrics using one of the supported [input data formats][].

### Configuration
## Configuration

```toml
[[inputs.mqtt_consumer]]
Expand Down Expand Up @@ -73,12 +73,71 @@ and creates metrics using one of the supported [input data formats][].
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = "influx"

## Enable extracting tag values from MQTT topics
## _ denotes an ignored entry in the topic path
# [[inputs.mqtt_consumer.topic_parsing]]
# topic = ""
# measurement = ""
# tags = ""
# fields = ""
## Value supported is int, float, unit
# [[inputs.mqtt_consumer.topic.types]]
# key = type
```

## About Topic Pasring
MyaLongmire marked this conversation as resolved.
Show resolved Hide resolved

The MQTT topic as a whole is stored as a tag, but this can be far too coarse
too be easily used when utilising the data further down the line. This
MyaLongmire marked this conversation as resolved.
Show resolved Hide resolved
change allows tag values to be extracted from the MQTT topic letting you
store the information provided in the topic in a meaningful way. An _ denotes an
MyaLongmire marked this conversation as resolved.
Show resolved Hide resolved
ignored entry in the topic path. Please see the following example.

## Example Configuration for topic parsing

```toml
[[inputs.mqtt_consumer]]
## Broker URLs for the MQTT server or cluster. To connect to multiple
## clusters or standalone servers, use a separate plugin instance.
## example: servers = ["tcp://localhost:1883"]
## servers = ["ssl://localhost:1883"]
## servers = ["ws://localhost:1883"]
servers = ["tcp://127.0.0.1:1883"]

## Topics that will be subscribed to.
topics = [
"telegraf/+/cpu/23",
]

## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = "value"
data_type = "float"

[[inputs.mqtt_consumer.topic_parsing]]
topic = "telegraf/one/cpu/23"
srebhan marked this conversation as resolved.
Show resolved Hide resolved
measurement = "_/_/measurement/_"
tags = "tag/_/_/_"
fields = "_/_/_/test"
[inputs.mqtt_consumer.topic_parsing.types]
test = "int"
MyaLongmire marked this conversation as resolved.
Show resolved Hide resolved
```

Result:

```shell
cpu,host=pop-os,tag=telegraf,topic=telegraf/one/cpu/23 value=45,test=23i 1637014942460689291
```

### Metrics

- All measurements are tagged with the incoming topic, ie
`topic=telegraf/host01/cpu`

- example when [[inputs.mqtt_consumer.topic_parsing]] is set

[mqtt]: https://mqtt.org
[input data formats]: /docs/DATA_FORMATS_INPUT.md
Loading