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:"-"`
// 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.

View File

@ -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
}
}

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() {
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 {

View File

@ -40,6 +40,7 @@ func TestHandleAlerting(t *testing.T) {
enabled := true
ep := &endpoint.Endpoint{
URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{
{
Type: alert.TypeCustom,
@ -84,6 +85,7 @@ func TestHandleAlertingWithBadAlertProvider(t *testing.T) {
enabled := true
ep := &endpoint.Endpoint{
URL: "http://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{
{
Type: alert.TypeCustom,
@ -119,6 +121,7 @@ func TestHandleAlertingWhenTriggeredAlertIsAlmostResolvedButendpointStartFailing
enabled := true
ep := &endpoint.Endpoint{
URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{
{
Type: alert.TypeCustom,
@ -154,6 +157,7 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedButSendOnResolvedIsFalse(t *t
disabled := false
ep := &endpoint.Endpoint{
URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{
{
Type: alert.TypeCustom,
@ -186,6 +190,7 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedPagerDuty(t *testing.T) {
enabled := true
ep := &endpoint.Endpoint{
URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{
{
Type: alert.TypePagerDuty,
@ -222,6 +227,7 @@ func TestHandleAlertingWhenTriggeredAlertIsResolvedPushover(t *testing.T) {
enabled := true
ep := &endpoint.Endpoint{
URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{
{
Type: alert.TypePushover,
@ -392,6 +398,7 @@ func TestHandleAlertingWithProviderThatReturnsAnError(t *testing.T) {
t.Run(scenario.Name, func(t *testing.T) {
ep := &endpoint.Endpoint{
URL: "https://example.com",
LastReminderSent: make(map[alert.Type]time.Time),
Alerts: []*alert.Alert{
{
Type: scenario.AlertType,
@ -452,6 +459,7 @@ func TestHandleAlertingWithProviderThatOnlyReturnsErrorOnResolve(t *testing.T) {
enabled := true
ep := &endpoint.Endpoint{
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",
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,
},
},
}