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

feat(agent): add ignore_error_inputs option for inputs #11304

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,23 @@ func (a *Agent) Run(ctx context.Context) error {

// initPlugins runs the Init function on plugins.
func (a *Agent) initPlugins() error {
inputs := make([]*models.RunningInput, 0)
for _, input := range a.Config.Inputs {
// Share the snmp translator setting with plugins that need it.
if tp, ok := input.Input.(snmp.TranslatorPlugin); ok {
tp.SetTranslator(a.Config.Agent.SnmpTranslator)
}
err := input.Init()
if err != nil {
if err != nil && a.Config.Agent.IgnoreErrorInputs {
Hipska marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("W! [agent] Ignore initialize error input %s: %v", input.LogName(), err)
continue
} else if err != nil {
return fmt.Errorf("could not initialize input %s: %v",
input.LogName(), err)
}
inputs = append(inputs, input)
}
a.Config.Inputs = inputs
for _, parser := range a.Config.Parsers {
err := parser.Init()
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -19,6 +22,50 @@ 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)
err = a.initPlugins()
assert.NoError(t, err)
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"}
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ type AgentConfig struct {
// Pick a timezone to use when logging or type 'local' for local time.
LogWithTimezone string `toml:"log_with_timezone"`

// Ignore Inputs that error to initialize
IgnoreErrorInputs bool `toml:"ignore_error_inputs"`

Hostname string
OmitHostname bool

Expand Down Expand Up @@ -418,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.
Expand Down
5 changes: 5 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][],
Expand Down
3 changes: 3 additions & 0 deletions etc/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions etc/telegraf_windows.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down