Skip to content

Commit 74c7dd3

Browse files
authored
Fix logger initialization in exec input (influxdata#6492)
1 parent d798891 commit 74c7dd3

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

internal/config/config_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ func TestConfig_LoadDirectory(t *testing.T) {
158158
MeasurementSuffix: "_myothercollector",
159159
}
160160
eConfig.Tags = make(map[string]string)
161+
162+
exec := c.Inputs[1].Input.(*exec.Exec)
163+
require.NotNil(t, exec.Log)
164+
exec.Log = nil
165+
161166
assert.Equal(t, ex, c.Inputs[1].Input,
162167
"Merged Testdata did not produce a correct exec struct.")
163168
assert.Equal(t, eConfig, c.Inputs[1].Config,

plugins/inputs/exec/exec.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ import (
1010
"sync"
1111
"time"
1212

13-
"github.com/kballard/go-shellquote"
14-
1513
"github.com/influxdata/telegraf"
1614
"github.com/influxdata/telegraf/internal"
1715
"github.com/influxdata/telegraf/plugins/inputs"
1816
"github.com/influxdata/telegraf/plugins/parsers"
1917
"github.com/influxdata/telegraf/plugins/parsers/nagios"
18+
"github.com/kballard/go-shellquote"
2019
)
2120

2221
const sampleConfig = `
@@ -50,7 +49,7 @@ type Exec struct {
5049
parser parsers.Parser
5150

5251
runner Runner
53-
log telegraf.Logger
52+
Log telegraf.Logger `toml:"-"`
5453
}
5554

5655
func NewExec() *Exec {
@@ -161,7 +160,7 @@ func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync
161160
if isNagios {
162161
metrics, err = nagios.TryAddState(runErr, metrics)
163162
if err != nil {
164-
e.log.Errorf("Failed to add nagios state: %s", err)
163+
e.Log.Errorf("Failed to add nagios state: %s", err)
165164
}
166165
}
167166

plugins/inputs/exec/exec_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func TestExec(t *testing.T) {
9696
MetricName: "exec",
9797
})
9898
e := &Exec{
99-
log: testutil.Logger{},
99+
Log: testutil.Logger{},
100100
runner: newRunnerMock([]byte(validJson), nil, nil),
101101
Commands: []string{"testcommand arg1"},
102102
parser: parser,
@@ -126,7 +126,7 @@ func TestExecMalformed(t *testing.T) {
126126
MetricName: "exec",
127127
})
128128
e := &Exec{
129-
log: testutil.Logger{},
129+
Log: testutil.Logger{},
130130
runner: newRunnerMock([]byte(malformedJson), nil, nil),
131131
Commands: []string{"badcommand arg1"},
132132
parser: parser,
@@ -143,7 +143,7 @@ func TestCommandError(t *testing.T) {
143143
MetricName: "exec",
144144
})
145145
e := &Exec{
146-
log: testutil.Logger{},
146+
Log: testutil.Logger{},
147147
runner: newRunnerMock(nil, nil, fmt.Errorf("exit status code 1")),
148148
Commands: []string{"badcommand"},
149149
parser: parser,

0 commit comments

Comments
 (0)