Skip to content

Commit

Permalink
alerting: add alerters type to avoid having to loop over alerters
Browse files Browse the repository at this point in the history
  • Loading branch information
darwinz committed Jun 16, 2022
1 parent 0130dd3 commit 0225281
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
18 changes: 17 additions & 1 deletion internal/alerting/alerter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package alerting

import (
"errors"
"fmt"

"github.com/moov-io/achgateway/internal/service"
)

type Alerters []Alerter

type Alerter interface {
AlertError(err error) error
}
Expand All @@ -14,7 +19,7 @@ func (mn *MockAlerter) AlertError(e error) error {
return nil
}

func NewAlerters(cfg service.ErrorAlerting) ([]Alerter, error) {
func NewAlerters(cfg service.ErrorAlerting) (Alerters, error) {
var alerters []Alerter
switch {
case cfg.Slack != nil:
Expand All @@ -38,3 +43,14 @@ func NewAlerters(cfg service.ErrorAlerting) ([]Alerter, error) {

return alerters, nil
}

func (s *Alerters) AlertError(err error) error {
for _, alerter := range *s {
err = alerter.AlertError(errors.New("error message"))
if err != nil {
return fmt.Errorf("alerting error: %v", err)
}
}

return nil
}
8 changes: 3 additions & 5 deletions internal/alerting/alerter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestNewAlertersPagerDutyAndSlack(t *testing.T) {
t.Skip("Skip as PD_API_KEY and SLACK_ACCESS_TOKEN are not set")
}
var cfg service.ErrorAlerting
var alerters []Alerter
var alerters Alerters
var err error

cfg = service.ErrorAlerting{
Expand All @@ -72,8 +72,6 @@ func TestNewAlertersPagerDutyAndSlack(t *testing.T) {
require.NoError(t, err)
require.Len(t, alerters, 2)

for _, alerter := range alerters {
err = alerter.AlertError(errors.New("error message"))
require.NoError(t, err)
}
err = alerters.AlertError(errors.New("error message"))
require.NoError(t, err)
}
8 changes: 3 additions & 5 deletions internal/incoming/odfi/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type PeriodicScheduler struct {
downloader Downloader
processors Processors

alerters []alerting.Alerter
alerters alerting.Alerters
}

func NewPeriodicScheduler(logger log.Logger, cfg *service.Config, consul *consul.Client, processors Processors) (Scheduler, error) {
Expand Down Expand Up @@ -195,9 +195,7 @@ func (s *PeriodicScheduler) alertOnError(err error) {
return
}

for _, alerter := range s.alerters {
if err := alerter.AlertError(err); err != nil {
s.logger.LogErrorf("ERROR sending alert: %v", err)
}
if err := s.alerters.AlertError(err); err != nil {
s.logger.LogErrorf("ERROR sending alert: %v", err)
}
}
8 changes: 3 additions & 5 deletions internal/pipeline/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type aggregator struct {
auditStorage audittrail.Storage
preuploadTransformers []transform.PreUpload
outputFormatter output.Formatter
alerters []alerting.Alerter
alerters alerting.Alerters
}

func newAggregator(
Expand Down Expand Up @@ -369,9 +369,7 @@ func (xfagg *aggregator) alertOnError(err error) {
return
}

for _, alerter := range xfagg.alerters {
if err := alerter.AlertError(err); err != nil {
xfagg.logger.LogErrorf("ERROR sending alert: %v", err)
}
if err := xfagg.alerters.AlertError(err); err != nil {
xfagg.logger.LogErrorf("ERROR sending alert: %v", err)
}
}

0 comments on commit 0225281

Please sign in to comment.