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"
|
2015-12-10 15:31:50 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-27 09:24:36 +00:00
|
|
|
"io"
|
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"
|
2022-07-01 12:23:23 +00:00
|
|
|
"strconv"
|
2013-08-09 17:32:55 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-07-01 07:51:32 +00:00
|
|
|
"github.com/prometheus/alertmanager/api/v2/models"
|
2024-05-31 14:40:09 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
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"
|
2024-09-10 01:41:53 +00:00
|
|
|
"github.com/prometheus/common/promslog"
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-30 07:45:42 +00:00
|
|
|
"go.uber.org/atomic"
|
2022-08-31 13:50:38 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-09-02 14:05:02 +00:00
|
|
|
|
2024-05-31 14:40:09 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
|
|
|
|
2016-08-09 08:08:15 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2024-09-26 12:53:48 +00:00
|
|
|
_ "github.com/prometheus/prometheus/discovery/file"
|
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"
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
|
|
|
"github.com/prometheus/prometheus/model/relabel"
|
2013-08-09 17:32:55 +00:00
|
|
|
)
|
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
func TestPostPath(t *testing.T) {
|
2021-10-22 08:06:44 +00:00
|
|
|
cases := []struct {
|
2016-02-04 10:56:14 +00:00
|
|
|
in, out string
|
|
|
|
}{
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "",
|
2024-10-18 13:23:14 +00:00
|
|
|
out: "/api/v2/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "/",
|
2024-10-18 13:23:14 +00:00
|
|
|
out: "/api/v2/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "/prefix",
|
2024-10-18 13:23:14 +00:00
|
|
|
out: "/prefix/api/v2/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "/prefix//",
|
2024-10-18 13:23:14 +00:00
|
|
|
out: "/prefix/api/v2/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-23 16:03:22 +00:00
|
|
|
in: "prefix//",
|
2024-10-18 13:23:14 +00:00
|
|
|
out: "/prefix/api/v2/alerts",
|
2016-02-04 10:56:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2024-10-18 13:23:14 +00:00
|
|
|
require.Equal(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV2))
|
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{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", strconv.Itoa(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
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, alertsEqual(expected[0:maxBatchSize], h.nextBatch()))
|
|
|
|
require.NoError(t, alertsEqual(expected[maxBatchSize:2*maxBatchSize], h.nextBatch()))
|
|
|
|
require.NoError(t, alertsEqual(expected[2*maxBatchSize:], h.nextBatch()))
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Empty(t, h.queue, "Expected queue to be empty but got %d alerts", len(h.queue))
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
func alertsEqual(a, b []*Alert) error {
|
2015-12-10 15:31:50 +00:00
|
|
|
if len(a) != len(b) {
|
2022-06-16 08:38:27 +00:00
|
|
|
return fmt.Errorf("length mismatch: %v != %v", a, b)
|
2015-12-10 15:31:50 +00:00
|
|
|
}
|
|
|
|
for i, alert := range a {
|
2016-12-29 15:53:11 +00:00
|
|
|
if !labels.Equal(alert.Labels, b[i].Labels) {
|
2022-06-16 08:38:27 +00:00
|
|
|
return fmt.Errorf("label mismatch at index %d: %s != %s", i, alert.Labels, b[i].Labels)
|
2015-12-10 15:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-02 14:05:02 +00:00
|
|
|
return nil
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 21:44:23 +00:00
|
|
|
func newTestHTTPServerBuilder(expected *[]*Alert, errc chan<- error, u, p string, status *atomic.Int32) *httptest.Server {
|
|
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case errc <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
user, pass, _ := r.BasicAuth()
|
|
|
|
if user != u || pass != p {
|
|
|
|
err = fmt.Errorf("unexpected user/password: %s/%s != %s/%s", user, pass, u, p)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2024-03-04 19:15:07 +00:00
|
|
|
err = fmt.Errorf("error reading body: %w", err)
|
2023-07-11 21:44:23 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var alerts []*Alert
|
|
|
|
err = json.Unmarshal(b, &alerts)
|
|
|
|
if err == nil {
|
|
|
|
err = alertsEqual(*expected, alerts)
|
|
|
|
}
|
|
|
|
w.WriteHeader(int(status.Load()))
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
func TestHandlerSendAll(t *testing.T) {
|
2015-12-10 15:31:50 +00:00
|
|
|
var (
|
2019-09-02 14:05:02 +00:00
|
|
|
errc = make(chan error, 1)
|
|
|
|
expected = make([]*Alert, 0, maxBatchSize)
|
2020-07-30 07:45:42 +00:00
|
|
|
status1, status2 atomic.Int32
|
2015-12-10 15:31:50 +00:00
|
|
|
)
|
2020-07-30 07:45:42 +00:00
|
|
|
status1.Store(int32(http.StatusOK))
|
|
|
|
status2.Store(int32(http.StatusOK))
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2023-07-11 21:44:23 +00:00
|
|
|
server1 := newTestHTTPServerBuilder(&expected, errc, "prometheus", "testing_password", &status1)
|
|
|
|
server2 := newTestHTTPServerBuilder(&expected, errc, "", "", &status2)
|
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
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
authClient, _ := config_util.NewClientFromConfig(
|
|
|
|
config_util.HTTPClientConfig{
|
|
|
|
BasicAuth: &config_util.BasicAuth{
|
|
|
|
Username: "prometheus",
|
|
|
|
Password: "testing_password",
|
|
|
|
},
|
2021-09-26 21:16:12 +00:00
|
|
|
}, "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{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", strconv.Itoa(i)),
|
2015-12-10 15:31:50 +00:00
|
|
|
})
|
2016-12-29 15:53:11 +00:00
|
|
|
expected = append(expected, &Alert{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", strconv.Itoa(i)),
|
2015-12-10 15:31:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
checkNoErr := func() {
|
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-02 14:05:02 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.True(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
|
2019-09-02 14:05:02 +00:00
|
|
|
checkNoErr()
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2020-07-30 07:45:42 +00:00
|
|
|
status1.Store(int32(http.StatusNotFound))
|
2020-10-29 09:43:23 +00:00
|
|
|
require.True(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
|
2019-09-02 14:05:02 +00:00
|
|
|
checkNoErr()
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2020-07-30 07:45:42 +00:00
|
|
|
status2.Store(int32(http.StatusInternalServerError))
|
2020-10-29 09:43:23 +00:00
|
|
|
require.False(t, h.sendAll(h.queue...), "all sends succeeded unexpectedly")
|
2019-09-02 14:05:02 +00:00
|
|
|
checkNoErr()
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 21:44:23 +00:00
|
|
|
func TestHandlerSendAllRemapPerAm(t *testing.T) {
|
|
|
|
var (
|
|
|
|
errc = make(chan error, 1)
|
|
|
|
expected1 = make([]*Alert, 0, maxBatchSize)
|
|
|
|
expected2 = make([]*Alert, 0, maxBatchSize)
|
2024-03-29 13:32:39 +00:00
|
|
|
expected3 = make([]*Alert, 0)
|
2023-07-11 21:44:23 +00:00
|
|
|
|
2024-03-29 13:32:39 +00:00
|
|
|
statusOK atomic.Int32
|
2023-07-11 21:44:23 +00:00
|
|
|
)
|
2024-03-29 13:32:39 +00:00
|
|
|
statusOK.Store(int32(http.StatusOK))
|
2023-07-11 21:44:23 +00:00
|
|
|
|
2024-03-29 13:32:39 +00:00
|
|
|
server1 := newTestHTTPServerBuilder(&expected1, errc, "", "", &statusOK)
|
|
|
|
server2 := newTestHTTPServerBuilder(&expected2, errc, "", "", &statusOK)
|
|
|
|
server3 := newTestHTTPServerBuilder(&expected3, errc, "", "", &statusOK)
|
2023-07-11 21:44:23 +00:00
|
|
|
|
|
|
|
defer server1.Close()
|
|
|
|
defer server2.Close()
|
2024-03-29 13:32:39 +00:00
|
|
|
defer server3.Close()
|
2023-07-11 21:44:23 +00:00
|
|
|
|
|
|
|
h := NewManager(&Options{}, nil)
|
|
|
|
h.alertmanagers = make(map[string]*alertmanagerSet)
|
|
|
|
|
|
|
|
am1Cfg := config.DefaultAlertmanagerConfig
|
|
|
|
am1Cfg.Timeout = model.Duration(time.Second)
|
|
|
|
|
|
|
|
am2Cfg := config.DefaultAlertmanagerConfig
|
|
|
|
am2Cfg.Timeout = model.Duration(time.Second)
|
|
|
|
am2Cfg.AlertRelabelConfigs = []*relabel.Config{
|
|
|
|
{
|
|
|
|
SourceLabels: model.LabelNames{"alertnamedrop"},
|
|
|
|
Action: "drop",
|
|
|
|
Regex: relabel.MustNewRegexp(".+"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-29 13:32:39 +00:00
|
|
|
am3Cfg := config.DefaultAlertmanagerConfig
|
|
|
|
am3Cfg.Timeout = model.Duration(time.Second)
|
|
|
|
am3Cfg.AlertRelabelConfigs = []*relabel.Config{
|
|
|
|
{
|
|
|
|
SourceLabels: model.LabelNames{"alertname"},
|
|
|
|
Action: "drop",
|
|
|
|
Regex: relabel.MustNewRegexp(".+"),
|
2023-07-11 21:44:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-29 13:32:39 +00:00
|
|
|
h.alertmanagers = map[string]*alertmanagerSet{
|
|
|
|
// Drop no alerts.
|
|
|
|
"1": {
|
|
|
|
ams: []alertmanager{
|
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return server1.URL },
|
|
|
|
},
|
2023-07-11 21:44:23 +00:00
|
|
|
},
|
2024-03-29 13:32:39 +00:00
|
|
|
cfg: &am1Cfg,
|
|
|
|
},
|
|
|
|
// Drop only alerts with the "alertnamedrop" label.
|
|
|
|
"2": {
|
|
|
|
ams: []alertmanager{
|
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return server2.URL },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cfg: &am2Cfg,
|
|
|
|
},
|
|
|
|
// Drop all alerts.
|
|
|
|
"3": {
|
|
|
|
ams: []alertmanager{
|
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return server3.URL },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cfg: &am3Cfg,
|
|
|
|
},
|
|
|
|
// Empty list of Alertmanager endpoints.
|
|
|
|
"4": {
|
|
|
|
ams: []alertmanager{},
|
|
|
|
cfg: &config.DefaultAlertmanagerConfig,
|
2023-07-11 21:44:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range make([]struct{}, maxBatchSize/2) {
|
2024-03-29 13:32:39 +00:00
|
|
|
h.queue = append(h.queue,
|
|
|
|
&Alert{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", strconv.Itoa(i)),
|
2024-03-29 13:32:39 +00:00
|
|
|
},
|
|
|
|
&Alert{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", "test", "alertnamedrop", strconv.Itoa(i)),
|
2024-03-29 13:32:39 +00:00
|
|
|
},
|
|
|
|
)
|
2023-07-11 21:44:23 +00:00
|
|
|
|
2024-03-29 13:32:39 +00:00
|
|
|
expected1 = append(expected1,
|
|
|
|
&Alert{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", strconv.Itoa(i)),
|
2024-03-29 13:32:39 +00:00
|
|
|
}, &Alert{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", "test", "alertnamedrop", strconv.Itoa(i)),
|
2024-03-29 13:32:39 +00:00
|
|
|
},
|
|
|
|
)
|
2023-07-11 21:44:23 +00:00
|
|
|
|
|
|
|
expected2 = append(expected2, &Alert{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", strconv.Itoa(i)),
|
2023-07-11 21:44:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
checkNoErr := func() {
|
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
require.NoError(t, err)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
|
|
|
|
checkNoErr()
|
2024-03-29 13:32:39 +00:00
|
|
|
|
|
|
|
// Verify that individual locks are released.
|
|
|
|
for k := range h.alertmanagers {
|
|
|
|
h.alertmanagers[k].mtx.Lock()
|
|
|
|
h.alertmanagers[k].ams = nil
|
|
|
|
h.alertmanagers[k].mtx.Unlock()
|
|
|
|
}
|
2023-07-11 21:44:23 +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
|
2022-04-27 09:24:36 +00:00
|
|
|
body, err := io.ReadAll(req.Body)
|
2018-03-29 15:07:26 +00:00
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-29 15:07:26 +00:00
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, testBody, string(body))
|
2018-03-29 15:07:26 +00:00
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, testURL, req.URL.String())
|
2018-03-29 15:07:26 +00:00
|
|
|
|
2017-02-27 19:31:16 +00:00
|
|
|
return &http.Response{
|
2022-04-27 09:24:36 +00:00
|
|
|
Body: io.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))
|
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.True(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,
|
2022-03-09 22:21:29 +00:00
|
|
|
ExternalLabels: labels.FromStrings("a", "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
|
|
|
}
|
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, alertsEqual(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
|
|
|
}
|
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, alertsEqual(expected, h.queue))
|
2016-08-09 08:08:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 14:54:09 +00:00
|
|
|
func TestHandlerQueuing(t *testing.T) {
|
2015-12-10 15:31:50 +00:00
|
|
|
var (
|
2019-09-02 14:05:02 +00:00
|
|
|
expectedc = make(chan []*Alert)
|
|
|
|
called = make(chan struct{})
|
|
|
|
done = make(chan struct{})
|
|
|
|
errc = make(chan error, 1)
|
2015-12-10 15:31:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-09-02 14:05:02 +00:00
|
|
|
// Notify the test function that we have received something.
|
|
|
|
select {
|
|
|
|
case called <- struct{}{}:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
// Wait for the test function to unblock us.
|
|
|
|
select {
|
|
|
|
case expected := <-expectedc:
|
|
|
|
var alerts []*Alert
|
2021-07-02 08:38:14 +00:00
|
|
|
|
2022-04-27 09:24:36 +00:00
|
|
|
b, err := io.ReadAll(r.Body)
|
2021-07-02 08:38:14 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, &alerts)
|
2019-09-02 14:05:02 +00:00
|
|
|
if err == nil {
|
|
|
|
err = alertsEqual(expected, alerts)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case errc <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
}
|
2015-12-10 15:31:50 +00:00
|
|
|
}))
|
2019-09-02 14:05:02 +00:00
|
|
|
defer func() {
|
|
|
|
close(done)
|
|
|
|
server.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
h := NewManager(
|
|
|
|
&Options{
|
|
|
|
QueueCapacity: 3 * maxBatchSize,
|
|
|
|
},
|
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
|
|
|
}
|
2019-09-02 14:05:02 +00:00
|
|
|
go h.Run(nil)
|
|
|
|
defer h.Stop()
|
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{
|
2024-05-13 15:36:19 +00:00
|
|
|
Labels: labels.FromStrings("alertname", strconv.Itoa(i)),
|
2015-12-10 15:31:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
assertAlerts := func(expected []*Alert) {
|
|
|
|
t.Helper()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-called:
|
|
|
|
expectedc <- expected
|
|
|
|
case err := <-errc:
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-02 14:05:02 +00:00
|
|
|
return
|
|
|
|
case <-time.After(5 * time.Second):
|
2021-09-13 19:19:20 +00:00
|
|
|
require.FailNow(t, "Alerts were not pushed.")
|
2019-09-02 14:05:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
// If the batch is larger than the queue capacity, it should be truncated
|
|
|
|
// from the front.
|
2015-12-10 15:31:50 +00:00
|
|
|
h.Send(alerts[:4*maxBatchSize]...)
|
|
|
|
for i := 1; i < 4; i++ {
|
2019-09-02 14:05:02 +00:00
|
|
|
assertAlerts(alerts[i*maxBatchSize : (i+1)*maxBatchSize])
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
// Send one batch, wait for it to arrive and block the server so the queue fills up.
|
2015-12-10 15:31:50 +00:00
|
|
|
h.Send(alerts[:maxBatchSize]...)
|
|
|
|
<-called
|
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
// Send several batches while the server is still blocked so the queue
|
|
|
|
// fills up to its maximum capacity (3*maxBatchSize). Then check that the
|
|
|
|
// queue is truncated in the front.
|
|
|
|
h.Send(alerts[1*maxBatchSize : 2*maxBatchSize]...) // this batch should be dropped.
|
2015-12-10 15:31:50 +00:00
|
|
|
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]...)
|
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
// Unblock the server.
|
|
|
|
expectedc <- alerts[:maxBatchSize]
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-02 14:05:02 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2021-09-13 19:19:20 +00:00
|
|
|
require.FailNow(t, "Alerts were not pushed.")
|
2019-09-02 14:05:02 +00:00
|
|
|
}
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2019-09-02 14:05:02 +00:00
|
|
|
// Verify that we receive the last 3 batches.
|
2019-05-06 07:02:40 +00:00
|
|
|
for i := 2; i < 5; i++ {
|
2019-09-02 14:05:02 +00:00
|
|
|
assertAlerts(alerts[i*maxBatchSize : (i+1)*maxBatchSize])
|
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()
|
2021-10-28 00:01:28 +00:00
|
|
|
_, _, err := AlertmanagerFromGroup(tg, &config.AlertmanagerConfig{})
|
2017-05-17 14:24:22 +00:00
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-29 15:07:26 +00:00
|
|
|
|
|
|
|
// Target modified during alertmanager extraction
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, tg, makeInputTargetGroup())
|
2017-05-17 14:24:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 20:28:34 +00:00
|
|
|
func TestReload(t *testing.T) {
|
2021-10-22 08:06:44 +00:00
|
|
|
tests := []struct {
|
2017-12-30 20:28:34 +00:00
|
|
|
in *targetgroup.Group
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: &targetgroup.Group{
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": "alertmanager:9093",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 17:19:12 +00:00
|
|
|
out: "http://alertmanager:9093/api/v2/alerts",
|
2017-12-30 20:28:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
`
|
2021-09-13 19:19:20 +00:00
|
|
|
err := yaml.UnmarshalStrict([]byte(s), cfg)
|
|
|
|
require.NoError(t, err, "Unable to load YAML config.")
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 1)
|
2017-12-30 20:28:34 +00:00
|
|
|
|
2021-09-13 19:19:20 +00:00
|
|
|
err = n.ApplyConfig(cfg)
|
|
|
|
require.NoError(t, err, "Error applying the config.")
|
2017-12-30 20:28:34 +00:00
|
|
|
|
|
|
|
tgs := make(map[string][]*targetgroup.Group)
|
|
|
|
for _, tt := range tests {
|
2019-12-12 16:00:19 +00:00
|
|
|
for k := range cfg.AlertingConfig.AlertmanagerConfigs.ToMap() {
|
|
|
|
tgs[k] = []*targetgroup.Group{
|
|
|
|
tt.in,
|
|
|
|
}
|
|
|
|
break
|
2017-12-30 20:28:34 +00:00
|
|
|
}
|
|
|
|
n.reload(tgs)
|
|
|
|
res := n.Alertmanagers()[0].String()
|
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, tt.out, res)
|
2017-12-30 20:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 09:00:07 +00:00
|
|
|
func TestDroppedAlertmanagers(t *testing.T) {
|
2021-10-22 08:06:44 +00:00
|
|
|
tests := []struct {
|
2018-02-21 09:00:07 +00:00
|
|
|
in *targetgroup.Group
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: &targetgroup.Group{
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": "alertmanager:9093",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 17:19:12 +00:00
|
|
|
out: "http://alertmanager:9093/api/v2/alerts",
|
2018-02-21 09:00:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
n := NewManager(&Options{}, nil)
|
|
|
|
|
|
|
|
cfg := &config.Config{}
|
|
|
|
s := `
|
|
|
|
alerting:
|
|
|
|
alertmanagers:
|
|
|
|
- static_configs:
|
|
|
|
relabel_configs:
|
|
|
|
- source_labels: ['__address__']
|
|
|
|
regex: 'alertmanager:9093'
|
|
|
|
action: drop
|
|
|
|
`
|
2021-09-13 19:19:20 +00:00
|
|
|
err := yaml.UnmarshalStrict([]byte(s), cfg)
|
|
|
|
require.NoError(t, err, "Unable to load YAML config.")
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 1)
|
2018-02-21 09:00:07 +00:00
|
|
|
|
2021-09-13 19:19:20 +00:00
|
|
|
err = n.ApplyConfig(cfg)
|
|
|
|
require.NoError(t, err, "Error applying the config.")
|
2018-02-21 09:00:07 +00:00
|
|
|
|
|
|
|
tgs := make(map[string][]*targetgroup.Group)
|
|
|
|
for _, tt := range tests {
|
2019-12-12 16:00:19 +00:00
|
|
|
for k := range cfg.AlertingConfig.AlertmanagerConfigs.ToMap() {
|
|
|
|
tgs[k] = []*targetgroup.Group{
|
|
|
|
tt.in,
|
|
|
|
}
|
|
|
|
break
|
2018-02-21 09:00:07 +00:00
|
|
|
}
|
2019-12-12 16:00:19 +00:00
|
|
|
|
2018-02-21 09:00:07 +00:00
|
|
|
n.reload(tgs)
|
|
|
|
res := n.DroppedAlertmanagers()[0].String()
|
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, res, tt.out)
|
2018-02-21 09:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 07:51:32 +00:00
|
|
|
|
|
|
|
func TestLabelsToOpenAPILabelSet(t *testing.T) {
|
2022-03-09 22:21:29 +00:00
|
|
|
require.Equal(t, models.LabelSet{"aaa": "111", "bbb": "222"}, labelsToOpenAPILabelSet(labels.FromStrings("aaa", "111", "bbb", "222")))
|
2020-07-01 07:51:32 +00:00
|
|
|
}
|
2022-07-01 12:23:23 +00:00
|
|
|
|
2024-06-03 16:09:51 +00:00
|
|
|
// TestHangingNotifier ensures that the notifier takes into account SD changes even when there are
|
2024-05-31 14:40:09 +00:00
|
|
|
// queued alerts. This test reproduces the issue described in https://github.com/prometheus/prometheus/issues/13676.
|
2024-06-03 16:09:51 +00:00
|
|
|
// and https://github.com/prometheus/prometheus/issues/8768.
|
|
|
|
func TestHangingNotifier(t *testing.T) {
|
2024-05-31 14:40:09 +00:00
|
|
|
const (
|
|
|
|
batches = 100
|
|
|
|
alertsCount = maxBatchSize * batches
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-08-12 13:01:20 +00:00
|
|
|
sendTimeout = 100 * time.Millisecond
|
2024-05-31 14:40:09 +00:00
|
|
|
sdUpdatert = sendTimeout / 2
|
|
|
|
|
|
|
|
done = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Set up a faulty Alertmanager.
|
|
|
|
var faultyCalled atomic.Bool
|
|
|
|
faultyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
faultyCalled.Store(true)
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Hour):
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
faultyURL, err := url.Parse(faultyServer.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Set up a functional Alertmanager.
|
|
|
|
var functionalCalled atomic.Bool
|
|
|
|
functionalServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
functionalCalled.Store(true)
|
|
|
|
}))
|
|
|
|
functionalURL, err := url.Parse(functionalServer.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Initialize the discovery manager
|
2024-06-03 16:09:51 +00:00
|
|
|
// This is relevant as the updates aren't sent continually in real life, but only each updatert.
|
2024-09-10 20:32:03 +00:00
|
|
|
// The old implementation of TestHangingNotifier didn't take that into account.
|
2024-05-31 14:40:09 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
sdMetrics, err := discovery.RegisterSDMetrics(reg, discovery.NewRefreshMetrics(reg))
|
|
|
|
require.NoError(t, err)
|
|
|
|
sdManager := discovery.NewManager(
|
|
|
|
ctx,
|
2024-09-10 01:41:53 +00:00
|
|
|
promslog.NewNopLogger(),
|
2024-05-31 14:40:09 +00:00
|
|
|
reg,
|
|
|
|
sdMetrics,
|
|
|
|
discovery.Name("sd-manager"),
|
|
|
|
discovery.Updatert(sdUpdatert),
|
|
|
|
)
|
|
|
|
go sdManager.Run()
|
|
|
|
|
|
|
|
// Set up the notifier with both faulty and functional Alertmanagers.
|
|
|
|
notifier := NewManager(
|
|
|
|
&Options{
|
|
|
|
QueueCapacity: alertsCount,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
notifier.alertmanagers = make(map[string]*alertmanagerSet)
|
|
|
|
amCfg := config.DefaultAlertmanagerConfig
|
|
|
|
amCfg.Timeout = model.Duration(sendTimeout)
|
|
|
|
notifier.alertmanagers["config-0"] = &alertmanagerSet{
|
|
|
|
ams: []alertmanager{
|
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return faultyURL.String() },
|
|
|
|
},
|
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return functionalURL.String() },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cfg: &amCfg,
|
|
|
|
metrics: notifier.metrics,
|
|
|
|
}
|
|
|
|
go notifier.Run(sdManager.SyncCh())
|
|
|
|
defer notifier.Stop()
|
|
|
|
|
|
|
|
require.Len(t, notifier.Alertmanagers(), 2)
|
|
|
|
|
|
|
|
// Enqueue the alerts.
|
|
|
|
var alerts []*Alert
|
|
|
|
for i := range make([]struct{}, alertsCount) {
|
|
|
|
alerts = append(alerts, &Alert{
|
|
|
|
Labels: labels.FromStrings("alertname", strconv.Itoa(i)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
notifier.Send(alerts...)
|
|
|
|
|
|
|
|
// Wait for the Alertmanagers to start receiving alerts.
|
|
|
|
// 10*sdUpdatert is used as an arbitrary timeout here.
|
|
|
|
timeout := time.After(10 * sdUpdatert)
|
|
|
|
loop1:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatalf("Timeout waiting for the alertmanagers to be reached for the first time.")
|
|
|
|
default:
|
|
|
|
if faultyCalled.Load() && functionalCalled.Load() {
|
|
|
|
break loop1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request to remove the faulty Alertmanager.
|
|
|
|
c := map[string]discovery.Configs{
|
|
|
|
"config-0": {
|
|
|
|
discovery.StaticConfig{
|
|
|
|
&targetgroup.Group{
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
model.AddressLabel: model.LabelValue(functionalURL.Host),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, sdManager.ApplyConfig(c))
|
|
|
|
|
|
|
|
// The notifier should not wait until the alerts queue is empty to apply the discovery changes
|
|
|
|
// A faulty Alertmanager could cause each alert sending cycle to take up to AlertmanagerConfig.Timeout
|
|
|
|
// The queue may never be emptied, as the arrival rate could be larger than the departure rate
|
|
|
|
// It could even overflow and alerts could be dropped.
|
|
|
|
timeout = time.After(batches * sendTimeout)
|
|
|
|
loop2:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatalf("Timeout, the faulty alertmanager not removed on time.")
|
|
|
|
default:
|
|
|
|
// The faulty alertmanager was dropped.
|
|
|
|
if len(notifier.Alertmanagers()) == 1 {
|
|
|
|
// Prevent from TOCTOU.
|
|
|
|
require.Positive(t, notifier.queueLen())
|
|
|
|
break loop2
|
|
|
|
}
|
|
|
|
require.Positive(t, notifier.queueLen(), "The faulty alertmanager wasn't dropped before the alerts queue was emptied.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-26 10:32:04 +00:00
|
|
|
|
|
|
|
func TestStop_DrainingDisabled(t *testing.T) {
|
|
|
|
releaseReceiver := make(chan struct{})
|
|
|
|
receiverReceivedRequest := make(chan struct{}, 2)
|
|
|
|
alertsReceived := atomic.NewInt64(0)
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Let the test know we've received a request.
|
|
|
|
receiverReceivedRequest <- struct{}{}
|
|
|
|
|
|
|
|
var alerts []*Alert
|
|
|
|
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, &alerts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
alertsReceived.Add(int64(len(alerts)))
|
|
|
|
|
|
|
|
// Wait for the test to release us.
|
|
|
|
<-releaseReceiver
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
defer func() {
|
|
|
|
server.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
m := NewManager(
|
|
|
|
&Options{
|
|
|
|
QueueCapacity: 10,
|
|
|
|
DrainOnShutdown: false,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
m.alertmanagers = make(map[string]*alertmanagerSet)
|
|
|
|
|
|
|
|
am1Cfg := config.DefaultAlertmanagerConfig
|
|
|
|
am1Cfg.Timeout = model.Duration(time.Second)
|
|
|
|
|
|
|
|
m.alertmanagers["1"] = &alertmanagerSet{
|
|
|
|
ams: []alertmanager{
|
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return server.URL },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cfg: &am1Cfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationManagerStopped := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(notificationManagerStopped)
|
|
|
|
m.Run(nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Queue two alerts. The first should be immediately sent to the receiver, which should block until we release it later.
|
|
|
|
m.Send(&Alert{Labels: labels.FromStrings(labels.AlertName, "alert-1")})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-receiverReceivedRequest:
|
|
|
|
// Nothing more to do.
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
require.FailNow(t, "gave up waiting for receiver to receive notification of first alert")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Send(&Alert{Labels: labels.FromStrings(labels.AlertName, "alert-2")})
|
|
|
|
|
|
|
|
// Stop the notification manager, pause to allow the shutdown to be observed, and then allow the receiver to proceed.
|
|
|
|
m.Stop()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
close(releaseReceiver)
|
|
|
|
|
|
|
|
// Wait for the notification manager to stop and confirm only the first notification was sent.
|
|
|
|
// The second notification should be dropped.
|
|
|
|
select {
|
|
|
|
case <-notificationManagerStopped:
|
|
|
|
// Nothing more to do.
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
require.FailNow(t, "gave up waiting for notification manager to stop")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, int64(1), alertsReceived.Load())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStop_DrainingEnabled(t *testing.T) {
|
|
|
|
releaseReceiver := make(chan struct{})
|
|
|
|
receiverReceivedRequest := make(chan struct{}, 2)
|
|
|
|
alertsReceived := atomic.NewInt64(0)
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Let the test know we've received a request.
|
|
|
|
receiverReceivedRequest <- struct{}{}
|
|
|
|
|
|
|
|
var alerts []*Alert
|
|
|
|
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, &alerts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
alertsReceived.Add(int64(len(alerts)))
|
|
|
|
|
|
|
|
// Wait for the test to release us.
|
|
|
|
<-releaseReceiver
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
defer func() {
|
|
|
|
server.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
m := NewManager(
|
|
|
|
&Options{
|
|
|
|
QueueCapacity: 10,
|
|
|
|
DrainOnShutdown: true,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
m.alertmanagers = make(map[string]*alertmanagerSet)
|
|
|
|
|
|
|
|
am1Cfg := config.DefaultAlertmanagerConfig
|
|
|
|
am1Cfg.Timeout = model.Duration(time.Second)
|
|
|
|
|
|
|
|
m.alertmanagers["1"] = &alertmanagerSet{
|
|
|
|
ams: []alertmanager{
|
|
|
|
alertmanagerMock{
|
|
|
|
urlf: func() string { return server.URL },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cfg: &am1Cfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationManagerStopped := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(notificationManagerStopped)
|
|
|
|
m.Run(nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Queue two alerts. The first should be immediately sent to the receiver, which should block until we release it later.
|
|
|
|
m.Send(&Alert{Labels: labels.FromStrings(labels.AlertName, "alert-1")})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-receiverReceivedRequest:
|
|
|
|
// Nothing more to do.
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
require.FailNow(t, "gave up waiting for receiver to receive notification of first alert")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Send(&Alert{Labels: labels.FromStrings(labels.AlertName, "alert-2")})
|
|
|
|
|
|
|
|
// Stop the notification manager and allow the receiver to proceed.
|
|
|
|
m.Stop()
|
|
|
|
close(releaseReceiver)
|
|
|
|
|
|
|
|
// Wait for the notification manager to stop and confirm both notifications were sent.
|
|
|
|
select {
|
|
|
|
case <-notificationManagerStopped:
|
|
|
|
// Nothing more to do.
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
|
|
|
require.FailNow(t, "gave up waiting for notification manager to stop")
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, int64(2), alertsReceived.Load())
|
|
|
|
}
|
2024-09-26 12:53:48 +00:00
|
|
|
|
2024-09-26 12:53:17 +00:00
|
|
|
func TestApplyConfig(t *testing.T) {
|
2024-09-26 12:53:48 +00:00
|
|
|
targetURL := "alertmanager:9093"
|
|
|
|
targetGroup := &targetgroup.Group{
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue(targetURL),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
alertmanagerURL := fmt.Sprintf("http://%s/api/v2/alerts", targetURL)
|
|
|
|
|
|
|
|
n := NewManager(&Options{}, nil)
|
|
|
|
cfg := &config.Config{}
|
|
|
|
s := `
|
|
|
|
alerting:
|
|
|
|
alertmanagers:
|
|
|
|
- file_sd_configs:
|
|
|
|
- files:
|
|
|
|
- foo.json
|
|
|
|
`
|
2024-09-26 12:53:17 +00:00
|
|
|
// 1. Ensure known alertmanagers are not dropped during ApplyConfig.
|
|
|
|
require.NoError(t, yaml.UnmarshalStrict([]byte(s), cfg))
|
2024-09-26 12:53:48 +00:00
|
|
|
require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 1)
|
|
|
|
|
2024-09-26 12:53:17 +00:00
|
|
|
// First, apply the config and reload.
|
|
|
|
require.NoError(t, n.ApplyConfig(cfg))
|
2024-09-26 12:53:48 +00:00
|
|
|
tgs := map[string][]*targetgroup.Group{"config-0": {targetGroup}}
|
|
|
|
n.reload(tgs)
|
|
|
|
require.Len(t, n.Alertmanagers(), 1)
|
|
|
|
require.Equal(t, alertmanagerURL, n.Alertmanagers()[0].String())
|
|
|
|
|
|
|
|
// Reapply the config.
|
2024-09-26 12:53:17 +00:00
|
|
|
require.NoError(t, n.ApplyConfig(cfg))
|
|
|
|
// Ensure the known alertmanagers are not dropped.
|
2024-09-26 12:53:48 +00:00
|
|
|
require.Len(t, n.Alertmanagers(), 1)
|
|
|
|
require.Equal(t, alertmanagerURL, n.Alertmanagers()[0].String())
|
2024-09-26 12:53:17 +00:00
|
|
|
|
|
|
|
// 2. Ensure known alertmanagers are not dropped during ApplyConfig even when
|
|
|
|
// the config order changes.
|
|
|
|
s = `
|
|
|
|
alerting:
|
|
|
|
alertmanagers:
|
|
|
|
- static_configs:
|
|
|
|
- file_sd_configs:
|
|
|
|
- files:
|
|
|
|
- foo.json
|
|
|
|
`
|
|
|
|
require.NoError(t, yaml.UnmarshalStrict([]byte(s), cfg))
|
|
|
|
require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 2)
|
|
|
|
|
|
|
|
require.NoError(t, n.ApplyConfig(cfg))
|
|
|
|
require.Len(t, n.Alertmanagers(), 1)
|
|
|
|
// Ensure no unnecessary alertmanagers are injected.
|
|
|
|
require.Empty(t, n.alertmanagers["config-0"].ams)
|
|
|
|
// Ensure the config order is taken into account.
|
|
|
|
ams := n.alertmanagers["config-1"].ams
|
|
|
|
require.Len(t, ams, 1)
|
|
|
|
require.Equal(t, alertmanagerURL, ams[0].url().String())
|
|
|
|
|
|
|
|
// 3. Ensure known alertmanagers are reused for new config with identical AlertmanagerConfig.
|
|
|
|
s = `
|
|
|
|
alerting:
|
|
|
|
alertmanagers:
|
|
|
|
- file_sd_configs:
|
|
|
|
- files:
|
|
|
|
- foo.json
|
|
|
|
- file_sd_configs:
|
|
|
|
- files:
|
|
|
|
- foo.json
|
|
|
|
`
|
|
|
|
require.NoError(t, yaml.UnmarshalStrict([]byte(s), cfg))
|
|
|
|
require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 2)
|
|
|
|
|
|
|
|
require.NoError(t, n.ApplyConfig(cfg))
|
|
|
|
require.Len(t, n.Alertmanagers(), 2)
|
|
|
|
for cfgIdx := range 2 {
|
|
|
|
ams := n.alertmanagers[fmt.Sprintf("config-%d", cfgIdx)].ams
|
|
|
|
require.Len(t, ams, 1)
|
|
|
|
require.Equal(t, alertmanagerURL, ams[0].url().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. Ensure known alertmanagers are reused only for identical AlertmanagerConfig.
|
|
|
|
s = `
|
|
|
|
alerting:
|
|
|
|
alertmanagers:
|
|
|
|
- file_sd_configs:
|
|
|
|
- files:
|
|
|
|
- foo.json
|
|
|
|
path_prefix: /bar
|
|
|
|
- file_sd_configs:
|
|
|
|
- files:
|
|
|
|
- foo.json
|
|
|
|
relabel_configs:
|
|
|
|
- source_labels: ['__address__']
|
|
|
|
regex: 'doesntmatter:1234'
|
|
|
|
action: drop
|
|
|
|
`
|
|
|
|
require.NoError(t, yaml.UnmarshalStrict([]byte(s), cfg))
|
|
|
|
require.Len(t, cfg.AlertingConfig.AlertmanagerConfigs, 2)
|
|
|
|
|
|
|
|
require.NoError(t, n.ApplyConfig(cfg))
|
|
|
|
require.Empty(t, n.Alertmanagers())
|
2024-09-26 12:53:48 +00:00
|
|
|
}
|