fix issue with shim use of config.Duration (#7996)
This commit is contained in:
parent
e9d5b1fb34
commit
11afd42617
|
|
@ -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
|
// If the input has a SetParser function, then this means it can accept
|
||||||
// arbitrary types of input, so build the parser and set it.
|
// arbitrary types of input, so build the parser and set it.
|
||||||
switch t := input.(type) {
|
if t, ok := input.(parsers.ParserInput); ok {
|
||||||
case parsers.ParserInput:
|
|
||||||
parser, err := buildParser(name, table)
|
parser, err := buildParser(name, table)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -1042,8 +1041,7 @@ func (c *Config) addInput(name string, table *ast.Table) error {
|
||||||
t.SetParser(parser)
|
t.SetParser(parser)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch t := input.(type) {
|
if t, ok := input.(parsers.ParserFuncInput); ok {
|
||||||
case parsers.ParserFuncInput:
|
|
||||||
config, err := getParserConfig(name, table)
|
config, err := getParserConfig(name, table)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,6 @@ type Duration time.Duration
|
||||||
// Size is an int64
|
// Size is an int64
|
||||||
type Size int64
|
type Size int64
|
||||||
|
|
||||||
// Number is a float
|
|
||||||
type Number float64
|
|
||||||
|
|
||||||
// UnmarshalTOML parses the duration from the TOML config file
|
// UnmarshalTOML parses the duration from the TOML config file
|
||||||
func (d *Duration) UnmarshalTOML(b []byte) error {
|
func (d *Duration) UnmarshalTOML(b []byte) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -56,20 +53,29 @@ func (d *Duration) UnmarshalTOML(b []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Duration) UnmarshalText(text []byte) error {
|
||||||
|
return d.UnmarshalTOML(text)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Size) UnmarshalTOML(b []byte) error {
|
func (s *Size) UnmarshalTOML(b []byte) error {
|
||||||
var err 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 {
|
if err == nil {
|
||||||
*s = Size(val)
|
*s = Size(val)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
uq, err := strconv.Unquote(string(b))
|
val, err = units.ParseStrictBytes(str)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
val, err = units.ParseStrictBytes(uq)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -77,12 +83,6 @@ func (s *Size) UnmarshalTOML(b []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Number) UnmarshalTOML(b []byte) error {
|
func (s *Size) UnmarshalText(text []byte) error {
|
||||||
value, err := strconv.ParseFloat(string(b), 64)
|
return s.UnmarshalTOML(text)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
*n = Number(value)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -3,8 +3,10 @@ package shim
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
tgConfig "github.com/influxdata/telegraf/config"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
@ -37,3 +39,34 @@ func TestDefaultImportedPluginsSelfRegisters(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, "test", cfg.Input.Description())
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
# testing custom field types
|
||||||
|
[[inputs.test]]
|
||||||
|
duration = "3s"
|
||||||
|
size = "3MB"
|
||||||
|
|
@ -887,7 +887,7 @@ func (e *Endpoint) chunkify(ctx context.Context, res *resourceKind, now time.Tim
|
||||||
// Determine time of last successful collection
|
// Determine time of last successful collection
|
||||||
metricName := e.getMetricNameForId(metric.CounterId)
|
metricName := e.getMetricNameForId(metric.CounterId)
|
||||||
if metricName == "" {
|
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
|
continue
|
||||||
}
|
}
|
||||||
start, ok := e.hwMarks.Get(object.ref.Value, metricName)
|
start, ok := e.hwMarks.Get(object.ref.Value, metricName)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue