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 (
|
|
|
|
"fmt"
|
2016-06-09 14:43:51 +00:00
|
|
|
"sort"
|
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"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
)
|
|
|
|
|
2017-05-10 09:49:02 +00:00
|
|
|
type AlertState string
|
2017-04-27 12:18:52 +00:00
|
|
|
|
|
|
|
const (
|
2017-05-10 09:49:02 +00:00
|
|
|
AlertStateUnprocessed AlertState = "unprocessed"
|
|
|
|
AlertStateActive = "active"
|
|
|
|
AlertStateSuppressed = "suppressed"
|
2017-04-27 12:18:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AlertStatus stores the state and values associated with an Alert.
|
|
|
|
type AlertStatus struct {
|
2017-05-10 09:49:02 +00:00
|
|
|
State AlertState `json:"state"`
|
|
|
|
SilencedBy []string `json:"silencedBy"`
|
|
|
|
InhibitedBy []string `json:"inhibitedBy"`
|
2017-04-27 12:18:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// Marker helps to mark alerts as silenced and/or inhibited.
|
|
|
|
// All methods are goroutine-safe.
|
2015-11-09 13:34:57 +00:00
|
|
|
type Marker interface {
|
2017-04-27 12:18:52 +00:00
|
|
|
SetActive(alert model.Fingerprint)
|
|
|
|
SetInhibited(alert model.Fingerprint, ids ...string)
|
|
|
|
SetSilenced(alert model.Fingerprint, ids ...string)
|
|
|
|
|
2017-09-15 13:33:47 +00:00
|
|
|
Count(...AlertState) int
|
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
Status(model.Fingerprint) AlertStatus
|
|
|
|
Delete(model.Fingerprint)
|
2015-11-09 13:34:57 +00:00
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
Unprocessed(model.Fingerprint) bool
|
|
|
|
Active(model.Fingerprint) bool
|
|
|
|
Silenced(model.Fingerprint) ([]string, bool)
|
|
|
|
Inhibited(model.Fingerprint) ([]string, bool)
|
2015-11-09 13:34:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// NewMarker returns an instance of a Marker implementation.
|
2015-11-09 13:34:57 +00:00
|
|
|
func NewMarker() Marker {
|
|
|
|
return &memMarker{
|
2017-04-27 12:18:52 +00:00
|
|
|
m: map[model.Fingerprint]*AlertStatus{},
|
2015-11-09 13:34:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type memMarker struct {
|
2017-04-27 12:18:52 +00:00
|
|
|
m map[model.Fingerprint]*AlertStatus
|
2015-11-09 13:34:57 +00:00
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-09-15 13:33:47 +00:00
|
|
|
// Count alerts of a given state.
|
|
|
|
func (m *memMarker) Count(states ...AlertState) int {
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
m.mtx.RLock()
|
|
|
|
defer m.mtx.RUnlock()
|
|
|
|
|
|
|
|
if len(states) == 0 {
|
|
|
|
count = len(m.m)
|
|
|
|
} else {
|
|
|
|
for _, status := range m.m {
|
|
|
|
for _, state := range states {
|
|
|
|
if status.State == state {
|
2017-11-01 22:08:34 +00:00
|
|
|
count++
|
2017-09-15 13:33:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
// SetSilenced sets the AlertStatus to suppressed and stores the associated silence IDs.
|
|
|
|
func (m *memMarker) SetSilenced(alert model.Fingerprint, ids ...string) {
|
2017-05-10 09:49:02 +00:00
|
|
|
m.mtx.Lock()
|
2015-11-09 13:34:57 +00:00
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
s, found := m.m[alert]
|
|
|
|
if !found {
|
|
|
|
s = &AlertStatus{}
|
|
|
|
m.m[alert] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are any silence or alert IDs associated with the
|
|
|
|
// fingerprint, it is suppressed. Otherwise, set it to
|
|
|
|
// AlertStateUnprocessed.
|
|
|
|
if len(ids) == 0 && len(s.InhibitedBy) == 0 {
|
2017-05-10 09:49:02 +00:00
|
|
|
m.mtx.Unlock()
|
2017-04-27 12:18:52 +00:00
|
|
|
m.SetActive(alert)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-10 09:49:02 +00:00
|
|
|
s.State = AlertStateSuppressed
|
2017-04-27 12:18:52 +00:00
|
|
|
s.SilencedBy = ids
|
2017-05-10 09:49:02 +00:00
|
|
|
|
|
|
|
m.mtx.Unlock()
|
2015-11-09 13:34:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
// SetInhibited sets the AlertStatus to suppressed and stores the associated alert IDs.
|
|
|
|
func (m *memMarker) SetInhibited(alert model.Fingerprint, ids ...string) {
|
2017-05-10 09:49:02 +00:00
|
|
|
m.mtx.Lock()
|
2015-11-09 13:34:57 +00:00
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
s, found := m.m[alert]
|
|
|
|
if !found {
|
|
|
|
s = &AlertStatus{}
|
|
|
|
m.m[alert] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are any silence or alert IDs associated with the
|
|
|
|
// fingerprint, it is suppressed. Otherwise, set it to
|
|
|
|
// AlertStateUnprocessed.
|
|
|
|
if len(ids) == 0 && len(s.SilencedBy) == 0 {
|
2017-05-10 09:49:02 +00:00
|
|
|
m.mtx.Unlock()
|
2017-04-27 12:18:52 +00:00
|
|
|
m.SetActive(alert)
|
|
|
|
return
|
|
|
|
}
|
2015-11-09 13:34:57 +00:00
|
|
|
|
2017-05-10 09:49:02 +00:00
|
|
|
s.State = AlertStateSuppressed
|
2017-04-27 12:18:52 +00:00
|
|
|
s.InhibitedBy = ids
|
2017-05-10 09:49:02 +00:00
|
|
|
|
|
|
|
m.mtx.Unlock()
|
2017-04-27 12:18:52 +00:00
|
|
|
}
|
2017-05-10 09:49:02 +00:00
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
func (m *memMarker) SetActive(alert model.Fingerprint) {
|
2017-05-10 09:49:02 +00:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2015-11-09 13:34:57 +00:00
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
s, found := m.m[alert]
|
|
|
|
if !found {
|
2017-05-10 09:49:02 +00:00
|
|
|
s = &AlertStatus{
|
|
|
|
SilencedBy: []string{},
|
|
|
|
InhibitedBy: []string{},
|
|
|
|
}
|
2017-04-27 12:18:52 +00:00
|
|
|
m.m[alert] = s
|
2015-11-09 13:34:57 +00:00
|
|
|
}
|
2017-04-27 12:18:52 +00:00
|
|
|
|
2017-05-10 09:49:02 +00:00
|
|
|
s.State = AlertStateActive
|
2017-04-27 12:18:52 +00:00
|
|
|
s.SilencedBy = []string{}
|
|
|
|
s.InhibitedBy = []string{}
|
2015-11-09 13:34:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
// Status returns the AlertStatus for the given Fingerprint.
|
|
|
|
func (m *memMarker) Status(alert model.Fingerprint) AlertStatus {
|
|
|
|
m.mtx.RLock()
|
|
|
|
defer m.mtx.RUnlock()
|
2015-11-09 13:34:57 +00:00
|
|
|
|
2017-04-27 12:18:52 +00:00
|
|
|
s, found := m.m[alert]
|
|
|
|
if !found {
|
2017-05-10 09:49:02 +00:00
|
|
|
s = &AlertStatus{
|
|
|
|
State: AlertStateUnprocessed,
|
|
|
|
SilencedBy: []string{},
|
|
|
|
InhibitedBy: []string{},
|
|
|
|
}
|
2015-11-09 13:34:57 +00:00
|
|
|
}
|
2017-04-27 12:18:52 +00:00
|
|
|
return *s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the given Fingerprint from the internal cache.
|
|
|
|
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
|
|
|
|
|
|
|
delete(m.m, alert)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unprocessed returns whether the alert for the given Fingerprint is in the
|
|
|
|
// Unprocessed state.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Active returns whether the alert for the given Fingerprint is in the Active
|
|
|
|
// state.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Inhibited returns whether the alert for the given Fingerprint is in the
|
|
|
|
// Inhibited state and any associated alert IDs.
|
|
|
|
func (m *memMarker) Inhibited(alert model.Fingerprint) ([]string, bool) {
|
|
|
|
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
|
|
|
|
// Silenced state and any associated silence IDs.
|
|
|
|
func (m *memMarker) Silenced(alert model.Fingerprint) ([]string, bool) {
|
|
|
|
s := m.Status(alert)
|
|
|
|
return s.SilencedBy,
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
func (as AlertSlice) Less(i, j int) bool { return as[i].UpdatedAt.Before(as[j].UpdatedAt) }
|
|
|
|
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-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
|
|
|
|
// If the end timestamp was set as the expected value in case
|
2015-10-11 10:33:54 +00:00
|
|
|
// of a timeout but is not reached yet, do not expose it.
|
|
|
|
if a.Timeout && !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
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:11:59 +00:00
|
|
|
// A non-timeout resolved timestamp always rules.
|
2015-10-02 14:52:04 +00:00
|
|
|
// The latest explicit resolved timestamp wins.
|
|
|
|
if a.EndsAt.After(o.EndsAt) && !a.Timeout {
|
|
|
|
res.EndsAt = a.EndsAt
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res
|
|
|
|
}
|
2015-09-25 12:38:22 +00:00
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// A Muter determines whether a given label set is muted.
|
2015-09-28 12:13:01 +00:00
|
|
|
type Muter interface {
|
2015-09-25 16:14:46 +00:00
|
|
|
Mutes(model.LabelSet) bool
|
|
|
|
}
|
|
|
|
|
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"`
|
2016-05-29 23:12:05 +00:00
|
|
|
// A set of matchers determining if a label set is affect
|
2015-09-27 12:07:04 +00:00
|
|
|
// by the silence.
|
2016-05-29 23:12:05 +00:00
|
|
|
Matchers Matchers `json:"matchers"`
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// timeFunc provides the time against which to evaluate
|
2016-05-29 23:12:05 +00:00
|
|
|
// the silence. Used for test injection.
|
|
|
|
now func() time.Time
|
2017-05-10 09:55:28 +00:00
|
|
|
|
|
|
|
Status SilenceStatus `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SilenceStatus struct {
|
|
|
|
State SilenceState `json:"state"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SilenceState string
|
|
|
|
|
|
|
|
const (
|
|
|
|
SilenceStateExpired SilenceState = "expired"
|
|
|
|
SilenceStateActive SilenceState = "active"
|
|
|
|
SilenceStatePending SilenceState = "pending"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
// Validate returns true iff all fields of the silence have valid values.
|
|
|
|
func (s *Silence) Validate() error {
|
2016-08-30 09:58:27 +00:00
|
|
|
if s.ID == "" {
|
2016-06-09 13:15:06 +00:00
|
|
|
return fmt.Errorf("ID missing")
|
|
|
|
}
|
2016-05-29 23:12:05 +00:00
|
|
|
if len(s.Matchers) == 0 {
|
|
|
|
return fmt.Errorf("at least one matcher required")
|
2015-09-27 12:25:56 +00:00
|
|
|
}
|
2015-10-01 15:50:15 +00:00
|
|
|
for _, m := range s.Matchers {
|
2016-05-29 23:12:05 +00:00
|
|
|
if err := m.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("invalid matcher: %s", err)
|
2015-09-27 12:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-29 23:12:05 +00:00
|
|
|
if s.StartsAt.IsZero() {
|
|
|
|
return fmt.Errorf("start time missing")
|
|
|
|
}
|
|
|
|
if s.EndsAt.IsZero() {
|
|
|
|
return fmt.Errorf("end time missing")
|
|
|
|
}
|
|
|
|
if s.EndsAt.Before(s.StartsAt) {
|
|
|
|
return fmt.Errorf("start time must be before end time")
|
|
|
|
}
|
|
|
|
if s.CreatedBy == "" {
|
|
|
|
return fmt.Errorf("creator information missing")
|
|
|
|
}
|
|
|
|
if s.Comment == "" {
|
|
|
|
return fmt.Errorf("comment missing")
|
|
|
|
}
|
|
|
|
// if s.CreatedAt.IsZero() {
|
|
|
|
// return fmt.Errorf("creation timestamp missing")
|
|
|
|
// }
|
|
|
|
return nil
|
2015-09-27 12:25:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
// Init initializes a silence. Must be called before using Mutes.
|
|
|
|
func (s *Silence) Init() error {
|
|
|
|
for _, m := range s.Matchers {
|
|
|
|
if err := m.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 14:43:51 +00:00
|
|
|
sort.Sort(s.Matchers)
|
2016-05-29 23:12:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-09-27 12:25:56 +00:00
|
|
|
|
2016-05-29 23:12:05 +00:00
|
|
|
// Mutes implements the Muter interface.
|
|
|
|
//
|
|
|
|
// TODO(fabxc): consider making this a function accepting a
|
|
|
|
// timestamp and returning a Muter, i.e. s.Muter(ts).Mutes(lset).
|
|
|
|
func (s *Silence) Mutes(lset model.LabelSet) bool {
|
|
|
|
var now time.Time
|
|
|
|
if s.now != nil {
|
|
|
|
now = s.now()
|
|
|
|
} else {
|
|
|
|
now = time.Now()
|
|
|
|
}
|
|
|
|
if now.Before(s.StartsAt) || now.After(s.EndsAt) {
|
2015-10-01 15:50:15 +00:00
|
|
|
return false
|
2015-09-27 12:25:56 +00:00
|
|
|
}
|
2016-05-29 23:12:05 +00:00
|
|
|
return s.Matchers.Match(lset)
|
2015-09-27 12:25:56 +00:00
|
|
|
}
|
2015-09-27 17:50:24 +00:00
|
|
|
|
2016-08-30 12:19:22 +00:00
|
|
|
// Deleted returns whether a silence is deleted. Semantically this means it had no effect
|
2016-06-24 13:50:10 +00:00
|
|
|
// on history at any point.
|
|
|
|
func (s *Silence) Deleted() bool {
|
|
|
|
return s.StartsAt.Equal(s.EndsAt)
|
|
|
|
}
|