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

@@ -15,7 +15,6 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/errchan"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/nagios"
@@ -49,8 +48,7 @@ type Exec struct {
parser parsers.Parser
runner Runner
errChan chan error
runner Runner
}
func NewExec() *Exec {
@@ -150,13 +148,13 @@ func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync
out, err := e.runner.Run(e, command, acc)
if err != nil {
e.errChan <- err
acc.AddError(err)
return
}
metrics, err := e.parser.Parse(out)
if err != nil {
e.errChan <- err
acc.AddError(err)
} else {
for _, metric := range metrics {
acc.AddFields(metric.Name(), metric.Fields(), metric.Tags(), metric.Time())
@@ -193,7 +191,8 @@ func (e *Exec) Gather(acc telegraf.Accumulator) error {
matches, err := filepath.Glob(cmdAndArgs[0])
if err != nil {
return err
acc.AddError(err)
continue
}
if len(matches) == 0 {
@@ -214,15 +213,12 @@ func (e *Exec) Gather(acc telegraf.Accumulator) error {
}
}
errChan := errchan.New(len(commands))
e.errChan = errChan.C
wg.Add(len(commands))
for _, command := range commands {
go e.ProcessCommand(command, acc, &wg)
}
wg.Wait()
return errChan.Error()
return nil
}
func init() {

View File

@@ -101,7 +101,7 @@ func TestExec(t *testing.T) {
}
var acc testutil.Accumulator
err := e.Gather(&acc)
err := acc.GatherError(e.Gather)
require.NoError(t, err)
assert.Equal(t, acc.NFields(), 8, "non-numeric measurements should be ignored")
@@ -127,8 +127,7 @@ func TestExecMalformed(t *testing.T) {
}
var acc testutil.Accumulator
err := e.Gather(&acc)
require.Error(t, err)
require.Error(t, acc.GatherError(e.Gather))
assert.Equal(t, acc.NFields(), 0, "No new points should have been added")
}
@@ -141,8 +140,7 @@ func TestCommandError(t *testing.T) {
}
var acc testutil.Accumulator
err := e.Gather(&acc)
require.Error(t, err)
require.Error(t, acc.GatherError(e.Gather))
assert.Equal(t, acc.NFields(), 0, "No new points should have been added")
}
@@ -155,8 +153,7 @@ func TestLineProtocolParse(t *testing.T) {
}
var acc testutil.Accumulator
err := e.Gather(&acc)
require.NoError(t, err)
require.NoError(t, acc.GatherError(e.Gather))
fields := map[string]interface{}{
"usage_idle": float64(99),
@@ -191,7 +188,7 @@ func TestLineProtocolShortParse(t *testing.T) {
}
var acc testutil.Accumulator
err := e.Gather(&acc)
err := acc.GatherError(e.Gather)
require.Error(t, err)
assert.Contains(t, err.Error(), "buffer too short", "A buffer too short error was expected")
}
@@ -205,7 +202,7 @@ func TestLineProtocolParseMultiple(t *testing.T) {
}
var acc testutil.Accumulator
err := e.Gather(&acc)
err := acc.GatherError(e.Gather)
require.NoError(t, err)
fields := map[string]interface{}{
@@ -231,7 +228,7 @@ func TestExecCommandWithGlob(t *testing.T) {
e.SetParser(parser)
var acc testutil.Accumulator
err := e.Gather(&acc)
err := acc.GatherError(e.Gather)
require.NoError(t, err)
fields := map[string]interface{}{
@@ -247,7 +244,7 @@ func TestExecCommandWithoutGlob(t *testing.T) {
e.SetParser(parser)
var acc testutil.Accumulator
err := e.Gather(&acc)
err := acc.GatherError(e.Gather)
require.NoError(t, err)
fields := map[string]interface{}{
@@ -263,7 +260,7 @@ func TestExecCommandWithoutGlobAndPath(t *testing.T) {
e.SetParser(parser)
var acc testutil.Accumulator
err := e.Gather(&acc)
err := acc.GatherError(e.Gather)
require.NoError(t, err)
fields := map[string]interface{}{