diff --git a/agent/agent_test.go b/agent/agent_test.go index 9cc631b17c465..e3e6f28a2445e 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -1,10 +1,13 @@ package agent import ( + "fmt" "testing" "time" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" + "github.com/influxdata/telegraf/models" _ "github.com/influxdata/telegraf/plugins/inputs/all" _ "github.com/influxdata/telegraf/plugins/outputs/all" "github.com/stretchr/testify/assert" @@ -19,6 +22,49 @@ func TestAgent_OmitHostname(t *testing.T) { assert.NotContains(t, c.Tags, "host") } +type testIgnoreErrorInput struct { +} + +func (i *testIgnoreErrorInput) Init() error { + return fmt.Errorf("could not initialize input: test error") +} + +func (i *testIgnoreErrorInput) Gather(telegraf.Accumulator) error { + return nil +} +func (i *testIgnoreErrorInput) SampleConfig() string { + return "" +} + +func TestAgent_IgnoreErrorInputs(t *testing.T) { + c := config.NewConfig() + assert.False(t, c.Agent.IgnoreErrorInputs) + c.Inputs = []*models.RunningInput{{}} + a, err := NewAgent(c) + assert.NoError(t, err) + a.initPlugins() + assert.Equal(t, 1, len(c.Inputs)) + + c.Inputs = []*models.RunningInput{{ + Config: &models.InputConfig{ + Name: "test error input", + Alias: "test alias", + Interval: 10 * time.Second, + }, + Input: &testIgnoreErrorInput{}, + }} + a, err = NewAgent(c) + assert.NoError(t, err) + err = a.initPlugins() + assert.Error(t, err) + + assert.Equal(t, 1, len(c.Inputs)) + c.Agent.IgnoreErrorInputs = true + err = a.initPlugins() + assert.NoError(t, err) + assert.Equal(t, 0, len(c.Inputs)) +} + func TestAgent_LoadPlugin(t *testing.T) { c := config.NewConfig() c.InputFilters = []string{"mysql"} diff --git a/config/config.go b/config/config.go index 9f9b41e39ff6d..163f5d6fed7a0 100644 --- a/config/config.go +++ b/config/config.go @@ -421,6 +421,9 @@ var agentConfig = ` ## Example: America/Chicago # log_with_timezone = "" + ## Indicated whether ignore input plugins that produce the error during the initialization. + # ignore_error_inputs = false + ## Override default hostname, if empty use os.Hostname() hostname = "" ## If set to true, do no set the "host" tag in the telegraf agent. diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 06644d12bbe56..caa0a577e7f2c 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -251,6 +251,11 @@ The agent table configures Telegraf and the defaults used across all plugins. translates by calling external programs snmptranslate and snmptable, or "gosmi" which translates using the built-in gosmi library. +- **ignore_error_inputs**: + If set to true, discard the input plugins that produce the error during initialization. + If set to false, the program will exit when an input plugin has an error occurred during the initialization. + Default: false + ## Plugins Telegraf plugins are divided into 4 types: [inputs][], [outputs][], diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 11a9a16f54639..60c615174768b 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -103,6 +103,9 @@ ## Example: America/Chicago # log_with_timezone = "" + ## Indicated whether ignore input plugins that produce the error during the initialization. + # ignore_error_inputs = false + ## Override default hostname, if empty use os.Hostname() hostname = "" ## If set to true, do no set the "host" tag in the telegraf agent. diff --git a/etc/telegraf_windows.conf b/etc/telegraf_windows.conf index 4d19e8047a61f..1a59146a8093e 100644 --- a/etc/telegraf_windows.conf +++ b/etc/telegraf_windows.conf @@ -103,6 +103,9 @@ ## Example: America/Chicago # log_with_timezone = "" + ## Indicated whether ignore input plugins that produce the error during the initialization. + # ignore_error_inputs = false + ## Override default hostname, if empty use os.Hostname() hostname = "" ## If set to true, do no set the "host" tag in the telegraf agent.