Skip to content

Commit 7759b72

Browse files
committed
Fix loop that never returns on http error
- While running this exporter in the grafana agent, we have identified a loop that never returns in case of http error. This in turn causes the agent to never release a lock, preventing it from making progress. - Note: I have not added additional metrics to report errors as I am trying to keep the scope of this change minimal, but it would be a good idea to add a gauge as stated in the [prometheus guidelines](https://prometheus.io/docs/instrumenting/writing_exporters/#failed-scrapes).
1 parent 239f9e2 commit 7759b72

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

exporter/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func asyncHTTPGets(targets []string, token string) ([]*Response, error) {
4040
case r := <-ch:
4141
if r.err != nil {
4242
log.Errorf("Error scraping API, Error: %v", r.err)
43-
break
43+
return nil, r.err
4444
}
4545
responses = append(responses, r)
4646

test/github_exporter_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ func TestGithubExporter(t *testing.T) {
5454
End()
5555
}
5656

57+
func TestGithubExporterHttpErrorHandling(t *testing.T) {
58+
test, collector := apiTest(withConfig("myOrg/myRepo"))
59+
defer prometheus.Unregister(&collector)
60+
61+
// Test that the exporter returns when an error occurs
62+
// Ideally a new gauge should be added to keep track of scrape errors
63+
// following prometheus exporter guidelines
64+
test.Mocks(
65+
githubPullsError(),
66+
).
67+
Get("/metrics").
68+
Expect(t).
69+
Status(http.StatusOK).
70+
End()
71+
}
72+
5773
func apiTest(conf config.Config) (*apitest.APITest, exporter.Exporter) {
5874
exp := exporter.Exporter{
5975
APIMetrics: exporter.AddMetrics(),
@@ -118,6 +134,15 @@ func githubPulls() *apitest.Mock {
118134
End()
119135
}
120136

137+
func githubPullsError() *apitest.Mock {
138+
return apitest.NewMock().
139+
Get("https://api.github.com/repos/myOrg/myRepo/pulls").
140+
Header("Authorization", "token 12345").
141+
RespondWith().
142+
Status(http.StatusBadRequest).
143+
End()
144+
}
145+
121146
func readFile(path string) string {
122147
bytes, err := ioutil.ReadFile(path)
123148
if err != nil {

0 commit comments

Comments
 (0)