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

Don't Close() nil client

Signed-off-by: Eugene Chupriyanov <e.chupriyanov@cpm.ru>
  • Loading branch information
echupriyanov authored and sparrc committed Apr 18, 2016
1 parent b95a90d commit 708a97d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 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 @@ -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
}

Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 708a97d

Please sign in to comment.