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 loop that never returns on http error #92

Merged
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
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