fix issue with shim use of config.Duration (#7996)

This commit is contained in:
Steven Soroka 2020-08-18 14:20:31 -04:00 committed by GitHub
parent e9d5b1fb34
commit 11afd42617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 89 additions and 23 deletions

View File

@ -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

View File

@ -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)
}

31
config/types_test.go Normal file
View File

@ -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)
}

View File

@ -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
}

View File

@ -0,0 +1,4 @@
# testing custom field types
[[inputs.test]]
duration = "3s"
size = "3MB"

View File

@ -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)