From aa77e45b1386621409e79e90d837beccdd4a8770 Mon Sep 17 00:00:00 2001 From: Brandon Johnson Date: Wed, 15 Jun 2022 15:01:16 -0600 Subject: [PATCH] alerting: add alerters type to avoid having to loop over alerters --- internal/alerting/alerter.go | 21 ++++++++++++++++++++- internal/alerting/alerter_test.go | 8 +++----- internal/incoming/odfi/scheduler.go | 8 +++----- internal/pipeline/aggregate.go | 8 +++----- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/internal/alerting/alerter.go b/internal/alerting/alerter.go index 4402792..df2094a 100644 --- a/internal/alerting/alerter.go +++ b/internal/alerting/alerter.go @@ -1,9 +1,13 @@ package alerting import ( + "fmt" + "github.com/moov-io/achgateway/internal/service" ) +type Alerters []Alerter + type Alerter interface { AlertError(err error) error } @@ -14,7 +18,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: @@ -38,3 +42,18 @@ func NewAlerters(cfg service.ErrorAlerting) ([]Alerter, error) { return alerters, nil } + +func (s *Alerters) AlertError(e error) error { + if e == nil { + return nil + } + + for _, alerter := range *s { + err := alerter.AlertError(e) + if err != nil { + return fmt.Errorf("alerting error: %v", err) + } + } + + return nil +} diff --git a/internal/alerting/alerter_test.go b/internal/alerting/alerter_test.go index a69dc04..02aa427 100644 --- a/internal/alerting/alerter_test.go +++ b/internal/alerting/alerter_test.go @@ -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{ @@ -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) } diff --git a/internal/incoming/odfi/scheduler.go b/internal/incoming/odfi/scheduler.go index 56cba4b..21a9e7b 100644 --- a/internal/incoming/odfi/scheduler.go +++ b/internal/incoming/odfi/scheduler.go @@ -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) { @@ -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) } } diff --git a/internal/pipeline/aggregate.go b/internal/pipeline/aggregate.go index 5e9b292..32dbb7f 100644 --- a/internal/pipeline/aggregate.go +++ b/internal/pipeline/aggregate.go @@ -56,7 +56,7 @@ type aggregator struct { auditStorage audittrail.Storage preuploadTransformers []transform.PreUpload outputFormatter output.Formatter - alerters []alerting.Alerter + alerters alerting.Alerters } func newAggregator( @@ -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) } }