alertmanager/manager/state.go

435 lines
7.6 KiB
Go
Raw Normal View History

2015-07-01 15:56:53 +00:00
package manager
import (
"encoding/json"
2015-07-01 11:17:08 +00:00
"fmt"
"path/filepath"
2015-07-04 11:02:49 +00:00
"sort"
2015-07-01 11:17:08 +00:00
"sync"
"time"
2015-07-01 11:17:08 +00:00
"github.com/prometheus/common/model"
"github.com/prometheus/log"
"github.com/prometheus/alertmanager/crdt"
)
// A State serves the Alertmanager's internal state about active silences.
type State interface {
2015-07-01 11:17:08 +00:00
Silence() SilenceState
Config() ConfigState
Notify() NotifyState
Alert() AlertState
}
type AlertState interface {
Add(...*Alert) error
Get(model.Fingerprint) (*Alert, error)
GetAll() ([]*Alert, error)
Iter() <-chan *Alert
}
type ConfigState interface {
Set(*Config) error
Get() (*Config, error)
}
2015-07-04 13:50:42 +00:00
type NotifyState interface {
Get(model.Fingerprint) (*NotifyInfo, error)
Set(model.Fingerprint, *NotifyInfo) error
List() ([]*NotifyInfo, error)
2015-07-04 13:50:42 +00:00
}
type SilenceState interface {
// Silences returns a list of all silences.
List() ([]*Silence, error)
// SetSilence sets the given silence.
2015-07-01 11:17:08 +00:00
Set(*Silence) error
Del(sid string) error
Get(sid string) (*Silence, error)
}
// simpleState implements the State interface based on in-memory storage.
type simpleState struct {
2015-07-01 11:17:08 +00:00
silences *memSilences
alerts *crdtAlerts
config *memConfig
notify *memNotify
}
func NewSimpleState() State {
state := &simpleState{
2015-07-01 11:17:08 +00:00
silences: &memSilences{
sils: map[string]*Silence{},
2015-07-01 11:17:08 +00:00
nextID: 1,
},
alerts: newCRDTAlerts(crdt.NewMemStorage()),
// alerts: &memAlerts{
// alerts: map[model.Fingerprint]*Alert{},
// updates: make(chan *Alert, 100),
// },
config: &memConfig{},
notify: &memNotify{
m: map[model.Fingerprint]*NotifyInfo{},
},
}
go state.alerts.run()
return state
}
func NewPersistentState(path string) State {
alertDB, err := crdt.NewLevelDBStorage(filepath.Join(path, "/alerts"))
if err != nil {
panic(err)
}
state := &simpleState{
silences: &memSilences{
sils: map[string]*Silence{},
nextID: 1,
},
alerts: newCRDTAlerts(alertDB),
// alerts: &memAlerts{
// alerts: map[model.Fingerprint]*Alert{},
// updates: make(chan *Alert, 100),
// },
config: &memConfig{},
notify: &memNotify{
m: map[model.Fingerprint]*NotifyInfo{},
},
}
go state.alerts.run()
return state
}
func (s *simpleState) Alert() AlertState {
return s.alerts
}
func (s *simpleState) Silence() SilenceState {
2015-07-01 11:17:08 +00:00
return s.silences
}
func (s *simpleState) Config() ConfigState {
return s.config
}
func (s *simpleState) Notify() NotifyState {
return s.notify
}
type NotifyInfo struct {
LastSent time.Time
LastResolved bool
Labels model.LabelSet
}
type memNotify struct {
m map[model.Fingerprint]*NotifyInfo
}
func (s *memNotify) Get(fp model.Fingerprint) (*NotifyInfo, error) {
if info, ok := s.m[fp]; ok {
return info, nil
}
return nil, fmt.Errorf("notify info for %s not found", fp)
}
func (s *memNotify) Set(fp model.Fingerprint, info *NotifyInfo) error {
s.m[fp] = info
return nil
}
func (s *memNotify) List() ([]*NotifyInfo, error) {
var res []*NotifyInfo
for _, ni := range s.m {
res = append(res, ni)
}
return res, nil
}
type memConfig struct {
config *Config
mtx sync.RWMutex
}
func (c *memConfig) Set(conf *Config) error {
c.mtx.Lock()
defer c.mtx.Unlock()
c.config = conf
return nil
}
func (c *memConfig) Get() (*Config, error) {
c.mtx.RLock()
defer c.mtx.RUnlock()
return c.config, nil
}
type crdtAlerts struct {
set crdt.Set
updates chan *Alert
subs []chan *Alert
mtx sync.RWMutex
}
func newCRDTAlerts(storage crdt.Storage) *crdtAlerts {
return &crdtAlerts{
set: crdt.NewLWW(storage),
updates: make(chan *Alert, 100),
}
}
func (s *crdtAlerts) run() {
for a := range s.updates {
s.mtx.RLock()
for _, sub := range s.subs {
select {
case <-time.After(100 * time.Millisecond):
log.Errorf("dropped alert %s for subscriber", a)
case sub <- a:
// Success
}
}
s.mtx.RUnlock()
}
}
func (s *crdtAlerts) Add(alerts ...*Alert) error {
for _, a := range alerts {
b, err := json.Marshal(a)
if err != nil {
return err
}
err = s.set.Add(a.Fingerprint().String(), uint64(a.Timestamp.UnixNano()/1e6), b)
if err != nil {
return err
}
s.updates <- a
}
return nil
}
func (s *crdtAlerts) Get(fp model.Fingerprint) (*Alert, error) {
e, err := s.set.Get(fp.String())
if err != nil {
return nil, err
}
var alert Alert
err = json.Unmarshal(e.Value, &alert)
if err != nil {
return nil, err
}
return &alert, nil
}
func (s *crdtAlerts) GetAll() ([]*Alert, error) {
list, err := s.set.List()
if err != nil {
return nil, err
}
var alerts []*Alert
for _, e := range list {
var alert Alert
err = json.Unmarshal(e.Value, &alert)
if err != nil {
return nil, err
}
alerts = append(alerts, &alert)
}
return alerts, nil
}
func (s *crdtAlerts) Iter() <-chan *Alert {
ch := make(chan *Alert, 100)
// As we append the channel to the subcription channels
// before sending the current list of all alerts, no alert is lost.
// Handling the some alert twice is effectively a noop.
s.mtx.Lock()
s.subs = append(s.subs, ch)
s.mtx.Unlock()
prev, err := s.GetAll()
if err != nil {
log.Error(err)
}
go func() {
for _, alert := range prev {
ch <- alert
}
}()
return ch
}
type memAlerts struct {
alerts map[model.Fingerprint]*Alert
updates chan *Alert
subs []chan *Alert
mtx sync.RWMutex
}
func (s *memAlerts) run() {
for a := range s.updates {
s.mtx.RLock()
for _, sub := range s.subs {
select {
case <-time.After(100 * time.Millisecond):
log.Errorf("dropped alert %s for subscriber", a)
case sub <- a:
// Success
}
}
s.mtx.RUnlock()
}
}
func (s *memAlerts) GetAll() ([]*Alert, error) {
s.mtx.RLock()
defer s.mtx.RUnlock()
2015-07-04 11:02:49 +00:00
alerts := make([]*Alert, 0, len(s.alerts))
for _, a := range s.alerts {
alerts = append(alerts, a)
}
2015-07-04 11:02:49 +00:00
// TODO(fabxc): specify whether time sorting is an interface
// requirement.
sort.Sort(alertTimeline(alerts))
return alerts, nil
}
func (s *memAlerts) Add(alerts ...*Alert) error {
s.mtx.Lock()
defer s.mtx.Unlock()
for _, alert := range alerts {
fp := alert.Fingerprint()
// Last write wins.
if prev, ok := s.alerts[fp]; !ok || !prev.Timestamp.After(alert.Timestamp) {
s.alerts[fp] = alert
}
s.updates <- alert
}
return nil
}
func (s *memAlerts) Get(fp model.Fingerprint) (*Alert, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
if a, ok := s.alerts[fp]; ok {
return a, nil
}
return nil, fmt.Errorf("alert with fingerprint %s does not exist", fp)
}
2015-07-04 13:50:42 +00:00
func (s *memAlerts) Del(fp model.Fingerprint) error {
s.mtx.Lock()
defer s.mtx.Unlock()
delete(s.alerts, fp)
return nil
}
func (s *memAlerts) Iter() <-chan *Alert {
ch := make(chan *Alert, 100)
s.mtx.Lock()
s.subs = append(s.subs, ch)
s.mtx.Unlock()
go func() {
prev, _ := s.GetAll()
for _, alert := range prev {
ch <- alert
}
}()
return ch
}
2015-07-01 11:17:08 +00:00
type memSilences struct {
sils map[string]*Silence
2015-07-01 11:17:08 +00:00
mtx sync.RWMutex
2015-07-01 11:17:08 +00:00
nextID uint64
}
func (s *memSilences) genID() string {
sid := fmt.Sprintf("%x", s.nextID)
s.nextID++
return sid
}
func (s *memSilences) Get(sid string) (*Silence, error) {
s.mtx.RLock()
defer s.mtx.RUnlock()
if sil, ok := s.sils[sid]; ok {
return sil, nil
}
return nil, fmt.Errorf("silence with ID %s does not exist", sid)
2015-07-01 11:17:08 +00:00
}
2015-07-01 11:17:08 +00:00
func (s *memSilences) Del(sid string) error {
if _, ok := s.sils[sid]; !ok {
2015-07-01 11:17:08 +00:00
return fmt.Errorf("silence with ID %s does not exist", sid)
}
delete(s.sils, sid)
2015-07-01 11:17:08 +00:00
return nil
}
func (s *memSilences) List() ([]*Silence, error) {
2015-07-01 11:17:08 +00:00
s.mtx.Lock()
defer s.mtx.Unlock()
sils := make([]*Silence, 0, len(s.sils))
for _, sil := range s.sils {
sils = append(sils, sil)
}
return sils, nil
}
2015-07-01 11:17:08 +00:00
func (s *memSilences) Set(sil *Silence) error {
s.mtx.RLock()
defer s.mtx.RUnlock()
if sil.ID == "" {
sil.ID = s.genID()
}
s.sils[sil.ID] = sil
return nil
}