Skip to content

Commit

Permalink
Fix logger initialization in exec input (influxdata#6492)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored and idohalevi committed Sep 23, 2020
1 parent c488e42 commit b15c165
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
5 changes: 5 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ func TestConfig_LoadDirectory(t *testing.T) {
MeasurementSuffix: "_myothercollector",
}
eConfig.Tags = make(map[string]string)

exec := c.Inputs[1].Input.(*exec.Exec)
require.NotNil(t, exec.Log)
exec.Log = nil

assert.Equal(t, ex, c.Inputs[1].Input,
"Merged Testdata did not produce a correct exec struct.")
assert.Equal(t, eConfig, c.Inputs[1].Config,
Expand Down
7 changes: 3 additions & 4 deletions plugins/inputs/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"sync"
"time"

"github.com/kballard/go-shellquote"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/nagios"
"github.com/kballard/go-shellquote"
)

const sampleConfig = `
Expand Down Expand Up @@ -50,7 +49,7 @@ type Exec struct {
parser parsers.Parser

runner Runner
log telegraf.Logger
Log telegraf.Logger `toml:"-"`
}

func NewExec() *Exec {
Expand Down Expand Up @@ -161,7 +160,7 @@ func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync
if isNagios {
metrics, err = nagios.TryAddState(runErr, metrics)
if err != nil {
e.log.Errorf("Failed to add nagios state: %s", err)
e.Log.Errorf("Failed to add nagios state: %s", err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestExec(t *testing.T) {
MetricName: "exec",
})
e := &Exec{
log: testutil.Logger{},
Log: testutil.Logger{},
runner: newRunnerMock([]byte(validJson), nil, nil),
Commands: []string{"testcommand arg1"},
parser: parser,
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestExecMalformed(t *testing.T) {
MetricName: "exec",
})
e := &Exec{
log: testutil.Logger{},
Log: testutil.Logger{},
runner: newRunnerMock([]byte(malformedJson), nil, nil),
Commands: []string{"badcommand arg1"},
parser: parser,
Expand All @@ -143,7 +143,7 @@ func TestCommandError(t *testing.T) {
MetricName: "exec",
})
e := &Exec{
log: testutil.Logger{},
Log: testutil.Logger{},
runner: newRunnerMock(nil, nil, fmt.Errorf("exit status code 1")),
Commands: []string{"badcommand"},
parser: parser,
Expand Down

0 comments on commit b15c165

Please sign in to comment.