From 8e496904be57f09d8e5b6b9b0d0169e5cfc69d71 Mon Sep 17 00:00:00 2001 From: Jackson Sabey Date: Sat, 31 Aug 2024 16:33:03 -0700 Subject: [PATCH] only set RepeatInterval if nil --- alerting/alert/alert.go | 2 +- alerting/provider/provider.go | 2 +- watchdog/alerting.go | 4 ++-- watchdog/alerting_test.go | 30 ++++++++++++++++++++---------- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/alerting/alert/alert.go b/alerting/alert/alert.go index b35a1818..e885551c 100644 --- a/alerting/alert/alert.go +++ b/alerting/alert/alert.go @@ -50,7 +50,7 @@ type Alert struct { ResolveKey string `yaml:"-"` // RepeatInterval is the interval between reminders - RepeatInterval time.Duration `yaml:"repeat-interval,omitempty"` + RepeatInterval *time.Duration `yaml:"repeat-interval"` // Triggered is used to determine whether an alert has been triggered. When an alert is resolved, this value // should be set back to false. It is used to prevent the same alert from going out twice. diff --git a/alerting/provider/provider.go b/alerting/provider/provider.go index 230f73cc..b66ad52b 100644 --- a/alerting/provider/provider.go +++ b/alerting/provider/provider.go @@ -57,7 +57,7 @@ func ParseWithDefaultAlert(providerDefaultAlert, endpointAlert *alert.Alert) { if endpointAlert.SuccessThreshold == 0 { endpointAlert.SuccessThreshold = providerDefaultAlert.SuccessThreshold } - if endpointAlert.RepeatInterval == 0 { + if endpointAlert.RepeatInterval == nil { endpointAlert.RepeatInterval = providerDefaultAlert.RepeatInterval } } diff --git a/watchdog/alerting.go b/watchdog/alerting.go index 99d2ee03..8a12d497 100644 --- a/watchdog/alerting.go +++ b/watchdog/alerting.go @@ -38,8 +38,8 @@ func handleAlertsToTrigger(ep *endpoint.Endpoint, result *endpoint.Result, alert 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) + sendReminder := endpointAlert.Triggered && endpointAlert.RepeatInterval != nil && + *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 { diff --git a/watchdog/alerting_test.go b/watchdog/alerting_test.go index 8b0a6149..8fc75eb4 100644 --- a/watchdog/alerting_test.go +++ b/watchdog/alerting_test.go @@ -39,7 +39,8 @@ func TestHandleAlerting(t *testing.T) { } enabled := true ep := &endpoint.Endpoint{ - URL: "https://example.com", + URL: "https://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: alert.TypeCustom, @@ -83,7 +84,8 @@ func TestHandleAlertingWithBadAlertProvider(t *testing.T) { enabled := true ep := &endpoint.Endpoint{ - URL: "http://example.com", + URL: "http://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: alert.TypeCustom, @@ -118,7 +120,8 @@ func TestHandleAlertingWhenTriggeredAlertIsAlmostResolvedButendpointStartFailing } enabled := true ep := &endpoint.Endpoint{ - URL: "https://example.com", + URL: "https://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: alert.TypeCustom, @@ -153,7 +156,8 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedButSendOnResolvedIsFalse(t *t enabled := true disabled := false ep := &endpoint.Endpoint{ - URL: "https://example.com", + URL: "https://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: alert.TypeCustom, @@ -185,7 +189,8 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedPagerDuty(t *testing.T) { } enabled := true ep := &endpoint.Endpoint{ - URL: "https://example.com", + URL: "https://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: alert.TypePagerDuty, @@ -221,7 +226,8 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedPushover(t *testing.T) { } enabled := true ep := &endpoint.Endpoint{ - URL: "https://example.com", + URL: "https://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: alert.TypePushover, @@ -391,7 +397,8 @@ func TestHandleAlertingWithProviderThatReturnsAnError(t *testing.T) { for _, scenario := range scenarios { t.Run(scenario.Name, func(t *testing.T) { ep := &endpoint.Endpoint{ - URL: "https://example.com", + URL: "https://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: scenario.AlertType, @@ -451,7 +458,8 @@ func TestHandleAlertingWithProviderThatOnlyReturnsErrorOnResolve(t *testing.T) { } enabled := true ep := &endpoint.Endpoint{ - URL: "https://example.com", + URL: "https://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: alert.TypeCustom, @@ -502,8 +510,10 @@ func TestHandleAlertingWithRepeatInterval(t *testing.T) { }, } enabled := true + repeatInterval := 1 * time.Second ep := &endpoint.Endpoint{ - URL: "https://example.com", + URL: "https://example.com", + LastReminderSent: make(map[alert.Type]time.Time), Alerts: []*alert.Alert{ { Type: alert.TypeCustom, @@ -512,7 +522,7 @@ func TestHandleAlertingWithRepeatInterval(t *testing.T) { SuccessThreshold: 3, SendOnResolved: &enabled, Triggered: false, - RepeatInterval: 1 * time.Second, + RepeatInterval: &repeatInterval, }, }, }