fix issue with execd restart_delay being ignored (#7867)

This commit is contained in:
Steven Soroka 2020-07-21 13:06:33 -04:00 committed by GitHub
parent 903a065a0d
commit 9f6b7092f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 14 deletions

View File

@ -18,14 +18,14 @@ type Size int64
type Number float64
// 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
b = bytes.Trim(b, `'`)
// see if we can directly convert it
dur, err := time.ParseDuration(string(b))
if err == nil {
d = Duration(dur)
*d = Duration(dur)
return nil
}
@ -33,7 +33,7 @@ func (d Duration) UnmarshalTOML(b []byte) error {
if uq, err := strconv.Unquote(string(b)); err == nil && len(uq) > 0 {
dur, err := time.ParseDuration(uq)
if err == nil {
d = Duration(dur)
*d = Duration(dur)
return nil
}
}
@ -42,27 +42,27 @@ func (d Duration) UnmarshalTOML(b []byte) error {
sI, err := strconv.ParseInt(string(b), 10, 64)
if err == nil {
dur := time.Second * time.Duration(sI)
d = Duration(dur)
*d = Duration(dur)
return nil
}
// Second try parsing as float seconds
sF, err := strconv.ParseFloat(string(b), 64)
if err == nil {
dur := time.Second * time.Duration(sF)
d = Duration(dur)
*d = Duration(dur)
return nil
}
return nil
}
func (s Size) UnmarshalTOML(b []byte) error {
func (s *Size) UnmarshalTOML(b []byte) error {
var err error
b = bytes.Trim(b, `'`)
val, err := strconv.ParseInt(string(b), 10, 64)
if err == nil {
s = Size(val)
*s = Size(val)
return nil
}
uq, err := strconv.Unquote(string(b))
@ -73,16 +73,16 @@ func (s Size) UnmarshalTOML(b []byte) error {
if err != nil {
return err
}
s = Size(val)
*s = Size(val)
return nil
}
func (n Number) UnmarshalTOML(b []byte) error {
func (n *Number) UnmarshalTOML(b []byte) error {
value, err := strconv.ParseFloat(string(b), 64)
if err != nil {
return err
}
n = Number(value)
*n = Number(value)
return nil
}

View File

@ -41,10 +41,10 @@ const sampleConfig = `
`
type Execd struct {
Command []string
Signal string
RestartDelay config.Duration
Log telegraf.Logger
Command []string `toml:"command"`
Signal string `toml:"signal"`
RestartDelay config.Duration `toml:"restart_delay"`
Log telegraf.Logger `toml:"-"`
process *process.Process
acc telegraf.Accumulator

View File

@ -24,6 +24,24 @@ import (
"github.com/influxdata/telegraf"
)
func TestSettingConfigWorks(t *testing.T) {
cfg := `
[[inputs.execd]]
command = ["a", "b", "c"]
restart_delay = "1m"
signal = "SIGHUP"
`
conf := config.NewConfig()
require.NoError(t, conf.LoadConfigData([]byte(cfg)))
require.Len(t, conf.Inputs, 1)
inp, ok := conf.Inputs[0].Input.(*Execd)
require.True(t, ok)
require.EqualValues(t, []string{"a", "b", "c"}, inp.Command)
require.EqualValues(t, 1*time.Minute, inp.RestartDelay)
require.EqualValues(t, "SIGHUP", inp.Signal)
}
func TestExternalInputWorks(t *testing.T) {
influxParser, err := parsers.NewInfluxParser()
require.NoError(t, err)