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.
|
|
|
|
|
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2013-08-09 17:32:55 +00:00
|
|
|
"io"
|
2013-07-30 15:18:07 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-03-23 18:17:42 +00:00
|
|
|
"strings"
|
2013-07-30 15:18:07 +00:00
|
|
|
"time"
|
|
|
|
|
2014-09-24 14:52:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-05-20 16:10:29 +00:00
|
|
|
"github.com/prometheus/log"
|
2013-08-12 15:18:02 +00:00
|
|
|
|
2013-07-30 15:18:07 +00:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2015-05-29 11:30:30 +00:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2013-07-30 15:18:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-12-10 15:16:49 +00:00
|
|
|
alertmanagerAPIEventsPath = "/api/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 (
|
2014-07-23 17:55:33 +00:00
|
|
|
namespace = "prometheus"
|
|
|
|
subsystem = "notifications"
|
2014-06-18 17:43:15 +00:00
|
|
|
)
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// NotificationReq is a request for sending a notification to the alert manager
|
|
|
|
// for a single alert vector element.
|
2013-08-09 17:32:55 +00: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
|
|
|
|
// Labels associated with this alert notification, including alert name.
|
|
|
|
Labels clientmodel.LabelSet
|
|
|
|
// Current value of alert
|
|
|
|
Value clientmodel.SampleValue
|
|
|
|
// 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 13:42:06 +00:00
|
|
|
// Prometheus console link to alert expression.
|
2014-10-27 15:59:38 +00:00
|
|
|
GeneratorURL string
|
2013-08-09 17:32:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00: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 17:32:55 +00:00
|
|
|
type NotificationReqs []*NotificationReq
|
|
|
|
|
|
|
|
type httpPoster interface {
|
|
|
|
Post(url string, bodyType string, body io.Reader) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
2013-07-30 15:18:07 +00: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 15:16:49 +00:00
|
|
|
alertmanagerURL string
|
2013-07-30 15:18:07 +00:00
|
|
|
// Buffer of notifications that have not yet been sent.
|
2014-10-10 12:19:02 +00:00
|
|
|
pendingNotifications chan NotificationReqs
|
2013-07-30 15:18:07 +00:00
|
|
|
// HTTP client with custom timeout settings.
|
2013-08-09 17:32:55 +00:00
|
|
|
httpClient httpPoster
|
2014-06-18 17:43:15 +00:00
|
|
|
|
2015-01-13 17:33:35 +00:00
|
|
|
notificationLatency prometheus.Summary
|
|
|
|
notificationErrors prometheus.Counter
|
|
|
|
notificationDropped prometheus.Counter
|
2014-07-23 17:55:33 +00:00
|
|
|
notificationsQueueLength prometheus.Gauge
|
|
|
|
notificationsQueueCapacity prometheus.Metric
|
2014-10-10 12:19:02 +00:00
|
|
|
|
|
|
|
stopped chan struct{}
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 11:03:17 +00:00
|
|
|
// NotificationHandlerOptions are the configurable parameters of a NotificationHandler.
|
|
|
|
type NotificationHandlerOptions struct {
|
|
|
|
AlertmanagerURL string
|
|
|
|
QueueCapacity int
|
|
|
|
Deadline time.Duration
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// NewNotificationHandler constructs a new NotificationHandler.
|
2015-06-15 11:03:17 +00:00
|
|
|
func NewNotificationHandler(o *NotificationHandlerOptions) *NotificationHandler {
|
2013-07-30 15:18:07 +00:00
|
|
|
return &NotificationHandler{
|
2015-06-15 11:03:17 +00:00
|
|
|
alertmanagerURL: strings.TrimRight(o.AlertmanagerURL, "/"),
|
|
|
|
pendingNotifications: make(chan NotificationReqs, o.QueueCapacity),
|
2014-10-10 12:19:02 +00:00
|
|
|
|
2015-06-15 11:03:17 +00:00
|
|
|
httpClient: httputil.NewDeadlineClient(o.Deadline),
|
2014-06-18 17:43:15 +00:00
|
|
|
|
2015-01-13 17:33:35 +00: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 17:47:51 +00:00
|
|
|
Help: "Total number of alert notifications dropped due to alert manager missing in configuration.",
|
2015-01-13 17:33:35 +00:00
|
|
|
}),
|
2014-07-23 17:55:33 +00: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 11:03:17 +00:00
|
|
|
float64(o.QueueCapacity),
|
2014-06-18 17:43:15 +00:00
|
|
|
),
|
2014-10-10 12:19:02 +00:00
|
|
|
stopped: make(chan struct{}),
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a list of notifications to the configured alert manager.
|
2013-08-09 17:32:55 +00:00
|
|
|
func (n *NotificationHandler) sendNotifications(reqs NotificationReqs) error {
|
2013-07-30 15:18:07 +00:00
|
|
|
alerts := make([]map[string]interface{}, 0, len(reqs))
|
|
|
|
for _, req := range reqs {
|
|
|
|
alerts = append(alerts, map[string]interface{}{
|
2015-05-30 13:35:51 +00:00
|
|
|
"summary": req.Summary,
|
|
|
|
"description": req.Description,
|
|
|
|
"labels": req.Labels,
|
|
|
|
"payload": map[string]interface{}{
|
|
|
|
"value": req.Value,
|
|
|
|
"activeSince": req.ActiveSince,
|
|
|
|
"generatorURL": req.GeneratorURL,
|
|
|
|
"alertingRule": req.RuleString,
|
2013-07-30 15:18:07 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(alerts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Debugln("Sending notifications to alertmanager:", string(buf))
|
2013-07-30 15:18:07 +00:00
|
|
|
resp, err := n.httpClient.Post(
|
2014-12-10 15:16:49 +00:00
|
|
|
n.alertmanagerURL+alertmanagerAPIEventsPath,
|
|
|
|
contentTypeJSON,
|
2013-07-30 15:18:07 +00: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 12:19:02 +00:00
|
|
|
// Run dispatches notifications continuously.
|
2013-07-30 15:18:07 +00:00
|
|
|
func (n *NotificationHandler) Run() {
|
|
|
|
for reqs := range n.pendingNotifications {
|
2014-12-10 15:16:49 +00:00
|
|
|
if n.alertmanagerURL == "" {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Warn("No alert manager configured, not dispatching notification")
|
2015-01-13 17:33:35 +00:00
|
|
|
n.notificationDropped.Inc()
|
2013-07-30 22:46:01 +00:00
|
|
|
continue
|
2013-07-30 21:06:38 +00:00
|
|
|
}
|
2013-07-30 22:46:01 +00:00
|
|
|
|
|
|
|
begin := time.Now()
|
|
|
|
err := n.sendNotifications(reqs)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Error("Error sending notification: ", err)
|
2015-01-13 17:33:35 +00:00
|
|
|
n.notificationErrors.Inc()
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2014-06-18 17:43:15 +00:00
|
|
|
|
2015-01-13 17:33:35 +00:00
|
|
|
n.notificationLatency.Observe(float64(time.Since(begin) / time.Millisecond))
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2014-10-10 12:19:02 +00: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 16:10:29 +00:00
|
|
|
log.Info("Stopping notification handler...")
|
2014-10-10 12:19:02 +00:00
|
|
|
close(n.pendingNotifications)
|
|
|
|
<-n.stopped
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Info("Notification handler stopped.")
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2014-06-18 17:43:15 +00:00
|
|
|
|
|
|
|
// Describe implements prometheus.Collector.
|
|
|
|
func (n *NotificationHandler) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
n.notificationLatency.Describe(ch)
|
2014-07-23 17:55:33 +00:00
|
|
|
ch <- n.notificationsQueueLength.Desc()
|
|
|
|
ch <- n.notificationsQueueCapacity.Desc()
|
2014-06-18 17:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect implements prometheus.Collector.
|
|
|
|
func (n *NotificationHandler) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
n.notificationLatency.Collect(ch)
|
2014-07-23 17:55:33 +00:00
|
|
|
n.notificationsQueueLength.Set(float64(len(n.pendingNotifications)))
|
|
|
|
ch <- n.notificationsQueueLength
|
|
|
|
ch <- n.notificationsQueueCapacity
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|