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 <hrishikeshbman@gmail.com>
This commit is contained in:
Hrishikesh Barman 2019-01-07 18:19:41 +05:30 committed by Max Leonard Inden
parent 6787dd1bde
commit 1f66a7e18e
No known key found for this signature in database
GPG Key ID: 5403C5464810BC26
2 changed files with 12 additions and 5 deletions

View File

@ -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=<arg> 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

View File

@ -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=<arg> 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, ","))
}