cache: Take alert status (firing, resolved) into account

Otherwise a resolved alert would not trigger a notification, since its
fingerprint - which is a generated hash from the labels - remains the
same.
This commit is contained in:
Thorben Günther 2023-02-08 20:30:56 +01:00
parent 1a4081a312
commit 7a6b7c9cdb
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
2 changed files with 13 additions and 6 deletions

View file

@ -6,9 +6,11 @@ import (
) )
type fingerprint string type fingerprint string
type status string
type cachedAlert struct { type cachedAlert struct {
expires time.Time expires time.Time
status status
} }
type cache struct { type cache struct {
@ -39,18 +41,23 @@ func (c *cache) cleanup() {
} }
} }
func (c *cache) set(f fingerprint) { func (c *cache) set(f fingerprint, s status) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
alert := new(cachedAlert) alert := new(cachedAlert)
alert.expires = time.Now().Add(c.duration) alert.expires = time.Now().Add(c.duration)
alert.status = s
c.alerts[f] = alert c.alerts[f] = alert
} }
func (c *cache) contains(f fingerprint) bool { func (c *cache) contains(f fingerprint, s status) bool {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
_, ok := c.alerts[f] alert, ok := c.alerts[f]
return ok if ok {
return alert.status == s
}
return false
} }

View file

@ -54,11 +54,11 @@ type notification struct {
func (rcv *receiver) singleAlertNotifications(p *payload) []*notification { func (rcv *receiver) singleAlertNotifications(p *payload) []*notification {
var notifications []*notification var notifications []*notification
for _, alert := range p.Alerts { for _, alert := range p.Alerts {
if rcv.cache.contains(alert.Fingerprint) { if rcv.cache.contains(alert.Fingerprint, status(alert.Status)) {
rcv.logger.Debugf("Alert %s skipped: Still in cache", alert.Fingerprint) rcv.logger.Debugf("Alert %s skipped: Still in cache", alert.Fingerprint)
continue continue
} }
rcv.cache.set(alert.Fingerprint) rcv.cache.set(alert.Fingerprint, status(alert.Status))
n := new(notification) n := new(notification)