From 45bfd479208fc0cff9d9023f910ecffce5d8400a Mon Sep 17 00:00:00 2001 From: Jackson Sabey Date: Sat, 31 Aug 2024 15:56:23 -0700 Subject: [PATCH] use last reminder based upon alert type --- config/endpoint/endpoint.go | 5 ++++- config/endpoint/external_endpoint.go | 3 +++ watchdog/alerting.go | 12 ++++++------ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/config/endpoint/endpoint.go b/config/endpoint/endpoint.go index e73d4075..bfc57872 100644 --- a/config/endpoint/endpoint.go +++ b/config/endpoint/endpoint.go @@ -123,7 +123,7 @@ type Endpoint struct { NumberOfSuccessesInARow int `yaml:"-"` // LastReminderSent is the time at which the last reminder was sent for this endpoint. - LastReminderSent time.Time `yaml:"-"` + LastReminderSent map[alert.Type]time.Time `yaml:"-"` } // IsEnabled returns whether the endpoint is enabled or not @@ -193,6 +193,9 @@ func (e *Endpoint) ValidateAndSetDefaults() error { if len(e.Headers) == 0 { e.Headers = make(map[string]string) } + if len(e.LastReminderSent) == 0 { + e.LastReminderSent = make(map[alert.Type]time.Time) + } // Automatically add user agent header if there isn't one specified in the endpoint configuration if _, userAgentHeaderExists := e.Headers[UserAgentHeader]; !userAgentHeaderExists { e.Headers[UserAgentHeader] = GatusUserAgent diff --git a/config/endpoint/external_endpoint.go b/config/endpoint/external_endpoint.go index 58f37fed..8527187c 100644 --- a/config/endpoint/external_endpoint.go +++ b/config/endpoint/external_endpoint.go @@ -2,6 +2,7 @@ package endpoint import ( "errors" + "time" "github.com/TwiN/gatus/v5/alerting/alert" ) @@ -75,6 +76,8 @@ func (externalEndpoint *ExternalEndpoint) ToEndpoint() *Endpoint { Enabled: externalEndpoint.Enabled, Name: externalEndpoint.Name, Group: externalEndpoint.Group, + Headers: make(map[string]string), + LastReminderSent: make(map[alert.Type]time.Time), Alerts: externalEndpoint.Alerts, NumberOfFailuresInARow: externalEndpoint.NumberOfFailuresInARow, NumberOfSuccessesInARow: externalEndpoint.NumberOfSuccessesInARow, diff --git a/watchdog/alerting.go b/watchdog/alerting.go index 72cacb19..99d2ee03 100644 --- a/watchdog/alerting.go +++ b/watchdog/alerting.go @@ -35,16 +35,16 @@ func handleAlertsToTrigger(ep *endpoint.Endpoint, result *endpoint.Result, alert sendInitialAlert := !endpointAlert.Triggered // Determine if a reminder should be sent var lastReminder time.Duration - if !ep.LastReminderSent.IsZero() { - lastReminder = time.Since(ep.LastReminderSent) + if lr, ok := ep.LastReminderSent[endpointAlert.Type]; ok && !lr.IsZero() { + lastReminder = time.Since(lr) } sendReminder := endpointAlert.Triggered && endpointAlert.RepeatInterval > 0 && (lastReminder == 0 || lastReminder >= endpointAlert.RepeatInterval) // If neither initial alert nor reminder needs to be sent, skip to the next alert if !sendInitialAlert && !sendReminder { if debug { - log.Printf("[watchdog.handleAlertsToTrigger] Alert for endpoint=%s with description='%s' is not due for triggering or reminding (interval: %s last: %s), skipping", - ep.Name, endpointAlert.GetDescription(), endpointAlert.RepeatInterval, lastReminder) + log.Printf("[watchdog.handleAlertsToTrigger] Alert %s for endpoint=%s with description='%s' is not due for triggering (interval: %s last: %s), skipping", + endpointAlert.Type, ep.Name, endpointAlert.GetDescription(), endpointAlert.RepeatInterval, lastReminder) } continue } @@ -70,7 +70,7 @@ func handleAlertsToTrigger(ep *endpoint.Endpoint, result *endpoint.Result, alert if sendInitialAlert { endpointAlert.Triggered = true } - ep.LastReminderSent = time.Now() + ep.LastReminderSent[endpointAlert.Type] = time.Now() if err := store.Get().UpsertTriggeredEndpointAlert(ep, endpointAlert); err != nil { log.Printf("[watchdog.handleAlertsToTrigger] Failed to persist triggered endpoint alert for endpoint with key=%s: %s", ep.Key(), err.Error()) } @@ -113,7 +113,7 @@ func handleAlertsToResolve(ep *endpoint.Endpoint, result *endpoint.Result, alert } else { log.Printf("[watchdog.handleAlertsToResolve] Not sending alert of type=%s despite being RESOLVED, because the provider wasn't configured properly", endpointAlert.Type) } + ep.LastReminderSent[endpointAlert.Type] = time.Now() } ep.NumberOfFailuresInARow = 0 - ep.LastReminderSent = time.Now() }