diff --git a/config/config.go b/config/config.go index f5e7db6bbc106..4798814c009f9 100644 --- a/config/config.go +++ b/config/config.go @@ -1033,8 +1033,7 @@ func (c *Config) addInput(name string, table *ast.Table) error { // If the input has a SetParser function, then this means it can accept // arbitrary types of input, so build the parser and set it. - switch t := input.(type) { - case parsers.ParserInput: + if t, ok := input.(parsers.ParserInput); ok { parser, err := buildParser(name, table) if err != nil { return err @@ -1042,8 +1041,7 @@ func (c *Config) addInput(name string, table *ast.Table) error { t.SetParser(parser) } - switch t := input.(type) { - case parsers.ParserFuncInput: + if t, ok := input.(parsers.ParserFuncInput); ok { config, err := getParserConfig(name, table) if err != nil { return err diff --git a/config/types.go b/config/types.go index 3acf3d1d6019e..7c1c50b9e3690 100644 --- a/config/types.go +++ b/config/types.go @@ -14,9 +14,6 @@ type Duration time.Duration // Size is an int64 type Size int64 -// Number is a float -type Number float64 - // UnmarshalTOML parses the duration from the TOML config file func (d *Duration) UnmarshalTOML(b []byte) error { var err error @@ -56,20 +53,29 @@ func (d *Duration) UnmarshalTOML(b []byte) error { return nil } +func (d *Duration) UnmarshalText(text []byte) error { + return d.UnmarshalTOML(text) +} + func (s *Size) UnmarshalTOML(b []byte) error { var err error - b = bytes.Trim(b, `'`) + if len(b) == 0 { + return nil + } + str := string(b) + if b[0] == '"' || b[0] == '\'' { + str, err = strconv.Unquote(str) + if err != nil { + return err + } + } - val, err := strconv.ParseInt(string(b), 10, 64) + val, err := strconv.ParseInt(str, 10, 64) if err == nil { *s = Size(val) return nil } - uq, err := strconv.Unquote(string(b)) - if err != nil { - return err - } - val, err = units.ParseStrictBytes(uq) + val, err = units.ParseStrictBytes(str) if err != nil { return err } @@ -77,12 +83,6 @@ func (s *Size) UnmarshalTOML(b []byte) error { return nil } -func (n *Number) UnmarshalTOML(b []byte) error { - value, err := strconv.ParseFloat(string(b), 64) - if err != nil { - return err - } - - *n = Number(value) - return nil +func (s *Size) UnmarshalText(text []byte) error { + return s.UnmarshalTOML(text) } diff --git a/config/types_test.go b/config/types_test.go new file mode 100644 index 0000000000000..8e35de6111c82 --- /dev/null +++ b/config/types_test.go @@ -0,0 +1,31 @@ +package config_test + +import ( + "testing" + "time" + + "github.com/influxdata/telegraf/config" + "github.com/influxdata/telegraf/plugins/processors/reverse_dns" + "github.com/stretchr/testify/require" +) + +func TestConfigDuration(t *testing.T) { + c := config.NewConfig() + err := c.LoadConfigData([]byte(` +[[processors.reverse_dns]] + cache_ttl = "3h" + lookup_timeout = "17s" + max_parallel_lookups = 13 + ordered = true + [[processors.reverse_dns.lookup]] + field = "source_ip" + dest = "source_name" +`)) + require.NoError(t, err) + require.Len(t, c.Processors, 1) + p := c.Processors[0].Processor.(*reverse_dns.ReverseDNS) + require.EqualValues(t, p.CacheTTL, 3*time.Hour) + require.EqualValues(t, p.LookupTimeout, 17*time.Second) + require.Equal(t, p.MaxParallelLookups, 13) + require.Equal(t, p.Ordered, true) +} diff --git a/plugins/common/shim/config_test.go b/plugins/common/shim/config_test.go index 1aee40df0f07a..be4ee4140feb5 100644 --- a/plugins/common/shim/config_test.go +++ b/plugins/common/shim/config_test.go @@ -3,8 +3,10 @@ package shim import ( "os" "testing" + "time" "github.com/influxdata/telegraf" + tgConfig "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/plugins/inputs" "github.com/stretchr/testify/require" ) @@ -37,3 +39,34 @@ func TestDefaultImportedPluginsSelfRegisters(t *testing.T) { require.NoError(t, err) require.Equal(t, "test", cfg.Input.Description()) } + +func TestLoadingSpecialTypes(t *testing.T) { + inputs.Add("test", func() telegraf.Input { + return &testDurationInput{} + }) + + c := "./testdata/special.conf" + conf, err := LoadConfig(&c) + require.NoError(t, err) + + inp := conf.Input.(*testDurationInput) + + require.EqualValues(t, 3*time.Second, inp.Duration) + require.EqualValues(t, 3*1000*1000, inp.Size) +} + +type testDurationInput struct { + Duration tgConfig.Duration `toml:"duration"` + Size tgConfig.Size `toml:"size"` +} + +func (i *testDurationInput) SampleConfig() string { + return "" +} + +func (i *testDurationInput) Description() string { + return "" +} +func (i *testDurationInput) Gather(acc telegraf.Accumulator) error { + return nil +} diff --git a/plugins/common/shim/testdata/special.conf b/plugins/common/shim/testdata/special.conf new file mode 100644 index 0000000000000..c324b638497c5 --- /dev/null +++ b/plugins/common/shim/testdata/special.conf @@ -0,0 +1,4 @@ +# testing custom field types +[[inputs.test]] + duration = "3s" + size = "3MB" \ No newline at end of file diff --git a/plugins/inputs/vsphere/endpoint.go b/plugins/inputs/vsphere/endpoint.go index 93d74e63f7a52..0a24efe6ef982 100644 --- a/plugins/inputs/vsphere/endpoint.go +++ b/plugins/inputs/vsphere/endpoint.go @@ -887,7 +887,7 @@ func (e *Endpoint) chunkify(ctx context.Context, res *resourceKind, now time.Tim // Determine time of last successful collection metricName := e.getMetricNameForId(metric.CounterId) if metricName == "" { - e.log.Info("Unable to find metric name for id %d. Skipping!", metric.CounterId) + e.log.Infof("Unable to find metric name for id %d. Skipping!", metric.CounterId) continue } start, ok := e.hwMarks.Get(object.ref.Value, metricName)