2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-07-30 15:18:07 +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-07-30 15:18:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-12-10 15:31:50 +00:00
|
|
|
"fmt"
|
2016-11-23 16:03:22 +00:00
|
|
|
"net"
|
2013-07-30 15:18:07 +00:00
|
|
|
"net/http"
|
2016-11-23 16:03:22 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2016-02-04 10:56:14 +00:00
|
|
|
"strings"
|
2015-09-01 20:17:02 +00:00
|
|
|
"sync"
|
2016-06-02 12:25:19 +00:00
|
|
|
"sync/atomic"
|
2013-07-30 15:18:07 +00:00
|
|
|
"time"
|
|
|
|
|
2014-09-24 14:52:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-10-03 08:21:43 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-12-10 15:31:50 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/net/context/ctxhttp"
|
2013-07-30 15:18:07 +00:00
|
|
|
|
2015-09-01 20:17:02 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2016-11-23 16:03:22 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2016-12-24 23:37:46 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2016-12-29 15:53:11 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/relabel"
|
2017-03-20 13:17:04 +00:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2013-07-30 15:18:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-12-10 15:31:50 +00:00
|
|
|
alertPushEndpoint = "/api/v1/alerts"
|
|
|
|
contentTypeJSON = "application/json"
|
2013-07-30 15:18:07 +00:00
|
|
|
)
|
|
|
|
|
2014-06-18 17:43:15 +00:00
|
|
|
// String constants for instrumentation.
|
|
|
|
const (
|
2016-06-02 12:25:19 +00:00
|
|
|
namespace = "prometheus"
|
|
|
|
subsystem = "notifications"
|
|
|
|
alertmanagerLabel = "alertmanager"
|
2014-06-18 17:43:15 +00:00
|
|
|
)
|
|
|
|
|
2016-12-24 23:37:46 +00:00
|
|
|
// Alert is a generic representation of an alert in the Prometheus eco-system.
|
|
|
|
type Alert struct {
|
|
|
|
// Label value pairs for purpose of aggregation, matching, and disposition
|
|
|
|
// dispatching. This must minimally include an "alertname" label.
|
|
|
|
Labels labels.Labels `json:"labels"`
|
|
|
|
|
|
|
|
// Extra key/value information which does not define alert identity.
|
|
|
|
Annotations labels.Labels `json:"annotations"`
|
|
|
|
|
|
|
|
// The known time range for this alert. Both ends are optional.
|
|
|
|
StartsAt time.Time `json:"startsAt,omitempty"`
|
|
|
|
EndsAt time.Time `json:"endsAt,omitempty"`
|
|
|
|
GeneratorURL string `json:"generatorURL,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the alert. It is equivalent to the "alertname" label.
|
|
|
|
func (a *Alert) Name() string {
|
|
|
|
return a.Labels.Get(labels.AlertName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash returns a hash over the alert. It is equivalent to the alert labels hash.
|
|
|
|
func (a *Alert) Hash() uint64 {
|
|
|
|
return a.Labels.Hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Alert) String() string {
|
|
|
|
s := fmt.Sprintf("%s[%s]", a.Name(), fmt.Sprintf("%016x", a.Hash())[:7])
|
|
|
|
if a.Resolved() {
|
|
|
|
return s + "[resolved]"
|
|
|
|
}
|
|
|
|
return s + "[active]"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolved returns true iff the activity interval ended in the past.
|
|
|
|
func (a *Alert) Resolved() bool {
|
|
|
|
return a.ResolvedAt(time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolvedAt returns true off the activity interval ended before
|
|
|
|
// the given timestamp.
|
|
|
|
func (a *Alert) ResolvedAt(ts time.Time) bool {
|
|
|
|
if a.EndsAt.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !a.EndsAt.After(ts)
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:20:36 +00:00
|
|
|
// Notifier is responsible for dispatching alert notifications to an
|
2015-12-10 15:31:50 +00:00
|
|
|
// alert manager service.
|
2016-03-01 11:37:22 +00:00
|
|
|
type Notifier struct {
|
2016-12-24 23:37:46 +00:00
|
|
|
queue []*Alert
|
2016-03-01 11:37:22 +00:00
|
|
|
opts *Options
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2017-03-02 21:23:16 +00:00
|
|
|
metrics *alertMetrics
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
more chan struct{}
|
|
|
|
mtx sync.RWMutex
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
2013-08-09 17:32:55 +00:00
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
alertmanagers []*alertmanagerSet
|
|
|
|
cancelDiscovery func()
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 12:20:36 +00:00
|
|
|
// Options are the configurable parameters of a Handler.
|
2016-03-01 11:37:22 +00:00
|
|
|
type Options struct {
|
2016-11-23 16:03:22 +00:00
|
|
|
QueueCapacity int
|
|
|
|
ExternalLabels model.LabelSet
|
|
|
|
RelabelConfigs []*config.RelabelConfig
|
2017-02-27 19:31:16 +00:00
|
|
|
// Used for sending HTTP requests to the Alertmanager.
|
|
|
|
Do func(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error)
|
2015-06-15 11:03:17 +00:00
|
|
|
|
2017-03-31 15:50:12 +00:00
|
|
|
Registerer prometheus.Registerer
|
2015-06-15 11:03:17 +00:00
|
|
|
}
|
2017-02-27 19:31:16 +00:00
|
|
|
|
2017-03-02 21:23:16 +00:00
|
|
|
type alertMetrics struct {
|
2017-05-04 11:54:08 +00:00
|
|
|
latency *prometheus.SummaryVec
|
|
|
|
errors *prometheus.CounterVec
|
|
|
|
sent *prometheus.CounterVec
|
|
|
|
dropped prometheus.Counter
|
|
|
|
queueLength prometheus.GaugeFunc
|
|
|
|
queueCapacity prometheus.Gauge
|
|
|
|
alertmanagersDiscovered prometheus.GaugeFunc
|
2017-03-02 21:23:16 +00:00
|
|
|
}
|
2014-06-18 17:43:15 +00:00
|
|
|
|
2017-05-04 11:54:08 +00:00
|
|
|
func newAlertMetrics(r prometheus.Registerer, queueCap int, queueLen, alertmanagersDiscovered func() float64) *alertMetrics {
|
2017-03-02 21:23:16 +00:00
|
|
|
m := &alertMetrics{
|
2016-06-02 12:25:19 +00:00
|
|
|
latency: prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
2015-01-13 17:33:35 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
2015-12-10 15:31:50 +00:00
|
|
|
Name: "latency_seconds",
|
2015-01-13 17:33:35 +00:00
|
|
|
Help: "Latency quantiles for sending alert notifications (not including dropped notifications).",
|
2016-06-02 12:25:19 +00:00
|
|
|
},
|
|
|
|
[]string{alertmanagerLabel},
|
|
|
|
),
|
|
|
|
errors: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2015-01-13 17:33:35 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "errors_total",
|
|
|
|
Help: "Total number of errors sending alert notifications.",
|
2016-06-02 12:25:19 +00:00
|
|
|
},
|
|
|
|
[]string{alertmanagerLabel},
|
|
|
|
),
|
|
|
|
sent: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2015-12-10 15:31:50 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "sent_total",
|
|
|
|
Help: "Total number of alerts successfully sent.",
|
2016-06-02 12:25:19 +00:00
|
|
|
},
|
|
|
|
[]string{alertmanagerLabel},
|
|
|
|
),
|
2015-12-10 15:31:50 +00:00
|
|
|
dropped: prometheus.NewCounter(prometheus.CounterOpts{
|
2015-01-13 17:33:35 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "dropped_total",
|
2017-01-13 23:36:00 +00:00
|
|
|
Help: "Total number of alerts dropped due to errors when sending to Alertmanager.",
|
2015-01-13 17:33:35 +00:00
|
|
|
}),
|
2017-03-31 18:44:30 +00:00
|
|
|
queueLength: prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
2014-07-23 17:55:33 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "queue_length",
|
|
|
|
Help: "The number of alert notifications in the queue.",
|
2017-03-31 18:44:30 +00:00
|
|
|
}, queueLen),
|
|
|
|
queueCapacity: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "queue_capacity",
|
|
|
|
Help: "The capacity of the alert notifications queue.",
|
2014-07-23 17:55:33 +00:00
|
|
|
}),
|
2017-05-04 11:54:08 +00:00
|
|
|
alertmanagersDiscovered: prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "prometheus_notifications_alertmanagers_discovered",
|
|
|
|
Help: "The number of alertmanagers discovered and active.",
|
|
|
|
}, alertmanagersDiscovered),
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2017-03-02 21:23:16 +00:00
|
|
|
|
2017-03-31 18:44:30 +00:00
|
|
|
m.queueCapacity.Set(float64(queueCap))
|
|
|
|
|
2017-03-02 21:23:16 +00:00
|
|
|
if r != nil {
|
|
|
|
r.MustRegister(
|
|
|
|
m.latency,
|
|
|
|
m.errors,
|
|
|
|
m.sent,
|
|
|
|
m.dropped,
|
2017-03-31 18:44:30 +00:00
|
|
|
m.queueLength,
|
|
|
|
m.queueCapacity,
|
2017-05-04 11:54:08 +00:00
|
|
|
m.alertmanagersDiscovered,
|
2017-03-02 21:23:16 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a new Notifier.
|
2017-03-31 15:50:12 +00:00
|
|
|
func New(o *Options) *Notifier {
|
2017-03-02 21:23:16 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
if o.Do == nil {
|
|
|
|
o.Do = ctxhttp.Do
|
|
|
|
}
|
|
|
|
|
2017-03-31 18:44:30 +00:00
|
|
|
n := &Notifier{
|
2017-04-04 12:44:39 +00:00
|
|
|
queue: make([]*Alert, 0, o.QueueCapacity),
|
2017-03-31 18:44:30 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
more: make(chan struct{}, 1),
|
|
|
|
opts: o,
|
2017-03-02 21:23:16 +00:00
|
|
|
}
|
2017-03-31 18:44:30 +00:00
|
|
|
|
|
|
|
queueLenFunc := func() float64 { return float64(n.queueLen()) }
|
2017-05-04 11:54:08 +00:00
|
|
|
alertmanagersDiscoveredFunc := func() float64 { return float64(len(n.Alertmanagers())) }
|
|
|
|
n.metrics = newAlertMetrics(o.Registerer, o.QueueCapacity, queueLenFunc, alertmanagersDiscoveredFunc)
|
2017-03-31 18:44:30 +00:00
|
|
|
return n
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 20:17:02 +00:00
|
|
|
// ApplyConfig updates the status state as the new config requires.
|
2016-07-11 14:24:54 +00:00
|
|
|
func (n *Notifier) ApplyConfig(conf *config.Config) error {
|
2015-09-01 20:17:02 +00:00
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
n.opts.ExternalLabels = conf.GlobalConfig.ExternalLabels
|
2016-08-09 11:09:36 +00:00
|
|
|
n.opts.RelabelConfigs = conf.AlertingConfig.AlertRelabelConfigs
|
2016-11-23 16:03:22 +00:00
|
|
|
|
|
|
|
amSets := []*alertmanagerSet{}
|
|
|
|
ctx, cancel := context.WithCancel(n.ctx)
|
|
|
|
|
2016-11-24 14:17:50 +00:00
|
|
|
for _, cfg := range conf.AlertingConfig.AlertmanagerConfigs {
|
2016-11-23 16:03:22 +00:00
|
|
|
ams, err := newAlertmanagerSet(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-02 18:28:15 +00:00
|
|
|
|
2017-03-02 21:23:16 +00:00
|
|
|
ams.metrics = n.metrics
|
2017-03-02 18:28:15 +00:00
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
amSets = append(amSets, ams)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After all sets were created successfully, start them and cancel the
|
|
|
|
// old ones.
|
|
|
|
for _, ams := range amSets {
|
|
|
|
go ams.ts.Run(ctx)
|
|
|
|
ams.ts.UpdateProviders(discovery.ProvidersFromConfig(ams.cfg.ServiceDiscoveryConfig))
|
|
|
|
}
|
|
|
|
if n.cancelDiscovery != nil {
|
|
|
|
n.cancelDiscovery()
|
|
|
|
}
|
|
|
|
|
|
|
|
n.cancelDiscovery = cancel
|
|
|
|
n.alertmanagers = amSets
|
|
|
|
|
2016-07-11 14:24:54 +00:00
|
|
|
return nil
|
2015-09-01 20:17:02 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
const maxBatchSize = 64
|
|
|
|
|
2016-03-01 11:37:22 +00:00
|
|
|
func (n *Notifier) queueLen() int {
|
2015-09-01 20:17:02 +00:00
|
|
|
n.mtx.RLock()
|
|
|
|
defer n.mtx.RUnlock()
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
return len(n.queue)
|
|
|
|
}
|
2013-07-30 15:18:07 +00:00
|
|
|
|
2016-12-24 23:37:46 +00:00
|
|
|
func (n *Notifier) nextBatch() []*Alert {
|
2015-12-10 15:31:50 +00:00
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
2016-12-24 23:37:46 +00:00
|
|
|
var alerts []*Alert
|
2015-12-10 15:31:50 +00:00
|
|
|
|
|
|
|
if len(n.queue) > maxBatchSize {
|
2016-12-24 23:37:46 +00:00
|
|
|
alerts = append(make([]*Alert, 0, maxBatchSize), n.queue[:maxBatchSize]...)
|
2015-12-10 15:31:50 +00:00
|
|
|
n.queue = n.queue[maxBatchSize:]
|
|
|
|
} else {
|
2016-12-24 23:37:46 +00:00
|
|
|
alerts = append(make([]*Alert, 0, len(n.queue)), n.queue...)
|
2015-12-10 15:31:50 +00:00
|
|
|
n.queue = n.queue[:0]
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2015-12-10 15:31:50 +00:00
|
|
|
|
|
|
|
return alerts
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 12:19:02 +00:00
|
|
|
// Run dispatches notifications continuously.
|
2016-03-01 11:37:22 +00:00
|
|
|
func (n *Notifier) Run() {
|
2015-12-10 15:31:50 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-n.more:
|
|
|
|
}
|
|
|
|
alerts := n.nextBatch()
|
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
if !n.sendAll(alerts...) {
|
2017-03-02 21:23:16 +00:00
|
|
|
n.metrics.dropped.Add(float64(len(alerts)))
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2015-12-10 15:31:50 +00:00
|
|
|
// If the queue still has items left, kick off the next iteration.
|
|
|
|
if n.queueLen() > 0 {
|
|
|
|
n.setMore()
|
|
|
|
}
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2014-10-10 12:19:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 21:58:32 +00:00
|
|
|
// Send queues the given notification requests for processing.
|
2016-01-18 15:47:31 +00:00
|
|
|
// Panics if called on a handler that is not running.
|
2016-12-24 23:37:46 +00:00
|
|
|
func (n *Notifier) Send(alerts ...*Alert) {
|
2015-12-10 15:31:50 +00:00
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
2016-09-27 12:34:56 +00:00
|
|
|
// Attach external labels before relabelling and sending.
|
|
|
|
for _, a := range alerts {
|
2016-12-24 23:37:46 +00:00
|
|
|
lb := labels.NewBuilder(a.Labels)
|
|
|
|
|
2016-09-27 12:34:56 +00:00
|
|
|
for ln, lv := range n.opts.ExternalLabels {
|
2016-12-24 23:37:46 +00:00
|
|
|
if a.Labels.Get(string(ln)) == "" {
|
|
|
|
lb.Set(string(ln), string(lv))
|
2016-09-27 12:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 15:53:11 +00:00
|
|
|
|
|
|
|
a.Labels = lb.Labels()
|
2016-09-27 12:34:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 08:08:15 +00:00
|
|
|
alerts = n.relabelAlerts(alerts)
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
// Queue capacity should be significantly larger than a single alert
|
|
|
|
// batch could be.
|
|
|
|
if d := len(alerts) - n.opts.QueueCapacity; d > 0 {
|
|
|
|
alerts = alerts[d:]
|
|
|
|
|
|
|
|
log.Warnf("Alert batch larger than queue capacity, dropping %d alerts", d)
|
2017-03-02 21:23:16 +00:00
|
|
|
n.metrics.dropped.Add(float64(d))
|
2015-12-10 15:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the queue is full, remove the oldest alerts in favor
|
|
|
|
// of newer ones.
|
|
|
|
if d := (len(n.queue) + len(alerts)) - n.opts.QueueCapacity; d > 0 {
|
|
|
|
n.queue = n.queue[d:]
|
|
|
|
|
|
|
|
log.Warnf("Alert notification queue full, dropping %d alerts", d)
|
2017-03-02 21:23:16 +00:00
|
|
|
n.metrics.dropped.Add(float64(d))
|
2015-12-10 15:31:50 +00:00
|
|
|
}
|
|
|
|
n.queue = append(n.queue, alerts...)
|
|
|
|
|
|
|
|
// Notify sending goroutine that there are alerts to be processed.
|
|
|
|
n.setMore()
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:37:46 +00:00
|
|
|
func (n *Notifier) relabelAlerts(alerts []*Alert) []*Alert {
|
2016-12-29 15:53:11 +00:00
|
|
|
var relabeledAlerts []*Alert
|
2016-12-24 23:37:46 +00:00
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
for _, alert := range alerts {
|
|
|
|
labels := relabel.Process(alert.Labels, n.opts.RelabelConfigs...)
|
|
|
|
if labels != nil {
|
|
|
|
alert.Labels = labels
|
|
|
|
relabeledAlerts = append(relabeledAlerts, alert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relabeledAlerts
|
2016-08-09 08:08:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 15:31:50 +00:00
|
|
|
// setMore signals that the alert queue has items.
|
2016-03-01 11:37:22 +00:00
|
|
|
func (n *Notifier) setMore() {
|
2015-12-10 15:31:50 +00:00
|
|
|
// If we cannot send on the channel, it means the signal already exists
|
|
|
|
// and has not been consumed yet.
|
|
|
|
select {
|
|
|
|
case n.more <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:42:33 +00:00
|
|
|
// Alertmanagers returns a slice of Alertmanager URLs.
|
|
|
|
func (n *Notifier) Alertmanagers() []*url.URL {
|
2016-11-23 17:23:09 +00:00
|
|
|
n.mtx.RLock()
|
|
|
|
amSets := n.alertmanagers
|
|
|
|
n.mtx.RUnlock()
|
|
|
|
|
2017-04-25 05:42:33 +00:00
|
|
|
var res []*url.URL
|
2016-11-23 17:23:09 +00:00
|
|
|
|
|
|
|
for _, ams := range amSets {
|
|
|
|
ams.mtx.RLock()
|
|
|
|
for _, am := range ams.ams {
|
|
|
|
res = append(res, am.url())
|
|
|
|
}
|
|
|
|
ams.mtx.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2016-11-24 14:17:50 +00:00
|
|
|
// sendAll sends the alerts to all configured Alertmanagers concurrently.
|
2016-11-25 07:47:04 +00:00
|
|
|
// It returns true if the alerts could be sent successfully to at least one Alertmanager.
|
2016-12-24 23:37:46 +00:00
|
|
|
func (n *Notifier) sendAll(alerts ...*Alert) bool {
|
2016-06-02 12:25:19 +00:00
|
|
|
begin := time.Now()
|
|
|
|
|
|
|
|
b, err := json.Marshal(alerts)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Encoding alerts failed: %s", err)
|
2016-11-23 16:03:22 +00:00
|
|
|
return false
|
2015-12-10 15:31:50 +00:00
|
|
|
}
|
2016-06-02 12:25:19 +00:00
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
n.mtx.RLock()
|
|
|
|
amSets := n.alertmanagers
|
|
|
|
n.mtx.RUnlock()
|
2015-12-10 15:31:50 +00:00
|
|
|
|
2016-06-02 12:25:19 +00:00
|
|
|
var (
|
2016-11-23 16:03:22 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
numSuccess uint64
|
2016-06-02 12:25:19 +00:00
|
|
|
)
|
2016-11-23 16:03:22 +00:00
|
|
|
for _, ams := range amSets {
|
|
|
|
ams.mtx.RLock()
|
|
|
|
|
|
|
|
for _, am := range ams.ams {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(n.ctx, ams.cfg.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func(am alertmanager) {
|
2017-04-25 05:42:33 +00:00
|
|
|
u := am.url().String()
|
2016-11-23 16:03:22 +00:00
|
|
|
|
2016-11-25 10:11:28 +00:00
|
|
|
if err := n.sendOne(ctx, ams.client, u, b); err != nil {
|
2016-11-23 16:03:22 +00:00
|
|
|
log.With("alertmanager", u).With("count", len(alerts)).Errorf("Error sending alerts: %s", err)
|
2017-03-02 21:23:16 +00:00
|
|
|
n.metrics.errors.WithLabelValues(u).Inc()
|
2016-11-23 16:03:22 +00:00
|
|
|
} else {
|
|
|
|
atomic.AddUint64(&numSuccess, 1)
|
|
|
|
}
|
2017-03-02 21:23:16 +00:00
|
|
|
n.metrics.latency.WithLabelValues(u).Observe(time.Since(begin).Seconds())
|
|
|
|
n.metrics.sent.WithLabelValues(u).Add(float64(len(alerts)))
|
2016-06-02 12:25:19 +00:00
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
wg.Done()
|
|
|
|
}(am)
|
|
|
|
}
|
|
|
|
ams.mtx.RUnlock()
|
2015-12-10 15:31:50 +00:00
|
|
|
}
|
2016-06-02 12:25:19 +00:00
|
|
|
wg.Wait()
|
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
return numSuccess > 0
|
2014-10-10 12:19:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 10:11:28 +00:00
|
|
|
func (n *Notifier) sendOne(ctx context.Context, c *http.Client, url string, b []byte) error {
|
2017-02-27 19:31:16 +00:00
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", contentTypeJSON)
|
|
|
|
resp, err := n.opts.Do(ctx, c, req)
|
2016-11-25 10:11:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Any HTTP status 2xx is OK.
|
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return fmt.Errorf("bad response status %v", resp.Status)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-10 12:19:02 +00:00
|
|
|
// Stop shuts down the notification handler.
|
2016-03-01 11:37:22 +00:00
|
|
|
func (n *Notifier) Stop() {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Info("Stopping notification handler...")
|
2015-12-10 15:31:50 +00:00
|
|
|
n.cancel()
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2014-06-18 17:43:15 +00:00
|
|
|
|
2016-11-24 14:17:50 +00:00
|
|
|
// alertmanager holds Alertmanager endpoint information.
|
2016-11-25 10:11:28 +00:00
|
|
|
type alertmanager interface {
|
2017-04-25 05:42:33 +00:00
|
|
|
url() *url.URL
|
2016-11-23 16:03:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
type alertmanagerLabels struct{ labels.Labels }
|
2016-11-25 10:11:28 +00:00
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
const pathLabel = "__alerts_path__"
|
|
|
|
|
2017-04-25 05:42:33 +00:00
|
|
|
func (a alertmanagerLabels) url() *url.URL {
|
|
|
|
return &url.URL{
|
2016-12-29 15:53:11 +00:00
|
|
|
Scheme: a.Get(model.SchemeLabel),
|
|
|
|
Host: a.Get(model.AddressLabel),
|
|
|
|
Path: a.Get(pathLabel),
|
2016-11-23 16:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// alertmanagerSet contains a set of Alertmanagers discovered via a group of service
|
|
|
|
// discovery definitions that have a common configuration on how alerts should be sent.
|
|
|
|
type alertmanagerSet struct {
|
|
|
|
ts *discovery.TargetSet
|
2016-11-24 14:17:50 +00:00
|
|
|
cfg *config.AlertmanagerConfig
|
2016-11-23 16:03:22 +00:00
|
|
|
client *http.Client
|
|
|
|
|
2017-03-02 21:23:16 +00:00
|
|
|
metrics *alertMetrics
|
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
ams []alertmanager
|
|
|
|
}
|
|
|
|
|
2016-11-24 14:17:50 +00:00
|
|
|
func newAlertmanagerSet(cfg *config.AlertmanagerConfig) (*alertmanagerSet, error) {
|
2017-03-20 13:17:04 +00:00
|
|
|
client, err := httputil.NewClientFromConfig(cfg.HTTPClientConfig)
|
2016-11-23 16:03:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := &alertmanagerSet{
|
|
|
|
client: client,
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
s.ts = discovery.NewTargetSet(s)
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync extracts a deduplicated set of Alertmanager endpoints from a list
|
|
|
|
// of target groups definitions.
|
|
|
|
func (s *alertmanagerSet) Sync(tgs []*config.TargetGroup) {
|
|
|
|
all := []alertmanager{}
|
|
|
|
|
|
|
|
for _, tg := range tgs {
|
|
|
|
ams, err := alertmanagerFromGroup(tg, s.cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.With("err", err).Error("generating discovered Alertmanagers failed")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
all = append(all, ams...)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
// Set new Alertmanagers and deduplicate them along their unique URL.
|
|
|
|
s.ams = []alertmanager{}
|
|
|
|
seen := map[string]struct{}{}
|
|
|
|
|
|
|
|
for _, am := range all {
|
2017-04-25 05:42:33 +00:00
|
|
|
us := am.url().String()
|
2016-11-23 16:03:22 +00:00
|
|
|
if _, ok := seen[us]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:29:20 +00:00
|
|
|
// This will initialise the Counters for the AM to 0.
|
2017-03-02 21:23:16 +00:00
|
|
|
s.metrics.sent.WithLabelValues(us)
|
|
|
|
s.metrics.errors.WithLabelValues(us)
|
2017-03-02 18:28:15 +00:00
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
seen[us] = struct{}{}
|
|
|
|
s.ams = append(s.ams, am)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func postPath(pre string) string {
|
|
|
|
return path.Join("/", pre, alertPushEndpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// alertmanagersFromGroup extracts a list of alertmanagers from a target group and an associcated
|
|
|
|
// AlertmanagerConfig.
|
2016-11-24 14:17:50 +00:00
|
|
|
func alertmanagerFromGroup(tg *config.TargetGroup, cfg *config.AlertmanagerConfig) ([]alertmanager, error) {
|
2016-11-23 16:03:22 +00:00
|
|
|
var res []alertmanager
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
for _, tlset := range tg.Targets {
|
|
|
|
lbls := make([]labels.Label, 0, len(tlset)+2+len(tg.Labels))
|
|
|
|
|
|
|
|
for ln, lv := range tlset {
|
|
|
|
lbls = append(lbls, labels.Label{Name: string(ln), Value: string(lv)})
|
|
|
|
}
|
2016-11-23 16:03:22 +00:00
|
|
|
// Set configured scheme as the initial scheme label for overwrite.
|
2016-12-29 15:53:11 +00:00
|
|
|
lbls = append(lbls, labels.Label{Name: model.SchemeLabel, Value: cfg.Scheme})
|
|
|
|
lbls = append(lbls, labels.Label{Name: pathLabel, Value: postPath(cfg.PathPrefix)})
|
2016-11-23 16:03:22 +00:00
|
|
|
|
|
|
|
// Combine target labels with target group labels.
|
|
|
|
for ln, lv := range tg.Labels {
|
2016-12-29 15:53:11 +00:00
|
|
|
if _, ok := tlset[ln]; !ok {
|
|
|
|
lbls = append(lbls, labels.Label{Name: string(ln), Value: string(lv)})
|
2016-11-23 16:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 15:53:11 +00:00
|
|
|
|
|
|
|
lset := relabel.Process(labels.New(lbls...), cfg.RelabelConfigs...)
|
2016-11-23 16:03:22 +00:00
|
|
|
if lset == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
lb := labels.NewBuilder(lset)
|
|
|
|
|
2016-11-23 16:03:22 +00:00
|
|
|
// addPort checks whether we should add a default port to the address.
|
|
|
|
// If the address is not valid, we don't append a port either.
|
|
|
|
addPort := func(s string) bool {
|
|
|
|
// If we can split, a port exists and we don't have to add one.
|
|
|
|
if _, _, err := net.SplitHostPort(s); err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// If adding a port makes it valid, the previous error
|
|
|
|
// was not due to an invalid address and we can append a port.
|
|
|
|
_, _, err := net.SplitHostPort(s + ":1234")
|
|
|
|
return err == nil
|
|
|
|
}
|
2016-12-29 15:53:11 +00:00
|
|
|
addr := lset.Get(model.AddressLabel)
|
2016-11-23 16:03:22 +00:00
|
|
|
// If it's an address with no trailing port, infer it based on the used scheme.
|
2016-12-29 15:53:11 +00:00
|
|
|
if addPort(addr) {
|
2016-11-23 16:03:22 +00:00
|
|
|
// Addresses reaching this point are already wrapped in [] if necessary.
|
2016-12-29 15:53:11 +00:00
|
|
|
switch lset.Get(model.SchemeLabel) {
|
2016-11-23 16:03:22 +00:00
|
|
|
case "http", "":
|
|
|
|
addr = addr + ":80"
|
|
|
|
case "https":
|
|
|
|
addr = addr + ":443"
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid scheme: %q", cfg.Scheme)
|
|
|
|
}
|
2016-12-29 15:53:11 +00:00
|
|
|
lb.Set(model.AddressLabel, addr)
|
2016-11-23 16:03:22 +00:00
|
|
|
}
|
2016-12-29 15:53:11 +00:00
|
|
|
|
|
|
|
if err := config.CheckTargetAddress(model.LabelValue(addr)); err != nil {
|
2016-11-23 16:03:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meta labels are deleted after relabelling. Other internal labels propagate to
|
|
|
|
// the target which decides whether they will be part of their label set.
|
2016-12-29 15:53:11 +00:00
|
|
|
for _, l := range lset {
|
|
|
|
if strings.HasPrefix(l.Name, model.MetaLabelPrefix) {
|
|
|
|
lb.Del(l.Name)
|
2016-11-23 16:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 15:53:11 +00:00
|
|
|
res = append(res, alertmanagerLabels{lset})
|
2016-11-23 16:03:22 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|