Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(outputs.event_hubs): Reconnect on link closed error #14050

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions plugins/outputs/event_hubs/event_hubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package event_hubs
import (
"context"
_ "embed"
"fmt"
"time"

eventhub "github.com/Azure/azure-event-hubs-go/v3"
Expand Down Expand Up @@ -75,7 +76,6 @@ func (*EventHubs) SampleConfig() string {

func (e *EventHubs) Init() error {
err := e.Hub.GetHub(e.ConnectionString)

if err != nil {
return err
}
Expand All @@ -96,7 +96,6 @@ func (e *EventHubs) Close() error {
defer cancel()

err := e.Hub.Close(ctx)

if err != nil {
return err
}
Expand All @@ -112,7 +111,6 @@ func (e *EventHubs) Write(metrics []telegraf.Metric) error {
events := make([]*eventhub.Event, 0, len(metrics))
for _, metric := range metrics {
payload, err := e.serializer.Serialize(metric)

if err != nil {
e.Log.Debugf("Could not serialize metric: %v", err)
continue
Expand All @@ -136,8 +134,18 @@ func (e *EventHubs) Write(metrics []telegraf.Metric) error {
defer cancel()

err := e.Hub.SendBatch(ctx, eventhub.NewEventBatchIterator(events...), e.batchOptions...)

if err != nil {
if err.Error() == "amqp: link closed" {
e.Log.Warn("Link is closed, reconnecting and retrying")
if err := e.Close(); err != nil {
return fmt.Errorf("could not close connection: %w", err)
}
err := e.Hub.GetHub(e.ConnectionString)
if err != nil {
return fmt.Errorf("could not reconnect: %w", err)
}
return e.Hub.SendBatch(ctx, eventhub.NewEventBatchIterator(events...), e.batchOptions...)
}
return err
}

Expand Down