use AddError everywhere (#2372)

This commit is contained in:
Patrick Hemmer
2017-04-24 14:13:26 -04:00
committed by Daniel Nelson
parent 801f6cb8a0
commit 06baf7cf78
95 changed files with 341 additions and 531 deletions

View File

@@ -68,7 +68,6 @@ func (_ *Ping) SampleConfig() string {
func (p *Ping) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
errorChannel := make(chan error, len(p.Urls)*2)
// Spin off a go routine for each url to ping
for _, url := range p.Urls {
@@ -80,14 +79,14 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
out, err := p.pingHost(totalTimeout, args...)
if err != nil {
// Combine go err + stderr output
errorChannel <- errors.New(
strings.TrimSpace(out) + ", " + err.Error())
acc.AddError(errors.New(
strings.TrimSpace(out) + ", " + err.Error()))
}
tags := map[string]string{"url": u}
trans, rec, avg, stddev, err := processPingOutput(out)
if err != nil {
// fatal error
errorChannel <- err
acc.AddError(err)
return
}
// Calculate packet loss percentage
@@ -108,18 +107,8 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
}
wg.Wait()
close(errorChannel)
// Get all errors and return them as one giant error
errorStrings := []string{}
for err := range errorChannel {
errorStrings = append(errorStrings, err.Error())
}
if len(errorStrings) == 0 {
return nil
}
return errors.New(strings.Join(errorStrings, "\n"))
return nil
}
func hostPinger(timeout float64, args ...string) (string, error) {

View File

@@ -144,7 +144,7 @@ func TestPingGather(t *testing.T) {
pingHost: mockHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
tags := map[string]string{"url": "www.google.com"}
fields := map[string]interface{}{
"packets_transmitted": 5,
@@ -182,7 +182,7 @@ func TestLossyPingGather(t *testing.T) {
pingHost: mockLossyHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
tags := map[string]string{"url": "www.google.com"}
fields := map[string]interface{}{
"packets_transmitted": 5,
@@ -215,7 +215,7 @@ func TestBadPingGather(t *testing.T) {
pingHost: mockErrorHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
tags := map[string]string{"url": "www.amazon.com"}
fields := map[string]interface{}{
"packets_transmitted": 2,
@@ -237,7 +237,7 @@ func TestFatalPingGather(t *testing.T) {
pingHost: mockFatalHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
assert.False(t, acc.HasMeasurement("packets_transmitted"),
"Fatal ping should not have packet measurements")
assert.False(t, acc.HasMeasurement("packets_received"),

View File

@@ -69,7 +69,7 @@ func TestPingGather(t *testing.T) {
pingHost: mockHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
tags := map[string]string{"url": "www.google.com"}
fields := map[string]interface{}{
"packets_transmitted": 4,
@@ -112,7 +112,7 @@ func TestBadPingGather(t *testing.T) {
pingHost: mockErrorHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
tags := map[string]string{"url": "www.amazon.com"}
fields := map[string]interface{}{
"packets_transmitted": 4,
@@ -155,7 +155,7 @@ func TestLossyPingGather(t *testing.T) {
pingHost: mockLossyHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
tags := map[string]string{"url": "www.google.com"}
fields := map[string]interface{}{
"packets_transmitted": 9,
@@ -214,7 +214,7 @@ func TestFatalPingGather(t *testing.T) {
pingHost: mockFatalHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
assert.True(t, acc.HasFloatField("ping", "errors"),
"Fatal ping should have packet measurements")
assert.False(t, acc.HasIntField("ping", "packets_transmitted"),
@@ -259,7 +259,7 @@ func TestUnreachablePingGather(t *testing.T) {
pingHost: mockUnreachableHostPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
tags := map[string]string{"url": "www.google.com"}
fields := map[string]interface{}{
@@ -305,7 +305,7 @@ func TestTTLExpiredPingGather(t *testing.T) {
pingHost: mockTTLExpiredPinger,
}
p.Gather(&acc)
acc.GatherError(p.Gather)
tags := map[string]string{"url": "www.google.com"}
fields := map[string]interface{}{