Add @alertmanager filter

This commit is contained in:
Łukasz Mierzwa
2017-07-01 13:54:55 -07:00
parent 812eceed39
commit cb69029c4f
7 changed files with 138 additions and 3 deletions

View File

@@ -129,6 +129,31 @@
</tr>
</thead>
<tbody>
<tr>
<td id="help-alertmanager">
<code>@alertmanager(= != =~ !~)$value</code>
</td>
<td>
<p>Match alerts based on the Alertmanager instance name they were collected from.</p>
<table class="table examples">
<tbody>
<tr>
<td><span class="label label-info">@alertmanager=prod</span></td>
<td>Match alerts collected from Alertmanager instance named <code>prod</code>.</td>
</tr>
<tr>
<td><span class="label label-info">@alertmanager!=dev</span></td>
<td>Match alerts collected from Alertmanager instances except for the one named <code>dev</code>.</td>
</tr>
<tr>
<td><span class="label label-info">@alertmanager=~prod</span></td>
<td>Match alerts collected from Alertmanager instances with names matching regular expression <code>.*prod.*</code>.</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td id="help-receiver">
<code>@receiver(= != =~ !~)$value</code>

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,61 @@
package filters
import (
"fmt"
"strings"
"github.com/cloudflare/unsee/models"
)
type alertmanagerInstanceFilter struct {
alertFilter
}
func (filter *alertmanagerInstanceFilter) Match(alert *models.Alert, matches int) bool {
if filter.IsValid {
var isMatch bool
for _, am := range alert.Alertmanager {
if filter.Matcher.Compare(am.Name, filter.Value) {
isMatch = true
}
}
if isMatch {
filter.Hits++
}
return isMatch
}
e := fmt.Sprintf("Match() called on invalid filter %#v", filter)
panic(e)
}
func newAlertmanagerInstanceFilter() FilterT {
f := alertmanagerInstanceFilter{}
return &f
}
func alertmanagerInstanceAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
tokens := map[string]models.Autocomplete{}
for _, alert := range alerts {
for _, am := range alert.Alertmanager {
for _, operator := range operators {
switch operator {
case equalOperator, notEqualOperator:
token := fmt.Sprintf("%s%s%s", name, operator, am.Name)
tokens[token] = makeAC(
token,
[]string{
name,
strings.TrimPrefix(name, "@"),
name + operator,
},
)
}
}
}
}
acData := []models.Autocomplete{}
for _, token := range tokens {
acData = append(acData, token)
}
return acData
}

View File

@@ -37,7 +37,7 @@ func (filter *stateFilter) Match(alert *models.Alert, matches int) bool {
panic(e)
}
func newstateFilter() FilterT {
func newStateFilter() FilterT {
f := stateFilter{}
return &f
}

View File

@@ -432,6 +432,42 @@ var tests = []filterTest{
Expression: "@inhibited=~false",
IsValid: false,
},
filterTest{
Expression: "@alertmanager=test",
IsValid: true,
Alert: models.Alert{},
IsMatch: true,
},
filterTest{
Expression: "@alertmanager=abc",
IsValid: true,
Alert: models.Alert{},
IsMatch: false,
},
filterTest{
Expression: "@alertmanager=~tes",
IsValid: true,
Alert: models.Alert{},
IsMatch: true,
},
filterTest{
Expression: "@alertmanager=~000",
IsValid: true,
Alert: models.Alert{},
IsMatch: false,
},
filterTest{
Expression: "@alertmanager!=tes",
IsValid: true,
Alert: models.Alert{},
IsMatch: true,
},
filterTest{
Expression: "@alertmanager!~abc",
IsValid: true,
Alert: models.Alert{},
IsMatch: true,
},
}
func TestFilters(t *testing.T) {

View File

@@ -40,11 +40,18 @@ type filterConfig struct {
// AllFilters contains the mapping of all filters along with operators they
// support
var AllFilters = []filterConfig{
filterConfig{
Label: "@alertmanager",
LabelRe: regexp.MustCompile("^@alertmanager$"),
SupportedOperators: []string{regexpOperator, negativeRegexOperator, equalOperator, notEqualOperator},
Factory: newAlertmanagerInstanceFilter,
Autocomplete: alertmanagerInstanceAutocomplete,
},
filterConfig{
Label: "@state",
LabelRe: regexp.MustCompile("^@state$"),
SupportedOperators: []string{equalOperator, notEqualOperator},
Factory: newstateFilter,
Factory: newStateFilter,
Autocomplete: stateAutocomplete,
},
filterConfig{

View File

@@ -209,6 +209,8 @@ var acTests = []acTestCase{
"alertname!=Host_Down",
"alertname!=HTTP_Probe_Failed",
"alertname!=Free_Disk_Space_Too_Low",
"@alertmanager=default",
"@alertmanager!=default",
"@age>1h",
"@age>10m",
"@age<1h",
@@ -226,6 +228,8 @@ var acTests = []acTestCase{
"alertname!=Host_Down",
"alertname!=HTTP_Probe_Failed",
"alertname!=Free_Disk_Space_Too_Low",
"@alertmanager=default",
"@alertmanager!=default",
},
},
acTestCase{
@@ -285,6 +289,8 @@ var acTests = []acTestCase{
"@receiver!=by-cluster-service",
"@limit=50",
"@limit=10",
"@alertmanager=default",
"@alertmanager!=default",
"@age>1h",
"@age>10m",
"@age<1h",