Allow 'd', 'w', 'y' to be specified at time suffix when creating silence (#1091)

The time.ParseDuration refused to parse them with the reason that a day
can be shorter or longer than 24 hours. But they are already accepted in
Prometheus range query and a custom parser is included in Prometheus
common package so there's no reason amtool cannot use that.

This will be handy in cases you need to create silence for longer periods,
which are unfortunately common.
This commit is contained in:
Binh Le 2017-11-11 21:51:33 +08:00 committed by stuart nelson
parent fdee5fcbfc
commit ea9a584e8d

View File

@ -11,6 +11,7 @@ import (
"time"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
@ -18,7 +19,7 @@ import (
type addResponse struct {
Status string `json:"status"`
Data struct {
Data struct {
SilenceID string `json:"silenceId"`
} `json:"data,omitempty"`
ErrorType string `json:"errorType,omitempty"`
@ -102,11 +103,14 @@ func add(cmd *cobra.Command, args []string) error {
return err
}
} else {
duration, err := time.ParseDuration(expires)
duration, err := model.ParseDuration(expires)
if err != nil {
return err
}
endsAt = time.Now().UTC().Add(duration)
if duration == 0 {
return fmt.Errorf("silence duration must be greater than 0")
}
endsAt = time.Now().UTC().Add(time.Duration(duration))
}
author := viper.GetString("author")