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.
|
|
|
|
|
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2013-08-09 10:32:55 -07:00
|
|
|
"io"
|
2013-07-30 08:18:07 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-03-23 11:17:42 -07:00
|
|
|
"strings"
|
2015-09-01 13:17:02 -07:00
|
|
|
"sync"
|
2013-07-30 08:18:07 -07:00
|
|
|
"time"
|
|
|
|
|
2014-09-24 07:52:00 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-08-22 00:42:45 -07:00
|
|
|
"github.com/prometheus/log"
|
2013-07-30 08:18:07 -07:00
|
|
|
|
2015-09-01 13:17:02 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-05-29 04:30:30 -07:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2013-07-30 08:18:07 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-12-10 07:16:49 -08:00
|
|
|
alertmanagerAPIEventsPath = "/api/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 (
|
2014-07-23 10:55:33 -07:00
|
|
|
namespace = "prometheus"
|
|
|
|
subsystem = "notifications"
|
2014-06-18 10:43:15 -07:00
|
|
|
)
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NotificationReq is a request for sending a notification to the alert manager
|
|
|
|
// for a single alert vector element.
|
2013-08-09 10:32:55 -07:00
|
|
|
type NotificationReq struct {
|
|
|
|
// Short-form alert summary. May contain text/template-style interpolations.
|
|
|
|
Summary string
|
|
|
|
// Longer alert description. May contain text/template-style interpolations.
|
|
|
|
Description string
|
2015-06-24 10:27:09 -07:00
|
|
|
// A reference to the runbook for the alert.
|
|
|
|
Runbook string
|
2013-08-09 10:32:55 -07:00
|
|
|
// Labels associated with this alert notification, including alert name.
|
2015-08-20 08:18:46 -07:00
|
|
|
Labels model.LabelSet
|
2013-08-09 10:32:55 -07:00
|
|
|
// Current value of alert
|
2015-08-20 08:18:46 -07:00
|
|
|
Value model.SampleValue
|
2013-08-09 10:32:55 -07:00
|
|
|
// Since when this alert has been active (pending or firing).
|
|
|
|
ActiveSince time.Time
|
|
|
|
// A textual representation of the rule that triggered the alert.
|
|
|
|
RuleString string
|
2013-08-20 06:42:06 -07:00
|
|
|
// Prometheus console link to alert expression.
|
2014-10-27 08:59:38 -07:00
|
|
|
GeneratorURL string
|
2013-08-09 10:32:55 -07:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NotificationReqs is just a short-hand for []*NotificationReq. No methods
|
|
|
|
// attached. Arguably, it's more confusing than helpful. Perhaps we should
|
|
|
|
// remove it...
|
2013-08-09 10:32:55 -07:00
|
|
|
type NotificationReqs []*NotificationReq
|
|
|
|
|
|
|
|
type httpPoster interface {
|
|
|
|
Post(url string, bodyType string, body io.Reader) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
2013-07-30 08:18:07 -07:00
|
|
|
// NotificationHandler is responsible for dispatching alert notifications to an
|
|
|
|
// alert manager service.
|
|
|
|
type NotificationHandler struct {
|
|
|
|
// The URL of the alert manager to send notifications to.
|
2014-12-10 07:16:49 -08:00
|
|
|
alertmanagerURL string
|
2013-07-30 08:18:07 -07:00
|
|
|
// Buffer of notifications that have not yet been sent.
|
2014-10-10 05:19:02 -07:00
|
|
|
pendingNotifications chan NotificationReqs
|
2013-07-30 08:18:07 -07:00
|
|
|
// HTTP client with custom timeout settings.
|
2013-08-09 10:32:55 -07:00
|
|
|
httpClient httpPoster
|
2014-06-18 10:43:15 -07:00
|
|
|
|
2015-01-13 09:33:35 -08:00
|
|
|
notificationLatency prometheus.Summary
|
|
|
|
notificationErrors prometheus.Counter
|
|
|
|
notificationDropped prometheus.Counter
|
2014-07-23 10:55:33 -07:00
|
|
|
notificationsQueueLength prometheus.Gauge
|
|
|
|
notificationsQueueCapacity prometheus.Metric
|
2014-10-10 05:19:02 -07:00
|
|
|
|
2015-09-01 13:17:02 -07:00
|
|
|
globalLabels model.LabelSet
|
|
|
|
mtx sync.RWMutex
|
|
|
|
stopped chan struct{}
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
|
|
|
|
2015-06-15 04:03:17 -07:00
|
|
|
// NotificationHandlerOptions are the configurable parameters of a NotificationHandler.
|
|
|
|
type NotificationHandlerOptions struct {
|
|
|
|
AlertmanagerURL string
|
|
|
|
QueueCapacity int
|
|
|
|
Deadline time.Duration
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewNotificationHandler constructs a new NotificationHandler.
|
2015-06-15 04:03:17 -07:00
|
|
|
func NewNotificationHandler(o *NotificationHandlerOptions) *NotificationHandler {
|
2013-07-30 08:18:07 -07:00
|
|
|
return &NotificationHandler{
|
2015-06-15 04:03:17 -07:00
|
|
|
alertmanagerURL: strings.TrimRight(o.AlertmanagerURL, "/"),
|
|
|
|
pendingNotifications: make(chan NotificationReqs, o.QueueCapacity),
|
2014-10-10 05:19:02 -07:00
|
|
|
|
2015-08-06 09:12:55 -07:00
|
|
|
httpClient: httputil.NewDeadlineClient(o.Deadline, nil),
|
2014-06-18 10:43:15 -07:00
|
|
|
|
2015-01-13 09:33:35 -08:00
|
|
|
notificationLatency: prometheus.NewSummary(prometheus.SummaryOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "latency_milliseconds",
|
|
|
|
Help: "Latency quantiles for sending alert notifications (not including dropped notifications).",
|
|
|
|
}),
|
|
|
|
notificationErrors: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "errors_total",
|
|
|
|
Help: "Total number of errors sending alert notifications.",
|
|
|
|
}),
|
|
|
|
notificationDropped: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "dropped_total",
|
2015-01-13 09:47:51 -08:00
|
|
|
Help: "Total number of alert notifications dropped due to alert manager missing in configuration.",
|
2015-01-13 09:33:35 -08:00
|
|
|
}),
|
2014-07-23 10:55:33 -07:00
|
|
|
notificationsQueueLength: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "queue_length",
|
|
|
|
Help: "The number of alert notifications in the queue.",
|
|
|
|
}),
|
|
|
|
notificationsQueueCapacity: prometheus.MustNewConstMetric(
|
|
|
|
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
|
|
|
),
|
2014-10-10 05:19:02 -07:00
|
|
|
stopped: make(chan struct{}),
|
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.
|
|
|
|
// Returns true on success.
|
|
|
|
func (n *NotificationHandler) ApplyConfig(conf *config.Config) bool {
|
|
|
|
n.mtx.Lock()
|
|
|
|
defer n.mtx.Unlock()
|
|
|
|
|
|
|
|
n.globalLabels = conf.GlobalConfig.Labels
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-07-30 08:18:07 -07:00
|
|
|
// Send a list of notifications to the configured alert manager.
|
2013-08-09 10:32:55 -07:00
|
|
|
func (n *NotificationHandler) sendNotifications(reqs NotificationReqs) error {
|
2015-09-01 13:17:02 -07:00
|
|
|
n.mtx.RLock()
|
|
|
|
defer n.mtx.RUnlock()
|
|
|
|
|
2013-07-30 08:18:07 -07:00
|
|
|
alerts := make([]map[string]interface{}, 0, len(reqs))
|
|
|
|
for _, req := range reqs {
|
2015-09-01 13:17:02 -07:00
|
|
|
for ln, lv := range n.globalLabels {
|
|
|
|
if _, ok := req.Labels[ln]; !ok {
|
|
|
|
req.Labels[ln] = lv
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 08:18:07 -07:00
|
|
|
alerts = append(alerts, map[string]interface{}{
|
2015-05-30 06:35:51 -07:00
|
|
|
"summary": req.Summary,
|
|
|
|
"description": req.Description,
|
2015-06-24 10:27:09 -07:00
|
|
|
"runbook": req.Runbook,
|
2015-05-30 06:35:51 -07:00
|
|
|
"labels": req.Labels,
|
|
|
|
"payload": map[string]interface{}{
|
|
|
|
"value": req.Value,
|
|
|
|
"activeSince": req.ActiveSince,
|
|
|
|
"generatorURL": req.GeneratorURL,
|
|
|
|
"alertingRule": req.RuleString,
|
2013-07-30 08:18:07 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(alerts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Debugln("Sending notifications to alertmanager:", string(buf))
|
2013-07-30 08:18:07 -07:00
|
|
|
resp, err := n.httpClient.Post(
|
2014-12-10 07:16:49 -08:00
|
|
|
n.alertmanagerURL+alertmanagerAPIEventsPath,
|
|
|
|
contentTypeJSON,
|
2013-07-30 08:18:07 -07:00
|
|
|
bytes.NewBuffer(buf),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
_, err = ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// BUG: Do we need to check the response code?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-10 05:19:02 -07:00
|
|
|
// Run dispatches notifications continuously.
|
2013-07-30 08:18:07 -07:00
|
|
|
func (n *NotificationHandler) Run() {
|
|
|
|
for reqs := range n.pendingNotifications {
|
2014-12-10 07:16:49 -08:00
|
|
|
if n.alertmanagerURL == "" {
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Warn("No alert manager configured, not dispatching notification")
|
2015-01-13 09:33:35 -08:00
|
|
|
n.notificationDropped.Inc()
|
2013-07-30 15:46:01 -07:00
|
|
|
continue
|
2013-07-30 14:06:38 -07:00
|
|
|
}
|
2013-07-30 15:46:01 -07:00
|
|
|
|
|
|
|
begin := time.Now()
|
|
|
|
err := n.sendNotifications(reqs)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Error("Error sending notification: ", err)
|
2015-01-13 09:33:35 -08:00
|
|
|
n.notificationErrors.Inc()
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
2014-06-18 10:43:15 -07:00
|
|
|
|
2015-01-13 09:33:35 -08:00
|
|
|
n.notificationLatency.Observe(float64(time.Since(begin) / time.Millisecond))
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
2014-10-10 05:19:02 -07:00
|
|
|
close(n.stopped)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitReqs queues the given notification requests for processing.
|
|
|
|
func (n *NotificationHandler) SubmitReqs(reqs NotificationReqs) {
|
|
|
|
n.pendingNotifications <- reqs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop shuts down the notification handler.
|
|
|
|
func (n *NotificationHandler) Stop() {
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Info("Stopping notification handler...")
|
2014-10-10 05:19:02 -07:00
|
|
|
close(n.pendingNotifications)
|
|
|
|
<-n.stopped
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Info("Notification handler stopped.")
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
2014-06-18 10:43:15 -07:00
|
|
|
|
|
|
|
// Describe implements prometheus.Collector.
|
|
|
|
func (n *NotificationHandler) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
n.notificationLatency.Describe(ch)
|
2014-07-23 10:55:33 -07:00
|
|
|
ch <- n.notificationsQueueLength.Desc()
|
|
|
|
ch <- n.notificationsQueueCapacity.Desc()
|
2014-06-18 10:43:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect implements prometheus.Collector.
|
|
|
|
func (n *NotificationHandler) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
n.notificationLatency.Collect(ch)
|
2014-07-23 10:55:33 -07:00
|
|
|
n.notificationsQueueLength.Set(float64(len(n.pendingNotifications)))
|
|
|
|
ch <- n.notificationsQueueLength
|
|
|
|
ch <- n.notificationsQueueCapacity
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|