Skip to content

Commit

Permalink
fix issue with execd restart_delay being ignored (influxdata#7867)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssoroka authored Jul 21, 2020
1 parent 903a065 commit 9f6b709
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
20 changes: 10 additions & 10 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ 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
}

// Parse string duration, ie, "1s"
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
}
}
Expand All @@ -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))
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions plugins/inputs/execd/execd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions plugins/inputs/execd/execd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9f6b709

Please sign in to comment.