The documentation stated that the `alertname=`
part of a matcher could be dropped and it would be
assumed that the first value was the alertname.
This behavior was only honored if there was a
single matcher, but failed if there were multiple.

Any time we have one or more matchers, check to
see if the first matcher fails parsing. If so,
assume it's intended to be used as the alertname,
and append that value to the matcher.

Signed-off-by: Stuart Nelson <stuartnelson3@gmail.com>
This commit is contained in:
stuart nelson 2018-10-08 13:06:12 +02:00 committed by GitHub
parent ce2f2ac380
commit 50b87ac720
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 9 deletions

View File

@ -77,17 +77,16 @@ func configureQueryAlertsCmd(cc *kingpin.CmdClause) {
func (a *alertQueryCmd) queryAlerts(ctx context.Context, _ *kingpin.ParseContext) error { func (a *alertQueryCmd) queryAlerts(ctx context.Context, _ *kingpin.ParseContext) error {
var filterString = "" var filterString = ""
if len(a.matcherGroups) == 1 { if len(a.matcherGroups) > 0 {
// If the parser fails then we likely don't have a (=|=~|!=|!~) so lets // Attempt to parse the first argument. If the parser fails
// assume that the user wants alertname=<arg> and prepend `alertname=` // then we likely don't have a (=|=~|!=|!~) so lets assume that
// to the front. // the user wants alertname=<arg> and prepend `alertname=` to
_, err := parse.Matcher(a.matcherGroups[0]) // the front.
m := a.matcherGroups[0]
_, err := parse.Matcher(m)
if err != nil { if err != nil {
filterString = fmt.Sprintf("{alertname=%s}", a.matcherGroups[0]) a.matcherGroups[0] = fmt.Sprintf("alertname=%s", m)
} else {
filterString = fmt.Sprintf("{%s}", strings.Join(a.matcherGroups, ","))
} }
} else if len(a.matcherGroups) > 1 {
filterString = fmt.Sprintf("{%s}", strings.Join(a.matcherGroups, ",")) filterString = fmt.Sprintf("{%s}", strings.Join(a.matcherGroups, ","))
} }