From 5734e446013947b9480ad283d21320026f20d0fe Mon Sep 17 00:00:00 2001 From: Daniel Lohse Date: Wed, 13 Nov 2019 12:22:54 +0100 Subject: [PATCH] Close metric family channel in case of errors `FetchMetricFamilies` fails to close the channel if the request fails or can't be constructed correctly (lines 154, 160 and 164). In these cases `ch` is never closed and the caller will probably leak a goroutine when used as a library. This fix doesn't break backwards compatibility. Signed-off-by: Daniel Lohse --- prom2json.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/prom2json.go b/prom2json.go index 6f601b30..10934145 100644 --- a/prom2json.go +++ b/prom2json.go @@ -149,16 +149,19 @@ func makeBuckets(m *dto.Metric) map[string]string { func FetchMetricFamilies(url string, ch chan<- *dto.MetricFamily, transport http.RoundTripper) error { req, err := http.NewRequest("GET", url, nil) if err != nil { + close(ch) return fmt.Errorf("creating GET request for URL %q failed: %v", url, err) } req.Header.Add("Accept", acceptHeader) client := http.Client{Transport: transport} resp, err := client.Do(req) if err != nil { + close(ch) return fmt.Errorf("executing GET request for URL %q failed: %v", url, err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { + close(ch) return fmt.Errorf("GET request for URL %q returned HTTP status %s", url, resp.Status) } return ParseResponse(resp, ch)