feat(outputs.mqtt): Add sprig for topic name generator for homie layout (#16491)

Co-authored-by: Pieter Slabbert <pieter.slabbert@ilovezoona.com>
This commit is contained in:
Pieter Slabbert 2025-02-13 17:08:01 +02:00 committed by GitHub
parent 1b6350ac9f
commit df2926978a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 1 deletions

View File

@ -201,6 +201,7 @@ while `homie_node_id` will provide a template for the `node-id` part of the
topic. Both options can contain [Go templates][GoTemplates] similar to `topic`
with `{{ .PluginName }}` referencing the metric name and `{{ .Tag "key"}}`
referencing the tag with the name `key`.
[Sprig](http://masterminds.github.io/sprig/) helper functions are available.
For example writing the metrics

View File

@ -8,6 +8,8 @@ import (
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
)
@ -102,7 +104,7 @@ type HomieGenerator struct {
}
func NewHomieGenerator(tmpl string) (*HomieGenerator, error) {
tt, err := template.New("topic_name").Parse(tmpl)
tt, err := template.New("topic_name").Funcs(sprig.TxtFuncMap()).Parse(tmpl)
if err != nil {
return nil, err
}