From 708a97d77355559a43397556199f755dedea86a6 Mon Sep 17 00:00:00 2001 From: Eugene Chupriyanov Date: Tue, 12 Apr 2016 20:20:27 +0300 Subject: [PATCH] Try to reconnect to Riemann if metrics upload failed. Signed-off-by: Eugene Chupriyanov Error checks added Don't Close() nil client Signed-off-by: Eugene Chupriyanov --- plugins/outputs/riemann/riemann.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/plugins/outputs/riemann/riemann.go b/plugins/outputs/riemann/riemann.go index c805bbd00..88c4b20de 100644 --- a/plugins/outputs/riemann/riemann.go +++ b/plugins/outputs/riemann/riemann.go @@ -1,7 +1,6 @@ package riemann import ( - "errors" "fmt" "os" "sort" @@ -33,6 +32,7 @@ func (r *Riemann) Connect() error { c, err := raidman.Dial(r.Transport, r.URL) if err != nil { + r.client = nil return err } @@ -41,7 +41,11 @@ func (r *Riemann) Connect() error { } func (r *Riemann) Close() error { + if r.client == nil { + return nil + } r.client.Close() + r.client = nil return nil } @@ -58,6 +62,13 @@ func (r *Riemann) Write(metrics []telegraf.Metric) error { return nil } + if r.client == nil { + err := r.Connect() + if err != nil { + return fmt.Errorf("FAILED to (re)connect to Riemann. Error: %s\n", err) + } + } + var events []*raidman.Event for _, p := range metrics { evs := buildEvents(p, r.Separator) @@ -68,8 +79,16 @@ func (r *Riemann) Write(metrics []telegraf.Metric) error { var senderr = r.client.SendMulti(events) if senderr != nil { - return errors.New(fmt.Sprintf("FAILED to send riemann message: %s\n", - senderr)) + r.Close() // always retuns nil + connerr := r.Connect() + if connerr != nil { + return fmt.Errorf("FAILED to (re)connect to Riemann. Error: %s\n", connerr) + } + senderr = r.client.SendMulti(events) + if senderr != nil { + return fmt.Errorf("FAILED to send riemann message (will try to reconnect). Error: %s\n", + senderr) + } } return nil