func()

in config/config.go [1243:1404]


func (c *Config) addInput(name string, table *ast.Table) error {
	if len(c.InputFilters) > 0 && !sliceContains(name, c.InputFilters) {
		return nil
	}

	// Legacy support renaming io input to diskio
	if name == "io" {
		if err := c.printUserDeprecation("inputs", name, nil); err != nil {
			return err
		}
		name = "diskio"
	}

	// For inputs with parsers we need to compute the set of
	// options that is not covered by both, the parser and the input.
	// We achieve this by keeping a local book of missing entries
	// that counts the number of misses. In case we have a parser
	// for the input both need to miss the entry. We count the
	// missing entries at the end.
	missThreshold := 0
	missCount := make(map[string]int)
	c.setLocalMissingTomlFieldTracker(missCount)
	defer c.resetMissingTomlFieldTracker()

	creator, ok := inputs.Inputs[name]
	if !ok {
		// Handle removed, deprecated plugins
		if di, deprecated := inputs.Deprecations[name]; deprecated {
			printHistoricPluginDeprecationNotice("inputs", name, di)
			return fmt.Errorf("plugin deprecated")
		}

		return fmt.Errorf("Undefined but requested input: %s", name)
	}
	input := creator()

	// If the input has a SetParser or SetParserFunc function, it can accept
	// arbitrary data-formats, so build the requested parser and set it.
	if t, ok := input.(telegraf.ParserInput); ok {
		missThreshold = 1
		if parser, err := c.addParser(name, table); err == nil {
			t.SetParser(parser)
		} else {
			missThreshold = 0
			// Fallback to the old way of instantiating the parsers.
			config, err := c.getParserConfig(name, table)
			if err != nil {
				return err
			}
			parser, err := c.buildParserOld(name, config)
			if err != nil {
				return err
			}
			t.SetParser(parser)
		}
	}

	// Keep the old interface for backward compatibility
	if t, ok := input.(parsers.ParserInput); ok {
		// DEPRECATED: Please switch your plugin to telegraf.ParserInput.
		missThreshold = 1
		if parser, err := c.addParser(name, table); err == nil {
			t.SetParser(parser)
		} else {
			missThreshold = 0
			// Fallback to the old way of instantiating the parsers.
			config, err := c.getParserConfig(name, table)
			if err != nil {
				return err
			}
			parser, err := c.buildParserOld(name, config)
			if err != nil {
				return err
			}
			t.SetParser(parser)
		}
	}

	if t, ok := input.(telegraf.ParserFuncInput); ok {
		missThreshold = 1
		if c.probeParser(table) {
			t.SetParserFunc(func() (telegraf.Parser, error) {
				parser, err := c.addParser(name, table)
				if err != nil {
					return nil, err
				}
				err = parser.Init()
				return parser, err
			})
		} else {
			missThreshold = 0
			// Fallback to the old way
			config, err := c.getParserConfig(name, table)
			if err != nil {
				return err
			}
			t.SetParserFunc(func() (telegraf.Parser, error) {
				return c.buildParserOld(name, config)
			})
		}
	}

	if t, ok := input.(parsers.ParserFuncInput); ok {
		// DEPRECATED: Please switch your plugin to telegraf.ParserFuncInput.
		missThreshold = 1
		if c.probeParser(table) {
			t.SetParserFunc(func() (parsers.Parser, error) {
				parser, err := c.addParser(name, table)
				if err != nil {
					return nil, err
				}
				err = parser.Init()
				return parser, err
			})
		} else {
			missThreshold = 0
			// Fallback to the old way
			config, err := c.getParserConfig(name, table)
			if err != nil {
				return err
			}
			t.SetParserFunc(func() (parsers.Parser, error) {
				return c.buildParserOld(name, config)
			})
		}
	}

	pluginConfig, err := c.buildInput(name, table)
	if err != nil {
		return err
	}

	if err := c.toml.UnmarshalTable(table, input); err != nil {
		return err
	}

	if err := c.printUserDeprecation("inputs", name, input); err != nil {
		return err
	}

	if c, ok := interface{}(input).(interface{ TLSConfig() (*tls.Config, error) }); ok {
		if _, err := c.TLSConfig(); err != nil {
			return err
		}
	}

	rp := models.NewRunningInput(input, pluginConfig)
	rp.SetDefaultTags(c.Tags)
	c.Inputs = append(c.Inputs, rp)

	// Check the number of misses against the threshold
	for key, count := range missCount {
		if count <= missThreshold {
			continue
		}
		if err := c.missingTomlField(nil, key); err != nil {
			return err
		}
	}

	return nil
}