From 3782eb703b55f8b4bbef9dd6c8e7a6ea8ae17b45 Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Mon, 12 Feb 2024 14:24:56 +0100 Subject: [PATCH] fix(outputs.mqtt): Retry metrics for server timeout --- plugins/outputs/mqtt/mqtt.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/outputs/mqtt/mqtt.go b/plugins/outputs/mqtt/mqtt.go index b40e23b774b8d..33e16e9a4ca62 100644 --- a/plugins/outputs/mqtt/mqtt.go +++ b/plugins/outputs/mqtt/mqtt.go @@ -167,6 +167,11 @@ func (m *MQTT) Write(metrics []telegraf.Metric) error { for _, msg := range topicMessages { if err := m.client.Publish(msg.topic, msg.payload); err != nil { + // We do receive a timeout error if the remote broker is down, + // so let's retry the metrics in this case and drop them otherwise. + if errors.Is(err, internal.ErrTimeout) { + return fmt.Errorf("could not publish message to MQTT server: %w", err) + } m.Log.Warnf("Could not publish message to MQTT server: %v", err) } }