From df2926978a0bfb97d37b2c3a4bef49428adf6d25 Mon Sep 17 00:00:00 2001 From: Pieter Slabbert Date: Thu, 13 Feb 2025 17:08:01 +0200 Subject: [PATCH] feat(outputs.mqtt): Add sprig for topic name generator for homie layout (#16491) Co-authored-by: Pieter Slabbert --- plugins/outputs/mqtt/README.md | 1 + plugins/outputs/mqtt/homie.go | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/outputs/mqtt/README.md b/plugins/outputs/mqtt/README.md index 30a174353..c52a1243a 100644 --- a/plugins/outputs/mqtt/README.md +++ b/plugins/outputs/mqtt/README.md @@ -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 diff --git a/plugins/outputs/mqtt/homie.go b/plugins/outputs/mqtt/homie.go index 16f56af77..9d250c043 100644 --- a/plugins/outputs/mqtt/homie.go +++ b/plugins/outputs/mqtt/homie.go @@ -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 }