From 6e444f6f79046b2890604f6c29b37dcdc9cac50b Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:51:57 +0200 Subject: [PATCH] fix(outputs.kafka): Simplify send-error handling (#14154) (cherry picked from commit fd773b3e28828ceb678fecb22dcf446146dd9ef6) --- plugins/outputs/kafka/kafka.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/plugins/outputs/kafka/kafka.go b/plugins/outputs/kafka/kafka.go index 175e717791cb1..89a653794fbc4 100644 --- a/plugins/outputs/kafka/kafka.go +++ b/plugins/outputs/kafka/kafka.go @@ -223,21 +223,21 @@ func (k *Kafka) Write(metrics []telegraf.Metric) error { if err != nil { // We could have many errors, return only the first encountered. var errs sarama.ProducerErrors - if errors.As(err, &errs) { - for _, prodErr := range errs { - if errors.Is(prodErr.Err, sarama.ErrMessageSizeTooLarge) { - k.Log.Error("Message too large, consider increasing `max_message_bytes`; dropping batch") - return nil - } - if errors.Is(prodErr.Err, sarama.ErrInvalidTimestamp) { - k.Log.Error( - "The timestamp of the message is out of acceptable range, consider increasing broker `message.timestamp.difference.max.ms`; " + - "dropping batch", - ) - return nil - } - return prodErr //nolint:staticcheck // Return first error encountered + if errors.As(err, &errs) && len(errs) > 0 { + // Just return the first error encountered + firstErr := errs[0] + if errors.Is(firstErr.Err, sarama.ErrMessageSizeTooLarge) { + k.Log.Error("Message too large, consider increasing `max_message_bytes`; dropping batch") + return nil } + if errors.Is(firstErr.Err, sarama.ErrInvalidTimestamp) { + k.Log.Error( + "The timestamp of the message is out of acceptable range, consider increasing broker `message.timestamp.difference.max.ms`; " + + "dropping batch", + ) + return nil + } + return firstErr } return err }