From 7a6b7c9cdb10c290e18fb2204982bb5cdf57c5b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 8 Feb 2023 20:30:56 +0100 Subject: [PATCH] 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. --- cache.go | 15 +++++++++++---- main.go | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cache.go b/cache.go index 17bead6..1bbef10 100644 --- a/cache.go +++ b/cache.go @@ -6,9 +6,11 @@ import ( ) type fingerprint string +type status string type cachedAlert struct { expires time.Time + status status } 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() defer c.mu.Unlock() alert := new(cachedAlert) alert.expires = time.Now().Add(c.duration) + alert.status = s c.alerts[f] = alert } -func (c *cache) contains(f fingerprint) bool { +func (c *cache) contains(f fingerprint, s status) bool { c.mu.Lock() defer c.mu.Unlock() - _, ok := c.alerts[f] - return ok + alert, ok := c.alerts[f] + if ok { + return alert.status == s + } + + return false } diff --git a/main.go b/main.go index 0a19912..7790c62 100644 --- a/main.go +++ b/main.go @@ -54,11 +54,11 @@ type notification struct { func (rcv *receiver) singleAlertNotifications(p *payload) []*notification { var notifications []*notification 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) continue } - rcv.cache.set(alert.Fingerprint) + rcv.cache.set(alert.Fingerprint, status(alert.Status)) n := new(notification) -- 2.44.0