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"
|
|
|
|
"flag"
|
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"
|
2013-07-30 08:18:07 -07:00
|
|
|
"time"
|
|
|
|
|
2014-09-24 07:52:00 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-05-20 09:10:29 -07:00
|
|
|
"github.com/prometheus/log"
|
2013-08-12 08:18:02 -07:00
|
|
|
|
2013-07-30 08:18:07 -07:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2013-07-30 08:18:07 -07:00
|
|
|
var (
|
2014-11-20 12:03:51 -08:00
|
|
|
deadline = flag.Duration("alertmanager.http-deadline", 10*time.Second, "Alert manager HTTP API timeout.")
|
2013-07-30 08:18:07 -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
|
|
|
|
// 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 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
|
|
|
|
|
|
|
stopped chan struct{}
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewNotificationHandler constructs a new NotificationHandler.
|
|
|
|
func NewNotificationHandler(alertmanagerURL string, notificationQueueCapacity int) *NotificationHandler {
|
2013-07-30 08:18:07 -07:00
|
|
|
return &NotificationHandler{
|
2015-03-23 11:17:42 -07:00
|
|
|
alertmanagerURL: strings.TrimRight(alertmanagerURL, "/"),
|
2014-10-10 05:19:02 -07:00
|
|
|
pendingNotifications: make(chan NotificationReqs, notificationQueueCapacity),
|
|
|
|
|
2015-05-28 12:11:48 -07:00
|
|
|
httpClient: httputil.NewDeadlineClient(*deadline),
|
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,
|
2014-10-10 05:19:02 -07:00
|
|
|
float64(notificationQueueCapacity),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2013-07-30 08:18:07 -07:00
|
|
|
alerts := make([]map[string]interface{}, 0, len(reqs))
|
|
|
|
for _, req := range reqs {
|
|
|
|
alerts = append(alerts, map[string]interface{}{
|
2015-05-30 06:35:51 -07: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 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
|
|
|
}
|