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

outputs.graphite: Retry sending metrics immediately after reconnect #3680

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
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
24 changes: 18 additions & 6 deletions plugins/outputs/graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,22 @@ func (g *Graphite) Write(metrics []telegraf.Metric) error {
batch = append(batch, buf...)
}

err = g.send(batch)

// try to reconnect and retry to send
if err != nil {
log.Println("E! Graphite: Reconnecting and retrying: ")
g.Connect()
err = g.send(batch)
}

return err
}

func (g *Graphite) send(batch []byte) error {
// This will get set to nil if a successful write occurs
err = errors.New("Could not write to any Graphite server in cluster\n")
err := errors.New("Could not write to any Graphite server in cluster\n")

// Send data to a random server
p := rand.Perm(len(g.conns))
for _, n := range p {
Expand All @@ -167,18 +181,16 @@ func (g *Graphite) Write(metrics []telegraf.Metric) error {
if _, e := g.conns[n].Write(batch); e != nil {
// Error
log.Println("E! Graphite Error: " + e.Error())
// Close explicitely
g.conns[n].Close()
// Let's try the next one
} else {
// Success
err = nil
break
}
}
// try to reconnect
if err != nil {
log.Println("E! Reconnecting: ")
g.Connect()
}

return err
}

Expand Down
6 changes: 2 additions & 4 deletions plugins/outputs/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,16 @@ func TestGraphiteOK(t *testing.T) {
err2 := g.Write(metrics)
require.NoError(t, err2)

// Waiting TCPserver
// Waiting TCPserver, should reconnect and resend
wg.Wait()
t.Log("Finished Waiting for first data")
var wg2 sync.WaitGroup
// Start TCP server
wg2.Add(1)
TCPServer2(t, &wg2)
//Write but expect an error, but reconnect
g.Write(metrics2)
err3 := g.Write(metrics2)
t.Log("Finished writing second data, it should have failed")
//Actually write the new metrics
t.Log("Finished writing second data, it should have reconnected automatically")

require.NoError(t, err3)
t.Log("Finished writing third data")
Expand Down