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-25 12:38:22 +00:00
|
|
|
|
package types
|
|
|
|
|
|
2015-09-25 16:14:46 +00:00
|
|
|
|
import (
|
2023-11-24 10:01:40 +00:00
|
|
|
|
"fmt"
|
2015-10-11 10:40:43 +00:00
|
|
|
|
"strings"
|
2015-11-09 13:34:57 +00:00
|
|
|
|
"sync"
|
2015-09-25 16:14:46 +00:00
|
|
|
|
"time"
|
|
|
|
|
|
2019-02-05 13:18:21 +00:00
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-09-25 16:14:46 +00:00
|
|
|
|
"github.com/prometheus/common/model"
|
2022-03-25 16:59:51 +00:00
|
|
|
|
|
2024-06-21 14:17:27 +00:00
|
|
|
|
"github.com/prometheus/alertmanager/matcher/compat"
|
2022-03-25 16:59:51 +00:00
|
|
|
|
"github.com/prometheus/alertmanager/pkg/labels"
|
2015-09-25 16:14:46 +00:00
|
|
|
|
)
|
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// AlertState is used as part of AlertStatus.
|
2017-05-10 09:49:02 +00:00
|
|
|
|
type AlertState string
|
2017-04-27 12:18:52 +00:00
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// Possible values for AlertState.
|
2017-04-27 12:18:52 +00:00
|
|
|
|
const (
|
2017-05-10 09:49:02 +00:00
|
|
|
|
AlertStateUnprocessed AlertState = "unprocessed"
|
2018-02-28 16:42:32 +00:00
|
|
|
|
AlertStateActive AlertState = "active"
|
|
|
|
|
AlertStateSuppressed AlertState = "suppressed"
|
2017-04-27 12:18:52 +00:00
|
|
|
|
)
|
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// AlertStatus stores the state of an alert and, as applicable, the IDs of
|
|
|
|
|
// silences silencing the alert and of other alerts inhibiting the alert. Note
|
|
|
|
|
// that currently, SilencedBy is supposed to be the complete set of the relevant
|
|
|
|
|
// silences while InhibitedBy may contain only a subset of the inhibiting alerts
|
|
|
|
|
// – in practice exactly one ID. (This somewhat confusing semantics might change
|
|
|
|
|
// in the future.)
|
2017-04-27 12:18:52 +00:00
|
|
|
|
type AlertStatus struct {
|
2017-05-10 09:49:02 +00:00
|
|
|
|
State AlertState `json:"state"`
|
|
|
|
|
SilencedBy []string `json:"silencedBy"`
|
|
|
|
|
InhibitedBy []string `json:"inhibitedBy"`
|
2019-02-27 11:33:46 +00:00
|
|
|
|
|
2021-05-20 22:13:16 +00:00
|
|
|
|
// For internal tracking, not exposed in the API.
|
|
|
|
|
pendingSilences []string
|
2019-02-27 11:33:46 +00:00
|
|
|
|
silencesVersion int
|
2017-04-27 12:18:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// groupStatus stores the state of the group, and, as applicable, the names
|
|
|
|
|
// of all active and mute time intervals that are muting it.
|
|
|
|
|
type groupStatus struct {
|
|
|
|
|
// mutedBy contains the names of all active and mute time intervals that
|
|
|
|
|
// are muting it.
|
|
|
|
|
mutedBy []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AlertMarker helps to mark alerts as silenced and/or inhibited.
|
2015-11-20 14:10:38 +00:00
|
|
|
|
// All methods are goroutine-safe.
|
2024-04-30 14:26:04 +00:00
|
|
|
|
type AlertMarker interface {
|
2022-06-14 13:40:22 +00:00
|
|
|
|
// SetActiveOrSilenced replaces the previous SilencedBy by the provided IDs of
|
2021-05-20 22:13:16 +00:00
|
|
|
|
// active and pending silences, including the version number of the
|
|
|
|
|
// silences state. The set of provided IDs is supposed to represent the
|
|
|
|
|
// complete set of relevant silences. If no active silence IDs are provided and
|
|
|
|
|
// InhibitedBy is already empty, it sets the provided alert to AlertStateActive.
|
|
|
|
|
// Otherwise, it sets the provided alert to AlertStateSuppressed.
|
2022-06-14 13:40:22 +00:00
|
|
|
|
SetActiveOrSilenced(alert model.Fingerprint, version int, activeSilenceIDs, pendingSilenceIDs []string)
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// SetInhibited replaces the previous InhibitedBy by the provided IDs of
|
2022-06-14 13:40:22 +00:00
|
|
|
|
// alerts. In contrast to SetActiveOrSilenced, the set of provided IDs is not
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// expected to represent the complete set of inhibiting alerts. (In
|
|
|
|
|
// practice, this method is only called with one or zero IDs. However,
|
2022-06-14 13:40:22 +00:00
|
|
|
|
// this expectation might change in the future. If no IDs are provided
|
2021-05-20 22:13:16 +00:00
|
|
|
|
// and InhibitedBy is already empty, it sets the provided alert to
|
|
|
|
|
// AlertStateActive. Otherwise, it sets the provided alert to
|
|
|
|
|
// AlertStateSuppressed.
|
2019-02-25 16:11:43 +00:00
|
|
|
|
SetInhibited(alert model.Fingerprint, alertIDs ...string)
|
|
|
|
|
|
|
|
|
|
// Count alerts of the given state(s). With no state provided, count all
|
|
|
|
|
// alerts.
|
2017-09-15 13:33:47 +00:00
|
|
|
|
Count(...AlertState) int
|
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// Status of the given alert.
|
2017-04-27 12:18:52 +00:00
|
|
|
|
Status(model.Fingerprint) AlertStatus
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// Delete the given alert.
|
2017-04-27 12:18:52 +00:00
|
|
|
|
Delete(model.Fingerprint)
|
2015-11-09 13:34:57 +00:00
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// Various methods to inquire if the given alert is in a certain
|
2021-05-20 22:13:16 +00:00
|
|
|
|
// AlertState. Silenced also returns all the active and pending
|
|
|
|
|
// silences, while Inhibited may return only a subset of inhibiting
|
|
|
|
|
// alerts. Silenced also returns the version of the silences state the
|
|
|
|
|
// result is based on.
|
2017-04-27 12:18:52 +00:00
|
|
|
|
Unprocessed(model.Fingerprint) bool
|
|
|
|
|
Active(model.Fingerprint) bool
|
2022-03-25 16:59:51 +00:00
|
|
|
|
Silenced(model.Fingerprint) (activeIDs, pendingIDs []string, version int, silenced bool)
|
2017-04-27 12:18:52 +00:00
|
|
|
|
Inhibited(model.Fingerprint) ([]string, bool)
|
2015-11-09 13:34:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// GroupMarker helps to mark groups as active or muted.
|
|
|
|
|
// All methods are goroutine-safe.
|
|
|
|
|
//
|
|
|
|
|
// TODO(grobinson): routeID is used in Muted and SetMuted because groupKey
|
|
|
|
|
// is not unique (see #3817). Once groupKey uniqueness is fixed routeID can
|
|
|
|
|
// be removed from the GroupMarker interface.
|
|
|
|
|
type GroupMarker interface {
|
|
|
|
|
// Muted returns true if the group is muted, otherwise false. If the group
|
|
|
|
|
// is muted then it also returns the names of the time intervals that muted
|
|
|
|
|
// it.
|
|
|
|
|
Muted(routeID, groupKey string) ([]string, bool)
|
|
|
|
|
|
|
|
|
|
// SetMuted marks the group as muted, and sets the names of the time
|
|
|
|
|
// intervals that mute it. If the list of names is nil or the empty slice
|
|
|
|
|
// then the muted marker is removed.
|
|
|
|
|
SetMuted(routeID, groupKey string, timeIntervalNames []string)
|
2024-05-13 10:16:26 +00:00
|
|
|
|
|
|
|
|
|
// DeleteByGroupKey removes all markers for the GroupKey.
|
|
|
|
|
DeleteByGroupKey(routeID, groupKey string)
|
2024-04-30 14:26:04 +00:00
|
|
|
|
}
|
2019-02-05 13:18:21 +00:00
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// NewMarker returns an instance of a AlertMarker implementation.
|
|
|
|
|
func NewMarker(r prometheus.Registerer) *MemMarker {
|
|
|
|
|
m := &MemMarker{
|
|
|
|
|
alerts: map[model.Fingerprint]*AlertStatus{},
|
|
|
|
|
groups: map[string]*groupStatus{},
|
|
|
|
|
}
|
2019-02-05 13:18:21 +00:00
|
|
|
|
m.registerMetrics(r)
|
|
|
|
|
return m
|
2015-11-09 13:34:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
type MemMarker struct {
|
|
|
|
|
alerts map[model.Fingerprint]*AlertStatus
|
|
|
|
|
groups map[string]*groupStatus
|
2015-11-09 13:34:57 +00:00
|
|
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// Muted implements GroupMarker.
|
|
|
|
|
func (m *MemMarker) Muted(routeID, groupKey string) ([]string, bool) {
|
|
|
|
|
m.mtx.Lock()
|
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
status, ok := m.groups[routeID+groupKey]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
return status.mutedBy, len(status.mutedBy) > 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetMuted implements GroupMarker.
|
|
|
|
|
func (m *MemMarker) SetMuted(routeID, groupKey string, timeIntervalNames []string) {
|
|
|
|
|
m.mtx.Lock()
|
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
status, ok := m.groups[routeID+groupKey]
|
|
|
|
|
if !ok {
|
|
|
|
|
status = &groupStatus{}
|
|
|
|
|
m.groups[routeID+groupKey] = status
|
|
|
|
|
}
|
|
|
|
|
status.mutedBy = timeIntervalNames
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 10:16:26 +00:00
|
|
|
|
func (m *MemMarker) DeleteByGroupKey(routeID, groupKey string) {
|
|
|
|
|
m.mtx.Lock()
|
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
delete(m.groups, routeID+groupKey)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
func (m *MemMarker) registerMetrics(r prometheus.Registerer) {
|
2022-06-16 10:16:06 +00:00
|
|
|
|
newMarkedAlertMetricByState := func(st AlertState) prometheus.GaugeFunc {
|
2019-02-05 13:18:21 +00:00
|
|
|
|
return prometheus.NewGaugeFunc(
|
|
|
|
|
prometheus.GaugeOpts{
|
2022-06-16 10:16:06 +00:00
|
|
|
|
Name: "alertmanager_marked_alerts",
|
|
|
|
|
Help: "How many alerts by state are currently marked in the Alertmanager regardless of their expiry.",
|
2019-02-05 13:18:21 +00:00
|
|
|
|
ConstLabels: prometheus.Labels{"state": string(st)},
|
|
|
|
|
},
|
|
|
|
|
func() float64 {
|
|
|
|
|
return float64(m.Count(st))
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 10:16:06 +00:00
|
|
|
|
alertsActive := newMarkedAlertMetricByState(AlertStateActive)
|
|
|
|
|
alertsSuppressed := newMarkedAlertMetricByState(AlertStateSuppressed)
|
|
|
|
|
alertStateUnprocessed := newMarkedAlertMetricByState(AlertStateUnprocessed)
|
2019-02-05 13:18:21 +00:00
|
|
|
|
|
|
|
|
|
r.MustRegister(alertsActive)
|
|
|
|
|
r.MustRegister(alertsSuppressed)
|
2022-06-16 10:16:06 +00:00
|
|
|
|
r.MustRegister(alertStateUnprocessed)
|
2019-02-05 13:18:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// Count implements AlertMarker.
|
|
|
|
|
func (m *MemMarker) Count(states ...AlertState) int {
|
2017-09-15 13:33:47 +00:00
|
|
|
|
m.mtx.RLock()
|
|
|
|
|
defer m.mtx.RUnlock()
|
|
|
|
|
|
|
|
|
|
if len(states) == 0 {
|
2024-04-30 14:26:04 +00:00
|
|
|
|
return len(m.alerts)
|
2019-05-24 11:36:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var count int
|
2024-04-30 14:26:04 +00:00
|
|
|
|
for _, status := range m.alerts {
|
2019-05-24 11:36:01 +00:00
|
|
|
|
for _, state := range states {
|
|
|
|
|
if status.State == state {
|
|
|
|
|
count++
|
2017-09-15 13:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// SetActiveOrSilenced implements AlertMarker.
|
|
|
|
|
func (m *MemMarker) SetActiveOrSilenced(alert model.Fingerprint, version int, activeIDs, pendingIDs []string) {
|
2017-05-10 09:49:02 +00:00
|
|
|
|
m.mtx.Lock()
|
2021-05-20 22:13:16 +00:00
|
|
|
|
defer m.mtx.Unlock()
|
2015-11-09 13:34:57 +00:00
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
s, found := m.alerts[alert]
|
2017-04-27 12:18:52 +00:00
|
|
|
|
if !found {
|
|
|
|
|
s = &AlertStatus{}
|
2024-04-30 14:26:04 +00:00
|
|
|
|
m.alerts[alert] = s
|
2017-04-27 12:18:52 +00:00
|
|
|
|
}
|
2021-05-20 22:13:16 +00:00
|
|
|
|
s.SilencedBy = activeIDs
|
|
|
|
|
s.pendingSilences = pendingIDs
|
2019-02-27 11:33:46 +00:00
|
|
|
|
s.silencesVersion = version
|
2017-04-27 12:18:52 +00:00
|
|
|
|
|
|
|
|
|
// If there are any silence or alert IDs associated with the
|
|
|
|
|
// fingerprint, it is suppressed. Otherwise, set it to
|
2021-05-20 22:13:16 +00:00
|
|
|
|
// AlertStateActive.
|
|
|
|
|
if len(activeIDs) == 0 && len(s.InhibitedBy) == 0 {
|
|
|
|
|
s.State = AlertStateActive
|
2017-04-27 12:18:52 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 09:49:02 +00:00
|
|
|
|
s.State = AlertStateSuppressed
|
2015-11-09 13:34:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// SetInhibited implements AlertMarker.
|
|
|
|
|
func (m *MemMarker) SetInhibited(alert model.Fingerprint, ids ...string) {
|
2017-05-10 09:49:02 +00:00
|
|
|
|
m.mtx.Lock()
|
2021-05-20 22:13:16 +00:00
|
|
|
|
defer m.mtx.Unlock()
|
2015-11-09 13:34:57 +00:00
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
s, found := m.alerts[alert]
|
2017-04-27 12:18:52 +00:00
|
|
|
|
if !found {
|
|
|
|
|
s = &AlertStatus{}
|
2024-04-30 14:26:04 +00:00
|
|
|
|
m.alerts[alert] = s
|
2017-04-27 12:18:52 +00:00
|
|
|
|
}
|
2021-05-20 22:13:16 +00:00
|
|
|
|
s.InhibitedBy = ids
|
2017-04-27 12:18:52 +00:00
|
|
|
|
|
|
|
|
|
// If there are any silence or alert IDs associated with the
|
|
|
|
|
// fingerprint, it is suppressed. Otherwise, set it to
|
2021-05-20 22:13:16 +00:00
|
|
|
|
// AlertStateActive.
|
2017-04-27 12:18:52 +00:00
|
|
|
|
if len(ids) == 0 && len(s.SilencedBy) == 0 {
|
2021-05-20 22:13:16 +00:00
|
|
|
|
s.State = AlertStateActive
|
2017-04-27 12:18:52 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2015-11-09 13:34:57 +00:00
|
|
|
|
|
2017-05-10 09:49:02 +00:00
|
|
|
|
s.State = AlertStateSuppressed
|
2015-11-09 13:34:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// Status implements AlertMarker.
|
|
|
|
|
func (m *MemMarker) Status(alert model.Fingerprint) AlertStatus {
|
2017-04-27 12:18:52 +00:00
|
|
|
|
m.mtx.RLock()
|
|
|
|
|
defer m.mtx.RUnlock()
|
2015-11-09 13:34:57 +00:00
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
if s, found := m.alerts[alert]; found {
|
2021-05-20 22:13:16 +00:00
|
|
|
|
return *s
|
|
|
|
|
}
|
|
|
|
|
return AlertStatus{
|
|
|
|
|
State: AlertStateUnprocessed,
|
|
|
|
|
SilencedBy: []string{},
|
|
|
|
|
InhibitedBy: []string{},
|
2015-11-09 13:34:57 +00:00
|
|
|
|
}
|
2017-04-27 12:18:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// Delete implements AlertMarker.
|
|
|
|
|
func (m *MemMarker) Delete(alert model.Fingerprint) {
|
2017-05-10 09:49:02 +00:00
|
|
|
|
m.mtx.Lock()
|
|
|
|
|
defer m.mtx.Unlock()
|
2017-04-27 12:18:52 +00:00
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
delete(m.alerts, alert)
|
2017-04-27 12:18:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// Unprocessed implements AlertMarker.
|
|
|
|
|
func (m *MemMarker) Unprocessed(alert model.Fingerprint) bool {
|
2017-05-10 09:49:02 +00:00
|
|
|
|
return m.Status(alert).State == AlertStateUnprocessed
|
2017-04-27 12:18:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// Active implements AlertMarker.
|
|
|
|
|
func (m *MemMarker) Active(alert model.Fingerprint) bool {
|
2017-05-10 09:49:02 +00:00
|
|
|
|
return m.Status(alert).State == AlertStateActive
|
2017-04-27 12:18:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// Inhibited implements AlertMarker.
|
|
|
|
|
func (m *MemMarker) Inhibited(alert model.Fingerprint) ([]string, bool) {
|
2017-04-27 12:18:52 +00:00
|
|
|
|
s := m.Status(alert)
|
|
|
|
|
return s.InhibitedBy,
|
2017-05-10 09:49:02 +00:00
|
|
|
|
s.State == AlertStateSuppressed && len(s.InhibitedBy) > 0
|
2017-04-27 12:18:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Silenced returns whether the alert for the given Fingerprint is in the
|
2019-02-27 11:33:46 +00:00
|
|
|
|
// Silenced state, any associated silence IDs, and the silences state version
|
|
|
|
|
// the result is based on.
|
2024-04-30 14:26:04 +00:00
|
|
|
|
func (m *MemMarker) Silenced(alert model.Fingerprint) (activeIDs, pendingIDs []string, version int, silenced bool) {
|
2017-04-27 12:18:52 +00:00
|
|
|
|
s := m.Status(alert)
|
2021-05-20 22:13:16 +00:00
|
|
|
|
return s.SilencedBy, s.pendingSilences, s.silencesVersion,
|
2017-05-10 09:49:02 +00:00
|
|
|
|
s.State == AlertStateSuppressed && len(s.SilencedBy) > 0
|
2015-11-09 13:34:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
|
// MultiError contains multiple errors and implements the error interface. Its
|
|
|
|
|
// zero value is ready to use. All its methods are goroutine safe.
|
|
|
|
|
type MultiError struct {
|
|
|
|
|
mtx sync.Mutex
|
|
|
|
|
errors []error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add adds an error to the MultiError.
|
|
|
|
|
func (e *MultiError) Add(err error) {
|
|
|
|
|
e.mtx.Lock()
|
|
|
|
|
defer e.mtx.Unlock()
|
|
|
|
|
|
|
|
|
|
e.errors = append(e.errors, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Len returns the number of errors added to the MultiError.
|
|
|
|
|
func (e *MultiError) Len() int {
|
|
|
|
|
e.mtx.Lock()
|
|
|
|
|
defer e.mtx.Unlock()
|
|
|
|
|
|
|
|
|
|
return len(e.errors)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Errors returns the errors added to the MuliError. The returned slice is a
|
|
|
|
|
// copy of the internal slice of errors.
|
|
|
|
|
func (e *MultiError) Errors() []error {
|
|
|
|
|
e.mtx.Lock()
|
|
|
|
|
defer e.mtx.Unlock()
|
|
|
|
|
|
|
|
|
|
return append(make([]error, 0, len(e.errors)), e.errors...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *MultiError) Error() string {
|
|
|
|
|
e.mtx.Lock()
|
|
|
|
|
defer e.mtx.Unlock()
|
2015-10-11 10:40:43 +00:00
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
|
es := make([]string, 0, len(e.errors))
|
|
|
|
|
for _, err := range e.errors {
|
2015-10-11 10:40:43 +00:00
|
|
|
|
es = append(es, err.Error())
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(es, "; ")
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 12:53:49 +00:00
|
|
|
|
// Alert wraps a model.Alert with additional information relevant
|
2015-10-01 13:46:10 +00:00
|
|
|
|
// to internal of the Alertmanager.
|
|
|
|
|
// The type is never exposed to external communication and the
|
|
|
|
|
// embedded alert has to be sanitized beforehand.
|
2015-09-25 12:38:22 +00:00
|
|
|
|
type Alert struct {
|
2015-10-01 12:53:49 +00:00
|
|
|
|
model.Alert
|
2015-09-25 12:38:22 +00:00
|
|
|
|
|
|
|
|
|
// The authoritative timestamp.
|
2017-10-10 13:49:39 +00:00
|
|
|
|
UpdatedAt time.Time
|
|
|
|
|
Timeout bool
|
2015-09-30 12:53:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 10:03:51 +00:00
|
|
|
|
func validateLs(ls model.LabelSet) error {
|
2023-11-24 10:01:40 +00:00
|
|
|
|
for ln, lv := range ls {
|
2024-01-15 10:03:51 +00:00
|
|
|
|
if !compat.IsValidLabelName(ln) {
|
2023-11-24 10:01:40 +00:00
|
|
|
|
return fmt.Errorf("invalid name %q", ln)
|
|
|
|
|
}
|
|
|
|
|
if !lv.IsValid() {
|
|
|
|
|
return fmt.Errorf("invalid value %q", lv)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate overrides the same method in model.Alert to allow UTF-8 labels.
|
|
|
|
|
// This can be removed once prometheus/common has support for UTF-8.
|
2024-01-15 10:03:51 +00:00
|
|
|
|
func (a *Alert) Validate() error {
|
2023-11-24 10:01:40 +00:00
|
|
|
|
if a.StartsAt.IsZero() {
|
|
|
|
|
return fmt.Errorf("start time missing")
|
|
|
|
|
}
|
|
|
|
|
if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) {
|
|
|
|
|
return fmt.Errorf("start time must be before end time")
|
|
|
|
|
}
|
|
|
|
|
if len(a.Labels) == 0 {
|
|
|
|
|
return fmt.Errorf("at least one label pair required")
|
|
|
|
|
}
|
2024-01-15 10:03:51 +00:00
|
|
|
|
if err := validateLs(a.Labels); err != nil {
|
2023-11-24 21:17:35 +00:00
|
|
|
|
return fmt.Errorf("invalid label set: %w", err)
|
2023-11-24 10:01:40 +00:00
|
|
|
|
}
|
2024-01-15 10:03:51 +00:00
|
|
|
|
if err := validateLs(a.Annotations); err != nil {
|
2023-11-24 21:17:35 +00:00
|
|
|
|
return fmt.Errorf("invalid annotations: %w", err)
|
2023-11-24 10:01:40 +00:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
|
// AlertSlice is a sortable slice of Alerts.
|
2015-10-27 17:24:09 +00:00
|
|
|
|
type AlertSlice []*Alert
|
|
|
|
|
|
2018-06-14 13:54:33 +00:00
|
|
|
|
func (as AlertSlice) Less(i, j int) bool {
|
|
|
|
|
// Look at labels.job, then labels.instance.
|
|
|
|
|
for _, overrideKey := range [...]model.LabelName{"job", "instance"} {
|
|
|
|
|
iVal, iOk := as[i].Labels[overrideKey]
|
|
|
|
|
jVal, jOk := as[j].Labels[overrideKey]
|
|
|
|
|
if !iOk && !jOk {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if !iOk {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if !jOk {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if iVal != jVal {
|
|
|
|
|
return iVal < jVal
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return as[i].Labels.Before(as[j].Labels)
|
|
|
|
|
}
|
|
|
|
|
func (as AlertSlice) Swap(i, j int) { as[i], as[j] = as[j], as[i] }
|
|
|
|
|
func (as AlertSlice) Len() int { return len(as) }
|
2015-10-27 17:24:09 +00:00
|
|
|
|
|
2015-10-01 13:46:10 +00:00
|
|
|
|
// Alerts turns a sequence of internal alerts into a list of
|
|
|
|
|
// exposable model.Alert structures.
|
|
|
|
|
func Alerts(alerts ...*Alert) model.Alerts {
|
2015-11-20 14:10:38 +00:00
|
|
|
|
res := make(model.Alerts, 0, len(alerts))
|
2015-10-01 13:46:10 +00:00
|
|
|
|
for _, a := range alerts {
|
|
|
|
|
v := a.Alert
|
2018-11-06 15:58:08 +00:00
|
|
|
|
// If the end timestamp is not reached yet, do not expose it.
|
|
|
|
|
if !a.Resolved() {
|
2015-10-01 13:46:10 +00:00
|
|
|
|
v.EndsAt = time.Time{}
|
|
|
|
|
}
|
|
|
|
|
res = append(res, &v)
|
2015-09-30 12:53:05 +00:00
|
|
|
|
}
|
2015-10-01 13:46:10 +00:00
|
|
|
|
return res
|
2015-09-25 12:38:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
|
// Merge merges the timespan of two alerts based and overwrites annotations
|
|
|
|
|
// based on the authoritative timestamp. A new alert is returned, the labels
|
|
|
|
|
// are assumed to be equal.
|
2015-10-02 14:52:04 +00:00
|
|
|
|
func (a *Alert) Merge(o *Alert) *Alert {
|
|
|
|
|
// Let o always be the younger alert.
|
2015-10-15 14:21:36 +00:00
|
|
|
|
if o.UpdatedAt.Before(a.UpdatedAt) {
|
2015-10-02 14:52:04 +00:00
|
|
|
|
return o.Merge(a)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res := *o
|
|
|
|
|
|
|
|
|
|
// Always pick the earliest starting time.
|
|
|
|
|
if a.StartsAt.Before(o.StartsAt) {
|
|
|
|
|
res.StartsAt = a.StartsAt
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 15:58:08 +00:00
|
|
|
|
if o.Resolved() {
|
|
|
|
|
// The latest explicit resolved timestamp wins if both alerts are effectively resolved.
|
|
|
|
|
if a.Resolved() && a.EndsAt.After(o.EndsAt) {
|
|
|
|
|
res.EndsAt = a.EndsAt
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// A non-timeout timestamp always rules if it is the latest.
|
|
|
|
|
if a.EndsAt.After(o.EndsAt) && !a.Timeout {
|
|
|
|
|
res.EndsAt = a.EndsAt
|
|
|
|
|
}
|
2015-10-02 14:52:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &res
|
|
|
|
|
}
|
2015-09-25 12:38:22 +00:00
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// A Muter determines whether a given label set is muted. Implementers that
|
2024-04-30 14:26:04 +00:00
|
|
|
|
// maintain an underlying AlertMarker are expected to update it during a call of
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// Mutes.
|
2015-09-28 12:13:01 +00:00
|
|
|
|
type Muter interface {
|
2015-09-25 16:14:46 +00:00
|
|
|
|
Mutes(model.LabelSet) bool
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 11:47:00 +00:00
|
|
|
|
// A TimeMuter determines if the time is muted by one or more active or mute
|
|
|
|
|
// time intervals. If the time is muted, it returns true and the names of the
|
|
|
|
|
// time intervals that muted it. Otherwise, it returns false and a nil slice.
|
2023-10-13 13:15:05 +00:00
|
|
|
|
type TimeMuter interface {
|
2024-04-30 11:47:00 +00:00
|
|
|
|
Mutes(timeIntervalNames []string, now time.Time) (bool, []string, error)
|
2023-10-13 13:15:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
|
// A MuteFunc is a function that implements the Muter interface.
|
2015-09-28 12:13:01 +00:00
|
|
|
|
type MuteFunc func(model.LabelSet) bool
|
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
|
// Mutes implements the Muter interface.
|
2015-09-28 12:13:01 +00:00
|
|
|
|
func (f MuteFunc) Mutes(lset model.LabelSet) bool { return f(lset) }
|
|
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
|
// A Silence determines whether a given label set is muted.
|
2015-09-25 16:14:46 +00:00
|
|
|
|
type Silence struct {
|
2016-05-29 23:12:05 +00:00
|
|
|
|
// A unique identifier across all connected instances.
|
2016-08-30 09:58:27 +00:00
|
|
|
|
ID string `json:"id"`
|
2021-01-18 20:32:15 +00:00
|
|
|
|
// A set of matchers determining if a label set is affected
|
2015-09-27 12:07:04 +00:00
|
|
|
|
// by the silence.
|
2021-01-13 14:11:28 +00:00
|
|
|
|
Matchers labels.Matchers `json:"matchers"`
|
2016-05-29 23:12:05 +00:00
|
|
|
|
|
|
|
|
|
// Time range of the silence.
|
|
|
|
|
//
|
|
|
|
|
// * StartsAt must not be before creation time
|
|
|
|
|
// * EndsAt must be after StartsAt
|
|
|
|
|
// * Deleting a silence means to set EndsAt to now
|
|
|
|
|
// * Time range must not be modified in different ways
|
|
|
|
|
//
|
|
|
|
|
// TODO(fabxc): this may potentially be extended by
|
|
|
|
|
// creation and update timestamps.
|
|
|
|
|
StartsAt time.Time `json:"startsAt"`
|
|
|
|
|
EndsAt time.Time `json:"endsAt"`
|
|
|
|
|
|
2016-05-30 17:08:57 +00:00
|
|
|
|
// The last time the silence was updated.
|
|
|
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
|
// Information about who created the silence for which reason.
|
|
|
|
|
CreatedBy string `json:"createdBy"`
|
|
|
|
|
Comment string `json:"comment,omitempty"`
|
2015-09-25 16:14:46 +00:00
|
|
|
|
|
2017-05-10 09:55:28 +00:00
|
|
|
|
Status SilenceStatus `json:"status"`
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-21 14:29:51 +00:00
|
|
|
|
// Expired return if the silence is expired
|
2024-03-21 11:26:46 +00:00
|
|
|
|
// meaning that both StartsAt and EndsAt are equal.
|
2018-01-21 14:29:51 +00:00
|
|
|
|
func (s *Silence) Expired() bool {
|
|
|
|
|
return s.StartsAt.Equal(s.EndsAt)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// SilenceStatus stores the state of a silence.
|
2017-05-10 09:55:28 +00:00
|
|
|
|
type SilenceStatus struct {
|
|
|
|
|
State SilenceState `json:"state"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// SilenceState is used as part of SilenceStatus.
|
2017-05-10 09:55:28 +00:00
|
|
|
|
type SilenceState string
|
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// Possible values for SilenceState.
|
2017-05-10 09:55:28 +00:00
|
|
|
|
const (
|
|
|
|
|
SilenceStateExpired SilenceState = "expired"
|
|
|
|
|
SilenceStateActive SilenceState = "active"
|
|
|
|
|
SilenceStatePending SilenceState = "pending"
|
|
|
|
|
)
|
|
|
|
|
|
2019-02-25 16:11:43 +00:00
|
|
|
|
// CalcSilenceState returns the SilenceState that a silence with the given start
|
|
|
|
|
// and end time would have right now.
|
2017-05-10 09:55:28 +00:00
|
|
|
|
func CalcSilenceState(start, end time.Time) SilenceState {
|
|
|
|
|
current := time.Now()
|
|
|
|
|
if current.Before(start) {
|
|
|
|
|
return SilenceStatePending
|
|
|
|
|
}
|
|
|
|
|
if current.Before(end) {
|
|
|
|
|
return SilenceStateActive
|
|
|
|
|
}
|
|
|
|
|
return SilenceStateExpired
|
2015-09-25 16:14:46 +00:00
|
|
|
|
}
|