Try to reconnect to Riemann if metrics upload failed.

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>
This commit is contained in:
Eugene Chupriyanov 2016-04-12 20:20:27 +03:00 committed by Cameron Sparr
parent b95a90dbd6
commit 708a97d773
1 changed files with 22 additions and 3 deletions

View File

@ -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