2026-01-05 07:46:21 -05:00
|
|
|
// Copyright The Prometheus Authors
|
2013-07-30 11:18:07 -04: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 06:37:22 -05:00
|
|
|
package notifier
|
2013-07-30 11:18:07 -04:00
|
|
|
|
|
|
|
|
import (
|
2017-10-25 00:21:42 -04:00
|
|
|
"context"
|
2015-12-10 10:31:50 -05:00
|
|
|
"fmt"
|
2024-09-09 21:41:53 -04:00
|
|
|
"log/slog"
|
2013-07-30 11:18:07 -04:00
|
|
|
"net/http"
|
2016-11-23 11:03:22 -05:00
|
|
|
"net/url"
|
2015-09-01 16:17:02 -04:00
|
|
|
"sync"
|
2013-07-30 11:18:07 -04:00
|
|
|
|
2014-09-24 10:52:00 -04:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2025-08-18 04:09:00 -04:00
|
|
|
"github.com/prometheus/common/model"
|
2024-09-09 21:41:53 -04:00
|
|
|
"github.com/prometheus/common/promslog"
|
2018-11-23 09:49:49 -05:00
|
|
|
"github.com/prometheus/common/version"
|
2020-10-22 05:00:08 -04:00
|
|
|
|
2015-09-01 16:17:02 -04: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 15:01:34 -05:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2021-11-08 09:23:17 -05:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
|
|
|
|
"github.com/prometheus/prometheus/model/relabel"
|
2013-07-30 11:18:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2025-03-24 10:22:19 -04:00
|
|
|
// DefaultMaxBatchSize is the default maximum number of alerts to send in a single request to the alertmanager.
|
|
|
|
|
DefaultMaxBatchSize = 256
|
|
|
|
|
|
2019-04-18 08:17:03 -04:00
|
|
|
contentTypeJSON = "application/json"
|
2013-07-30 11:18:07 -04:00
|
|
|
)
|
|
|
|
|
|
2014-06-18 13:43:15 -04:00
|
|
|
// String constants for instrumentation.
|
|
|
|
|
const (
|
2016-06-02 08:25:19 -04:00
|
|
|
namespace = "prometheus"
|
|
|
|
|
subsystem = "notifications"
|
|
|
|
|
alertmanagerLabel = "alertmanager"
|
2014-06-18 13:43:15 -04:00
|
|
|
)
|
|
|
|
|
|
2024-12-05 00:47:15 -05:00
|
|
|
var userAgent = version.PrometheusUserAgent()
|
2018-11-23 09:49:49 -05:00
|
|
|
|
2018-01-30 12:45:37 -05:00
|
|
|
// Manager is responsible for dispatching alert notifications to an
|
2015-12-10 10:31:50 -05:00
|
|
|
// alert manager service.
|
2018-01-30 12:45:37 -05:00
|
|
|
type Manager struct {
|
2026-01-20 04:33:07 -05:00
|
|
|
opts *Options
|
2013-08-09 13:32:55 -04:00
|
|
|
|
2017-03-02 16:23:16 -05:00
|
|
|
metrics *alertMetrics
|
|
|
|
|
|
2026-01-20 04:33:07 -05:00
|
|
|
mtx sync.RWMutex
|
2024-06-26 06:32:04 -04:00
|
|
|
|
|
|
|
|
stopOnce *sync.Once
|
|
|
|
|
stopRequested chan struct{}
|
2013-08-09 13:32:55 -04:00
|
|
|
|
2017-12-30 12:27:50 -05:00
|
|
|
alertmanagers map[string]*alertmanagerSet
|
2024-09-09 21:41:53 -04:00
|
|
|
logger *slog.Logger
|
2013-07-30 11:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-11 08:20:36 -04:00
|
|
|
// Options are the configurable parameters of a Handler.
|
2016-03-01 06:37:22 -05:00
|
|
|
type Options struct {
|
2024-06-26 06:32:04 -04:00
|
|
|
QueueCapacity int
|
|
|
|
|
DrainOnShutdown bool
|
|
|
|
|
ExternalLabels labels.Labels
|
|
|
|
|
RelabelConfigs []*relabel.Config
|
2017-02-27 14:31:16 -05:00
|
|
|
// Used for sending HTTP requests to the Alertmanager.
|
2018-11-19 06:31:16 -05:00
|
|
|
Do func(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error)
|
2015-06-15 07:03:17 -04:00
|
|
|
|
2017-03-31 11:50:12 -04:00
|
|
|
Registerer prometheus.Registerer
|
2025-03-24 10:22:19 -04:00
|
|
|
|
|
|
|
|
// MaxBatchSize determines the maximum number of alerts to send in a single request to the alertmanager.
|
|
|
|
|
MaxBatchSize int
|
2015-06-15 07:03:17 -04:00
|
|
|
}
|
2017-02-27 14:31:16 -05:00
|
|
|
|
2018-11-19 06:31:16 -05:00
|
|
|
func do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
|
|
|
|
|
if client == nil {
|
|
|
|
|
client = http.DefaultClient
|
|
|
|
|
}
|
|
|
|
|
return client.Do(req.WithContext(ctx))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 12:45:37 -05:00
|
|
|
// NewManager is the manager constructor.
|
2025-08-18 04:09:00 -04:00
|
|
|
func NewManager(o *Options, nameValidationScheme model.ValidationScheme, logger *slog.Logger) *Manager {
|
2017-03-02 16:23:16 -05:00
|
|
|
if o.Do == nil {
|
2018-11-19 06:31:16 -05:00
|
|
|
o.Do = do
|
2017-03-02 16:23:16 -05:00
|
|
|
}
|
2025-03-24 10:22:19 -04:00
|
|
|
// Set default MaxBatchSize if not provided.
|
|
|
|
|
if o.MaxBatchSize <= 0 {
|
|
|
|
|
o.MaxBatchSize = DefaultMaxBatchSize
|
|
|
|
|
}
|
2017-08-11 14:45:52 -04:00
|
|
|
if logger == nil {
|
2024-09-09 21:41:53 -04:00
|
|
|
logger = promslog.NewNopLogger()
|
2017-08-11 14:45:52 -04:00
|
|
|
}
|
2017-03-02 16:23:16 -05:00
|
|
|
|
2025-08-18 04:09:00 -04:00
|
|
|
for _, rc := range o.RelabelConfigs {
|
|
|
|
|
switch rc.NameValidationScheme {
|
|
|
|
|
case model.LegacyValidation, model.UTF8Validation:
|
|
|
|
|
default:
|
|
|
|
|
rc.NameValidationScheme = nameValidationScheme
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 12:45:37 -05:00
|
|
|
n := &Manager{
|
2024-06-26 06:32:04 -04:00
|
|
|
stopRequested: make(chan struct{}),
|
|
|
|
|
stopOnce: &sync.Once{},
|
|
|
|
|
opts: o,
|
|
|
|
|
logger: logger,
|
2017-03-02 16:23:16 -05:00
|
|
|
}
|
2017-03-31 14:44:30 -04:00
|
|
|
|
2017-05-04 07:54:08 -04:00
|
|
|
alertmanagersDiscoveredFunc := func() float64 { return float64(len(n.Alertmanagers())) }
|
2017-08-11 14:45:52 -04:00
|
|
|
|
2026-01-20 04:33:07 -05:00
|
|
|
n.metrics = newAlertMetrics(o.Registerer, alertmanagersDiscoveredFunc)
|
|
|
|
|
n.metrics.queueCapacity.Set(float64(o.QueueCapacity))
|
2017-08-11 14:45:52 -04:00
|
|
|
|
2017-03-31 14:44:30 -04:00
|
|
|
return n
|
2013-07-30 11:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
2015-09-01 16:17:02 -04:00
|
|
|
// ApplyConfig updates the status state as the new config requires.
|
2018-01-30 12:45:37 -05:00
|
|
|
func (n *Manager) ApplyConfig(conf *config.Config) error {
|
2015-09-01 16:17:02 -04:00
|
|
|
n.mtx.Lock()
|
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
|
2015-12-10 10:31:50 -05:00
|
|
|
n.opts.ExternalLabels = conf.GlobalConfig.ExternalLabels
|
2016-08-09 07:09:36 -04:00
|
|
|
n.opts.RelabelConfigs = conf.AlertingConfig.AlertRelabelConfigs
|
2025-08-18 04:09:00 -04:00
|
|
|
for i, rc := range n.opts.RelabelConfigs {
|
|
|
|
|
switch rc.NameValidationScheme {
|
|
|
|
|
case model.LegacyValidation, model.UTF8Validation:
|
|
|
|
|
default:
|
|
|
|
|
n.opts.RelabelConfigs[i].NameValidationScheme = conf.GlobalConfig.MetricNameValidationScheme
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-23 11:03:22 -05:00
|
|
|
|
2017-12-30 12:27:50 -05:00
|
|
|
amSets := make(map[string]*alertmanagerSet)
|
2024-09-26 08:53:17 -04:00
|
|
|
// configToAlertmanagers maps alertmanager sets for each unique AlertmanagerConfig,
|
|
|
|
|
// helping to avoid dropping known alertmanagers and re-use them without waiting for SD updates when applying the config.
|
|
|
|
|
configToAlertmanagers := make(map[string]*alertmanagerSet, len(n.alertmanagers))
|
|
|
|
|
for _, oldAmSet := range n.alertmanagers {
|
|
|
|
|
hash, err := oldAmSet.configHash()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
configToAlertmanagers[hash] = oldAmSet
|
|
|
|
|
}
|
2016-11-23 11:03:22 -05:00
|
|
|
|
2019-12-12 11:00:19 -05:00
|
|
|
for k, cfg := range conf.AlertingConfig.AlertmanagerConfigs.ToMap() {
|
2026-01-20 04:33:07 -05:00
|
|
|
ams, err := newAlertmanagerSet(cfg, n.opts, n.logger, n.metrics)
|
2016-11-23 11:03:22 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-03-02 13:28:15 -05:00
|
|
|
|
2024-09-26 08:53:17 -04:00
|
|
|
hash, err := ams.configHash()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if oldAmSet, ok := configToAlertmanagers[hash]; ok {
|
|
|
|
|
ams.ams = oldAmSet.ams
|
|
|
|
|
ams.droppedAms = oldAmSet.droppedAms
|
2026-01-22 16:22:44 -05:00
|
|
|
// Only transfer sendLoops to the first new config with this hash.
|
|
|
|
|
// Subsequent configs with the same hash should not share the sendLoops
|
|
|
|
|
// map reference, as that would cause shared mutable state between
|
|
|
|
|
// alertmanagerSets (cleanup in one would affect the other).
|
|
|
|
|
oldAmSet.mtx.Lock()
|
|
|
|
|
if oldAmSet.sendLoops != nil {
|
|
|
|
|
ams.mtx.Lock()
|
|
|
|
|
ams.sendLoops = oldAmSet.sendLoops
|
|
|
|
|
oldAmSet.sendLoops = nil
|
|
|
|
|
ams.mtx.Unlock()
|
|
|
|
|
}
|
|
|
|
|
oldAmSet.mtx.Unlock()
|
2024-09-26 08:53:17 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-12 11:00:19 -05:00
|
|
|
amSets[k] = ams
|
2016-11-23 11:03:22 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-22 16:22:44 -05:00
|
|
|
// Clean up sendLoops that weren't transferred to new config.
|
|
|
|
|
// This happens when: (1) key was removed, or (2) key exists but hash changed.
|
|
|
|
|
// After the transfer loop above, any oldAmSet with non-nil sendLoops
|
|
|
|
|
// had its sendLoops NOT transferred (since we set it to nil on transfer).
|
|
|
|
|
for _, oldAmSet := range n.alertmanagers {
|
|
|
|
|
oldAmSet.mtx.Lock()
|
|
|
|
|
if oldAmSet.sendLoops != nil {
|
2026-01-20 04:33:07 -05:00
|
|
|
oldAmSet.cleanSendLoops(oldAmSet.ams...)
|
|
|
|
|
}
|
2026-01-22 16:22:44 -05:00
|
|
|
oldAmSet.mtx.Unlock()
|
2026-01-20 04:33:07 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 11:03:22 -05:00
|
|
|
n.alertmanagers = amSets
|
|
|
|
|
|
2016-07-11 10:24:54 -04:00
|
|
|
return nil
|
2015-09-01 16:17:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-06-26 06:32:04 -04:00
|
|
|
// Run dispatches notifications continuously, returning once Stop has been called and all
|
|
|
|
|
// pending notifications have been drained from the queue (if draining is enabled).
|
|
|
|
|
//
|
|
|
|
|
// Dispatching of notifications occurs in parallel to processing target updates to avoid one starving the other.
|
|
|
|
|
// Refer to https://github.com/prometheus/prometheus/issues/13676 for more details.
|
|
|
|
|
func (n *Manager) Run(tsets <-chan map[string][]*targetgroup.Group) {
|
2026-01-20 04:33:07 -05:00
|
|
|
n.targetUpdateLoop(tsets)
|
2015-12-10 10:31:50 -05:00
|
|
|
|
2026-01-20 04:33:07 -05:00
|
|
|
n.mtx.Lock()
|
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
for _, ams := range n.alertmanagers {
|
|
|
|
|
ams.mtx.Lock()
|
|
|
|
|
ams.cleanSendLoops(ams.ams...)
|
|
|
|
|
ams.mtx.Unlock()
|
2013-07-30 11:18:07 -04:00
|
|
|
}
|
2014-10-10 08:19:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-06-26 06:32:04 -04:00
|
|
|
// targetUpdateLoop receives updates of target groups and triggers a reload.
|
|
|
|
|
func (n *Manager) targetUpdateLoop(tsets <-chan map[string][]*targetgroup.Group) {
|
2024-06-03 12:09:51 -04:00
|
|
|
for {
|
2024-06-26 06:32:04 -04:00
|
|
|
// If we've been asked to stop, that takes priority over processing any further target group updates.
|
2024-06-03 12:09:51 -04:00
|
|
|
select {
|
2024-06-26 06:32:04 -04:00
|
|
|
case <-n.stopRequested:
|
2024-06-03 12:09:51 -04:00
|
|
|
return
|
2024-06-26 06:32:04 -04:00
|
|
|
default:
|
|
|
|
|
select {
|
|
|
|
|
case <-n.stopRequested:
|
|
|
|
|
return
|
2025-07-11 12:15:01 -04:00
|
|
|
case ts, ok := <-tsets:
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
2024-06-26 06:32:04 -04:00
|
|
|
n.reload(ts)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 12:45:37 -05:00
|
|
|
func (n *Manager) reload(tgs map[string][]*targetgroup.Group) {
|
2017-12-30 12:27:50 -05:00
|
|
|
n.mtx.Lock()
|
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
|
|
|
|
|
for id, tgroup := range tgs {
|
|
|
|
|
am, ok := n.alertmanagers[id]
|
|
|
|
|
if !ok {
|
2024-09-09 21:41:53 -04:00
|
|
|
n.logger.Error("couldn't sync alert manager set", "err", fmt.Sprintf("invalid id:%v", id))
|
2017-12-30 12:27:50 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
am.sync(tgroup)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 16:58:32 -05:00
|
|
|
// Send queues the given notification requests for processing.
|
2016-01-18 10:47:31 -05:00
|
|
|
// Panics if called on a handler that is not running.
|
2018-01-30 12:45:37 -05:00
|
|
|
func (n *Manager) Send(alerts ...*Alert) {
|
2026-01-20 04:33:07 -05:00
|
|
|
// If we've been asked to stop, that takes priority over accepting new alerts.
|
|
|
|
|
select {
|
|
|
|
|
case <-n.stopRequested:
|
2019-05-06 03:02:40 -04:00
|
|
|
return
|
2026-01-20 04:33:07 -05:00
|
|
|
default:
|
2019-05-06 03:02:40 -04:00
|
|
|
}
|
2016-08-09 04:08:15 -04:00
|
|
|
|
2026-01-20 04:33:07 -05:00
|
|
|
n.mtx.RLock()
|
|
|
|
|
defer n.mtx.RUnlock()
|
2015-12-10 10:31:50 -05:00
|
|
|
|
2026-01-20 04:33:07 -05:00
|
|
|
alerts = relabelAlerts(n.opts.RelabelConfigs, n.opts.ExternalLabels, alerts)
|
|
|
|
|
if len(alerts) == 0 {
|
|
|
|
|
return
|
2015-12-10 10:31:50 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 04:33:07 -05:00
|
|
|
for _, ams := range n.alertmanagers {
|
|
|
|
|
ams.send(alerts...)
|
2015-12-10 10:31:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 01:42:33 -04:00
|
|
|
// Alertmanagers returns a slice of Alertmanager URLs.
|
2018-01-30 12:45:37 -05:00
|
|
|
func (n *Manager) Alertmanagers() []*url.URL {
|
2016-11-23 12:23:09 -05:00
|
|
|
n.mtx.RLock()
|
|
|
|
|
amSets := n.alertmanagers
|
|
|
|
|
n.mtx.RUnlock()
|
|
|
|
|
|
2017-04-25 01:42:33 -04:00
|
|
|
var res []*url.URL
|
2016-11-23 12:23:09 -05:00
|
|
|
|
|
|
|
|
for _, ams := range amSets {
|
|
|
|
|
ams.mtx.RLock()
|
|
|
|
|
for _, am := range ams.ams {
|
|
|
|
|
res = append(res, am.url())
|
|
|
|
|
}
|
|
|
|
|
ams.mtx.RUnlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 04:00:07 -05:00
|
|
|
// DroppedAlertmanagers returns a slice of Alertmanager URLs.
|
|
|
|
|
func (n *Manager) DroppedAlertmanagers() []*url.URL {
|
|
|
|
|
n.mtx.RLock()
|
|
|
|
|
amSets := n.alertmanagers
|
|
|
|
|
n.mtx.RUnlock()
|
|
|
|
|
|
|
|
|
|
var res []*url.URL
|
|
|
|
|
|
|
|
|
|
for _, ams := range amSets {
|
|
|
|
|
ams.mtx.RLock()
|
|
|
|
|
for _, dam := range ams.droppedAms {
|
|
|
|
|
res = append(res, dam.url())
|
|
|
|
|
}
|
|
|
|
|
ams.mtx.RUnlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 06:32:04 -04:00
|
|
|
// Stop signals the notification manager to shut down and immediately returns.
|
|
|
|
|
//
|
|
|
|
|
// Run will return once the notification manager has successfully shut down.
|
|
|
|
|
//
|
2026-01-20 04:33:07 -05:00
|
|
|
// The manager will optionally drain send loops before shutting down.
|
2024-06-26 06:32:04 -04:00
|
|
|
//
|
|
|
|
|
// Stop is safe to call multiple times.
|
2018-01-30 12:45:37 -05:00
|
|
|
func (n *Manager) Stop() {
|
2024-09-09 21:41:53 -04:00
|
|
|
n.logger.Info("Stopping notification manager...")
|
2024-06-26 06:32:04 -04:00
|
|
|
|
|
|
|
|
n.stopOnce.Do(func() {
|
|
|
|
|
close(n.stopRequested)
|
|
|
|
|
})
|
2013-07-30 11:18:07 -04:00
|
|
|
}
|