2018-05-14 12:36:24 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
package v1
|
2017-11-04 13:38:16 +00:00
|
|
|
|
|
|
|
import (
|
2018-02-13 15:26:34 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2017-11-04 13:38:16 +00:00
|
|
|
"fmt"
|
2022-07-18 12:25:32 +00:00
|
|
|
"io"
|
2018-02-13 15:26:34 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-11-12 16:35:49 +00:00
|
|
|
"regexp"
|
2017-11-04 13:38:16 +00:00
|
|
|
"testing"
|
2018-02-13 15:26:34 +00:00
|
|
|
"time"
|
2017-11-04 13:38:16 +00:00
|
|
|
|
2019-09-16 08:56:29 +00:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-05-07 16:07:19 +00:00
|
|
|
"github.com/prometheus/alertmanager/config"
|
2018-02-13 15:26:34 +00:00
|
|
|
"github.com/prometheus/alertmanager/dispatch"
|
2019-09-16 08:56:29 +00:00
|
|
|
"github.com/prometheus/alertmanager/pkg/labels"
|
2018-02-13 15:26:34 +00:00
|
|
|
"github.com/prometheus/alertmanager/provider"
|
2017-11-15 19:29:06 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2017-11-04 13:38:16 +00:00
|
|
|
)
|
|
|
|
|
2018-05-07 16:07:19 +00:00
|
|
|
// fakeAlerts is a struct implementing the provider.Alerts interface for tests.
|
2018-02-13 15:26:34 +00:00
|
|
|
type fakeAlerts struct {
|
2018-05-07 16:07:19 +00:00
|
|
|
fps map[model.Fingerprint]int
|
|
|
|
alerts []*types.Alert
|
|
|
|
err error
|
2018-02-13 15:26:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:07:19 +00:00
|
|
|
func newFakeAlerts(alerts []*types.Alert, withErr bool) *fakeAlerts {
|
|
|
|
fps := make(map[model.Fingerprint]int)
|
|
|
|
for i, a := range alerts {
|
|
|
|
fps[a.Fingerprint()] = i
|
|
|
|
}
|
|
|
|
f := &fakeAlerts{
|
|
|
|
alerts: alerts,
|
|
|
|
fps: fps,
|
|
|
|
}
|
|
|
|
if withErr {
|
2019-02-25 10:04:42 +00:00
|
|
|
f.err = errors.New("error occurred")
|
2018-05-07 16:07:19 +00:00
|
|
|
}
|
|
|
|
return f
|
2018-02-13 15:26:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:07:19 +00:00
|
|
|
func (f *fakeAlerts) Subscribe() provider.AlertIterator { return nil }
|
2018-02-13 15:26:34 +00:00
|
|
|
func (f *fakeAlerts) Get(model.Fingerprint) (*types.Alert, error) { return nil, nil }
|
|
|
|
func (f *fakeAlerts) Put(alerts ...*types.Alert) error {
|
2018-05-07 16:07:19 +00:00
|
|
|
return f.err
|
|
|
|
}
|
2022-03-25 16:59:51 +00:00
|
|
|
|
2018-05-07 16:07:19 +00:00
|
|
|
func (f *fakeAlerts) GetPending() provider.AlertIterator {
|
|
|
|
ch := make(chan *types.Alert)
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
for _, a := range f.alerts {
|
|
|
|
ch <- a
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return provider.NewAlertIterator(ch, done, f.err)
|
2018-02-13 15:26:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:07:19 +00:00
|
|
|
func newGetAlertStatus(f *fakeAlerts) func(model.Fingerprint) types.AlertStatus {
|
|
|
|
return func(fp model.Fingerprint) types.AlertStatus {
|
|
|
|
status := types.AlertStatus{SilencedBy: []string{}, InhibitedBy: []string{}}
|
|
|
|
|
|
|
|
i, ok := f.fps[fp]
|
|
|
|
if !ok {
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
alert := f.alerts[i]
|
|
|
|
switch alert.Labels["state"] {
|
|
|
|
case "active":
|
|
|
|
status.State = types.AlertStateActive
|
|
|
|
case "unprocessed":
|
|
|
|
status.State = types.AlertStateUnprocessed
|
|
|
|
case "suppressed":
|
|
|
|
status.State = types.AlertStateSuppressed
|
|
|
|
}
|
|
|
|
if alert.Labels["silenced_by"] != "" {
|
|
|
|
status.SilencedBy = append(status.SilencedBy, string(alert.Labels["silenced_by"]))
|
|
|
|
}
|
|
|
|
if alert.Labels["inhibited_by"] != "" {
|
|
|
|
status.InhibitedBy = append(status.InhibitedBy, string(alert.Labels["inhibited_by"]))
|
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 15:26:34 +00:00
|
|
|
|
|
|
|
func TestAddAlerts(t *testing.T) {
|
|
|
|
now := func(offset int) time.Time {
|
|
|
|
return time.Now().Add(time.Duration(offset) * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range []struct {
|
|
|
|
start, end time.Time
|
|
|
|
err bool
|
|
|
|
code int
|
|
|
|
}{
|
|
|
|
{time.Time{}, time.Time{}, false, 200},
|
|
|
|
{now(0), time.Time{}, false, 200},
|
|
|
|
{time.Time{}, now(-1), false, 200},
|
|
|
|
{time.Time{}, now(0), false, 200},
|
|
|
|
{time.Time{}, now(1), false, 200},
|
|
|
|
{now(-2), now(-1), false, 200},
|
|
|
|
{now(1), now(2), false, 200},
|
|
|
|
{now(1), now(0), false, 400},
|
|
|
|
{now(0), time.Time{}, true, 500},
|
|
|
|
} {
|
|
|
|
alerts := []model.Alert{{
|
|
|
|
StartsAt: tc.start,
|
|
|
|
EndsAt: tc.end,
|
|
|
|
Labels: model.LabelSet{"label1": "test1"},
|
|
|
|
Annotations: model.LabelSet{"annotation1": "some text"},
|
|
|
|
}}
|
|
|
|
b, err := json.Marshal(&alerts)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:07:19 +00:00
|
|
|
alertsProvider := newFakeAlerts([]*types.Alert{}, tc.err)
|
Various improvements after code review
Most importantly, `api.New` now takes an `Options` struct as an
argument, which allows some other things done here as well:
- Timout and concurrency limit are now in the options, streamlining
the registration and the implementation of the limiting middleware.
- A local registry is used for metrics, and the metrics used so far
inside any of the api packages are using it now.
The 'in flight' metric now contains the 'get' as a method label. I
have also added a TODO to instrument other methods in the same way
(otherwise, the label doesn't reall make sense, semantically). I have
also added an explicit error counter for requests rejected because of
the concurrency limit. (They also show up as 503s in the generic HTTP
instrumentation (or they would, if v2 were instrumented, too), but
those 503s might have a number of reasons, while users might want to
alert on concurrency limit problems explicitly).
Signed-off-by: beorn7 <beorn@soundcloud.com>
2019-02-12 16:42:11 +00:00
|
|
|
api := New(alertsProvider, nil, newGetAlertStatus(alertsProvider), nil, nil, nil)
|
2019-02-06 18:06:31 +00:00
|
|
|
defaultGlobalConfig := config.DefaultGlobalConfig()
|
|
|
|
route := config.Route{}
|
|
|
|
api.Update(&config.Config{
|
|
|
|
Global: &defaultGlobalConfig,
|
|
|
|
Route: &route,
|
|
|
|
})
|
2018-02-13 15:26:34 +00:00
|
|
|
|
|
|
|
r, err := http.NewRequest("POST", "/api/v1/alerts", bytes.NewReader(b))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
api.addAlerts(w, r)
|
|
|
|
res := w.Result()
|
2022-07-18 12:25:32 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2018-02-13 15:26:34 +00:00
|
|
|
|
|
|
|
require.Equal(t, tc.code, w.Code, fmt.Sprintf("test case: %d, StartsAt %v, EndsAt %v, Response: %s", i, tc.start, tc.end, string(body)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:07:19 +00:00
|
|
|
func TestListAlerts(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
alerts := []*types.Alert{
|
2022-03-25 16:59:51 +00:00
|
|
|
{
|
2018-05-07 16:07:19 +00:00
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"state": "active", "alertname": "alert1"},
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
},
|
|
|
|
},
|
2022-03-25 16:59:51 +00:00
|
|
|
{
|
2018-05-07 16:07:19 +00:00
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"state": "unprocessed", "alertname": "alert2"},
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
},
|
|
|
|
},
|
2022-03-25 16:59:51 +00:00
|
|
|
{
|
2018-05-07 16:07:19 +00:00
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"state": "suppressed", "silenced_by": "abc", "alertname": "alert3"},
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
},
|
|
|
|
},
|
2022-03-25 16:59:51 +00:00
|
|
|
{
|
2018-05-07 16:07:19 +00:00
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"state": "suppressed", "inhibited_by": "abc", "alertname": "alert4"},
|
|
|
|
StartsAt: now.Add(-time.Minute),
|
|
|
|
},
|
|
|
|
},
|
2022-03-25 16:59:51 +00:00
|
|
|
{
|
2018-05-07 16:07:19 +00:00
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{"alertname": "alert5"},
|
|
|
|
StartsAt: now.Add(-2 * time.Minute),
|
|
|
|
EndsAt: now.Add(-time.Minute),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range []struct {
|
|
|
|
err bool
|
|
|
|
params map[string]string
|
|
|
|
|
|
|
|
code int
|
|
|
|
anames []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{},
|
|
|
|
200,
|
|
|
|
[]string{"alert1", "alert2", "alert3", "alert4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"active": "true", "unprocessed": "true", "silenced": "true", "inhibited": "true"},
|
|
|
|
200,
|
|
|
|
[]string{"alert1", "alert2", "alert3", "alert4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"active": "false", "unprocessed": "true", "silenced": "true", "inhibited": "true"},
|
|
|
|
200,
|
|
|
|
[]string{"alert2", "alert3", "alert4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"active": "true", "unprocessed": "false", "silenced": "true", "inhibited": "true"},
|
|
|
|
200,
|
|
|
|
[]string{"alert1", "alert3", "alert4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"active": "true", "unprocessed": "true", "silenced": "false", "inhibited": "true"},
|
|
|
|
200,
|
|
|
|
[]string{"alert1", "alert2", "alert4"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"active": "true", "unprocessed": "true", "silenced": "true", "inhibited": "false"},
|
|
|
|
200,
|
|
|
|
[]string{"alert1", "alert2", "alert3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"filter": "{alertname=\"alert3\""},
|
|
|
|
200,
|
|
|
|
[]string{"alert3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"filter": "{alertname"},
|
|
|
|
400,
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"receiver": "other"},
|
|
|
|
200,
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
map[string]string{"active": "invalid"},
|
|
|
|
400,
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
true,
|
|
|
|
map[string]string{},
|
|
|
|
500,
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
alertsProvider := newFakeAlerts(alerts, tc.err)
|
Various improvements after code review
Most importantly, `api.New` now takes an `Options` struct as an
argument, which allows some other things done here as well:
- Timout and concurrency limit are now in the options, streamlining
the registration and the implementation of the limiting middleware.
- A local registry is used for metrics, and the metrics used so far
inside any of the api packages are using it now.
The 'in flight' metric now contains the 'get' as a method label. I
have also added a TODO to instrument other methods in the same way
(otherwise, the label doesn't reall make sense, semantically). I have
also added an explicit error counter for requests rejected because of
the concurrency limit. (They also show up as 503s in the generic HTTP
instrumentation (or they would, if v2 were instrumented, too), but
those 503s might have a number of reasons, while users might want to
alert on concurrency limit problems explicitly).
Signed-off-by: beorn7 <beorn@soundcloud.com>
2019-02-12 16:42:11 +00:00
|
|
|
api := New(alertsProvider, nil, newGetAlertStatus(alertsProvider), nil, nil, nil)
|
2018-05-07 16:07:19 +00:00
|
|
|
api.route = dispatch.NewRoute(&config.Route{Receiver: "def-receiver"}, nil)
|
|
|
|
|
|
|
|
r, err := http.NewRequest("GET", "/api/v1/alerts", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
q := r.URL.Query()
|
|
|
|
for k, v := range tc.params {
|
|
|
|
q.Add(k, v)
|
|
|
|
}
|
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
api.listAlerts(w, r)
|
2022-07-18 12:25:32 +00:00
|
|
|
body, _ := io.ReadAll(w.Result().Body)
|
2018-05-07 16:07:19 +00:00
|
|
|
|
|
|
|
var res response
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, tc.code, w.Code, fmt.Sprintf("test case: %d, response: %s", i, string(body)))
|
|
|
|
if w.Code != 200 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data needs to be serialized/deserialized to be converted to the real type.
|
|
|
|
b, err := json.Marshal(res.Data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
2018-08-23 14:03:49 +00:00
|
|
|
retAlerts := []*Alert{}
|
2018-05-07 16:07:19 +00:00
|
|
|
err = json.Unmarshal(b, &retAlerts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
anames := []string{}
|
|
|
|
for _, a := range retAlerts {
|
|
|
|
name, ok := a.Labels["alertname"]
|
|
|
|
if ok {
|
|
|
|
anames = append(anames, string(name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, tc.anames, anames, fmt.Sprintf("test case: %d, alert names are not equal", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 13:38:16 +00:00
|
|
|
func TestAlertFiltering(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
alert *model.Alert
|
|
|
|
msg string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal
|
|
|
|
equal, err := labels.NewMatcher(labels.MatchEqual, "label1", "test1")
|
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-04 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test1"}}, "label1=test1", true},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test2"}}, "label1=test2", false},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label2": "test2"}}, "label2=test2", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := alertMatchesFilterLabels(test.alert, []*labels.Matcher{equal})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not Equal
|
|
|
|
notEqual, err := labels.NewMatcher(labels.MatchNotEqual, "label1", "test1")
|
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-04 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test1"}}, "label1!=test1", false},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test2"}}, "label1!=test2", true},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label2": "test2"}}, "label2!=test2", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := alertMatchesFilterLabels(test.alert, []*labels.Matcher{notEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp Equal
|
|
|
|
regexpEqual, err := labels.NewMatcher(labels.MatchRegexp, "label1", "tes.*")
|
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-04 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test1"}}, "label1=~test1", true},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test2"}}, "label1=~test2", true},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label2": "test2"}}, "label2=~test2", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := alertMatchesFilterLabels(test.alert, []*labels.Matcher{regexpEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp Not Equal
|
|
|
|
regexpNotEqual, err := labels.NewMatcher(labels.MatchNotRegexp, "label1", "tes.*")
|
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-04 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
2017-11-15 19:25:46 +00:00
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test1"}}, "label1!~test1", false},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label1": "test2"}}, "label1!~test2", false},
|
|
|
|
{&model.Alert{Labels: model.LabelSet{"label2": "test2"}}, "label2!~test2", true},
|
2017-11-04 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := alertMatchesFilterLabels(test.alert, []*labels.Matcher{regexpNotEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 16:35:49 +00:00
|
|
|
|
2017-11-15 19:29:06 +00:00
|
|
|
func TestSilenceFiltering(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
silence *types.Silence
|
|
|
|
msg string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal
|
|
|
|
equal, err := labels.NewMatcher(labels.MatchEqual, "label1", "test1")
|
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-15 19:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test1"})},
|
|
|
|
"label1=test1",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test2"})},
|
|
|
|
"label1=test2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label2": "test2"})},
|
|
|
|
"label2=test2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := silenceMatchesFilterLabels(test.silence, []*labels.Matcher{equal})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not Equal
|
|
|
|
notEqual, err := labels.NewMatcher(labels.MatchNotEqual, "label1", "test1")
|
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-15 19:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test1"})},
|
|
|
|
"label1!=test1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test2"})},
|
|
|
|
"label1!=test2",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label2": "test2"})},
|
|
|
|
"label2!=test2",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := silenceMatchesFilterLabels(test.silence, []*labels.Matcher{notEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp Equal
|
|
|
|
regexpEqual, err := labels.NewMatcher(labels.MatchRegexp, "label1", "tes.*")
|
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-15 19:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test1"})},
|
|
|
|
"label1=~test1",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test2"})},
|
|
|
|
"label1=~test2",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label2": "test2"})},
|
|
|
|
"label2=~test2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := silenceMatchesFilterLabels(test.silence, []*labels.Matcher{regexpEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp Not Equal
|
|
|
|
regexpNotEqual, err := labels.NewMatcher(labels.MatchNotRegexp, "label1", "tes.*")
|
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-15 19:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests = []test{
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test1"})},
|
|
|
|
"label1!~test1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label1": "test2"})},
|
|
|
|
"label1!~test2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&types.Silence{Matchers: newMatcher(model.LabelSet{"label2": "test2"})},
|
|
|
|
"label2!~test2",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
actual := silenceMatchesFilterLabels(test.silence, []*labels.Matcher{regexpNotEqual})
|
|
|
|
msg := fmt.Sprintf("Expected %t for %s", test.expected, test.msg)
|
|
|
|
require.Equal(t, test.expected, actual, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 16:35:49 +00:00
|
|
|
func TestReceiversMatchFilter(t *testing.T) {
|
2020-06-11 13:51:10 +00:00
|
|
|
receivers := []string{"pagerduty", "slack", "pushover"}
|
2017-11-12 16:35:49 +00:00
|
|
|
|
2020-06-11 13:51:10 +00:00
|
|
|
filter, err := regexp.Compile(fmt.Sprintf("^(?:%s)$", "push.*"))
|
2017-11-12 16:35:49 +00:00
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-12 16:35:49 +00:00
|
|
|
}
|
|
|
|
require.True(t, receiversMatchFilter(receivers, filter))
|
|
|
|
|
2020-06-11 13:51:10 +00:00
|
|
|
filter, err = regexp.Compile(fmt.Sprintf("^(?:%s)$", "push"))
|
2017-11-12 16:35:49 +00:00
|
|
|
if err != nil {
|
2018-02-13 15:26:34 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2017-11-12 16:35:49 +00:00
|
|
|
}
|
|
|
|
require.False(t, receiversMatchFilter(receivers, filter))
|
|
|
|
}
|
2017-11-15 19:29:06 +00:00
|
|
|
|
2018-03-20 09:08:20 +00:00
|
|
|
func TestMatchFilterLabels(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
matcher labels.MatchType
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{labels.MatchEqual, true},
|
|
|
|
{labels.MatchRegexp, true},
|
|
|
|
{labels.MatchNotEqual, false},
|
|
|
|
{labels.MatchNotRegexp, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
l, err := labels.NewMatcher(tc.matcher, "foo", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
sms := map[string]string{
|
|
|
|
"baz": "bar",
|
|
|
|
}
|
|
|
|
ls := []*labels.Matcher{l}
|
|
|
|
|
|
|
|
require.Equal(t, tc.expected, matchFilterLabels(ls, sms))
|
|
|
|
|
|
|
|
l, err = labels.NewMatcher(tc.matcher, "foo", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
sms = map[string]string{
|
|
|
|
"baz": "bar",
|
|
|
|
"foo": "quux",
|
|
|
|
}
|
|
|
|
ls = []*labels.Matcher{l}
|
|
|
|
require.NotEqual(t, tc.expected, matchFilterLabels(ls, sms))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:11:28 +00:00
|
|
|
func newMatcher(labelSet model.LabelSet) labels.Matchers {
|
|
|
|
matchers := make([]*labels.Matcher, 0, len(labelSet))
|
2017-11-15 19:29:06 +00:00
|
|
|
for key, val := range labelSet {
|
2021-01-13 14:11:28 +00:00
|
|
|
matchers = append(matchers, &labels.Matcher{
|
|
|
|
Type: labels.MatchEqual,
|
|
|
|
Name: string(key),
|
|
|
|
Value: string(val),
|
|
|
|
})
|
2017-11-15 19:29:06 +00:00
|
|
|
}
|
|
|
|
return matchers
|
|
|
|
}
|