From ea9a584e8de8fe3642bac9e1006f59ffeefa24a3 Mon Sep 17 00:00:00 2001 From: Binh Le Date: Sat, 11 Nov 2017 21:51:33 +0800 Subject: [PATCH] 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. --- cli/silence_add.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cli/silence_add.go b/cli/silence_add.go index e249c01998..5c0cb58e45 100644 --- a/cli/silence_add.go +++ b/cli/silence_add.go @@ -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")