Switch to model.Silence
This commit is contained in:
parent
a31b79cb91
commit
46e09e2b2c
7
api.go
7
api.go
|
@ -7,7 +7,6 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/common/model"
|
||||
"github.com/prometheus/common/route"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
|
@ -133,7 +132,7 @@ func (api *API) getSilence(w http.ResponseWriter, r *http.Request) {
|
|||
}, nil)
|
||||
}
|
||||
|
||||
sil, err := api.silences.Get(model.Fingerprint(sid))
|
||||
sil, err := api.silences.Get(sid)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprint("Error getting silence: ", err), http.StatusNotFound)
|
||||
return
|
||||
|
@ -157,7 +156,7 @@ func (api *API) setSilence(w http.ResponseWriter, r *http.Request) {
|
|||
err: err,
|
||||
}, nil)
|
||||
}
|
||||
sil.ID = model.Fingerprint(sid)
|
||||
sil.ID = sid
|
||||
|
||||
if err := api.silences.Set(&sil); err != nil {
|
||||
respondError(w, apiError{
|
||||
|
@ -179,7 +178,7 @@ func (api *API) delSilence(w http.ResponseWriter, r *http.Request) {
|
|||
}, nil)
|
||||
}
|
||||
|
||||
if err := api.silences.Del(model.Fingerprint(sid)); err != nil {
|
||||
if err := api.silences.Del(sid); err != nil {
|
||||
respondError(w, apiError{
|
||||
typ: errorBadData,
|
||||
err: err,
|
||||
|
|
|
@ -239,12 +239,12 @@ func (n *MemNotifies) Get(dest string, fps ...model.Fingerprint) ([]*types.Notif
|
|||
|
||||
type MemSilences struct {
|
||||
mtx sync.RWMutex
|
||||
silences map[model.Fingerprint]*types.Silence
|
||||
silences map[uint64]*types.Silence
|
||||
}
|
||||
|
||||
func NewMemSilences() *MemSilences {
|
||||
return &MemSilences{
|
||||
silences: map[model.Fingerprint]*types.Silence{},
|
||||
silences: map[uint64]*types.Silence{},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -276,14 +276,14 @@ func (s *MemSilences) Set(sil *types.Silence) error {
|
|||
defer s.mtx.Unlock()
|
||||
|
||||
if sil.ID == 0 {
|
||||
sil.ID = model.Fingerprint(len(s.silences) + 1)
|
||||
sil.ID = uint64(len(s.silences) + 1)
|
||||
}
|
||||
|
||||
s.silences[sil.ID] = sil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemSilences) Del(id model.Fingerprint) error {
|
||||
func (s *MemSilences) Del(id uint64) error {
|
||||
s.mtx.Lock()
|
||||
defer s.mtx.Unlock()
|
||||
|
||||
|
@ -291,7 +291,7 @@ func (s *MemSilences) Del(id model.Fingerprint) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *MemSilences) Get(id model.Fingerprint) (*types.Silence, error) {
|
||||
func (s *MemSilences) Get(id uint64) (*types.Silence, error) {
|
||||
s.mtx.RLock()
|
||||
defer s.mtx.RUnlock()
|
||||
|
||||
|
|
|
@ -56,9 +56,9 @@ type Silences interface {
|
|||
// Set a new silence.
|
||||
Set(*types.Silence) error
|
||||
// Del removes a silence.
|
||||
Del(model.Fingerprint) error
|
||||
Del(uint64) error
|
||||
// Get a silence associated with a fingerprint.
|
||||
Get(model.Fingerprint) (*types.Silence, error)
|
||||
Get(uint64) (*types.Silence, error)
|
||||
}
|
||||
|
||||
// Notifies provides information about pending and successful
|
||||
|
|
107
types/types.go
107
types/types.go
|
@ -1,7 +1,6 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"time"
|
||||
|
@ -25,8 +24,8 @@ type Alert struct {
|
|||
model.Alert
|
||||
|
||||
// The authoritative timestamp.
|
||||
UpdatedAt time.Time
|
||||
Timeout bool
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
Timeout bool `json:"-"`
|
||||
}
|
||||
|
||||
// Alerts turns a sequence of internal alerts into a list of
|
||||
|
@ -74,22 +73,39 @@ func (f MuteFunc) Mutes(lset model.LabelSet) bool { return f(lset) }
|
|||
// A Silence determines whether a given label set is muted
|
||||
// at the current time.
|
||||
type Silence struct {
|
||||
ID model.Fingerprint
|
||||
model.Silence
|
||||
|
||||
// A set of matchers determining if an alert is affected
|
||||
// by the silence.
|
||||
Matchers Matchers
|
||||
// The activity interval of the silence.
|
||||
StartsAt, EndsAt time.Time
|
||||
|
||||
// Additional creation information.
|
||||
CreateBy, Comment string
|
||||
Matchers Matchers `json:"-"`
|
||||
|
||||
// timeFunc provides the time against which to evaluate
|
||||
// the silence.
|
||||
timeFunc func() time.Time
|
||||
}
|
||||
|
||||
// NewSilence creates a new internal Silence from a public silence
|
||||
// object.
|
||||
func NewSilence(s *model.Silence) *Silence {
|
||||
sil := &Silence{
|
||||
Silence: *s,
|
||||
timeFunc: time.Now,
|
||||
}
|
||||
for _, m := range s.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 {
|
||||
// Must have been sanitized beforehand.
|
||||
panic(err)
|
||||
}
|
||||
sil.Matchers = append(sil.Matchers, rem)
|
||||
}
|
||||
return sil
|
||||
}
|
||||
|
||||
func (sil *Silence) Mutes(lset model.LabelSet) bool {
|
||||
t := sil.timeFunc()
|
||||
|
||||
|
@ -100,75 +116,8 @@ func (sil *Silence) Mutes(lset model.LabelSet) bool {
|
|||
return sil.Matchers.Match(lset)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Notify holds information about the last notification state
|
||||
// of an Alert.
|
||||
type Notify struct {
|
||||
Alert model.Fingerprint
|
||||
SendTo string
|
||||
|
|
Loading…
Reference in New Issue