provider/mesh: add silence state

This commit is contained in:
Fabian Reinartz 2016-05-30 10:08:57 -07:00
parent 8e9bd4cfd4
commit b92c5f5bd4
3 changed files with 72 additions and 0 deletions

View File

@ -302,6 +302,7 @@ func (s *Silences) Set(sil *types.Silence) (uuid.UUID, error) {
b := tx.Bucket(bktSilences)
// Silences are immutable and we always create a new one.
sil.ID = uuid.NewV4()
sil.UpdatedAt = time.Now()
msb, err := json.Marshal(sil)
if err != nil {

View File

@ -6,6 +6,8 @@ import (
"sync"
"time"
"github.com/prometheus/alertmanager/types"
"github.com/satori/go.uuid"
"github.com/weaveworks/mesh"
)
@ -85,3 +87,69 @@ func (st *notificationState) mergeDelta(set map[string]notificationEntry) *notif
}
return &notificationState{set: d}
}
type silenceState struct {
mtx sync.RWMutex
set map[uuid.UUID]*types.Silence
}
func (st *silenceState) Encode() [][]byte {
st.mtx.RLock()
defer st.mtx.RUnlock()
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(&st.set); err != nil {
panic(err)
}
return [][]byte{buf.Bytes()}
}
func (st *silenceState) Merge(other mesh.GossipData) mesh.GossipData {
o := other.(*silenceState)
o.mtx.RLock()
defer o.mtx.RUnlock()
return st.mergeComplete(o.set)
}
func (st *silenceState) mergeComplete(set map[uuid.UUID]*types.Silence) *silenceState {
st.mtx.Lock()
defer st.mtx.Unlock()
for k, v := range set {
if prev, ok := st.set[k]; !ok || prev.UpdatedAt.Before(v.UpdatedAt) {
st.set[k] = v
}
}
return st
}
func (st *silenceState) mergeDelta(set map[uuid.UUID]*types.Silence) *silenceState {
st.mtx.Lock()
defer st.mtx.Unlock()
d := map[uuid.UUID]*types.Silence{}
for k, v := range set {
if prev, ok := st.set[k]; !ok || prev.UpdatedAt.Before(v.UpdatedAt) {
st.set[k] = v
d[k] = v
}
}
return &silenceState{set: d}
}
func (s *silenceState) copy() *silenceState {
s.mtx.RLock()
defer s.mtx.RUnlock()
res := &silenceState{
set: make(map[uuid.UUID]*types.Silence, len(s.set)),
}
for k, v := range s.set {
res.set[k] = v
}
return res
}

View File

@ -223,6 +223,9 @@ type Silence struct {
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
// The last time the silence was updated.
UpdatedAt time.Time `json:"updatedAt"`
// Information about who created the silence for which reason.
CreatedBy string `json:"createdBy"`
Comment string `json:"comment,omitempty"`