Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing notification on error #3076

Merged
merged 8 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions utils/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@
err := e.sendNotifications(api, object, opts)
if err != nil {
logCtx.Errorf("Notifications failed to send for eventReason %s with error: %s", opts.EventReason, err)
e.NotificationFailedCounter.WithLabelValues(namespace, name, opts.EventType, opts.EventReason).Inc()
}
e.NotificationSuccessCounter.WithLabelValues(namespace, name, opts.EventType, opts.EventReason).Inc()
}
}

Expand Down Expand Up @@ -248,7 +246,7 @@
}

// Send notifications for triggered event if user is subscribed
func (e *EventRecorderAdapter) sendNotifications(notificationsAPI api.API, object runtime.Object, opts EventOptions) error {
func (e *EventRecorderAdapter) sendNotifications(notificationsAPI api.API, object runtime.Object, opts EventOptions) []error {
logCtx := logutil.WithObject(object)
_, namespace, name := logutil.KindNamespaceName(logCtx)
startTime := timeutil.Now()
Expand All @@ -259,7 +257,7 @@
}()

if notificationsAPI == nil {
return fmt.Errorf("notificationsAPI is nil")
return []error{fmt.Errorf("NotificationsAPI is nil")}
}

cfg := notificationsAPI.GetConfig()
Expand All @@ -274,39 +272,53 @@

objMap, err := toObjectMap(object)
if err != nil {
return err
return []error{err}

Check warning on line 275 in utils/record/record.go

View check run for this annotation

Codecov / codecov/patch

utils/record/record.go#L275

Added line #L275 was not covered by tests
}

emptyCondition := hash("")

// We should not return in these loops because we want other configured notifications to still send if they can.
errors := []error{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I am understanding this correctly but I can't find where this list of errors is returned..

for _, destination := range destinations {
res, err := notificationsAPI.RunTrigger(trigger, objMap)
if err != nil {
log.Errorf("Failed to execute condition of trigger %s: %v", trigger, err)
return err
log.Errorf("Failed to run trigger, trigger: %s, destination: %s, namespace config: %s : %v",
trigger, destination, notificationsAPI.GetConfig().Namespace, err)
errors = append(errors, err)
continue
}
log.Infof("Trigger %s result: %v", trigger, res)

for _, c := range res {
log.Infof("Res When Condition hash: %s, Templates: %s", c.Key, c.Templates)
log.Infof("Result when condition hash: %s, templates: %s", c.Key, c.Templates)
s := strings.Split(c.Key, ".")[1]
if s != emptyCondition && c.Triggered == true {
err = notificationsAPI.Send(objMap, c.Templates, destination)
if err != nil {
log.Errorf("notification error: %s", err.Error())
return err
log.Errorf("Failed to execute the sending of notification on not empty condition, trigger: %s, destination: %s, namespace config: %s : %v",
trigger, destination, notificationsAPI.GetConfig().Namespace, err)
e.NotificationFailedCounter.WithLabelValues(namespace, name, opts.EventType, opts.EventReason).Inc()
errors = append(errors, err)
continue

Check warning on line 302 in utils/record/record.go

View check run for this annotation

Codecov / codecov/patch

utils/record/record.go#L298-L302

Added lines #L298 - L302 were not covered by tests
}
e.NotificationSuccessCounter.WithLabelValues(namespace, name, opts.EventType, opts.EventReason).Inc()

Check warning on line 304 in utils/record/record.go

View check run for this annotation

Codecov / codecov/patch

utils/record/record.go#L304

Added line #L304 was not covered by tests
} else if s == emptyCondition {
err = notificationsAPI.Send(objMap, c.Templates, destination)
if err != nil {
log.Errorf("notification error: %s", err.Error())
return err
log.Errorf("Failed to execute the sending of notification on empty condition, trigger: %s, destination: %s, namespace config: %s : %v",
trigger, destination, notificationsAPI.GetConfig().Namespace, err)
e.NotificationFailedCounter.WithLabelValues(namespace, name, opts.EventType, opts.EventReason).Inc()
errors = append(errors, err)
continue
}
e.NotificationSuccessCounter.WithLabelValues(namespace, name, opts.EventType, opts.EventReason).Inc()
}
}
}

return nil
if len(errors) == 0 {
return nil
}
return errors
}

// This function is copied over from notification engine to make sure we honour emptyCondition
Expand Down
14 changes: 7 additions & 7 deletions utils/record/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestSendNotifications(t *testing.T) {
rec.EventRecorderAdapter.apiFactory = apiFactory
//ch := make(chan prometheus.HistogramVec, 1)
err := rec.sendNotifications(mockAPI, &r, EventOptions{EventReason: "FooReason"})
assert.NoError(t, err)
assert.Nil(t, err)
}

func TestSendNotificationsWhenCondition(t *testing.T) {
Expand All @@ -140,7 +140,7 @@ func TestSendNotificationsWhenCondition(t *testing.T) {
rec.EventRecorderAdapter.apiFactory = apiFactory
//ch := make(chan prometheus.HistogramVec, 1)
err := rec.sendNotifications(mockAPI, &r, EventOptions{EventReason: "FooReason"})
assert.NoError(t, err)
assert.Nil(t, err)
}

func TestSendNotificationsWhenConditionTime(t *testing.T) {
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestSendNotificationsFails(t *testing.T) {
rec.EventRecorderAdapter.apiFactory = apiFactory

err := rec.sendNotifications(mockAPI, &r, EventOptions{EventReason: "FooReason"})
assert.Error(t, err)
assert.Len(t, err, 1)
})

t.Run("GetAPIError", func(t *testing.T) {
Expand All @@ -349,7 +349,7 @@ func TestSendNotificationsFails(t *testing.T) {
rec.EventRecorderAdapter.apiFactory = apiFactory

err := rec.sendNotifications(nil, &r, EventOptions{EventReason: "FooReason"})
assert.Error(t, err)
assert.NotNil(t, err)
})

}
Expand Down Expand Up @@ -380,7 +380,7 @@ func TestSendNotificationsFailsWithRunTriggerError(t *testing.T) {
rec.EventRecorderAdapter.apiFactory = apiFactory

err := rec.sendNotifications(mockAPI, &r, EventOptions{EventReason: "FooReason"})
assert.Error(t, err)
assert.Len(t, err, 1)
})

t.Run("GetAPIError", func(t *testing.T) {
Expand All @@ -389,7 +389,7 @@ func TestSendNotificationsFailsWithRunTriggerError(t *testing.T) {
rec.EventRecorderAdapter.apiFactory = apiFactory

err := rec.sendNotifications(nil, &r, EventOptions{EventReason: "FooReason"})
assert.Error(t, err)
assert.NotNil(t, err)
})

}
Expand Down Expand Up @@ -419,7 +419,7 @@ func TestSendNotificationsNoTrigger(t *testing.T) {
rec.EventRecorderAdapter.apiFactory = apiFactory

err := rec.sendNotifications(mockAPI, &r, EventOptions{EventReason: "MissingReason"})
assert.Error(t, err)
assert.Len(t, err, 1)
}

func TestNewAPIFactorySettings(t *testing.T) {
Expand Down
Loading