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 execd restart_delay being ignored #7867

Merged
merged 1 commit into from
Jul 21, 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
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 @@ -40,10 +40,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