From 1f66a7e18ea3c588ec05b6bd348e4d4c5993cce5 Mon Sep 17 00:00:00 2001 From: Hrishikesh Barman Date: Mon, 7 Jan 2019 18:19:41 +0530 Subject: [PATCH] support for assuming first label is alertname in silence add and query (#1693) * simplified setting first assumed alertname in cli/silence_query.go * added assumed first label to alertname when adding silences Signed-off-by: Hrishikesh Barman --- cli/silence_add.go | 10 ++++++++++ cli/silence_query.go | 7 ++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cli/silence_add.go b/cli/silence_add.go index 091c000b..bc66cbc5 100644 --- a/cli/silence_add.go +++ b/cli/silence_add.go @@ -89,6 +89,16 @@ func configureSilenceAddCmd(cc *kingpin.CmdClause) { func (c *silenceAddCmd) add(ctx context.Context, _ *kingpin.ParseContext) error { var err error + if len(c.matchers) > 0 { + // If the parser fails then we likely don't have a (=|=~|!=|!~) so lets + // assume that the user wants alertname= and prepend `alertname=` + // to the front. + _, err := parseMatchers([]string{c.matchers[0]}) + if err != nil { + c.matchers[0] = fmt.Sprintf("alertname=%s", c.matchers[0]) + } + } + matchers, err := parseMatchers(c.matchers) if err != nil { return err diff --git a/cli/silence_query.go b/cli/silence_query.go index 34d72a74..db6039ad 100644 --- a/cli/silence_query.go +++ b/cli/silence_query.go @@ -93,17 +93,14 @@ func configureSilenceQueryCmd(cc *kingpin.CmdClause) { func (c *silenceQueryCmd) query(ctx context.Context, _ *kingpin.ParseContext) error { var filterString = "" - if len(c.matchers) == 1 { + if len(c.matchers) > 0 { // If the parser fails then we likely don't have a (=|=~|!=|!~) so lets // assume that the user wants alertname= and prepend `alertname=` // to the front. _, err := parse.Matcher(c.matchers[0]) if err != nil { - filterString = fmt.Sprintf("{alertname=%s}", c.matchers[0]) - } else { - filterString = fmt.Sprintf("{%s}", strings.Join(c.matchers, ",")) + c.matchers[0] = fmt.Sprintf("alertname=%s", c.matchers[0]) } - } else if len(c.matchers) > 1 { filterString = fmt.Sprintf("{%s}", strings.Join(c.matchers, ",")) }