alertmanager/types/types.go

240 lines
5.5 KiB
Go
Raw Normal View History

2015-09-25 12:38:22 +00:00
package types
2015-09-25 16:14:46 +00:00
import (
2015-09-27 12:25:56 +00:00
"encoding/json"
2015-09-25 16:14:46 +00:00
"fmt"
"hash/fnv"
2015-09-25 16:14:46 +00:00
"time"
"github.com/prometheus/common/model"
2015-09-29 10:22:13 +00:00
"github.com/prometheus/alertmanager/config"
2015-09-25 16:14:46 +00:00
)
2015-09-29 10:22:13 +00:00
// Reloadable is a component that can change its state based
// on a new configuration.
type Reloadable interface {
ApplyConfig(*config.Config) bool
}
type Annotations map[model.LabelName]string
2015-09-29 12:45:16 +00:00
type AlertStatus string
const (
AlertFiring AlertStatus = "firing"
AlertResolved AlertStatus = "resolved"
)
2015-09-25 12:38:22 +00:00
type Alert struct {
// Label value pairs for purpose of aggregation, matching, and disposition
// dispatching. This must minimally include an "alertname" label.
Labels model.LabelSet `json:"labels"`
// Extra key/value information which does not define alert identity.
Annotations Annotations `json:"annotations"`
2015-09-25 12:38:22 +00:00
2015-09-29 15:26:44 +00:00
StartsAt time.Time `json:"startsAt,omitempty"`
EndsAt time.Time `json:"endsAt,omitempty"`
2015-09-25 12:38:22 +00:00
// The authoritative timestamp.
UpdatedAt time.Time `json:"-"`
Timeout bool `json:"-"`
}
func (a *Alert) MarshalJSON() ([]byte, error) {
b := *a
if b.Timeout {
b.EndsAt = time.Time{}
}
return json.Marshal(b)
2015-09-25 12:38:22 +00:00
}
// Name returns the name of the alert. It is equivalent to the "alertname" label.
func (a *Alert) Name() string {
return string(a.Labels[model.AlertNameLabel])
}
2015-09-28 12:13:01 +00:00
// 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.
// func (a *Alert) Merge(o *Alert) *Alert {
// // Let o always be the younger alert.
// if a.Timestamp.Before(a.Timestamp) {
// return o.Merge(a)
// }
// res := &Alert{
// Labels: o.Labels,
// Annotiations: o.Annotations,
// Timestamp: o.Timestamp,
// }
2015-09-25 12:38:22 +00:00
// }
// Fingerprint returns a unique hash for the alert. It is equivalent to
// the fingerprint of the alert's label set.
func (a *Alert) Fingerprint() model.Fingerprint {
return a.Labels.Fingerprint()
}
func (a *Alert) String() string {
s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint())
if a.Resolved() {
return s + "[resolved]"
}
return s + "[active]"
}
func (a *Alert) Resolved() bool {
2015-09-29 15:26:44 +00:00
if a.EndsAt.IsZero() {
2015-09-25 12:38:22 +00:00
return false
}
2015-09-29 15:26:44 +00:00
return !a.EndsAt.After(time.Now())
2015-09-25 12:38:22 +00:00
}
// alertTimeline is a list of alerts sorted by their timestamp.
type AlertTimeline []*Alert
2015-09-25 12:38:22 +00:00
func (at AlertTimeline) Len() int { return len(at) }
func (at AlertTimeline) Swap(i, j int) { at[i], at[j] = at[j], at[i] }
func (at AlertTimeline) Less(i, j int) bool {
if at[i].StartsAt.Before(at[j].StartsAt) {
return true
}
if at[i].EndsAt.Before(at[j].EndsAt) {
return true
}
return at[i].Fingerprint() < at[j].Fingerprint()
}
2015-09-25 16:14:46 +00:00
// A Silencer 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-09-28 12:13:01 +00:00
type MuteFunc func(model.LabelSet) bool
func (f MuteFunc) Mutes(lset model.LabelSet) bool { return f(lset) }
2015-09-25 16:14:46 +00:00
// A Silence determines whether a given label set is muted
// at the current time.
type Silence struct {
ID model.Fingerprint
2015-09-27 12:07:04 +00:00
// A set of matchers determining if an alert is affected
// by the silence.
2015-09-25 16:14:46 +00:00
Matchers Matchers
2015-09-27 12:07:04 +00:00
// The activity interval of the silence.
2015-09-27 12:25:56 +00:00
StartsAt, EndsAt time.Time
2015-09-25 16:14:46 +00:00
2015-09-27 12:07:04 +00:00
// Additional creation information.
CreateBy, Comment string
2015-09-25 16:14:46 +00:00
// timeFunc provides the time against which to evaluate
// the silence.
timeFunc func() time.Time
}
func (sil *Silence) Mutes(lset model.LabelSet) bool {
t := sil.timeFunc()
2015-09-27 12:25:56 +00:00
if t.Before(sil.StartsAt) || t.After(sil.EndsAt) {
2015-09-25 16:14:46 +00:00
return false
}
return sil.Matchers.Match(lset)
}
2015-09-27 12:25:56 +00:00
func (sil *Silence) UnmarshalJSON(b []byte) error {
var v = struct {
ID model.Fingerprint
Matchers []struct {
Name model.LabelName `json:"name"`
Value string `json:"value"`
IsRegex bool `json:"isRegex"`
} `json:"matchers"`
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
CreatedBy string `json:"createdBy"`
Comment string `json:"comment,omitempty"`
}{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
sil.ID = v.ID
sil.CreateBy = v.CreatedBy
sil.Comment = v.Comment
sil.StartsAt = v.StartsAt
sil.EndsAt = v.EndsAt
for _, m := range v.Matchers {
if !m.IsRegex {
sil.Matchers = append(sil.Matchers, NewMatcher(m.Name, m.Value))
continue
}
rem, err := NewRegexMatcher(m.Name, m.Value)
if err != nil {
return err
}
sil.Matchers = append(sil.Matchers, rem)
}
return nil
}
func (sil *Silence) MarshalJSON() ([]byte, error) {
type matcher struct {
Name model.LabelName `json:"name"`
Value string `json:"value"`
IsRegex bool `json:"isRegex"`
}
var v = struct {
ID model.Fingerprint
Matchers []matcher `json:"matchers"`
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
CreatedBy string `json:"createdBy"`
Comment string `json:"comment,omitempty"`
}{
ID: sil.ID,
StartsAt: sil.StartsAt,
EndsAt: sil.EndsAt,
CreatedBy: sil.CreateBy,
Comment: sil.Comment,
}
for _, m := range sil.Matchers {
v.Matchers = append(v.Matchers, matcher{
Name: m.Name,
Value: m.Value,
IsRegex: m.isRegex,
})
}
return json.Marshal(v)
}
type Notify struct {
Alert model.Fingerprint
SendTo string
Resolved bool
Delivered bool
Timestamp time.Time
}
func (n *Notify) String() string {
return fmt.Sprintf("<Notify:%q@%s to=%v res=%v deli=%v>", n.Alert, n.Timestamp, n.SendTo, n.Resolved, n.Delivered)
}
func (n *Notify) Fingerprint() model.Fingerprint {
h := fnv.New64a()
h.Write([]byte(n.SendTo))
fp := model.Fingerprint(h.Sum64())
return fp ^ n.Alert
}