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

fix issue with shim use of config.Duration #7996

Merged
merged 4 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,17 +1033,15 @@ 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
}
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
Expand Down
36 changes: 18 additions & 18 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,33 +53,36 @@ 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
}
*s = Size(val)
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 changes: 31 additions & 0 deletions config/types_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
33 changes: 33 additions & 0 deletions plugins/common/shim/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions plugins/common/shim/testdata/special.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# testing custom field types
[[inputs.test]]
duration = "3s"
size = "3MB"
2 changes: 1 addition & 1 deletion plugins/inputs/vsphere/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good change but looks unrelated. Was it supposed to be in this pr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated; just don't want to make a new pr for one line. :D

continue
}
start, ok := e.hwMarks.Get(object.ref.Value, metricName)
Expand Down