2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-07-30 08:18:07 -07: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 03:37:22 -08:00
|
|
|
package notifier
|
2013-07-30 08:18:07 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-12-10 07:31:50 -08:00
|
|
|
"fmt"
|
2013-07-30 08:18:07 -07:00
|
|
|
"net/http"
|
2016-02-04 02:56:14 -08:00
|
|
|
"strings"
|
2015-09-01 13:17:02 -07:00
|
|
|
"sync"
|
2016-06-02 05:25:19 -07:00
|
|
|
"sync/atomic"
|
2013-07-30 08:18:07 -07:00
|
|
|
"time"
|
|
|
|
|
2014-09-24 07:52:00 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-10-03 01:21:43 -07:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-12-10 07:31:50 -08:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/net/context/ctxhttp"
|
2013-07-30 08:18:07 -07:00
|
|
|
|
2015-09-01 13:17:02 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2016-08-09 01:08:15 -07:00
|
|
|
"github.com/prometheus/prometheus/retrieval"
|
2013-07-30 08:18:07 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-12-10 07:31:50 -08:00
|
|
|
alertPushEndpoint = "/api/v1/alerts"
|
|
|
|
contentTypeJSON = "application/json"
|
2013-07-30 08:18:07 -07:00
|
|
|
)
|
|
|
|
|
2014-06-18 10:43:15 -07:00
|
|
|
// String constants for instrumentation.
|
|
|
|
const (
|
2016-06-02 05:25:19 -07:00
|
|
|
namespace = "prometheus"
|
|
|
|
subsystem = "notifications"
|
|
|
|
alertmanagerLabel = "alertmanager"
|
2014-06-18 10:43:15 -07:00
|
|
|
)
|
|
|
|
|
2016-05-11 05:20:36 -07:00
|
|
|
// Notifier is responsible for dispatching alert notifications to an
|
2015-12-10 07:31:50 -08:00
|
|
|
// alert manager service.
|
2016-03-01 03:37:22 -08:00
|
|
|
type Notifier struct {
|
2015-12-10 07:31:50 -08:00
|
|
|
queue model.Alerts
|
2016-03-01 03:37:22 -08:00
|
|
|
opts *Options
|
2013-08-09 10:32:55 -07:00
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
more chan struct{}
|
|
|
|
mtx sync.RWMutex
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
2013-08-09 10:32:55 -07:00
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
latency *prometheus.SummaryVec
|
|
|
|
errors *prometheus.CounterVec
|
|
|
|
sent *prometheus.CounterVec
|
2015-12-10 07:31:50 -08:00
|
|
|
dropped prometheus.Counter
|
|
|
|
queueLength prometheus.Gauge
|
|
|
|
queueCapacity prometheus.Metric
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
|
|
|
|
2016-05-11 05:20:36 -07:00
|
|
|
// Options are the configurable parameters of a Handler.
|
2016-03-01 03:37:22 -08:00
|
|
|
type Options struct {
|
2016-06-02 05:25:19 -07:00
|
|
|
AlertmanagerURLs []string
|
|
|
|
QueueCapacity int
|
|
|
|
Timeout time.Duration
|
|
|
|
ExternalLabels model.LabelSet
|
2016-08-09 01:08:15 -07:00
|
|
|
RelabelConfigs []*config.RelabelConfig
|
2015-06-15 04:03:17 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 01:08:15 -07:00
|
|
|
// New constructs a new Notifier.
|
2016-03-01 03:37:22 -08:00
|
|
|
func New(o *Options) *Notifier {
|
2015-12-10 07:31:50 -08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2014-10-10 05:19:02 -07:00
|
|
|
|
2016-03-01 03:37:22 -08:00
|
|
|
return &Notifier{
|
2015-12-10 07:31:50 -08:00
|
|
|
queue: make(model.Alerts, 0, o.QueueCapacity),
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
more: make(chan struct{}, 1),
|
|
|
|
opts: o,
|
2014-06-18 10:43:15 -07:00
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
latency: prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
2015-01-13 09:33:35 -08:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
2015-12-10 07:31:50 -08:00
|
|
|
Name: "latency_seconds",
|
2015-01-13 09:33:35 -08:00
|
|
|
Help: "Latency quantiles for sending alert notifications (not including dropped notifications).",
|
2016-06-02 05:25:19 -07:00
|
|
|
},
|
|
|
|
[]string{alertmanagerLabel},
|
|
|
|
),
|
|
|
|
errors: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2015-01-13 09:33:35 -08:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "errors_total",
|
|
|
|
Help: "Total number of errors sending alert notifications.",
|
2016-06-02 05:25:19 -07:00
|
|
|
},
|
|
|
|
[]string{alertmanagerLabel},
|
|
|
|
),
|
|
|
|
sent: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2015-12-10 07:31:50 -08:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "sent_total",
|
|
|
|
Help: "Total number of alerts successfully sent.",
|
2016-06-02 05:25:19 -07:00
|
|
|
},
|
|
|
|
[]string{alertmanagerLabel},
|
|
|
|
),
|
2015-12-10 07:31:50 -08:00
|
|
|
dropped: prometheus.NewCounter(prometheus.CounterOpts{
|
2015-01-13 09:33:35 -08:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "dropped_total",
|
2015-12-10 07:31:50 -08:00
|
|
|
Help: "Total number of alerts dropped due to alert manager missing in configuration.",
|
2015-01-13 09:33:35 -08:00
|
|
|
}),
|
2015-12-10 07:31:50 -08:00
|
|
|
queueLength: prometheus.NewGauge(prometheus.GaugeOpts{
|
2014-07-23 10:55:33 -07:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "queue_length",
|
|
|
|
Help: "The number of alert notifications in the queue.",
|
|
|
|
}),
|
2015-12-10 07:31:50 -08:00
|
|
|
queueCapacity: prometheus.MustNewConstMetric(
|
2014-07-23 10:55:33 -07:00
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, subsystem, "queue_capacity"),
|
|
|
|
"The capacity of the alert notifications queue.",
|
|
|
|
nil, nil,
|
|
|
|
),
|
|
|
|
prometheus.GaugeValue,
|
2015-06-15 04:03:17 -07:00
|
|
|
float64(o.QueueCapacity),
|
2014-06-18 10:43:15 -07:00
|
|
|
),
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 13:17:02 -07:00
|
|
|
// ApplyConfig updates the status state as the new config requires.
|
2016-07-11 07:24:54 -07:00
|
|
|
func (n *Notifier) ApplyConfig(conf *config.Config) error {
|
2015-09-01 13:17:02 -07:00
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
n.opts.ExternalLabels = conf.GlobalConfig.ExternalLabels
|
2016-08-09 01:08:15 -07:00
|
|
|
n.opts.RelabelConfigs = conf.AlertRelabelConfigs
|
2016-07-11 07:24:54 -07:00
|
|
|
return nil
|
2015-09-01 13:17:02 -07:00
|
|
|
}
|
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
const maxBatchSize = 64
|
|
|
|
|
2016-03-01 03:37:22 -08:00
|
|
|
func (n *Notifier) queueLen() int {
|
2015-09-01 13:17:02 -07:00
|
|
|
n.mtx.RLock()
|
|
|
|
defer n.mtx.RUnlock()
|
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
return len(n.queue)
|
|
|
|
}
|
2013-07-30 08:18:07 -07:00
|
|
|
|
2016-03-01 03:37:22 -08:00
|
|
|
func (n *Notifier) nextBatch() []*model.Alert {
|
2015-12-10 07:31:50 -08:00
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
|
|
|
var alerts model.Alerts
|
|
|
|
|
|
|
|
if len(n.queue) > maxBatchSize {
|
|
|
|
alerts = append(make(model.Alerts, 0, maxBatchSize), n.queue[:maxBatchSize]...)
|
|
|
|
n.queue = n.queue[maxBatchSize:]
|
|
|
|
} else {
|
|
|
|
alerts = append(make(model.Alerts, 0, len(n.queue)), n.queue...)
|
|
|
|
n.queue = n.queue[:0]
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
2015-12-10 07:31:50 -08:00
|
|
|
|
|
|
|
return alerts
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
|
|
|
|
2014-10-10 05:19:02 -07:00
|
|
|
// Run dispatches notifications continuously.
|
2016-03-01 03:37:22 -08:00
|
|
|
func (n *Notifier) Run() {
|
2016-06-02 05:25:19 -07:00
|
|
|
numAMs := len(n.opts.AlertmanagerURLs)
|
2015-12-17 02:46:10 -08:00
|
|
|
// Just warn once in the beginning to prevent noisy logs.
|
2016-06-02 05:25:19 -07:00
|
|
|
if numAMs == 0 {
|
|
|
|
log.Warnf("No AlertManagers configured, not dispatching any alerts")
|
|
|
|
return
|
2015-12-15 03:11:38 -08:00
|
|
|
}
|
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-n.more:
|
|
|
|
}
|
|
|
|
alerts := n.nextBatch()
|
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
if numAMs > 0 {
|
2013-07-30 15:46:01 -07:00
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
if len(alerts) > 0 {
|
|
|
|
numErrors := n.sendAll(alerts...)
|
|
|
|
// Increment the dropped counter if we could not send
|
|
|
|
// successfully to a single AlertManager.
|
|
|
|
if numErrors == numAMs {
|
|
|
|
n.dropped.Add(float64(len(alerts)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-12-10 07:31:50 -08:00
|
|
|
n.dropped.Add(float64(len(alerts)))
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
2015-12-10 07:31:50 -08:00
|
|
|
// If the queue still has items left, kick off the next iteration.
|
|
|
|
if n.queueLen() > 0 {
|
|
|
|
n.setMore()
|
|
|
|
}
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
2014-10-10 05:19:02 -07:00
|
|
|
}
|
|
|
|
|
2016-02-29 13:58:32 -08:00
|
|
|
// Send queues the given notification requests for processing.
|
2016-01-18 07:47:31 -08:00
|
|
|
// Panics if called on a handler that is not running.
|
2016-03-01 03:37:22 -08:00
|
|
|
func (n *Notifier) Send(alerts ...*model.Alert) {
|
2015-12-10 07:31:50 -08:00
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
2016-08-09 01:08:15 -07:00
|
|
|
alerts = n.relabelAlerts(alerts)
|
|
|
|
|
2015-12-10 07:31:50 -08: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)
|
|
|
|
n.dropped.Add(float64(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
n.dropped.Add(float64(d))
|
|
|
|
}
|
|
|
|
n.queue = append(n.queue, alerts...)
|
|
|
|
|
|
|
|
// Notify sending goroutine that there are alerts to be processed.
|
|
|
|
n.setMore()
|
|
|
|
}
|
|
|
|
|
2016-08-09 01:08:15 -07:00
|
|
|
func (n *Notifier) relabelAlerts(alerts []*model.Alert) []*model.Alert {
|
|
|
|
var relabeledAlerts []*model.Alert
|
|
|
|
for _, alert := range alerts {
|
|
|
|
labels, _ := retrieval.Relabel(alert.Labels, n.opts.RelabelConfigs...)
|
|
|
|
if labels != nil {
|
|
|
|
alert.Labels = labels
|
|
|
|
relabeledAlerts = append(relabeledAlerts, alert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relabeledAlerts
|
|
|
|
}
|
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
// setMore signals that the alert queue has items.
|
2016-03-01 03:37:22 -08:00
|
|
|
func (n *Notifier) setMore() {
|
2015-12-10 07:31:50 -08: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:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
func postURL(u string) string {
|
|
|
|
return strings.TrimRight(u, "/") + alertPushEndpoint
|
2015-12-10 07:31:50 -08:00
|
|
|
}
|
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
// sendAll sends the alerts to all configured Alertmanagers at concurrently.
|
|
|
|
// It returns the number of sends that have failed.
|
|
|
|
func (n *Notifier) sendAll(alerts ...*model.Alert) int {
|
|
|
|
begin := time.Now()
|
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
// Attach external labels before sending alerts.
|
|
|
|
for _, a := range alerts {
|
|
|
|
for ln, lv := range n.opts.ExternalLabels {
|
|
|
|
if _, ok := a.Labels[ln]; !ok {
|
|
|
|
a.Labels[ln] = lv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
b, err := json.Marshal(alerts)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Encoding alerts failed: %s", err)
|
|
|
|
return len(n.opts.AlertmanagerURLs)
|
2015-12-10 07:31:50 -08:00
|
|
|
}
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), n.opts.Timeout)
|
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
send := func(u string) error {
|
|
|
|
resp, err := ctxhttp.Post(ctx, http.DefaultClient, postURL(u), contentTypeJSON, bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return fmt.Errorf("bad response status %v", resp.Status)
|
|
|
|
}
|
2015-12-10 07:31:50 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
numErrors uint64
|
|
|
|
)
|
|
|
|
for _, u := range n.opts.AlertmanagerURLs {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func(u string) {
|
|
|
|
if err := send(u); err != nil {
|
|
|
|
log.With("alertmanager", u).With("count", fmt.Sprintf("%d", len(alerts))).Errorf("Error sending alerts: %s", err)
|
|
|
|
n.errors.WithLabelValues(u).Inc()
|
|
|
|
atomic.AddUint64(&numErrors, 1)
|
|
|
|
}
|
2016-07-07 06:24:35 -07:00
|
|
|
n.latency.WithLabelValues(u).Observe(time.Since(begin).Seconds())
|
2016-06-02 05:25:19 -07:00
|
|
|
n.sent.WithLabelValues(u).Add(float64(len(alerts)))
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}(u)
|
2015-12-10 07:31:50 -08:00
|
|
|
}
|
2016-06-02 05:25:19 -07:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return int(numErrors)
|
2014-10-10 05:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop shuts down the notification handler.
|
2016-03-01 03:37:22 -08:00
|
|
|
func (n *Notifier) Stop() {
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Info("Stopping notification handler...")
|
2015-12-10 07:31:50 -08:00
|
|
|
n.cancel()
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
2014-06-18 10:43:15 -07:00
|
|
|
|
|
|
|
// Describe implements prometheus.Collector.
|
2016-03-01 03:37:22 -08:00
|
|
|
func (n *Notifier) Describe(ch chan<- *prometheus.Desc) {
|
2016-06-02 05:25:19 -07:00
|
|
|
n.latency.Describe(ch)
|
|
|
|
n.errors.Describe(ch)
|
|
|
|
n.sent.Describe(ch)
|
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
ch <- n.dropped.Desc()
|
|
|
|
ch <- n.queueLength.Desc()
|
|
|
|
ch <- n.queueCapacity.Desc()
|
2014-06-18 10:43:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect implements prometheus.Collector.
|
2016-03-01 03:37:22 -08:00
|
|
|
func (n *Notifier) Collect(ch chan<- prometheus.Metric) {
|
2015-12-10 07:31:50 -08:00
|
|
|
n.queueLength.Set(float64(n.queueLen()))
|
|
|
|
|
2016-06-02 05:25:19 -07:00
|
|
|
n.latency.Collect(ch)
|
|
|
|
n.errors.Collect(ch)
|
|
|
|
n.sent.Collect(ch)
|
|
|
|
|
2015-12-10 07:31:50 -08:00
|
|
|
ch <- n.dropped
|
|
|
|
ch <- n.queueLength
|
|
|
|
ch <- n.queueCapacity
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|