Skip to content

Commit

Permalink
Try to reconnect to Riemann if metrics upload failed.
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Chupriyanov <tchu@tchu.ru>

Error checks added

Signed-off-by: Eugene Chupriyanov <e.chupriyanov@cpm.ru>
  • Loading branch information
echupriyanov authored and Eugene Chupriyanov committed Apr 12, 2016
1 parent e428d11 commit 95b104f
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions plugins/outputs/riemann/riemann.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package riemann

import (
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -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
}

Expand All @@ -42,6 +42,7 @@ func (r *Riemann) Connect() error {

func (r *Riemann) Close() error {
r.client.Close()
r.client = nil
return nil
}

Expand All @@ -58,6 +59,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)
Expand All @@ -68,8 +76,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
Expand Down

0 comments on commit 95b104f

Please sign in to comment.