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

@@ -8,7 +8,6 @@ import (
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
@@ -96,16 +95,13 @@ func (m *Mesos) SetDefaults() {
// Gather() metrics from given list of Mesos Masters
func (m *Mesos) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
var errorChannel chan error
m.SetDefaults()
errorChannel = make(chan error, len(m.Masters)+2*len(m.Slaves))
for _, v := range m.Masters {
wg.Add(1)
go func(c string) {
errorChannel <- m.gatherMainMetrics(c, ":5050", MASTER, acc)
acc.AddError(m.gatherMainMetrics(c, ":5050", MASTER, acc))
wg.Done()
return
}(v)
@@ -114,7 +110,7 @@ func (m *Mesos) Gather(acc telegraf.Accumulator) error {
for _, v := range m.Slaves {
wg.Add(1)
go func(c string) {
errorChannel <- m.gatherMainMetrics(c, ":5051", SLAVE, acc)
acc.AddError(m.gatherMainMetrics(c, ":5051", SLAVE, acc))
wg.Done()
return
}(v)
@@ -125,26 +121,14 @@ func (m *Mesos) Gather(acc telegraf.Accumulator) error {
// wg.Add(1)
// go func(c string) {
// errorChannel <- m.gatherSlaveTaskMetrics(c, ":5051", acc)
// acc.AddError(m.gatherSlaveTaskMetrics(c, ":5051", acc))
// wg.Done()
// return
// }(v)
}
wg.Wait()
close(errorChannel)
errorStrings := []string{}
// Gather all errors for returning them at once
for err := range errorChannel {
if err != nil {
errorStrings = append(errorStrings, err.Error())
}
}
if len(errorStrings) > 0 {
return errors.New(strings.Join(errorStrings, "\n"))
}
return nil
}

View File

@@ -282,7 +282,7 @@ func TestMesosMaster(t *testing.T) {
Timeout: 10,
}
err := m.Gather(&acc)
err := acc.GatherError(m.Gather)
if err != nil {
t.Errorf(err.Error())
@@ -330,7 +330,7 @@ func TestMesosSlave(t *testing.T) {
Timeout: 10,
}
err := m.Gather(&acc)
err := acc.GatherError(m.Gather)
if err != nil {
t.Errorf(err.Error())