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

add AddError method to accumulator #1536

Merged
merged 1 commit into from
Jul 25, 2016
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
2 changes: 2 additions & 0 deletions accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Accumulator interface {
tags map[string]string,
t ...time.Time)

AddError(err error)

Debug() bool
SetDebug(enabled bool)

Expand Down
14 changes: 14 additions & 0 deletions agent/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"math"
"sync/atomic"
"time"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -35,6 +36,8 @@ type accumulator struct {
prefix string

precision time.Duration

errCount uint64
}

func (ac *accumulator) Add(
Expand Down Expand Up @@ -161,6 +164,17 @@ func (ac *accumulator) AddFields(
ac.metrics <- m
}

// AddError passes a runtime error to the accumulator.
// The error will be tagged with the plugin name and written to the log.
func (ac *accumulator) AddError(err error) {
if err == nil {
return
}
atomic.AddUint64(&ac.errCount, 1)
//TODO suppress/throttle consecutive duplicate errors?
log.Printf("ERROR in input [%s]: %s", ac.inputConfig.Name, err)
Copy link
Contributor

@sparrc sparrc Jul 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading ac.inputConfig.Name could produce a race condition here. Since this bit of code shouldn't be hugely high-traffic, I would remove the atomic package, add a sync.Mutex to the accumulator, and change to:

    if err == nil {
        return
    }
    ac.Lock()
    ac.errCount++
    log.Printf("ERROR in input [%s]: %s", ac.inputConfig.Name, err)
    ac.Unlock()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only place where inputConfig.Name is touched is in internal/config/config.go:798 (during initialization). It is never read or written to ever again (prior to this PR it was an unused variable). Thus there's no need to protect it.

Copy link
Contributor

@sparrc sparrc Jul 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but this function is reading it, so unless you make the function a value receiver, there can be a race condition here. nevermind, not sure why I was under the impression that reading struct fields was not thread-safe, should be fine...

}

func (ac *accumulator) Debug() bool {
return ac.debug
}
Expand Down
28 changes: 28 additions & 0 deletions agent/accumulator_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package agent

import (
"bytes"
"fmt"
"log"
"math"
"os"
"testing"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/models"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAdd(t *testing.T) {
Expand Down Expand Up @@ -454,3 +458,27 @@ func TestAccFilterTags(t *testing.T) {
fmt.Sprintf("acctest value=101 %d", now.UnixNano()),
actual)
}

func TestAccAddError(t *testing.T) {
errBuf := bytes.NewBuffer(nil)
log.SetOutput(errBuf)
defer log.SetOutput(os.Stderr)

a := accumulator{}
a.inputConfig = &internal_models.InputConfig{}
a.inputConfig.Name = "mock_plugin"

a.AddError(fmt.Errorf("foo"))
a.AddError(fmt.Errorf("bar"))
a.AddError(fmt.Errorf("baz"))

errs := bytes.Split(errBuf.Bytes(), []byte{'\n'})
assert.EqualValues(t, 3, a.errCount)
require.Len(t, errs, 4) // 4 because of trailing newline
assert.Contains(t, string(errs[0]), "mock_plugin")
assert.Contains(t, string(errs[0]), "foo")
assert.Contains(t, string(errs[1]), "mock_plugin")
assert.Contains(t, string(errs[1]), "bar")
assert.Contains(t, string(errs[2]), "mock_plugin")
assert.Contains(t, string(errs[2]), "baz")
}
3 changes: 3 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ func (a *Agent) Test() error {
if err := input.Input.Gather(acc); err != nil {
return err
}
if acc.errCount > 0 {
return fmt.Errorf("Errors encountered during processing")
}

// Special instructions for some inputs. cpu, for example, needs to be
// run twice in order to return cpu usage percentages.
Expand Down
11 changes: 11 additions & 0 deletions testutil/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Accumulator struct {
sync.Mutex

Metrics []*Metric
Errors []error
debug bool
}

Expand Down Expand Up @@ -84,6 +85,16 @@ func (a *Accumulator) AddFields(
a.Metrics = append(a.Metrics, p)
}

// AddError appends the given error to Accumulator.Errors.
func (a *Accumulator) AddError(err error) {
if err == nil {
return
}
a.Lock()
a.Errors = append(a.Errors, err)
a.Unlock()
}

func (a *Accumulator) SetPrecision(precision, interval time.Duration) {
return
}
Expand Down