only set RepeatInterval if nil

This commit is contained in:
Jackson Sabey 2024-08-31 16:33:03 -07:00
parent 45bfd47920
commit 8e496904be
4 changed files with 24 additions and 14 deletions

View File

@ -50,7 +50,7 @@ type Alert struct {
ResolveKey string `yaml:"-"` ResolveKey string `yaml:"-"`
// RepeatInterval is the interval between reminders // 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 // 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. // should be set back to false. It is used to prevent the same alert from going out twice.

View File

@ -57,7 +57,7 @@ func ParseWithDefaultAlert(providerDefaultAlert, endpointAlert *alert.Alert) {
if endpointAlert.SuccessThreshold == 0 { if endpointAlert.SuccessThreshold == 0 {
endpointAlert.SuccessThreshold = providerDefaultAlert.SuccessThreshold endpointAlert.SuccessThreshold = providerDefaultAlert.SuccessThreshold
} }
if endpointAlert.RepeatInterval == 0 { if endpointAlert.RepeatInterval == nil {
endpointAlert.RepeatInterval = providerDefaultAlert.RepeatInterval endpointAlert.RepeatInterval = providerDefaultAlert.RepeatInterval
} }
} }

View File

@ -38,8 +38,8 @@ func handleAlertsToTrigger(ep *endpoint.Endpoint, result *endpoint.Result, alert
if lr, ok := ep.LastReminderSent[endpointAlert.Type]; ok && !lr.IsZero() { if lr, ok := ep.LastReminderSent[endpointAlert.Type]; ok && !lr.IsZero() {
lastReminder = time.Since(lr) lastReminder = time.Since(lr)
} }
sendReminder := endpointAlert.Triggered && endpointAlert.RepeatInterval > 0 && sendReminder := endpointAlert.Triggered && endpointAlert.RepeatInterval != nil &&
(lastReminder == 0 || lastReminder >= endpointAlert.RepeatInterval) *endpointAlert.RepeatInterval > 0 && (lastReminder == 0 || lastReminder >= *endpointAlert.RepeatInterval)
// If neither initial alert nor reminder needs to be sent, skip to the next alert // If neither initial alert nor reminder needs to be sent, skip to the next alert
if !sendInitialAlert && !sendReminder { if !sendInitialAlert && !sendReminder {
if debug { if debug {

View File

@ -39,7 +39,8 @@ func TestHandleAlerting(t *testing.T) {
} }
enabled := true enabled := true
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "https://example.com", URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: alert.TypeCustom, Type: alert.TypeCustom,
@ -83,7 +84,8 @@ func TestHandleAlertingWithBadAlertProvider(t *testing.T) {
enabled := true enabled := true
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "http://example.com", URL: "http://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: alert.TypeCustom, Type: alert.TypeCustom,
@ -118,7 +120,8 @@ func TestHandleAlertingWhenTriggeredAlertIsAlmostResolvedButendpointStartFailing
} }
enabled := true enabled := true
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "https://example.com", URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: alert.TypeCustom, Type: alert.TypeCustom,
@ -153,7 +156,8 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedButSendOnResolvedIsFalse(t *t
enabled := true enabled := true
disabled := false disabled := false
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "https://example.com", URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: alert.TypeCustom, Type: alert.TypeCustom,
@ -185,7 +189,8 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedPagerDuty(t *testing.T) {
} }
enabled := true enabled := true
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "https://example.com", URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: alert.TypePagerDuty, Type: alert.TypePagerDuty,
@ -221,7 +226,8 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedPushover(t *testing.T) {
} }
enabled := true enabled := true
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "https://example.com", URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: alert.TypePushover, Type: alert.TypePushover,
@ -391,7 +397,8 @@ func TestHandleAlertingWithProviderThatReturnsAnError(t *testing.T) {
for _, scenario := range scenarios { for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) { t.Run(scenario.Name, func(t *testing.T) {
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "https://example.com", URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: scenario.AlertType, Type: scenario.AlertType,
@ -451,7 +458,8 @@ func TestHandleAlertingWithProviderThatOnlyReturnsErrorOnResolve(t *testing.T) {
} }
enabled := true enabled := true
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "https://example.com", URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: alert.TypeCustom, Type: alert.TypeCustom,
@ -502,8 +510,10 @@ func TestHandleAlertingWithRepeatInterval(t *testing.T) {
}, },
} }
enabled := true enabled := true
repeatInterval := 1 * time.Second
ep := &endpoint.Endpoint{ ep := &endpoint.Endpoint{
URL: "https://example.com", URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{ Alerts: []*alert.Alert{
{ {
Type: alert.TypeCustom, Type: alert.TypeCustom,
@ -512,7 +522,7 @@ func TestHandleAlertingWithRepeatInterval(t *testing.T) {
SuccessThreshold: 3, SuccessThreshold: 3,
SendOnResolved: &enabled, SendOnResolved: &enabled,
Triggered: false, Triggered: false,
RepeatInterval: 1 * time.Second, RepeatInterval: &repeatInterval,
}, },
}, },
} }