From e39e52b85ebf1c3a172ce08517f9960efc3097d2 Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Thu, 30 May 2024 11:03:33 -0600 Subject: [PATCH] feat(outputs.mqtt): Add client trace logging fixes: #15418 --- plugins/outputs/mqtt/mqtt.go | 11 +++++++++++ plugins/outputs/mqtt/sample.conf | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/plugins/outputs/mqtt/mqtt.go b/plugins/outputs/mqtt/mqtt.go index 33e16e9a4ca62..2ef44794d9c68 100644 --- a/plugins/outputs/mqtt/mqtt.go +++ b/plugins/outputs/mqtt/mqtt.go @@ -9,6 +9,8 @@ import ( "sync" "time" + mqttLib "github.com/eclipse/paho.mqtt.golang" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/internal" @@ -28,6 +30,7 @@ type message struct { type MQTT struct { TopicPrefix string `toml:"topic_prefix" deprecated:"1.25.0;use 'topic' instead"` Topic string `toml:"topic"` + ClientTrace bool `toml:"client_trace"` BatchMessage bool `toml:"batch" deprecated:"1.25.2;use 'layout = \"batch\"' instead"` Layout string `toml:"layout"` HomieDeviceName string `toml:"homie_device_name"` @@ -51,6 +54,14 @@ func (*MQTT) SampleConfig() string { } func (m *MQTT) Init() error { + if m.ClientTrace { + log := &mqttLogger{m.Log} + mqttLib.ERROR = log + mqttLib.CRITICAL = log + mqttLib.WARN = log + mqttLib.DEBUG = log + } + if len(m.Servers) == 0 { return errors.New("no servers specified") } diff --git a/plugins/outputs/mqtt/sample.conf b/plugins/outputs/mqtt/sample.conf index ecfb47ce8aee4..96f5e037ff91f 100644 --- a/plugins/outputs/mqtt/sample.conf +++ b/plugins/outputs/mqtt/sample.conf @@ -67,6 +67,12 @@ ## actually reads it # retain = false + ## Client trace messages + ## When set to true, and debug mode enabled in the agent settings, the MQTT + ## client's messages are included in telegraf logs. These messages are very + ## noisey, but essential for debugging issues. + # client_trace = false + ## Layout of the topics published. ## The following choices are available: ## non-batch -- send individual messages, one for each metric