2015-10-11 15:24:49 +00:00
|
|
|
// Copyright 2015 Prometheus Team
|
|
|
|
// 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.
|
|
|
|
|
2015-09-29 12:45:38 +00:00
|
|
|
package notify
|
|
|
|
|
|
|
|
import (
|
2018-11-09 09:00:23 +00:00
|
|
|
"context"
|
2015-09-29 12:45:38 +00:00
|
|
|
"fmt"
|
2017-03-13 12:44:36 +00:00
|
|
|
"sort"
|
2015-09-29 13:12:31 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2015-09-29 12:45:38 +00:00
|
|
|
|
2020-05-18 13:00:36 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2021-10-18 23:09:37 +00:00
|
|
|
"github.com/cespare/xxhash/v2"
|
2021-07-30 08:11:43 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2020-06-04 08:38:48 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-08-12 17:18:26 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-09-29 13:12:31 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-29 12:45:38 +00:00
|
|
|
|
2019-02-26 10:59:14 +00:00
|
|
|
"github.com/prometheus/alertmanager/inhibit"
|
2016-08-16 12:09:06 +00:00
|
|
|
"github.com/prometheus/alertmanager/nflog"
|
2016-08-16 12:32:24 +00:00
|
|
|
"github.com/prometheus/alertmanager/nflog/nflogpb"
|
2016-08-30 09:58:27 +00:00
|
|
|
"github.com/prometheus/alertmanager/silence"
|
2020-10-13 00:04:16 +00:00
|
|
|
"github.com/prometheus/alertmanager/timeinterval"
|
2015-09-29 12:45:38 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
2019-06-07 08:37:49 +00:00
|
|
|
// ResolvedSender returns true if resolved notifications should be sent.
|
|
|
|
type ResolvedSender interface {
|
2018-06-08 09:37:38 +00:00
|
|
|
SendResolved() bool
|
|
|
|
}
|
|
|
|
|
2021-02-19 19:02:06 +00:00
|
|
|
// Peer represents the cluster node from where we are the sending the notification.
|
|
|
|
type Peer interface {
|
|
|
|
// WaitReady waits until the node silences and notifications have settled before attempting to send a notification.
|
2021-03-09 13:25:34 +00:00
|
|
|
WaitReady(context.Context) error
|
2021-02-19 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 12:18:36 +00:00
|
|
|
// MinTimeout is the minimum timeout that is set for the context of a call
|
2015-10-09 06:58:44 +00:00
|
|
|
// to a notification pipeline.
|
|
|
|
const MinTimeout = 10 * time.Second
|
|
|
|
|
2019-06-18 13:34:46 +00:00
|
|
|
// Notifier notifies about alerts under constraints of the given context. It
|
|
|
|
// returns an error if unsuccessful and a flag whether the error is
|
|
|
|
// recoverable. This information is useful for a retry logic.
|
|
|
|
type Notifier interface {
|
|
|
|
Notify(context.Context, ...*types.Alert) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Integration wraps a notifier and its configuration to be uniquely identified
|
|
|
|
// by name and index from its origin in the configuration.
|
|
|
|
type Integration struct {
|
|
|
|
notifier Notifier
|
|
|
|
rs ResolvedSender
|
|
|
|
name string
|
|
|
|
idx int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIntegration returns a new integration.
|
|
|
|
func NewIntegration(notifier Notifier, rs ResolvedSender, name string, idx int) Integration {
|
|
|
|
return Integration{
|
|
|
|
notifier: notifier,
|
|
|
|
rs: rs,
|
|
|
|
name: name,
|
|
|
|
idx: idx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify implements the Notifier interface.
|
|
|
|
func (i *Integration) Notify(ctx context.Context, alerts ...*types.Alert) (bool, error) {
|
|
|
|
return i.notifier.Notify(ctx, alerts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendResolved implements the ResolvedSender interface.
|
|
|
|
func (i *Integration) SendResolved() bool {
|
|
|
|
return i.rs.SendResolved()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the integration.
|
|
|
|
func (i *Integration) Name() string {
|
|
|
|
return i.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index returns the index of the integration.
|
|
|
|
func (i *Integration) Index() int {
|
|
|
|
return i.idx
|
|
|
|
}
|
|
|
|
|
2020-06-04 08:38:48 +00:00
|
|
|
// String implements the Stringer interface.
|
|
|
|
func (i *Integration) String() string {
|
|
|
|
return fmt.Sprintf("%s[%d]", i.name, i.idx)
|
|
|
|
}
|
|
|
|
|
2015-11-12 12:18:36 +00:00
|
|
|
// notifyKey defines a custom type with which a context is populated to
|
|
|
|
// avoid accidental collisions.
|
2015-09-29 13:12:31 +00:00
|
|
|
type notifyKey int
|
|
|
|
|
|
|
|
const (
|
2016-08-16 12:22:47 +00:00
|
|
|
keyReceiverName notifyKey = iota
|
2015-10-09 06:43:39 +00:00
|
|
|
keyRepeatInterval
|
2015-10-16 14:55:56 +00:00
|
|
|
keyGroupLabels
|
2015-10-21 11:08:53 +00:00
|
|
|
keyGroupKey
|
2017-03-13 12:44:36 +00:00
|
|
|
keyFiringAlerts
|
|
|
|
keyResolvedAlerts
|
2015-10-09 06:43:39 +00:00
|
|
|
keyNow
|
2020-11-24 04:02:07 +00:00
|
|
|
keyMuteTimeIntervals
|
2022-03-04 14:24:29 +00:00
|
|
|
keyActiveTimeIntervals
|
2015-09-29 13:12:31 +00:00
|
|
|
)
|
|
|
|
|
2016-08-16 12:22:47 +00:00
|
|
|
// WithReceiverName populates a context with a receiver name.
|
|
|
|
func WithReceiverName(ctx context.Context, rcv string) context.Context {
|
|
|
|
return context.WithValue(ctx, keyReceiverName, rcv)
|
2015-10-09 06:43:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 12:18:36 +00:00
|
|
|
// WithGroupKey populates a context with a group key.
|
2017-04-21 09:43:12 +00:00
|
|
|
func WithGroupKey(ctx context.Context, s string) context.Context {
|
|
|
|
return context.WithValue(ctx, keyGroupKey, s)
|
2015-10-21 11:08:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
// WithFiringAlerts populates a context with a slice of firing alerts.
|
|
|
|
func WithFiringAlerts(ctx context.Context, alerts []uint64) context.Context {
|
|
|
|
return context.WithValue(ctx, keyFiringAlerts, alerts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithResolvedAlerts populates a context with a slice of resolved alerts.
|
|
|
|
func WithResolvedAlerts(ctx context.Context, alerts []uint64) context.Context {
|
|
|
|
return context.WithValue(ctx, keyResolvedAlerts, alerts)
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 12:18:36 +00:00
|
|
|
// WithGroupLabels populates a context with grouping labels.
|
2015-10-16 14:55:56 +00:00
|
|
|
func WithGroupLabels(ctx context.Context, lset model.LabelSet) context.Context {
|
|
|
|
return context.WithValue(ctx, keyGroupLabels, lset)
|
2015-10-09 06:43:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 12:18:36 +00:00
|
|
|
// WithNow populates a context with a now timestamp.
|
2015-10-09 06:43:39 +00:00
|
|
|
func WithNow(ctx context.Context, t time.Time) context.Context {
|
|
|
|
return context.WithValue(ctx, keyNow, t)
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
// WithRepeatInterval populates a context with a repeat interval.
|
|
|
|
func WithRepeatInterval(ctx context.Context, t time.Duration) context.Context {
|
|
|
|
return context.WithValue(ctx, keyRepeatInterval, t)
|
|
|
|
}
|
|
|
|
|
2020-11-24 04:02:07 +00:00
|
|
|
// WithMuteTimeIntervals populates a context with a slice of mute time names.
|
|
|
|
func WithMuteTimeIntervals(ctx context.Context, mt []string) context.Context {
|
|
|
|
return context.WithValue(ctx, keyMuteTimeIntervals, mt)
|
2020-10-06 10:07:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 14:24:29 +00:00
|
|
|
func WithActiveTimeIntervals(ctx context.Context, at []string) context.Context {
|
|
|
|
return context.WithValue(ctx, keyActiveTimeIntervals, at)
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
// RepeatInterval extracts a repeat interval from the context. Iff none exists, the
|
|
|
|
// second argument is false.
|
|
|
|
func RepeatInterval(ctx context.Context) (time.Duration, bool) {
|
|
|
|
v, ok := ctx.Value(keyRepeatInterval).(time.Duration)
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:32:24 +00:00
|
|
|
// ReceiverName extracts a receiver name from the context. Iff none exists, the
|
|
|
|
// second argument is false.
|
|
|
|
func ReceiverName(ctx context.Context) (string, bool) {
|
|
|
|
v, ok := ctx.Value(keyReceiverName).(string)
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2015-11-12 12:18:36 +00:00
|
|
|
// GroupKey extracts a group key from the context. Iff none exists, the
|
|
|
|
// second argument is false.
|
2017-04-21 09:43:12 +00:00
|
|
|
func GroupKey(ctx context.Context) (string, bool) {
|
|
|
|
v, ok := ctx.Value(keyGroupKey).(string)
|
2015-10-21 11:08:53 +00:00
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2015-11-12 12:18:36 +00:00
|
|
|
// GroupLabels extracts grouping label set from the context. Iff none exists, the
|
|
|
|
// second argument is false.
|
2015-10-16 14:55:56 +00:00
|
|
|
func GroupLabels(ctx context.Context) (model.LabelSet, bool) {
|
|
|
|
v, ok := ctx.Value(keyGroupLabels).(model.LabelSet)
|
2015-10-09 06:43:39 +00:00
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2015-11-12 12:18:36 +00:00
|
|
|
// Now extracts a now timestamp from the context. Iff none exists, the
|
|
|
|
// second argument is false.
|
2015-10-09 06:43:39 +00:00
|
|
|
func Now(ctx context.Context) (time.Time, bool) {
|
|
|
|
v, ok := ctx.Value(keyNow).(time.Time)
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
// FiringAlerts extracts a slice of firing alerts from the context.
|
|
|
|
// Iff none exists, the second argument is false.
|
|
|
|
func FiringAlerts(ctx context.Context) ([]uint64, bool) {
|
|
|
|
v, ok := ctx.Value(keyFiringAlerts).([]uint64)
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolvedAlerts extracts a slice of firing alerts from the context.
|
|
|
|
// Iff none exists, the second argument is false.
|
|
|
|
func ResolvedAlerts(ctx context.Context) ([]uint64, bool) {
|
|
|
|
v, ok := ctx.Value(keyResolvedAlerts).([]uint64)
|
2016-08-16 12:09:06 +00:00
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:24:29 +00:00
|
|
|
// MuteTimeIntervalNames extracts a slice of mute time names from the context. If and only if none exists, the
|
2020-10-06 10:07:42 +00:00
|
|
|
// second argument is false.
|
2020-11-24 04:02:07 +00:00
|
|
|
func MuteTimeIntervalNames(ctx context.Context) ([]string, bool) {
|
|
|
|
v, ok := ctx.Value(keyMuteTimeIntervals).([]string)
|
2020-10-06 10:07:42 +00:00
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:24:29 +00:00
|
|
|
// ActiveTimeIntervalNames extracts a slice of active time names from the context. If none exists, the
|
|
|
|
// second argument is false.
|
|
|
|
func ActiveTimeIntervalNames(ctx context.Context) ([]string, bool) {
|
|
|
|
v, ok := ctx.Value(keyActiveTimeIntervals).([]string)
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// A Stage processes alerts under the constraints of the given context.
|
2016-08-12 13:22:17 +00:00
|
|
|
type Stage interface {
|
2017-10-22 05:59:33 +00:00
|
|
|
Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error)
|
2016-08-11 13:04:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// StageFunc wraps a function to represent a Stage.
|
2017-10-22 05:59:33 +00:00
|
|
|
type StageFunc func(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error)
|
2016-08-12 13:22:17 +00:00
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// Exec implements Stage interface.
|
2017-10-22 05:59:33 +00:00
|
|
|
func (f StageFunc) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
|
|
|
return f(ctx, l, alerts...)
|
2016-08-12 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
type NotificationLog interface {
|
|
|
|
Log(r *nflogpb.Receiver, gkey string, firingAlerts, resolvedAlerts []uint64) error
|
|
|
|
Query(params ...nflog.QueryParam) ([]*nflogpb.Entry, error)
|
|
|
|
}
|
|
|
|
|
2021-03-24 07:07:58 +00:00
|
|
|
type Metrics struct {
|
2020-11-06 14:24:18 +00:00
|
|
|
numNotifications *prometheus.CounterVec
|
|
|
|
numTotalFailedNotifications *prometheus.CounterVec
|
|
|
|
numNotificationRequestsTotal *prometheus.CounterVec
|
|
|
|
numNotificationRequestsFailedTotal *prometheus.CounterVec
|
|
|
|
notificationLatencySeconds *prometheus.HistogramVec
|
2019-08-26 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 07:07:58 +00:00
|
|
|
func NewMetrics(r prometheus.Registerer) *Metrics {
|
|
|
|
m := &Metrics{
|
2019-08-26 14:37:13 +00:00
|
|
|
numNotifications: prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Namespace: "alertmanager",
|
|
|
|
Name: "notifications_total",
|
|
|
|
Help: "The total number of attempted notifications.",
|
|
|
|
}, []string{"integration"}),
|
2020-11-06 14:24:18 +00:00
|
|
|
numTotalFailedNotifications: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2019-08-26 14:37:13 +00:00
|
|
|
Namespace: "alertmanager",
|
|
|
|
Name: "notifications_failed_total",
|
|
|
|
Help: "The total number of failed notifications.",
|
|
|
|
}, []string{"integration"}),
|
2020-11-06 14:24:18 +00:00
|
|
|
numNotificationRequestsTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Namespace: "alertmanager",
|
|
|
|
Name: "notification_requests_total",
|
|
|
|
Help: "The total number of attempted notification requests.",
|
|
|
|
}, []string{"integration"}),
|
|
|
|
numNotificationRequestsFailedTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Namespace: "alertmanager",
|
|
|
|
Name: "notification_requests_failed_total",
|
|
|
|
Help: "The total number of failed notification requests.",
|
|
|
|
}, []string{"integration"}),
|
2019-08-26 14:37:13 +00:00
|
|
|
notificationLatencySeconds: prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
Namespace: "alertmanager",
|
|
|
|
Name: "notification_latency_seconds",
|
|
|
|
Help: "The latency of notifications in seconds.",
|
|
|
|
Buckets: []float64{1, 5, 10, 15, 20},
|
|
|
|
}, []string{"integration"}),
|
|
|
|
}
|
|
|
|
for _, integration := range []string{
|
|
|
|
"email",
|
|
|
|
"pagerduty",
|
|
|
|
"wechat",
|
|
|
|
"pushover",
|
|
|
|
"slack",
|
|
|
|
"opsgenie",
|
|
|
|
"webhook",
|
|
|
|
"victorops",
|
2021-06-10 02:13:26 +00:00
|
|
|
"sns",
|
2022-02-22 16:51:02 +00:00
|
|
|
"telegram",
|
2019-08-26 14:37:13 +00:00
|
|
|
} {
|
|
|
|
m.numNotifications.WithLabelValues(integration)
|
2020-11-06 14:24:18 +00:00
|
|
|
m.numTotalFailedNotifications.WithLabelValues(integration)
|
|
|
|
m.numNotificationRequestsTotal.WithLabelValues(integration)
|
|
|
|
m.numNotificationRequestsFailedTotal.WithLabelValues(integration)
|
2019-08-26 14:37:13 +00:00
|
|
|
m.notificationLatencySeconds.WithLabelValues(integration)
|
|
|
|
}
|
2020-11-06 14:24:18 +00:00
|
|
|
r.MustRegister(
|
|
|
|
m.numNotifications, m.numTotalFailedNotifications,
|
|
|
|
m.numNotificationRequestsTotal, m.numNotificationRequestsFailedTotal,
|
|
|
|
m.notificationLatencySeconds,
|
|
|
|
)
|
2019-08-26 14:37:13 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type PipelineBuilder struct {
|
2021-03-24 07:07:58 +00:00
|
|
|
metrics *Metrics
|
2019-08-26 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPipelineBuilder(r prometheus.Registerer) *PipelineBuilder {
|
|
|
|
return &PipelineBuilder{
|
2021-03-24 07:07:58 +00:00
|
|
|
metrics: NewMetrics(r),
|
2019-08-26 14:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a map of receivers to Stages.
|
|
|
|
func (pb *PipelineBuilder) New(
|
2019-06-07 08:37:49 +00:00
|
|
|
receivers map[string][]Integration,
|
2016-08-16 12:32:24 +00:00
|
|
|
wait func() time.Duration,
|
2019-02-26 10:59:14 +00:00
|
|
|
inhibitor *inhibit.Inhibitor,
|
|
|
|
silencer *silence.Silencer,
|
2022-03-04 14:24:29 +00:00
|
|
|
times map[string][]timeinterval.TimeInterval,
|
2018-02-07 15:36:47 +00:00
|
|
|
notificationLog NotificationLog,
|
2021-02-19 19:02:06 +00:00
|
|
|
peer Peer,
|
2016-08-12 17:18:26 +00:00
|
|
|
) RoutingStage {
|
2019-06-07 08:37:49 +00:00
|
|
|
rs := make(RoutingStage, len(receivers))
|
2016-08-12 17:18:26 +00:00
|
|
|
|
2018-03-02 14:45:21 +00:00
|
|
|
ms := NewGossipSettleStage(peer)
|
2019-02-26 10:59:14 +00:00
|
|
|
is := NewMuteStage(inhibitor)
|
2022-03-04 14:24:29 +00:00
|
|
|
tas := NewTimeActiveStage(times)
|
2022-04-29 21:59:31 +00:00
|
|
|
tms := NewTimeMuteStage(times)
|
|
|
|
ss := NewMuteStage(silencer)
|
2016-08-16 12:32:24 +00:00
|
|
|
|
2019-06-07 08:37:49 +00:00
|
|
|
for name := range receivers {
|
2019-08-26 14:37:13 +00:00
|
|
|
st := createReceiverStage(name, receivers[name], wait, notificationLog, pb.metrics)
|
2022-03-04 14:24:29 +00:00
|
|
|
rs[name] = MultiStage{ms, is, tas, tms, ss, st}
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:37:49 +00:00
|
|
|
// createReceiverStage creates a pipeline of stages for a receiver.
|
2019-08-26 14:37:13 +00:00
|
|
|
func createReceiverStage(
|
|
|
|
name string,
|
|
|
|
integrations []Integration,
|
|
|
|
wait func() time.Duration,
|
|
|
|
notificationLog NotificationLog,
|
2021-03-24 07:07:58 +00:00
|
|
|
metrics *Metrics,
|
2019-08-26 14:37:13 +00:00
|
|
|
) Stage {
|
2016-08-16 12:32:24 +00:00
|
|
|
var fs FanoutStage
|
2019-06-07 08:37:49 +00:00
|
|
|
for i := range integrations {
|
2016-08-16 12:32:24 +00:00
|
|
|
recv := &nflogpb.Receiver{
|
2019-06-07 08:37:49 +00:00
|
|
|
GroupName: name,
|
|
|
|
Integration: integrations[i].Name(),
|
|
|
|
Idx: uint32(integrations[i].Index()),
|
2016-08-16 12:32:24 +00:00
|
|
|
}
|
2016-08-12 13:22:17 +00:00
|
|
|
var s MultiStage
|
2016-08-16 12:32:24 +00:00
|
|
|
s = append(s, NewWaitStage(wait))
|
2019-06-07 08:37:49 +00:00
|
|
|
s = append(s, NewDedupStage(&integrations[i], notificationLog, recv))
|
2019-08-26 14:37:13 +00:00
|
|
|
s = append(s, NewRetryStage(integrations[i], name, metrics))
|
2016-08-16 12:09:06 +00:00
|
|
|
s = append(s, NewSetNotifiesStage(notificationLog, recv))
|
2016-08-12 13:22:17 +00:00
|
|
|
|
2016-08-16 12:32:24 +00:00
|
|
|
fs = append(fs, s)
|
|
|
|
}
|
|
|
|
return fs
|
2016-08-12 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// RoutingStage executes the inner stages based on the receiver specified in
|
|
|
|
// the context.
|
|
|
|
type RoutingStage map[string]Stage
|
2016-08-12 13:22:17 +00:00
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// Exec implements the Stage interface.
|
2017-10-22 05:59:33 +00:00
|
|
|
func (rs RoutingStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-16 12:22:47 +00:00
|
|
|
receiver, ok := ReceiverName(ctx)
|
2016-08-12 17:18:26 +00:00
|
|
|
if !ok {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.New("receiver missing")
|
2016-08-12 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
s, ok := rs[receiver]
|
2016-08-12 13:22:17 +00:00
|
|
|
if !ok {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.New("stage for receiver missing")
|
2016-08-11 13:04:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
return s.Exec(ctx, l, alerts...)
|
2016-08-11 13:04:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:57:08 +00:00
|
|
|
// A MultiStage executes a series of stages sequentially.
|
2016-08-12 17:18:26 +00:00
|
|
|
type MultiStage []Stage
|
2016-08-11 13:04:03 +00:00
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// Exec implements the Stage interface.
|
2017-10-22 05:59:33 +00:00
|
|
|
func (ms MultiStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-12 17:18:26 +00:00
|
|
|
var err error
|
|
|
|
for _, s := range ms {
|
|
|
|
if len(alerts) == 0 {
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, nil, nil
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
2016-08-11 13:04:03 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
ctx, alerts, err = s.Exec(ctx, l, alerts...)
|
2016-08-11 13:04:03 +00:00
|
|
|
if err != nil {
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, nil, err
|
2016-08-11 13:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, alerts, nil
|
2016-08-11 13:04:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// FanoutStage executes its stages concurrently
|
2016-08-16 12:32:24 +00:00
|
|
|
type FanoutStage []Stage
|
2016-08-12 17:18:26 +00:00
|
|
|
|
2016-08-16 12:32:24 +00:00
|
|
|
// Exec attempts to execute all stages concurrently and discards the results.
|
|
|
|
// It returns its input alerts and a types.MultiError if one or more stages fail.
|
2017-10-22 05:59:33 +00:00
|
|
|
func (fs FanoutStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2015-10-11 10:40:43 +00:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
me types.MultiError
|
|
|
|
)
|
2016-08-12 13:22:17 +00:00
|
|
|
wg.Add(len(fs))
|
2015-09-29 13:12:31 +00:00
|
|
|
|
2016-08-16 12:32:24 +00:00
|
|
|
for _, s := range fs {
|
2016-08-12 13:22:17 +00:00
|
|
|
go func(s Stage) {
|
2017-10-22 05:59:33 +00:00
|
|
|
if _, _, err := s.Exec(ctx, l, alerts...); err != nil {
|
2015-11-20 14:10:38 +00:00
|
|
|
me.Add(err)
|
2015-09-29 13:12:31 +00:00
|
|
|
}
|
|
|
|
wg.Done()
|
2016-08-12 13:22:17 +00:00
|
|
|
}(s)
|
2015-09-29 13:12:31 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
if me.Len() > 0 {
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, alerts, &me
|
2015-10-11 14:54:31 +00:00
|
|
|
}
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, alerts, nil
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 14:45:21 +00:00
|
|
|
// GossipSettleStage waits until the Gossip has settled to forward alerts.
|
|
|
|
type GossipSettleStage struct {
|
2021-02-19 19:02:06 +00:00
|
|
|
peer Peer
|
2018-03-02 14:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGossipSettleStage returns a new GossipSettleStage.
|
2021-02-19 19:02:06 +00:00
|
|
|
func NewGossipSettleStage(p Peer) *GossipSettleStage {
|
2018-03-02 14:45:21 +00:00
|
|
|
return &GossipSettleStage{peer: p}
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:24:18 +00:00
|
|
|
func (n *GossipSettleStage) Exec(ctx context.Context, _ log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2018-03-02 14:45:21 +00:00
|
|
|
if n.peer != nil {
|
2021-03-09 13:25:34 +00:00
|
|
|
if err := n.peer.WaitReady(ctx); err != nil {
|
|
|
|
return ctx, nil, err
|
|
|
|
}
|
2018-03-02 14:45:21 +00:00
|
|
|
}
|
|
|
|
return ctx, alerts, nil
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:59:14 +00:00
|
|
|
// MuteStage filters alerts through a Muter.
|
|
|
|
type MuteStage struct {
|
2018-03-03 10:07:47 +00:00
|
|
|
muter types.Muter
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 10:59:14 +00:00
|
|
|
// NewMuteStage return a new MuteStage.
|
|
|
|
func NewMuteStage(m types.Muter) *MuteStage {
|
|
|
|
return &MuteStage{muter: m}
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exec implements the Stage interface.
|
2020-11-06 14:24:18 +00:00
|
|
|
func (n *MuteStage) Exec(ctx context.Context, _ log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-12 17:18:26 +00:00
|
|
|
var filtered []*types.Alert
|
|
|
|
for _, a := range alerts {
|
|
|
|
// TODO(fabxc): increment total alerts counter.
|
2019-02-26 10:59:14 +00:00
|
|
|
// Do not send the alert if muted.
|
2016-08-12 17:18:26 +00:00
|
|
|
if !n.muter.Mutes(a.Labels) {
|
|
|
|
filtered = append(filtered, a)
|
2016-08-05 08:18:10 +00:00
|
|
|
}
|
2019-02-26 10:59:14 +00:00
|
|
|
// TODO(fabxc): increment muted alerts counter if muted.
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, filtered, nil
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitStage waits for a certain amount of time before continuing or until the
|
|
|
|
// context is done.
|
|
|
|
type WaitStage struct {
|
|
|
|
wait func() time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWaitStage returns a new WaitStage.
|
|
|
|
func NewWaitStage(wait func() time.Duration) *WaitStage {
|
|
|
|
return &WaitStage{
|
|
|
|
wait: wait,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec implements the Stage interface.
|
2020-11-06 14:24:18 +00:00
|
|
|
func (ws *WaitStage) Exec(ctx context.Context, _ log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-12 17:18:26 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(ws.wait()):
|
|
|
|
case <-ctx.Done():
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, nil, ctx.Err()
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, alerts, nil
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DedupStage filters alerts.
|
2016-08-16 12:09:06 +00:00
|
|
|
// Filtering happens based on a notification log.
|
2016-08-12 17:18:26 +00:00
|
|
|
type DedupStage struct {
|
2019-06-07 08:37:49 +00:00
|
|
|
rs ResolvedSender
|
2018-02-07 15:36:47 +00:00
|
|
|
nflog NotificationLog
|
2017-11-02 10:12:12 +00:00
|
|
|
recv *nflogpb.Receiver
|
2016-08-16 12:09:06 +00:00
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
now func() time.Time
|
|
|
|
hash func(*types.Alert) uint64
|
2015-09-29 12:45:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
// NewDedupStage wraps a DedupStage that runs against the given notification log.
|
2019-06-07 08:37:49 +00:00
|
|
|
func NewDedupStage(rs ResolvedSender, l NotificationLog, recv *nflogpb.Receiver) *DedupStage {
|
2016-08-16 12:32:24 +00:00
|
|
|
return &DedupStage{
|
2019-06-07 08:37:49 +00:00
|
|
|
rs: rs,
|
2017-11-02 10:12:12 +00:00
|
|
|
nflog: l,
|
|
|
|
recv: recv,
|
|
|
|
now: utcNow,
|
|
|
|
hash: hashAlert,
|
2016-08-16 12:32:24 +00:00
|
|
|
}
|
2015-09-29 12:45:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
func utcNow() time.Time {
|
|
|
|
return time.Now().UTC()
|
|
|
|
}
|
|
|
|
|
2021-10-18 23:09:37 +00:00
|
|
|
// Wrap a slice in a struct so we can store a pointer in sync.Pool
|
|
|
|
type hashBuffer struct {
|
|
|
|
buf []byte
|
2017-03-13 12:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 23:09:37 +00:00
|
|
|
var hashBuffers = sync.Pool{
|
|
|
|
New: func() interface{} { return &hashBuffer{buf: make([]byte, 0, 1024)} },
|
2017-03-13 12:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func hashAlert(a *types.Alert) uint64 {
|
|
|
|
const sep = '\xff'
|
2017-04-21 09:43:12 +00:00
|
|
|
|
2021-10-18 23:09:37 +00:00
|
|
|
hb := hashBuffers.Get().(*hashBuffer)
|
|
|
|
defer hashBuffers.Put(hb)
|
|
|
|
b := hb.buf[:0]
|
2017-04-21 09:43:12 +00:00
|
|
|
|
|
|
|
names := make(model.LabelNames, 0, len(a.Labels))
|
2017-03-13 12:44:36 +00:00
|
|
|
|
2017-10-07 09:57:53 +00:00
|
|
|
for ln := range a.Labels {
|
2017-04-21 09:43:12 +00:00
|
|
|
names = append(names, ln)
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
2017-04-21 09:43:12 +00:00
|
|
|
sort.Sort(names)
|
2017-03-13 12:44:36 +00:00
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
for _, ln := range names {
|
|
|
|
b = append(b, string(ln)...)
|
2017-03-13 12:44:36 +00:00
|
|
|
b = append(b, sep)
|
2017-04-21 09:43:12 +00:00
|
|
|
b = append(b, string(a.Labels[ln])...)
|
2017-03-13 12:44:36 +00:00
|
|
|
b = append(b, sep)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := xxhash.Sum64(b)
|
|
|
|
|
|
|
|
return hash
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 09:37:38 +00:00
|
|
|
func (n *DedupStage) needsUpdate(entry *nflogpb.Entry, firing, resolved map[uint64]struct{}, repeat time.Duration) bool {
|
2016-08-16 12:09:06 +00:00
|
|
|
// If we haven't notified about the alert group before, notify right away
|
|
|
|
// unless we only have resolved alerts.
|
|
|
|
if entry == nil {
|
2018-06-08 09:37:38 +00:00
|
|
|
return len(firing) > 0
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
2017-03-13 12:44:36 +00:00
|
|
|
|
|
|
|
if !entry.IsFiringSubset(firing) {
|
2018-06-08 09:37:38 +00:00
|
|
|
return true
|
2017-03-13 12:44:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 09:37:38 +00:00
|
|
|
// Notify about all alerts being resolved.
|
|
|
|
// This is done irrespective of the send_resolved flag to make sure that
|
|
|
|
// the firing alerts are cleared from the notification log.
|
2018-01-23 15:52:03 +00:00
|
|
|
if len(firing) == 0 {
|
2018-06-08 09:37:38 +00:00
|
|
|
// If the current alert group and last notification contain no firing
|
|
|
|
// alert, it means that some alerts have been fired and resolved during the
|
|
|
|
// last interval. In this case, there is no need to notify the receiver
|
|
|
|
// since it doesn't know about them.
|
|
|
|
return len(entry.FiringAlerts) > 0
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:37:49 +00:00
|
|
|
if n.rs.SendResolved() && !entry.IsResolvedSubset(resolved) {
|
2018-06-08 09:37:38 +00:00
|
|
|
return true
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing changed, only notify if the repeat interval has passed.
|
2018-06-08 09:37:38 +00:00
|
|
|
return entry.Timestamp.Before(n.now().Add(-repeat))
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// Exec implements the Stage interface.
|
2020-11-06 14:24:18 +00:00
|
|
|
func (n *DedupStage) Exec(ctx context.Context, _ log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-16 12:09:06 +00:00
|
|
|
gkey, ok := GroupKey(ctx)
|
2015-09-29 13:12:31 +00:00
|
|
|
if !ok {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.New("group key missing")
|
2015-09-29 12:45:38 +00:00
|
|
|
}
|
2015-09-29 13:12:31 +00:00
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
repeatInterval, ok := RepeatInterval(ctx)
|
2015-10-09 06:26:41 +00:00
|
|
|
if !ok {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.New("repeat interval missing")
|
2015-10-09 06:26:41 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
firingSet := map[uint64]struct{}{}
|
|
|
|
resolvedSet := map[uint64]struct{}{}
|
|
|
|
firing := []uint64{}
|
|
|
|
resolved := []uint64{}
|
|
|
|
|
|
|
|
var hash uint64
|
|
|
|
for _, a := range alerts {
|
|
|
|
hash = n.hash(a)
|
|
|
|
if a.Resolved() {
|
|
|
|
resolved = append(resolved, hash)
|
|
|
|
resolvedSet[hash] = struct{}{}
|
|
|
|
} else {
|
|
|
|
firing = append(firing, hash)
|
|
|
|
firingSet[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 12:45:38 +00:00
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
ctx = WithFiringAlerts(ctx, firing)
|
|
|
|
ctx = WithResolvedAlerts(ctx, resolved)
|
2016-08-16 12:09:06 +00:00
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
entries, err := n.nflog.Query(nflog.QGroupKey(gkey), nflog.QReceiver(n.recv))
|
2016-08-16 12:09:06 +00:00
|
|
|
if err != nil && err != nflog.ErrNotFound {
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, nil, err
|
2015-09-29 12:45:38 +00:00
|
|
|
}
|
2019-11-19 08:00:06 +00:00
|
|
|
|
2016-08-16 12:09:06 +00:00
|
|
|
var entry *nflogpb.Entry
|
|
|
|
switch len(entries) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
entry = entries[0]
|
2019-11-19 08:00:06 +00:00
|
|
|
default:
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.Errorf("unexpected entry result size %d", len(entries))
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
2019-11-19 08:00:06 +00:00
|
|
|
|
2018-06-08 09:37:38 +00:00
|
|
|
if n.needsUpdate(entry, firingSet, resolvedSet, repeatInterval) {
|
2016-08-16 12:09:06 +00:00
|
|
|
return ctx, alerts, nil
|
2015-09-30 17:03:04 +00:00
|
|
|
}
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, nil, nil
|
2016-06-02 17:29:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// RetryStage notifies via passed integration with exponential backoff until it
|
|
|
|
// succeeds. It aborts if the context is canceled or timed out.
|
|
|
|
type RetryStage struct {
|
|
|
|
integration Integration
|
2018-01-29 15:00:04 +00:00
|
|
|
groupName string
|
2021-03-24 07:07:58 +00:00
|
|
|
metrics *Metrics
|
2016-06-02 17:29:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// NewRetryStage returns a new instance of a RetryStage.
|
2021-03-24 07:07:58 +00:00
|
|
|
func NewRetryStage(i Integration, groupName string, metrics *Metrics) *RetryStage {
|
2016-08-12 17:18:26 +00:00
|
|
|
return &RetryStage{
|
|
|
|
integration: i,
|
2018-01-29 15:00:04 +00:00
|
|
|
groupName: groupName,
|
2019-08-26 14:37:13 +00:00
|
|
|
metrics: metrics,
|
2015-09-29 13:12:31 +00:00
|
|
|
}
|
2016-08-12 13:22:17 +00:00
|
|
|
}
|
2015-09-29 13:12:31 +00:00
|
|
|
|
2017-10-22 05:59:33 +00:00
|
|
|
func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2020-11-06 14:24:18 +00:00
|
|
|
r.metrics.numNotifications.WithLabelValues(r.integration.Name()).Inc()
|
|
|
|
ctx, alerts, err := r.exec(ctx, l, alerts...)
|
|
|
|
if err != nil {
|
|
|
|
r.metrics.numTotalFailedNotifications.WithLabelValues(r.integration.Name()).Inc()
|
|
|
|
}
|
|
|
|
return ctx, alerts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RetryStage) exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2018-06-08 09:37:38 +00:00
|
|
|
var sent []*types.Alert
|
|
|
|
|
2017-11-02 10:12:12 +00:00
|
|
|
// If we shouldn't send notifications for resolved alerts, but there are only
|
|
|
|
// resolved alerts, report them all as successfully notified (we still want the
|
2018-06-08 09:37:38 +00:00
|
|
|
// notification log to log them for the next run of DedupStage).
|
2019-06-07 08:37:49 +00:00
|
|
|
if !r.integration.SendResolved() {
|
2017-11-02 10:12:12 +00:00
|
|
|
firing, ok := FiringAlerts(ctx)
|
|
|
|
if !ok {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.New("firing alerts missing")
|
2017-11-02 10:12:12 +00:00
|
|
|
}
|
|
|
|
if len(firing) == 0 {
|
|
|
|
return ctx, alerts, nil
|
|
|
|
}
|
2018-06-08 09:37:38 +00:00
|
|
|
for _, a := range alerts {
|
|
|
|
if a.Status() != model.AlertResolved {
|
|
|
|
sent = append(sent, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sent = alerts
|
2017-11-02 10:12:12 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 07:50:35 +00:00
|
|
|
b := backoff.NewExponentialBackOff()
|
|
|
|
b.MaxElapsedTime = 0 // Always retry.
|
|
|
|
|
|
|
|
tick := backoff.NewTicker(b)
|
|
|
|
defer tick.Stop()
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
var (
|
|
|
|
i = 0
|
2016-09-05 15:51:03 +00:00
|
|
|
iErr error
|
2016-08-12 17:18:26 +00:00
|
|
|
)
|
2020-06-04 08:38:48 +00:00
|
|
|
l = log.With(l, "receiver", r.groupName, "integration", r.integration.String())
|
2015-10-11 14:54:31 +00:00
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
for {
|
|
|
|
i++
|
|
|
|
// Always check the context first to not notify again.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-06-04 08:38:48 +00:00
|
|
|
if iErr == nil {
|
|
|
|
iErr = ctx.Err()
|
2016-09-05 15:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.Wrapf(iErr, "%s/%s: notify retry canceled after %d attempts", r.groupName, r.integration.String(), i)
|
2016-08-12 17:18:26 +00:00
|
|
|
default:
|
|
|
|
}
|
2015-09-29 13:12:31 +00:00
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
select {
|
|
|
|
case <-tick.C:
|
2018-04-23 12:23:01 +00:00
|
|
|
now := time.Now()
|
2018-06-08 09:37:38 +00:00
|
|
|
retry, err := r.integration.Notify(ctx, sent...)
|
2019-08-26 14:37:13 +00:00
|
|
|
r.metrics.notificationLatencySeconds.WithLabelValues(r.integration.Name()).Observe(time.Since(now).Seconds())
|
2020-11-06 14:24:18 +00:00
|
|
|
r.metrics.numNotificationRequestsTotal.WithLabelValues(r.integration.Name()).Inc()
|
2018-04-23 12:23:01 +00:00
|
|
|
if err != nil {
|
2020-11-06 14:24:18 +00:00
|
|
|
r.metrics.numNotificationRequestsFailedTotal.WithLabelValues(r.integration.Name()).Inc()
|
2016-09-05 15:51:03 +00:00
|
|
|
if !retry {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, alerts, errors.Wrapf(err, "%s/%s: notify retry canceled due to unrecoverable error after %d attempts", r.groupName, r.integration.String(), i)
|
|
|
|
}
|
|
|
|
if ctx.Err() == nil && (iErr == nil || err.Error() != iErr.Error()) {
|
|
|
|
// Log the error if the context isn't done and the error isn't the same as before.
|
|
|
|
level.Warn(l).Log("msg", "Notify attempt failed, will retry later", "attempts", i, "err", err)
|
2016-09-05 15:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save this error to be able to return the last seen error by an
|
|
|
|
// integration upon context timeout.
|
|
|
|
iErr = err
|
2016-08-12 17:18:26 +00:00
|
|
|
} else {
|
2020-06-04 08:38:48 +00:00
|
|
|
lvl := level.Debug(l)
|
|
|
|
if i > 1 {
|
|
|
|
lvl = level.Info(l)
|
|
|
|
}
|
|
|
|
lvl.Log("msg", "Notify success", "attempts", i)
|
2016-08-17 08:54:17 +00:00
|
|
|
return ctx, alerts, nil
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
2020-06-04 08:38:48 +00:00
|
|
|
if iErr == nil {
|
|
|
|
iErr = ctx.Err()
|
2016-09-05 15:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.Wrapf(iErr, "%s/%s: notify retry canceled after %d attempts", r.groupName, r.integration.String(), i)
|
2015-12-03 16:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// SetNotifiesStage sets the notification information about passed alerts. The
|
|
|
|
// passed alerts should have already been sent to the receivers.
|
|
|
|
type SetNotifiesStage struct {
|
2018-02-07 15:36:47 +00:00
|
|
|
nflog NotificationLog
|
2016-08-16 12:09:06 +00:00
|
|
|
recv *nflogpb.Receiver
|
2015-12-03 16:27:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:18:26 +00:00
|
|
|
// NewSetNotifiesStage returns a new instance of a SetNotifiesStage.
|
2018-02-07 15:36:47 +00:00
|
|
|
func NewSetNotifiesStage(l NotificationLog, recv *nflogpb.Receiver) *SetNotifiesStage {
|
2016-08-12 17:18:26 +00:00
|
|
|
return &SetNotifiesStage{
|
2017-03-13 12:44:36 +00:00
|
|
|
nflog: l,
|
|
|
|
recv: recv,
|
2015-12-03 16:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 13:22:17 +00:00
|
|
|
// Exec implements the Stage interface.
|
2017-10-22 05:59:33 +00:00
|
|
|
func (n SetNotifiesStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2016-08-16 12:09:06 +00:00
|
|
|
gkey, ok := GroupKey(ctx)
|
|
|
|
if !ok {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.New("group key missing")
|
2016-08-12 17:18:26 +00:00
|
|
|
}
|
2016-08-12 13:22:17 +00:00
|
|
|
|
2017-03-13 12:44:36 +00:00
|
|
|
firing, ok := FiringAlerts(ctx)
|
|
|
|
if !ok {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.New("firing alerts missing")
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
2017-03-13 12:44:36 +00:00
|
|
|
|
|
|
|
resolved, ok := ResolvedAlerts(ctx)
|
|
|
|
if !ok {
|
2020-06-04 08:38:48 +00:00
|
|
|
return ctx, nil, errors.New("resolved alerts missing")
|
2017-03-13 12:44:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
return ctx, alerts, n.nflog.Log(n.recv, gkey, firing, resolved)
|
2016-08-12 13:22:17 +00:00
|
|
|
}
|
2020-10-06 10:07:42 +00:00
|
|
|
|
2022-03-04 14:24:29 +00:00
|
|
|
type timeStage struct {
|
|
|
|
Times map[string][]timeinterval.TimeInterval
|
2020-10-06 10:07:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 14:24:29 +00:00
|
|
|
type TimeMuteStage timeStage
|
|
|
|
|
|
|
|
func NewTimeMuteStage(ti map[string][]timeinterval.TimeInterval) *TimeMuteStage {
|
|
|
|
return &TimeMuteStage{ti}
|
2020-10-06 10:07:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 01:45:40 +00:00
|
|
|
// Exec implements the stage interface for TimeMuteStage.
|
|
|
|
// TimeMuteStage is responsible for muting alerts whose route is not in an active time.
|
2020-11-24 01:42:27 +00:00
|
|
|
func (tms TimeMuteStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
2020-11-24 04:02:07 +00:00
|
|
|
muteTimeIntervalNames, ok := MuteTimeIntervalNames(ctx)
|
2020-10-06 10:07:42 +00:00
|
|
|
if !ok {
|
|
|
|
return ctx, alerts, nil
|
|
|
|
}
|
|
|
|
now, ok := Now(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx, alerts, errors.New("missing now timestamp")
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:24:29 +00:00
|
|
|
muted, err := inTimeIntervals(now, tms.Times, muteTimeIntervalNames)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, alerts, err
|
2020-10-06 10:07:42 +00:00
|
|
|
}
|
2022-03-04 14:24:29 +00:00
|
|
|
|
2020-11-24 01:45:40 +00:00
|
|
|
// If the current time is inside a mute time, all alerts are removed from the pipeline.
|
2020-10-06 10:07:42 +00:00
|
|
|
if muted {
|
2020-11-24 03:14:11 +00:00
|
|
|
level.Debug(l).Log("msg", "Notifications not sent, route is within mute time")
|
2020-10-06 10:07:42 +00:00
|
|
|
return ctx, nil, nil
|
|
|
|
}
|
|
|
|
return ctx, alerts, nil
|
|
|
|
}
|
2022-03-04 14:24:29 +00:00
|
|
|
|
|
|
|
type TimeActiveStage timeStage
|
|
|
|
|
|
|
|
func NewTimeActiveStage(ti map[string][]timeinterval.TimeInterval) *TimeActiveStage {
|
|
|
|
return &TimeActiveStage{ti}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec implements the stage interface for TimeActiveStage.
|
|
|
|
// TimeActiveStage is responsible for muting alerts whose route is not in an active time.
|
|
|
|
func (tas TimeActiveStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
|
|
|
|
activeTimeIntervalNames, ok := ActiveTimeIntervalNames(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx, alerts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't have active time intervals at all it is always active.
|
|
|
|
if len(activeTimeIntervalNames) == 0 {
|
|
|
|
return ctx, alerts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
now, ok := Now(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx, alerts, errors.New("missing now timestamp")
|
|
|
|
}
|
|
|
|
|
|
|
|
active, err := inTimeIntervals(now, tas.Times, activeTimeIntervalNames)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, alerts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current time is not inside an active time, all alerts are removed from the pipeline
|
|
|
|
if !active {
|
|
|
|
level.Debug(l).Log("msg", "Notifications not sent, route is not within active time")
|
|
|
|
return ctx, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx, alerts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// inTimeIntervals returns true if the current time is contained in one of the given time intervals.
|
|
|
|
func inTimeIntervals(now time.Time, intervals map[string][]timeinterval.TimeInterval, intervalNames []string) (bool, error) {
|
|
|
|
for _, name := range intervalNames {
|
|
|
|
interval, ok := intervals[name]
|
|
|
|
if !ok {
|
|
|
|
return false, errors.Errorf("time interval %s doesn't exist in config", name)
|
|
|
|
}
|
|
|
|
for _, ti := range interval {
|
|
|
|
if ti.ContainsTime(now.UTC()) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|