Skip to content

Commit

Permalink
Merge pull request #92 from jcreixell/fix-error-handling-upstream
Browse files Browse the repository at this point in the history
Fix loop that never returns on http error
  • Loading branch information
henrymcconville authored Aug 23, 2023
2 parents ed3b89f + 7759b72 commit d6d3e44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion exporter/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func asyncHTTPGets(targets []string, token string) ([]*Response, error) {
case r := <-ch:
if r.err != nil {
log.Errorf("Error scraping API, Error: %v", r.err)
break
return nil, r.err
}
responses = append(responses, r)

Expand Down
25 changes: 25 additions & 0 deletions test/github_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ func TestGithubExporter(t *testing.T) {
End()
}

func TestGithubExporterHttpErrorHandling(t *testing.T) {
test, collector := apiTest(withConfig("myOrg/myRepo"))
defer prometheus.Unregister(&collector)

// Test that the exporter returns when an error occurs
// Ideally a new gauge should be added to keep track of scrape errors
// following prometheus exporter guidelines
test.Mocks(
githubPullsError(),
).
Get("/metrics").
Expect(t).
Status(http.StatusOK).
End()
}

func apiTest(conf config.Config) (*apitest.APITest, exporter.Exporter) {
exp := exporter.Exporter{
APIMetrics: exporter.AddMetrics(),
Expand Down Expand Up @@ -118,6 +134,15 @@ func githubPulls() *apitest.Mock {
End()
}

func githubPullsError() *apitest.Mock {
return apitest.NewMock().
Get("https://api.github.com/repos/myOrg/myRepo/pulls").
Header("Authorization", "token 12345").
RespondWith().
Status(http.StatusBadRequest).
End()
}

func readFile(path string) string {
bytes, err := ioutil.ReadFile(path)
if err != nil {
Expand Down

0 comments on commit d6d3e44

Please sign in to comment.