Skip to content

Commit

Permalink
Introduce an ErrorType for important errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kgeckhart committed Jul 1, 2024
1 parent b29371e commit 280fd6a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
22 changes: 15 additions & 7 deletions pkg/job/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func NewScraper(logger logging.Logger,
}
}

type ErrorType string

var (
AccountErr ErrorType = "Account for job was not found"
ResourceMetadataErr ErrorType = "Failed to run resource metadata for job"
CloudWatchCollectionErr ErrorType = "Failed to gather cloudwatch metrics for job"
)

func (s Scraper) Scrape(ctx context.Context) ([]model.TaggedResourceResult, []model.CloudwatchMetricResult, []Error) {
// Setup so we only do one GetAccount call per region + role combo when running jobs
roleRegionToAccount := map[model.Role]map[string]func() (string, error){}
Expand Down Expand Up @@ -86,7 +94,7 @@ func (s Scraper) Scrape(ctx context.Context) ([]model.TaggedResourceResult, []mo

accountID, err := roleRegionToAccount[role][region]()
if err != nil {
jobError := NewError(jobContext, "Account for job was not found", err)
jobError := NewError(jobContext, AccountErr, err)
mux.Lock()
jobErrors = append(jobErrors, jobError)
mux.Unlock()
Expand All @@ -102,7 +110,7 @@ func (s Scraper) Scrape(ctx context.Context) ([]model.TaggedResourceResult, []mo
rmRunner := s.runnerFactory.NewResourceMetadataRunner(jobLogger, region, role)
resources, err := rmRunner.Run(ctx, region, job)
if err != nil {
jobError := NewError(jobContext, "Failed to run resource metadata for job", err)
jobError := NewError(jobContext, ResourceMetadataErr, err)
mux.Lock()
jobErrors = append(jobErrors, jobError)
mux.Unlock()
Expand Down Expand Up @@ -136,7 +144,7 @@ func (s Scraper) Scrape(ctx context.Context) ([]model.TaggedResourceResult, []mo
cwRunner := s.runnerFactory.NewCloudWatchRunner(jobLogger, region, role, jobToRun)
metricResult, err := cwRunner.Run(ctx)
if err != nil {
jobError := NewError(jobContext, "Failed to gather cloudwatch metrics for job", err)
jobError := NewError(jobContext, CloudWatchCollectionErr, err)
mux.Lock()
jobErrors = append(jobErrors, jobError)
mux.Unlock()
Expand Down Expand Up @@ -219,14 +227,14 @@ func (jc JobContext) ToScrapeContext(customTags []model.Tag) *model.ScrapeContex

type Error struct {
JobContext
Message string
Err error
ErrorType ErrorType
Err error
}

func NewError(context JobContext, message string, err error) Error {
func NewError(context JobContext, errorType ErrorType, err error) Error {
return Error{
JobContext: context,
Message: message,
ErrorType: errorType,
Err: err,
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/job/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ func TestScrapeRunner_Run(t *testing.T) {
return "", errors.New("failed to get account")
},
expectedErrs: []job.Error{
{JobContext: job.JobContext{AccountID: "", Namespace: "aws-namespace", Region: "us-east-1", RoleARN: "aws-arn-1"}},
{JobContext: job.JobContext{AccountID: "", Namespace: "custom-namespace", Region: "us-east-2", RoleARN: "aws-arn-2"}},
{JobContext: job.JobContext{AccountID: "", Namespace: "aws-namespace", Region: "us-east-1", RoleARN: "aws-arn-1"}, ErrorType: job.AccountErr},
{JobContext: job.JobContext{AccountID: "", Namespace: "custom-namespace", Region: "us-east-2", RoleARN: "aws-arn-2"}, ErrorType: job.AccountErr},
},
},
{
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestScrapeRunner_Run(t *testing.T) {
},
},
expectedErrs: []job.Error{
{JobContext: job.JobContext{AccountID: "aws-account-1", Namespace: "aws-namespace", Region: "us-east-1", RoleARN: "aws-arn-1"}},
{JobContext: job.JobContext{AccountID: "aws-account-1", Namespace: "aws-namespace", Region: "us-east-1", RoleARN: "aws-arn-1"}, ErrorType: job.ResourceMetadataErr},
},
},
{
Expand Down Expand Up @@ -421,7 +421,7 @@ func TestScrapeRunner_Run(t *testing.T) {
},
},
expectedErrs: []job.Error{
{JobContext: job.JobContext{AccountID: "aws-account-1", Namespace: "custom-namespace", Region: "us-east-2", RoleARN: "aws-arn-2"}},
{JobContext: job.JobContext{AccountID: "aws-account-1", Namespace: "custom-namespace", Region: "us-east-2", RoleARN: "aws-arn-2"}, ErrorType: job.CloudWatchCollectionErr},
},
},
}
Expand All @@ -443,9 +443,9 @@ func TestScrapeRunner_Run(t *testing.T) {
assert.NoError(t, err, "failed to diff metrics")
assert.Len(t, changelog, 0, changelog)

// We don't need the check the exact error or message
// We don't want to check the exact error just the message
changelog, err = diff.Diff(tc.expectedErrs, errs, diff.Filter(func(_ []string, _ reflect.Type, field reflect.StructField) bool {
return !(field.Name == "Err" || field.Name == "Message")
return !(field.Name == "Err")
}))
assert.NoError(t, err, "failed to diff errs")
assert.Len(t, changelog, 0, changelog)
Expand Down

0 comments on commit 280fd6a

Please sign in to comment.