2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-08-09 17:32:55 +00:00
|
|
|
// 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.
|
|
|
|
|
2016-03-01 11:37:22 +00:00
|
|
|
package notifier
|
2013-08-09 17:32:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-18 08:50:37 +00:00
|
|
|
"bytes"
|
2017-10-25 04:21:42 +00:00
|
|
|
"context"
|
2018-01-16 11:25:52 +00:00
|
|
|
"crypto/md5"
|
2015-12-10 15:31:50 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-02-27 19:31:16 +00:00
|
|
|
"io/ioutil"
|
2013-08-09 17:32:55 +00:00
|
|
|
"net/http"
|
2015-12-10 15:31:50 +00:00
|
|
|
"net/http/httptest"
|
2017-04-25 05:42:33 +00:00
|
|
|
"net/url"
|
2013-08-09 17:32:55 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-12-18 11:26:36 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/relabel"
|
|
|
|
|
2017-12-30 20:28:34 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2017-02-27 19:31:16 +00:00
|
|
|
|
2018-01-11 15:10:25 +00:00
|
|
|
config_util "github.com/prometheus/common/config"
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2016-08-09 08:08:15 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 20:01:34 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2016-12-29 15:53:11 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2017-12-30 20:28:34 +00:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2013-08-09 17:32:55 +00:00
|
|
|
)
|
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
func TestPostPath(t *testing.T) {
|
2016-02-04 10:56:14 +00:00
|
|
|
var cases = []struct {
|
|
|
|
in, out string
|
|
|
|
}{
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "",
|
|
|
|
out: "/api/v1/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "/",
|
|
|
|
out: "/api/v1/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "/prefix",
|
|
|
|
out: "/prefix/api/v1/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "/prefix//",
|
|
|
|
out: "/prefix/api/v1/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "prefix//",
|
|
|
|
out: "/prefix/api/v1/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2019-04-18 12:17:03 +00:00
|
|
|
testutil.Equals(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV1))
|
2016-02-04 10:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
func TestHandlerNextBatch(t *testing.T) {
|
2018-01-30 17:45:37 +00:00
|
|
|
h := NewManager(&Options{}, nil)
|
2015-12-10 15:31:50 +00:00
|
|
|
|
|
|
|
for i := range make([]struct{}, 2*maxBatchSize+1) {
|
2016-12-29 15:53:11 +00:00
|
|
|
h.queue = append(h.queue, &Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", fmt.Sprintf("%d", i)),
|
2015-12-10 15:31:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
expected := append([]*Alert{}, h.queue...)
|
2015-12-10 15:31:50 +00:00
|
|
|
|
|
|
|
b := h.nextBatch()
|
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Equals(t, maxBatchSize, len(b))
|
|
|
|
|
|
|
|
testutil.Assert(t, alertsEqual(expected[0:maxBatchSize], b), "First batch did not match")
|
2015-12-10 15:31:50 +00:00
|
|
|
|
|
|
|
b = h.nextBatch()
|
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Equals(t, maxBatchSize, len(b))
|
|
|
|
|
|
|
|
testutil.Assert(t, alertsEqual(expected[maxBatchSize:2*maxBatchSize], b), "Second batch did not match")
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
b = h.nextBatch()
|
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Equals(t, 1, len(b))
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, alertsEqual(expected[2*maxBatchSize:], b), "Third batch did not match")
|
|
|
|
|
|
|
|
testutil.Assert(t, len(h.queue) == 0, "Expected queue to be empty but got %d alerts", len(h.queue))
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
func alertsEqual(a, b []*Alert) bool {
|
2015-12-10 15:31:50 +00:00
|
|
|
if len(a) != len(b) {
|
2016-12-29 15:53:11 +00:00
|
|
|
fmt.Println("len mismatch")
|
2015-12-10 15:31:50 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, alert := range a {
|
2016-12-29 15:53:11 +00:00
|
|
|
if !labels.Equal(alert.Labels, b[i].Labels) {
|
|
|
|
fmt.Println("mismatch", alert.Labels, b[i].Labels)
|
2015-12-10 15:31:50 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
func TestHandlerSendAll(t *testing.T) {
|
2015-12-10 15:31:50 +00:00
|
|
|
var (
|
2016-12-29 15:53:11 +00:00
|
|
|
expected []*Alert
|
2016-06-02 12:25:19 +00:00
|
|
|
status1, status2 int
|
2015-12-10 15:31:50 +00:00
|
|
|
)
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
f := func(w http.ResponseWriter, r *http.Request) {
|
2015-12-10 15:31:50 +00:00
|
|
|
defer r.Body.Close()
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
var alerts []*Alert
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Ok(t, json.NewDecoder(r.Body).Decode(&alerts))
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, alertsEqual(alerts, expected), "Unexpected alerts received %v exp %v", alerts, expected)
|
2016-06-02 12:25:19 +00:00
|
|
|
}
|
|
|
|
server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-12-12 13:40:00 +00:00
|
|
|
user, pass, _ := r.BasicAuth()
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(
|
|
|
|
t,
|
|
|
|
user == "prometheus" || pass == "testing_password",
|
|
|
|
"Incorrect auth details for an alertmanager",
|
|
|
|
)
|
2017-12-12 13:40:00 +00:00
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
f(w, r)
|
|
|
|
w.WriteHeader(status1)
|
|
|
|
}))
|
|
|
|
server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-12-12 13:40:00 +00:00
|
|
|
user, pass, _ := r.BasicAuth()
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(
|
|
|
|
t,
|
|
|
|
user == "" || pass == "",
|
|
|
|
"Incorrectly received auth details for an alertmanager",
|
|
|
|
)
|
2017-12-12 13:40:00 +00:00
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
f(w, r)
|
|
|
|
w.WriteHeader(status2)
|
2015-12-10 15:31:50 +00:00
|
|
|
}))
|
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
defer server1.Close()
|
|
|
|
defer server2.Close()
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2018-01-30 17:45:37 +00:00
|
|
|
h := NewManager(&Options{}, nil)
|
2017-12-12 13:40:00 +00:00
|
|
|
|
2018-04-25 17:19:06 +00:00
|
|
|
authClient, _ := config_util.NewClientFromConfig(config_util.HTTPClientConfig{
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 20:01:34 +00:00
|
|
|
BasicAuth: &config_util.BasicAuth{
|
2017-12-12 13:40:00 +00:00
|
|
|
Username: "prometheus",
|
|
|
|
Password: "testing_password",
|
|
|
|
},
|
|
|
|
}, "auth_alertmanager")
|
2017-12-30 17:47:18 +00:00
|
|
|
|
|
|
|
h.alertmanagers = make(map[string]*alertmanagerSet)
|
|
|
|
|
2019-04-18 12:17:03 +00:00
|
|
|
am1Cfg := config.DefaultAlertmanagerConfig
|
|
|
|
am1Cfg.Timeout = model.Duration(time.Second)
|
|
|
|
|
|
|
|
am2Cfg := config.DefaultAlertmanagerConfig
|
|
|
|
am2Cfg.Timeout = model.Duration(time.Second)
|
|
|
|
|
2017-12-30 17:47:18 +00:00
|
|
|
h.alertmanagers["1"] = &alertmanagerSet{
|
2016-11-23 16:03:22 +00:00
|
|
|
ams: []alertmanager{
|
2016-11-25 10:11:28 +00:00
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return server1.URL },
|
|
|
|
},
|
2017-12-12 13:40:00 +00:00
|
|
|
},
|
2019-04-18 12:17:03 +00:00
|
|
|
cfg: &am1Cfg,
|
2017-12-12 13:40:00 +00:00
|
|
|
client: authClient,
|
2017-12-30 17:47:18 +00:00
|
|
|
}
|
2017-12-12 13:40:00 +00:00
|
|
|
|
2017-12-30 17:47:18 +00:00
|
|
|
h.alertmanagers["2"] = &alertmanagerSet{
|
2017-12-12 13:40:00 +00:00
|
|
|
ams: []alertmanager{
|
2016-11-25 10:11:28 +00:00
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return server2.URL },
|
|
|
|
},
|
2016-11-23 16:03:22 +00:00
|
|
|
},
|
2019-04-18 12:17:03 +00:00
|
|
|
cfg: &am2Cfg,
|
2017-12-30 17:47:18 +00:00
|
|
|
}
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
for i := range make([]struct{}, maxBatchSize) {
|
2016-12-29 15:53:11 +00:00
|
|
|
h.queue = append(h.queue, &Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", fmt.Sprintf("%d", i)),
|
2015-12-10 15:31:50 +00:00
|
|
|
})
|
2016-12-29 15:53:11 +00:00
|
|
|
expected = append(expected, &Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", fmt.Sprintf("%d", i)),
|
2015-12-10 15:31:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
status1 = http.StatusOK
|
|
|
|
status2 = http.StatusOK
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
status1 = http.StatusNotFound
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
status2 = http.StatusInternalServerError
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, !h.sendAll(h.queue...), "all sends succeeded unexpectedly")
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 19:31:16 +00:00
|
|
|
func TestCustomDo(t *testing.T) {
|
|
|
|
const testURL = "http://testurl.com/"
|
|
|
|
const testBody = "testbody"
|
|
|
|
|
|
|
|
var received bool
|
2018-01-30 17:45:37 +00:00
|
|
|
h := NewManager(&Options{
|
2018-11-19 11:31:16 +00:00
|
|
|
Do: func(_ context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
|
2017-02-27 19:31:16 +00:00
|
|
|
received = true
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
2018-03-29 15:07:26 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
testutil.Equals(t, testBody, string(body))
|
|
|
|
|
|
|
|
testutil.Equals(t, testURL, req.URL.String())
|
|
|
|
|
2017-02-27 19:31:16 +00:00
|
|
|
return &http.Response{
|
2019-04-18 08:50:37 +00:00
|
|
|
Body: ioutil.NopCloser(bytes.NewBuffer(nil)),
|
2017-02-27 19:31:16 +00:00
|
|
|
}, nil
|
|
|
|
},
|
2017-08-11 18:45:52 +00:00
|
|
|
}, nil)
|
2017-02-27 19:31:16 +00:00
|
|
|
|
|
|
|
h.sendOne(context.Background(), nil, testURL, []byte(testBody))
|
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, received, "Expected to receive an alert, but didn't")
|
2017-02-27 19:31:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 12:34:56 +00:00
|
|
|
func TestExternalLabels(t *testing.T) {
|
2018-01-30 17:45:37 +00:00
|
|
|
h := NewManager(&Options{
|
2016-09-27 12:34:56 +00:00
|
|
|
QueueCapacity: 3 * maxBatchSize,
|
2019-03-08 16:29:25 +00:00
|
|
|
ExternalLabels: labels.Labels{{Name: "a", Value: "b"}},
|
2018-12-18 11:26:36 +00:00
|
|
|
RelabelConfigs: []*relabel.Config{
|
2016-09-27 12:34:56 +00:00
|
|
|
{
|
|
|
|
SourceLabels: model.LabelNames{"alertname"},
|
|
|
|
TargetLabel: "a",
|
|
|
|
Action: "replace",
|
2018-12-18 11:26:36 +00:00
|
|
|
Regex: relabel.MustNewRegexp("externalrelabelthis"),
|
2016-09-27 12:34:56 +00:00
|
|
|
Replacement: "c",
|
|
|
|
},
|
|
|
|
},
|
2017-08-11 18:45:52 +00:00
|
|
|
}, nil)
|
2016-09-27 12:34:56 +00:00
|
|
|
|
|
|
|
// This alert should get the external label attached.
|
2016-12-29 15:53:11 +00:00
|
|
|
h.Send(&Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", "test"),
|
2016-09-27 12:34:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// This alert should get the external label attached, but then set to "c"
|
|
|
|
// through relabelling.
|
2016-12-29 15:53:11 +00:00
|
|
|
h.Send(&Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", "externalrelabelthis"),
|
2016-09-27 12:34:56 +00:00
|
|
|
})
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
expected := []*Alert{
|
|
|
|
{Labels: labels.FromStrings("alertname", "test", "a", "b")},
|
|
|
|
{Labels: labels.FromStrings("alertname", "externalrelabelthis", "a", "c")},
|
2016-09-27 12:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, alertsEqual(expected, h.queue), "Expected alerts %v, got %v", expected, h.queue)
|
2016-09-27 12:34:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 08:08:15 +00:00
|
|
|
func TestHandlerRelabel(t *testing.T) {
|
2018-01-30 17:45:37 +00:00
|
|
|
h := NewManager(&Options{
|
2016-08-09 08:08:15 +00:00
|
|
|
QueueCapacity: 3 * maxBatchSize,
|
2018-12-18 11:26:36 +00:00
|
|
|
RelabelConfigs: []*relabel.Config{
|
2016-09-15 03:13:27 +00:00
|
|
|
{
|
2016-08-09 08:08:15 +00:00
|
|
|
SourceLabels: model.LabelNames{"alertname"},
|
|
|
|
Action: "drop",
|
2018-12-18 11:26:36 +00:00
|
|
|
Regex: relabel.MustNewRegexp("drop"),
|
2016-08-09 08:08:15 +00:00
|
|
|
},
|
2016-09-15 03:13:27 +00:00
|
|
|
{
|
2016-08-09 08:08:15 +00:00
|
|
|
SourceLabels: model.LabelNames{"alertname"},
|
|
|
|
TargetLabel: "alertname",
|
|
|
|
Action: "replace",
|
2018-12-18 11:26:36 +00:00
|
|
|
Regex: relabel.MustNewRegexp("rename"),
|
2016-08-09 08:08:15 +00:00
|
|
|
Replacement: "renamed",
|
|
|
|
},
|
|
|
|
},
|
2017-08-11 18:45:52 +00:00
|
|
|
}, nil)
|
2016-08-09 08:08:15 +00:00
|
|
|
|
|
|
|
// This alert should be dropped due to the configuration
|
2016-12-29 15:53:11 +00:00
|
|
|
h.Send(&Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", "drop"),
|
2016-08-09 08:08:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// This alert should be replaced due to the configuration
|
2016-12-29 15:53:11 +00:00
|
|
|
h.Send(&Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", "rename"),
|
2016-08-09 08:08:15 +00:00
|
|
|
})
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
expected := []*Alert{
|
|
|
|
{Labels: labels.FromStrings("alertname", "renamed")},
|
2016-08-09 08:08:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, alertsEqual(expected, h.queue), "Expected alerts %v, got %v", expected, h.queue)
|
2016-08-09 08:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandlerQueueing(t *testing.T) {
|
2015-12-10 15:31:50 +00:00
|
|
|
var (
|
|
|
|
unblock = make(chan struct{})
|
|
|
|
called = make(chan struct{})
|
2016-12-29 15:53:11 +00:00
|
|
|
expected []*Alert
|
2015-12-10 15:31:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
called <- struct{}{}
|
|
|
|
<-unblock
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
var alerts []*Alert
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Ok(t, json.NewDecoder(r.Body).Decode(&alerts))
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Assert(t, alertsEqual(expected, alerts), "Expected alerts %v, got %v", expected, alerts)
|
2015-12-10 15:31:50 +00:00
|
|
|
}))
|
|
|
|
|
2018-01-30 17:45:37 +00:00
|
|
|
h := NewManager(&Options{
|
2016-11-23 16:03:22 +00:00
|
|
|
QueueCapacity: 3 * maxBatchSize,
|
2017-04-28 15:12:38 +00:00
|
|
|
},
|
2017-08-11 18:45:52 +00:00
|
|
|
nil,
|
2017-04-28 15:12:38 +00:00
|
|
|
)
|
2017-12-30 17:47:18 +00:00
|
|
|
|
|
|
|
h.alertmanagers = make(map[string]*alertmanagerSet)
|
|
|
|
|
2019-04-18 12:17:03 +00:00
|
|
|
am1Cfg := config.DefaultAlertmanagerConfig
|
|
|
|
am1Cfg.Timeout = model.Duration(time.Second)
|
|
|
|
|
2017-12-30 17:47:18 +00:00
|
|
|
h.alertmanagers["1"] = &alertmanagerSet{
|
2016-11-23 16:03:22 +00:00
|
|
|
ams: []alertmanager{
|
2016-11-25 10:11:28 +00:00
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return server.URL },
|
|
|
|
},
|
2016-11-23 16:03:22 +00:00
|
|
|
},
|
2019-04-18 12:17:03 +00:00
|
|
|
cfg: &am1Cfg,
|
2017-12-30 17:47:18 +00:00
|
|
|
}
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
var alerts []*Alert
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
for i := range make([]struct{}, 20*maxBatchSize) {
|
2016-12-29 15:53:11 +00:00
|
|
|
alerts = append(alerts, &Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", fmt.Sprintf("%d", i)),
|
2015-12-10 15:31:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-30 17:47:18 +00:00
|
|
|
c := make(chan map[string][]*targetgroup.Group)
|
|
|
|
go h.Run(c)
|
2015-12-10 15:31:50 +00:00
|
|
|
defer h.Stop()
|
|
|
|
|
|
|
|
h.Send(alerts[:4*maxBatchSize]...)
|
|
|
|
|
|
|
|
// If the batch is larger than the queue size, the front should be truncated
|
|
|
|
// from the front. Thus, we start at i=1.
|
|
|
|
for i := 1; i < 4; i++ {
|
|
|
|
select {
|
|
|
|
case <-called:
|
|
|
|
expected = alerts[i*maxBatchSize : (i+1)*maxBatchSize]
|
|
|
|
unblock <- struct{}{}
|
2016-01-25 17:31:54 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-12-10 15:31:50 +00:00
|
|
|
t.Fatalf("Alerts were not pushed")
|
|
|
|
}
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
// Send one batch, wait for it to arrive and block so the queue fills up.
|
|
|
|
// Then check whether the queue is truncated in the front once its full.
|
|
|
|
h.Send(alerts[:maxBatchSize]...)
|
|
|
|
<-called
|
|
|
|
|
|
|
|
// Fill the 3*maxBatchSize queue.
|
|
|
|
h.Send(alerts[1*maxBatchSize : 2*maxBatchSize]...)
|
|
|
|
h.Send(alerts[2*maxBatchSize : 3*maxBatchSize]...)
|
|
|
|
h.Send(alerts[3*maxBatchSize : 4*maxBatchSize]...)
|
|
|
|
|
|
|
|
// Send the batch that drops the first one.
|
|
|
|
h.Send(alerts[4*maxBatchSize : 5*maxBatchSize]...)
|
|
|
|
|
|
|
|
expected = alerts[:maxBatchSize]
|
|
|
|
unblock <- struct{}{}
|
|
|
|
|
2019-05-06 07:02:40 +00:00
|
|
|
for i := 2; i < 5; i++ {
|
2015-12-10 15:31:50 +00:00
|
|
|
select {
|
|
|
|
case <-called:
|
|
|
|
expected = alerts[i*maxBatchSize : (i+1)*maxBatchSize]
|
|
|
|
unblock <- struct{}{}
|
2016-01-25 17:31:54 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-12-10 15:31:50 +00:00
|
|
|
t.Fatalf("Alerts were not pushed")
|
|
|
|
}
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-25 10:11:28 +00:00
|
|
|
|
|
|
|
type alertmanagerMock struct {
|
|
|
|
urlf func() string
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:42:33 +00:00
|
|
|
func (a alertmanagerMock) url() *url.URL {
|
|
|
|
u, err := url.Parse(a.urlf())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return u
|
2016-11-25 10:11:28 +00:00
|
|
|
}
|
2017-05-17 14:24:22 +00:00
|
|
|
|
|
|
|
func TestLabelSetNotReused(t *testing.T) {
|
|
|
|
tg := makeInputTargetGroup()
|
2018-02-21 09:00:07 +00:00
|
|
|
_, _, err := alertmanagerFromGroup(tg, &config.AlertmanagerConfig{})
|
2017-05-17 14:24:22 +00:00
|
|
|
|
2018-03-29 15:07:26 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
// Target modified during alertmanager extraction
|
|
|
|
testutil.Equals(t, tg, makeInputTargetGroup())
|
2017-05-17 14:24:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 20:28:34 +00:00
|
|
|
func TestReload(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
in *targetgroup.Group
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: &targetgroup.Group{
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": "alertmanager:9093",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: "http://alertmanager:9093/api/v1/alerts",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:45:37 +00:00
|
|
|
n := NewManager(&Options{}, nil)
|
2017-12-30 20:28:34 +00:00
|
|
|
|
|
|
|
cfg := &config.Config{}
|
|
|
|
s := `
|
|
|
|
alerting:
|
|
|
|
alertmanagers:
|
|
|
|
- static_configs:
|
|
|
|
`
|
2018-04-04 08:07:39 +00:00
|
|
|
if err := yaml.UnmarshalStrict([]byte(s), cfg); err != nil {
|
2017-12-30 20:28:34 +00:00
|
|
|
t.Fatalf("Unable to load YAML config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n.ApplyConfig(cfg); err != nil {
|
|
|
|
t.Fatalf("Error Applying the config:%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tgs := make(map[string][]*targetgroup.Group)
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
2018-01-16 11:25:52 +00:00
|
|
|
b, err := json.Marshal(cfg.AlertingConfig.AlertmanagerConfigs[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating config hash:%v", err)
|
|
|
|
}
|
|
|
|
tgs[fmt.Sprintf("%x", md5.Sum(b))] = []*targetgroup.Group{
|
2017-12-30 20:28:34 +00:00
|
|
|
tt.in,
|
|
|
|
}
|
|
|
|
n.reload(tgs)
|
|
|
|
res := n.Alertmanagers()[0].String()
|
|
|
|
|
|
|
|
testutil.Equals(t, res, tt.out)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-21 09:00:07 +00:00
|
|
|
func TestDroppedAlertmanagers(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
in *targetgroup.Group
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: &targetgroup.Group{
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": "alertmanager:9093",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: "http://alertmanager:9093/api/v1/alerts",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
n := NewManager(&Options{}, nil)
|
|
|
|
|
|
|
|
cfg := &config.Config{}
|
|
|
|
s := `
|
|
|
|
alerting:
|
|
|
|
alertmanagers:
|
|
|
|
- static_configs:
|
|
|
|
relabel_configs:
|
|
|
|
- source_labels: ['__address__']
|
|
|
|
regex: 'alertmanager:9093'
|
|
|
|
action: drop
|
|
|
|
`
|
2018-04-04 08:07:39 +00:00
|
|
|
if err := yaml.UnmarshalStrict([]byte(s), cfg); err != nil {
|
2018-02-21 09:00:07 +00:00
|
|
|
t.Fatalf("Unable to load YAML config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n.ApplyConfig(cfg); err != nil {
|
|
|
|
t.Fatalf("Error Applying the config:%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tgs := make(map[string][]*targetgroup.Group)
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
|
|
|
b, err := json.Marshal(cfg.AlertingConfig.AlertmanagerConfigs[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating config hash:%v", err)
|
|
|
|
}
|
|
|
|
tgs[fmt.Sprintf("%x", md5.Sum(b))] = []*targetgroup.Group{
|
|
|
|
tt.in,
|
|
|
|
}
|
|
|
|
n.reload(tgs)
|
|
|
|
res := n.DroppedAlertmanagers()[0].String()
|
|
|
|
|
|
|
|
testutil.Equals(t, res, tt.out)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 20:01:34 +00:00
|
|
|
func makeInputTargetGroup() *targetgroup.Group {
|
|
|
|
return &targetgroup.Group{
|
2017-05-17 14:24:22 +00:00
|
|
|
Targets: []model.LabelSet{
|
2017-08-29 08:00:11 +00:00
|
|
|
{
|
2017-05-17 14:24:22 +00:00
|
|
|
model.AddressLabel: model.LabelValue("1.1.1.1:9090"),
|
|
|
|
model.LabelName("notcommon1"): model.LabelValue("label"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
model.LabelName("common"): model.LabelValue("label"),
|
|
|
|
},
|
|
|
|
Source: "testsource",
|
|
|
|
}
|
|
|
|
}
|