Skip to content

Commit

Permalink
notify: fix race of writing to Alert.WasSilenced
Browse files Browse the repository at this point in the history
  • Loading branch information
brancz committed Oct 4, 2017
1 parent 837cf5c commit 82aeae2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (n *InhibitStage) Exec(ctx context.Context, alerts ...*types.Alert) (contex
// TODO(fabxc): increment muted alerts counter.
filtered = append(filtered, a)
// Store whether a previously inhibited alert is firing again.
a.WasInhibited = ok
a.SetInhibited(ok)
}
}

Expand Down Expand Up @@ -359,7 +359,7 @@ func (n *SilenceStage) Exec(ctx context.Context, alerts ...*types.Alert) (contex
filtered = append(filtered, a)
n.marker.SetSilenced(a.Labels.Fingerprint())
// Store whether a previously silenced alert is firing again.
a.WasSilenced = ok
a.SetSilenced(ok)
} else {
ids := make([]string, len(sils))
for i, s := range sils {
Expand Down
36 changes: 34 additions & 2 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ type Alert struct {
// The authoritative timestamp.
UpdatedAt time.Time
Timeout bool
WasSilenced bool `json:"-"`
WasInhibited bool `json:"-"`
wasSilenced bool
wasInhibited bool

mtx *sync.RWMutex
}

// AlertSlice is a sortable slice of Alerts.
Expand All @@ -262,11 +264,41 @@ func Alerts(alerts ...*Alert) model.Alerts {
if a.Timeout && !a.Resolved() {
v.EndsAt = time.Time{}
}

a.mtx = &sync.RWMutex{}
res = append(res, &v)
}
return res
}

func (a *Alert) SetSilenced(wasSilenced bool) {
a.mtx.Lock()
defer a.mtx.Unlock()

a.wasSilenced = wasSilenced
}

func (a *Alert) WasSilenced() bool {
a.mtx.RLock()
defer a.mtx.RUnlock()

return a.wasSilenced
}

func (a *Alert) SetInhibited(wasInhibited bool) {
a.mtx.Lock()
defer a.mtx.Unlock()

a.wasInhibited = wasInhibited
}

func (a *Alert) WasInhibited() bool {
a.mtx.RLock()
defer a.mtx.RUnlock()

return a.wasInhibited
}

// 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.
Expand Down

0 comments on commit 82aeae2

Please sign in to comment.