go.d fix duplicate closeBody func (#18544)
Ilya Mashchenko committed
Sep 13, 2024 at 12:55 UTC
730846aef0b3ae1340f5b97ebd4314bbee3162ef
37 files changed
+111
-337
src/go/plugin/go.d/modules/activemq/apiclient.go
+2
-10
@@ -5,7 +5,6 @@ package activemq
5
import (
6
"encoding/xml"
7
"fmt"
8
- "io"
8
"net/http"
9
"net/url"
10
"path"
@@ -67,7 +66,7 @@ func (a *apiClient) getQueues() (*queues, error) {
66
67
resp, err := a.doRequestOK(req)
68
70
- defer closeBody(resp)
69
+ defer web.CloseBody(resp)
70
71
if err != nil {
72
return nil, err
@@ -90,7 +89,7 @@ func (a *apiClient) getTopics() (*topics, error) {
89
90
resp, err := a.doRequestOK(req)
91
93
- defer closeBody(resp)
92
+ defer web.CloseBody(resp)
93
94
if err != nil {
95
return nil, err
@@ -128,10 +127,3 @@ func (a *apiClient) createRequest(urlPath string) (*http.Request, error) {
127
req.URL = u.String()
128
return web.NewHTTPRequest(req)
129
}
131
-
132
-func closeBody(resp *http.Response) {
133
- if resp != nil && resp.Body != nil {
134
- _, _ = io.Copy(io.Discard, resp.Body)
135
- _ = resp.Body.Close()
136
- }
137
-}
src/go/plugin/go.d/modules/apache/collect.go
+2
-8
@@ -40,7 +40,8 @@ func (a *Apache) scrapeStatus() (*serverStatus, error) {
40
if err != nil {
41
return nil, fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
42
}
43
- defer closeBody(resp)
43
+
44
+ defer web.CloseBody(resp)
45
46
if resp.StatusCode != http.StatusOK {
47
return nil, fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -154,10 +155,3 @@ func parseFloat(value string) *float64 {
155
}
156
return &v
157
}
157
-
158
-func closeBody(resp *http.Response) {
159
- if resp != nil && resp.Body != nil {
160
- _, _ = io.Copy(io.Discard, resp.Body)
161
- _ = resp.Body.Close()
162
- }
163
-}
src/go/plugin/go.d/modules/bind/json_client.go
+2
-9
@@ -5,7 +5,6 @@ package bind
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"net/url"
10
"path"
@@ -61,7 +60,8 @@ func (c jsonClient) serverStats() (*serverStats, error) {
60
if err != nil {
61
return nil, fmt.Errorf("error on request : %v", err)
62
}
64
- defer closeBody(resp)
63
+
64
+ defer web.CloseBody(resp)
65
66
if resp.StatusCode != http.StatusOK {
67
return nil, fmt.Errorf("%s returned HTTP status %d", httpReq.URL, resp.StatusCode)
@@ -73,10 +73,3 @@ func (c jsonClient) serverStats() (*serverStats, error) {
73
}
74
return stats, nil
75
}
76
-
77
-func closeBody(resp *http.Response) {
78
- if resp != nil && resp.Body != nil {
79
- _, _ = io.Copy(io.Discard, resp.Body)
80
- _ = resp.Body.Close()
81
- }
82
-}
src/go/plugin/go.d/modules/bind/xml3_client.go
+2
-1
@@ -62,7 +62,8 @@ func (c xml3Client) serverStats() (*serverStats, error) {
62
if err != nil {
63
return nil, fmt.Errorf("error on request : %v", err)
64
}
65
- defer closeBody(resp)
65
+
66
+ defer web.CloseBody(resp)
67
68
if resp.StatusCode != http.StatusOK {
69
return nil, fmt.Errorf("%s returned HTTP status %d", httpReq.URL, resp.StatusCode)
src/go/plugin/go.d/modules/clickhouse/collect.go
+4
-8
@@ -9,6 +9,8 @@ import (
9
"net/http"
10
"net/url"
11
"slices"
12
+
13
+ "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
14
)
15
16
const precision = 1000
@@ -43,7 +45,8 @@ func (c *ClickHouse) doOKDecodeCSV(req *http.Request, assign func(column, value
45
if err != nil {
46
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
47
}
46
- defer closeBody(resp)
48
+
49
+ defer web.CloseBody(resp)
50
51
if resp.StatusCode != http.StatusOK {
52
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -87,10 +90,3 @@ func readCSVResponseData(reader io.Reader, assign func(column, value string, lin
90
func makeURLQuery(q string) string {
91
return url.Values{"query": {q}}.Encode()
92
}
90
-
91
-func closeBody(resp *http.Response) {
92
- if resp != nil && resp.Body != nil {
93
- _, _ = io.Copy(io.Discard, resp.Body)
94
- _ = resp.Body.Close()
95
- }
96
-}
src/go/plugin/go.d/modules/consul/collect.go
+1
-9
@@ -5,7 +5,6 @@ package consul
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
10
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
@@ -83,7 +82,7 @@ func (c *Consul) doOKDecode(urlPath string, in interface{}, statusCodes ...int)
82
return fmt.Errorf("error on request to %s : %v", req.URL, err)
83
}
84
86
- defer closeBody(resp)
85
+ defer web.CloseBody(resp)
86
87
codes := map[int]bool{http.StatusOK: true}
88
for _, v := range statusCodes {
@@ -101,13 +100,6 @@ func (c *Consul) doOKDecode(urlPath string, in interface{}, statusCodes ...int)
100
return nil
101
}
102
104
-func closeBody(resp *http.Response) {
105
- if resp != nil && resp.Body != nil {
106
- _, _ = io.Copy(io.Discard, resp.Body)
107
- _ = resp.Body.Close()
108
- }
109
-}
110
-
103
func boolToInt(v bool) int64 {
104
if v {
105
return 1
src/go/plugin/go.d/modules/couchbase/collect.go
+2
-9
@@ -5,7 +5,6 @@ package couchbase
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"net/url"
10
@@ -130,7 +129,8 @@ func (cb *Couchbase) doOKDecode(req *http.Request, in interface{}) error {
129
if err != nil {
130
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
131
}
133
- defer closeBody(resp)
132
+
133
+ defer web.CloseBody(resp)
134
135
if resp.StatusCode != http.StatusOK {
136
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -142,13 +142,6 @@ func (cb *Couchbase) doOKDecode(req *http.Request, in interface{}) error {
142
return nil
143
}
144
145
-func closeBody(resp *http.Response) {
146
- if resp != nil && resp.Body != nil {
147
- _, _ = io.Copy(io.Discard, resp.Body)
148
- _ = resp.Body.Close()
149
- }
150
-}
151
-
145
func indexDimID(name, metric string) string {
146
return fmt.Sprintf("bucket_%s_%s", name, metric)
147
}
src/go/plugin/go.d/modules/couchdb/collect.go
+10
-10
@@ -212,11 +212,18 @@ func (cdb *CouchDB) doOKDecode(req *http.Request, in interface{}) error {
212
if err != nil {
213
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
214
}
215
- defer closeBody(resp)
215
217
- // TODO: read resp body, it contains reason
218
- // ex.: {"error":"bad_request","reason":"`keys` member must exist."} (400)
216
+ defer web.CloseBody(resp)
217
+
218
if resp.StatusCode != http.StatusOK {
219
+ var msg struct {
220
+ Error string `json:"error"`
221
+ Reason string `json:"reason"`
222
+ }
223
+ if err := json.NewDecoder(resp.Body).Decode(&msg); err == nil && msg.Error != "" {
224
+ return fmt.Errorf("'%s' returned HTTP status code: %d (err '%s', reason '%s')",
225
+ req.URL, resp.StatusCode, msg.Error, msg.Reason)
226
+ }
227
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
228
}
229
@@ -226,13 +233,6 @@ func (cdb *CouchDB) doOKDecode(req *http.Request, in interface{}) error {
233
return nil
234
}
235
229
-func closeBody(resp *http.Response) {
230
- if resp != nil && resp.Body != nil {
231
- _, _ = io.Copy(io.Discard, resp.Body)
232
- _ = resp.Body.Close()
233
- }
234
-}
235
-
236
func merge(dst, src map[string]int64, prefix string) {
237
for k, v := range src {
238
dst[prefix+"_"+k] = v
src/go/plugin/go.d/modules/dnsdist/collect.go
+2
-9
@@ -5,7 +5,6 @@ package dnsdist
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"net/url"
10
@@ -55,7 +54,8 @@ func (d *DNSdist) doOKDecode(req *http.Request, in interface{}) error {
54
if err != nil {
55
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
56
}
58
- defer closeBody(resp)
57
+
58
+ defer web.CloseBody(resp)
59
60
if resp.StatusCode != http.StatusOK {
61
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -67,10 +67,3 @@ func (d *DNSdist) doOKDecode(req *http.Request, in interface{}) error {
67
68
return nil
69
}
70
-
71
-func closeBody(resp *http.Response) {
72
- if resp != nil && resp.Body != nil {
73
- _, _ = io.Copy(io.Discard, resp.Body)
74
- _ = resp.Body.Close()
75
- }
76
-}
src/go/plugin/go.d/modules/dockerhub/apiclient.go
+15
-21
@@ -5,7 +5,6 @@ package dockerhub
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"net/url"
10
"path"
@@ -37,34 +36,35 @@ func (a apiClient) getRepository(repoName string) (*repository, error) {
36
return nil, fmt.Errorf("error on creating http request : %v", err)
37
}
38
40
- resp, err := a.doRequestOK(req)
41
- defer closeBody(resp)
42
- if err != nil {
43
- return nil, err
44
- }
45
-
39
var repo repository
47
- if err := json.NewDecoder(resp.Body).Decode(&repo); err != nil {
48
- return nil, fmt.Errorf("error on parsing response from %s : %v", req.URL, err)
40
+ if err := a.doOKDecode(req, &repo); err != nil {
41
+ return nil, err
42
}
50
-
43
return &repo, nil
44
}
45
54
-func (a apiClient) doRequestOK(req *http.Request) (*http.Response, error) {
46
+func (a apiClient) doOKDecode(req *http.Request, in any) error {
47
resp, err := a.httpClient.Do(req)
48
if err != nil {
57
- return nil, fmt.Errorf("error on request: %v", err)
49
+ return fmt.Errorf("error on request: %v", err)
50
}
51
52
+ defer web.CloseBody(resp)
53
+
54
if resp.StatusCode != http.StatusOK {
61
- return resp, fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode)
55
+ return fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode)
56
}
63
- return resp, nil
57
+
58
+ if err := json.NewDecoder(resp.Body).Decode(in); err != nil {
59
+ return fmt.Errorf("error on decoding response from '%s': %v", req.URL, err)
60
+ }
61
+
62
+ return nil
63
}
64
65
func (a apiClient) createRequest(urlPath string) (*http.Request, error) {
66
req := a.request.Copy()
67
+
68
u, err := url.Parse(req.URL)
69
if err != nil {
70
return nil, err
@@ -72,12 +72,6 @@ func (a apiClient) createRequest(urlPath string) (*http.Request, error) {
72
73
u.Path = path.Join(u.Path, urlPath)
74
req.URL = u.String()
75
- return web.NewHTTPRequest(req)
76
-}
75
78
-func closeBody(resp *http.Response) {
79
- if resp != nil && resp.Body != nil {
80
- _, _ = io.Copy(io.Discard, resp.Body)
81
- _ = resp.Body.Close()
82
- }
76
+ return web.NewHTTPRequest(req)
77
}
src/go/plugin/go.d/modules/elasticsearch/collect.go
+2
-9
@@ -6,7 +6,6 @@ import (
6
"encoding/json"
7
"errors"
8
"fmt"
9
- "io"
9
"math"
10
"net/http"
11
"strconv"
@@ -236,7 +235,8 @@ func (es *Elasticsearch) doOKDecode(req *http.Request, in interface{}) error {
235
if err != nil {
236
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
237
}
239
- defer closeBody(resp)
238
+
239
+ defer web.CloseBody(resp)
240
241
if resp.StatusCode != http.StatusOK {
242
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -248,13 +248,6 @@ func (es *Elasticsearch) doOKDecode(req *http.Request, in interface{}) error {
248
return nil
249
}
250
251
-func closeBody(resp *http.Response) {
252
- if resp != nil && resp.Body != nil {
253
- _, _ = io.Copy(io.Discard, resp.Body)
254
- _ = resp.Body.Close()
255
- }
256
-}
257
-
251
func convertIndexStoreSizeToBytes(size string) int64 {
252
var num float64
253
switch {
src/go/plugin/go.d/modules/fluentd/apiclient.go
+13
-20
@@ -5,7 +5,6 @@ package fluentd
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"net/url"
10
"path"
@@ -55,30 +54,31 @@ func (a apiClient) getPluginsInfo() (*pluginsInfo, error) {
54
return nil, fmt.Errorf("error on creating request : %v", err)
55
}
56
58
- resp, err := a.doRequestOK(req)
59
- defer closeBody(resp)
60
- if err != nil {
61
- return nil, err
62
- }
63
-
57
var info pluginsInfo
65
- if err = json.NewDecoder(resp.Body).Decode(&info); err != nil {
66
- return nil, fmt.Errorf("error on decoding response from %s : %v", req.URL, err)
58
+ if err := a.doOKDecode(req, &info); err != nil {
59
+ return nil, err
60
}
61
62
return &info, nil
63
}
64
72
-func (a apiClient) doRequestOK(req *http.Request) (*http.Response, error) {
65
+func (a apiClient) doOKDecode(req *http.Request, in any) error {
66
resp, err := a.httpClient.Do(req)
67
if err != nil {
75
- return nil, fmt.Errorf("error on request: %v", err)
68
+ return fmt.Errorf("error on request: %v", err)
69
}
70
71
+ defer web.CloseBody(resp)
72
+
73
if resp.StatusCode != http.StatusOK {
79
- return resp, fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode)
74
+ return fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode)
75
}
81
- return resp, nil
76
+
77
+ if err := json.NewDecoder(resp.Body).Decode(in); err != nil {
78
+ return fmt.Errorf("error on decoding response from %s : %v", req.URL, err)
79
+ }
80
+
81
+ return nil
82
}
83
84
func (a apiClient) createRequest(urlPath string) (*http.Request, error) {
@@ -92,10 +92,3 @@ func (a apiClient) createRequest(urlPath string) (*http.Request, error) {
92
req.URL = u.String()
93
return web.NewHTTPRequest(req)
94
}
95
-
96
-func closeBody(resp *http.Response) {
97
- if resp != nil && resp.Body != nil {
98
- _, _ = io.Copy(io.Discard, resp.Body)
99
- _ = resp.Body.Close()
100
- }
101
-}
src/go/plugin/go.d/modules/hdfs/client.go
+1
-9
@@ -5,7 +5,6 @@ package hdfs
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
10
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
@@ -49,7 +48,7 @@ func (c *client) doOK() (*http.Response, error) {
48
49
func (c *client) doOKWithDecodeJSON(dst interface{}) error {
50
resp, err := c.doOK()
52
- defer closeBody(resp)
51
+ defer web.CloseBody(resp)
52
if err != nil {
53
return err
54
}
@@ -60,10 +59,3 @@ func (c *client) doOKWithDecodeJSON(dst interface{}) error {
59
}
60
return nil
61
}
63
-
64
-func closeBody(resp *http.Response) {
65
- if resp != nil && resp.Body != nil {
66
- _, _ = io.Copy(io.Discard, resp.Body)
67
- _ = resp.Body.Close()
68
- }
69
-}
src/go/plugin/go.d/modules/httpcheck/collect.go
+1
-9
@@ -40,7 +40,7 @@ func (hc *HTTPCheck) collect() (map[string]int64, error) {
40
resp, err := hc.httpClient.Do(req)
41
dur := time.Since(start)
42
43
- defer closeBody(resp)
43
+ defer web.CloseBody(resp)
44
45
var mx metrics
46
@@ -176,14 +176,6 @@ func (hc *HTTPCheck) readCookieFile() error {
176
return nil
177
}
178
179
-func closeBody(resp *http.Response) {
180
- if resp == nil || resp.Body == nil {
181
- return
182
- }
183
- _, _ = io.Copy(io.Discard, resp.Body)
184
- _ = resp.Body.Close()
185
-}
186
-
179
func durationToMs(duration time.Duration) int {
180
return int(duration) / (int(time.Millisecond) / int(time.Nanosecond))
181
}
src/go/plugin/go.d/modules/icecast/collect.go
+2
-9
@@ -5,7 +5,6 @@ package icecast
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
10
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
@@ -87,7 +86,8 @@ func (ic *Icecast) doOKDecode(req *http.Request, in interface{}) error {
86
if err != nil {
87
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
88
}
90
- defer closeBody(resp)
89
+
90
+ defer web.CloseBody(resp)
91
92
if resp.StatusCode != http.StatusOK {
93
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -98,10 +98,3 @@ func (ic *Icecast) doOKDecode(req *http.Request, in interface{}) error {
98
}
99
return nil
100
}
101
-
102
-func closeBody(resp *http.Response) {
103
- if resp != nil && resp.Body != nil {
104
- _, _ = io.Copy(io.Discard, resp.Body)
105
- _ = resp.Body.Close()
106
- }
107
-}
src/go/plugin/go.d/modules/ipfs/collect.go
+2
-9
@@ -5,7 +5,6 @@ package ipfs
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
10
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
@@ -189,7 +188,8 @@ func (ip *IPFS) doOKDecode(req *http.Request, in interface{}) error {
188
if err != nil {
189
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
190
}
192
- defer closeBody(resp)
191
+
192
+ defer web.CloseBody(resp)
193
194
if resp.StatusCode != http.StatusOK {
195
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -200,10 +200,3 @@ func (ip *IPFS) doOKDecode(req *http.Request, in interface{}) error {
200
}
201
return nil
202
}
203
-
204
-func closeBody(resp *http.Response) {
205
- if resp != nil && resp.Body != nil {
206
- _, _ = io.Copy(io.Discard, resp.Body)
207
- _ = resp.Body.Close()
208
- }
209
-}
src/go/plugin/go.d/modules/lighttpd/apiclient.go
+1
-8
@@ -43,7 +43,7 @@ func (a apiClient) getServerStatus() (*serverStatus, error) {
43
44
resp, err := a.doRequestOK(req)
45
46
- defer closeBody(resp)
46
+ defer web.CloseBody(resp)
47
48
if err != nil {
49
return nil, err
@@ -161,10 +161,3 @@ func mustParseInt(value string) *int64 {
161
}
162
return &v
163
}
164
-
165
-func closeBody(resp *http.Response) {
166
- if resp != nil && resp.Body != nil {
167
- _, _ = io.Copy(io.Discard, resp.Body)
168
- _ = resp.Body.Close()
169
- }
170
-}
src/go/plugin/go.d/modules/logstash/collect.go
+3
-8
@@ -61,11 +61,13 @@ func (l *Logstash) queryNodeStats() (*nodeStats, error) {
61
62
func (l *Logstash) doWithDecode(dst interface{}, req *http.Request) error {
63
l.Debugf("executing %s '%s'", req.Method, req.URL)
64
+
65
resp, err := l.httpClient.Do(req)
66
if err != nil {
67
return err
68
}
68
- defer closeBody(resp)
69
+
70
+ defer web.CloseBody(resp)
71
72
if resp.StatusCode != http.StatusOK {
73
return fmt.Errorf("%s returned %d status code (%s)", req.URL, resp.StatusCode, resp.Status)
@@ -82,10 +84,3 @@ func (l *Logstash) doWithDecode(dst interface{}, req *http.Request) error {
84
85
return nil
86
}
85
-
86
-func closeBody(resp *http.Response) {
87
- if resp != nil && resp.Body != nil {
88
- _, _ = io.Copy(io.Discard, resp.Body)
89
- _ = resp.Body.Close()
90
- }
91
-}
src/go/plugin/go.d/modules/monit/collect.go
+2
-9
@@ -6,7 +6,6 @@ import (
6
"encoding/xml"
7
"errors"
8
"fmt"
9
- "io"
9
"net/http"
10
"net/url"
11
@@ -93,7 +92,8 @@ func (m *Monit) doOKDecode(req *http.Request, in interface{}) error {
92
if err != nil {
93
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
94
}
96
- defer closeBody(resp)
95
+
96
+ defer web.CloseBody(resp)
97
98
if resp.StatusCode != http.StatusOK {
99
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -108,10 +108,3 @@ func (m *Monit) doOKDecode(req *http.Request, in interface{}) error {
108
109
return nil
110
}
111
-
112
-func closeBody(resp *http.Response) {
113
- if resp != nil && resp.Body != nil {
114
- _, _ = io.Copy(io.Discard, resp.Body)
115
- _ = resp.Body.Close()
116
- }
117
-}
src/go/plugin/go.d/modules/nginx/apiclient.go
+1
-8
@@ -65,7 +65,7 @@ func (a apiClient) getStubStatus() (*stubStatus, error) {
65
}
66
67
resp, err := a.doRequestOK(req)
68
- defer closeBody(resp)
68
+ defer web.CloseBody(resp)
69
if err != nil {
70
return nil, err
71
}
@@ -91,13 +91,6 @@ func (a apiClient) doRequestOK(req *http.Request) (*http.Response, error) {
91
return resp, err
92
}
93
94
-func closeBody(resp *http.Response) {
95
- if resp != nil && resp.Body != nil {
96
- _, _ = io.Copy(io.Discard, resp.Body)
97
- _ = resp.Body.Close()
98
- }
99
-}
100
-
94
func parseStubStatus(r io.Reader) (*stubStatus, error) {
95
sc := bufio.NewScanner(r)
96
var lines []string
src/go/plugin/go.d/modules/nginxplus/nginx_http_api_query.go
+2
-8
@@ -330,7 +330,8 @@ func (n *NginxPlus) doWithDecode(dst interface{}, req *http.Request) error {
330
if err != nil {
331
return err
332
}
333
- defer closeBody(resp)
333
+
334
+ defer web.CloseBody(resp)
335
336
if resp.StatusCode == http.StatusNotFound {
337
return fmt.Errorf("%s returned %d status code (%w)", req.URL, resp.StatusCode, errPathNotFound)
@@ -351,13 +352,6 @@ func (n *NginxPlus) doWithDecode(dst interface{}, req *http.Request) error {
352
return nil
353
}
354
354
-func closeBody(resp *http.Response) {
355
- if resp != nil && resp.Body != nil {
356
- _, _ = io.Copy(io.Discard, resp.Body)
357
- _ = resp.Body.Close()
358
- }
359
-}
360
-
355
func (n *nginxMetrics) empty() bool {
356
return n.info != nil &&
357
n.connections == nil &&
src/go/plugin/go.d/modules/nginxvts/collect.go
+2
-9
@@ -5,7 +5,6 @@ package nginxvts
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
10
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/stm"
@@ -61,7 +60,8 @@ func (vts *NginxVTS) doOKDecode(req *http.Request, in interface{}) error {
60
if err != nil {
61
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
62
}
64
- defer closeBody(resp)
63
+
64
+ defer web.CloseBody(resp)
65
66
if resp.StatusCode != http.StatusOK {
67
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -72,10 +72,3 @@ func (vts *NginxVTS) doOKDecode(req *http.Request, in interface{}) error {
72
}
73
return nil
74
}
75
-
76
-func closeBody(resp *http.Response) {
77
- if resp != nil && resp.Body != nil {
78
- _, _ = io.Copy(io.Discard, resp.Body)
79
- _ = resp.Body.Close()
80
- }
81
-}
src/go/plugin/go.d/modules/phpdaemon/client.go
+1
-8
@@ -44,7 +44,7 @@ func (c *client) doWithDecode(dst interface{}, decode decodeFunc, request web.Re
44
}
45
46
resp, err := c.doOK(req)
47
- defer closeBody(resp)
47
+ defer web.CloseBody(resp)
48
if err != nil {
49
return err
50
}
@@ -68,10 +68,3 @@ func (c *client) doOK(req *http.Request) (*http.Response, error) {
68
69
return resp, err
70
}
71
-
72
-func closeBody(resp *http.Response) {
73
- if resp != nil && resp.Body != nil {
74
- _, _ = io.Copy(io.Discard, resp.Body)
75
- _ = resp.Body.Close()
76
- }
77
-}
src/go/plugin/go.d/modules/pihole/collect.go
+2
-8
@@ -220,7 +220,8 @@ func (p *Pihole) doWithDecode(dst interface{}, req *http.Request) error {
220
if err != nil {
221
return err
222
}
223
- defer closeBody(resp)
223
+
224
+ defer web.CloseBody(resp)
225
226
if resp.StatusCode != http.StatusOK {
227
return fmt.Errorf("%s returned %d status code", req.URL, resp.StatusCode)
@@ -248,13 +249,6 @@ func isEmptyArray(data []byte) bool {
249
return len(data) == len(empty) && string(data) == empty
250
}
251
251
-func closeBody(resp *http.Response) {
252
- if resp != nil && resp.Body != nil {
253
- _, _ = io.Copy(io.Discard, resp.Body)
254
- _ = resp.Body.Close()
255
- }
256
-}
257
-
252
func boolToInt(b bool) int64 {
253
if !b {
254
return 0
src/go/plugin/go.d/modules/powerdns/collect.go
+2
-9
@@ -6,7 +6,6 @@ import (
6
"encoding/json"
7
"errors"
8
"fmt"
9
- "io"
9
"net/http"
10
"strconv"
11
@@ -80,7 +79,8 @@ func (ns *AuthoritativeNS) doOKDecode(req *http.Request, in interface{}) error {
79
if err != nil {
80
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
81
}
83
- defer closeBody(resp)
82
+
83
+ defer web.CloseBody(resp)
84
85
if resp.StatusCode != http.StatusOK {
86
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -91,10 +91,3 @@ func (ns *AuthoritativeNS) doOKDecode(req *http.Request, in interface{}) error {
91
}
92
return nil
93
}
94
-
95
-func closeBody(resp *http.Response) {
96
- if resp != nil && resp.Body != nil {
97
- _, _ = io.Copy(io.Discard, resp.Body)
98
- _ = resp.Body.Close()
99
- }
100
-}
src/go/plugin/go.d/modules/powerdns_recursor/collect.go
+2
-9
@@ -6,7 +6,6 @@ import (
6
"encoding/json"
7
"errors"
8
"fmt"
9
- "io"
9
"net/http"
10
"strconv"
11
@@ -80,7 +79,8 @@ func (r *Recursor) doOKDecode(req *http.Request, in interface{}) error {
79
if err != nil {
80
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
81
}
83
- defer closeBody(resp)
82
+
83
+ defer web.CloseBody(resp)
84
85
if resp.StatusCode != http.StatusOK {
86
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -91,10 +91,3 @@ func (r *Recursor) doOKDecode(req *http.Request, in interface{}) error {
91
}
92
return nil
93
}
94
-
95
-func closeBody(resp *http.Response) {
96
- if resp != nil && resp.Body != nil {
97
- _, _ = io.Copy(io.Discard, resp.Body)
98
- _ = resp.Body.Close()
99
- }
100
-}
src/go/plugin/go.d/modules/puppet/collect.go
+2
-9
@@ -5,7 +5,6 @@ package puppet
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"net/url"
10
@@ -55,7 +54,8 @@ func (p *Puppet) doOKDecode(req *http.Request, in interface{}) error {
54
if err != nil {
55
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
56
}
58
- defer closeBody(resp)
57
+
58
+ defer web.CloseBody(resp)
59
60
if resp.StatusCode != http.StatusOK {
61
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -66,10 +66,3 @@ func (p *Puppet) doOKDecode(req *http.Request, in interface{}) error {
66
}
67
return nil
68
}
69
-
70
-func closeBody(resp *http.Response) {
71
- if resp != nil && resp.Body != nil {
72
- _, _ = io.Copy(io.Discard, resp.Body)
73
- _ = resp.Body.Close()
74
- }
75
-}
src/go/plugin/go.d/modules/rabbitmq/collect.go
+1
-9
@@ -5,7 +5,6 @@ package rabbitmq
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"path/filepath"
10
@@ -156,7 +155,7 @@ func (r *RabbitMQ) doOKDecode(urlPath string, in interface{}) error {
155
return fmt.Errorf("error on request to %s: %v", req.URL, err)
156
}
157
159
- defer closeBody(resp)
158
+ defer web.CloseBody(resp)
159
160
if resp.StatusCode != http.StatusOK {
161
return fmt.Errorf("%s returned HTTP status %d (%s)", req.URL, resp.StatusCode, resp.Status)
@@ -168,10 +167,3 @@ func (r *RabbitMQ) doOKDecode(urlPath string, in interface{}) error {
167
168
return nil
169
}
171
-
172
-func closeBody(resp *http.Response) {
173
- if resp != nil && resp.Body != nil {
174
- _, _ = io.Copy(io.Discard, resp.Body)
175
- _ = resp.Body.Close()
176
- }
177
-}
src/go/plugin/go.d/modules/riakkv/collect.go
+2
-9
@@ -6,7 +6,6 @@ import (
6
"encoding/json"
7
"errors"
8
"fmt"
9
- "io"
9
"net/http"
10
11
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/stm"
@@ -49,7 +48,8 @@ func (r *RiakKv) doOKDecode(req *http.Request, in interface{}) error {
48
if err != nil {
49
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
50
}
52
- defer closeBody(resp)
51
+
52
+ defer web.CloseBody(resp)
53
54
if resp.StatusCode != http.StatusOK {
55
msg := fmt.Sprintf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -65,10 +65,3 @@ func (r *RiakKv) doOKDecode(req *http.Request, in interface{}) error {
65
66
return nil
67
}
68
-
69
-func closeBody(resp *http.Response) {
70
- if resp != nil && resp.Body != nil {
71
- _, _ = io.Copy(io.Discard, resp.Body)
72
- _ = resp.Body.Close()
73
- }
74
-}
src/go/plugin/go.d/modules/rspamd/collect.go
+2
-9
@@ -5,7 +5,6 @@ package rspamd
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
10
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/stm"
@@ -72,7 +71,8 @@ func (r *Rspamd) doOKDecode(req *http.Request, in interface{}) error {
71
if err != nil {
72
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
73
}
75
- defer closeBody(resp)
74
+
75
+ defer web.CloseBody(resp)
76
77
if resp.StatusCode != http.StatusOK {
78
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -83,10 +83,3 @@ func (r *Rspamd) doOKDecode(req *http.Request, in interface{}) error {
83
}
84
return nil
85
}
86
-
87
-func closeBody(resp *http.Response) {
88
- if resp != nil && resp.Body != nil {
89
- _, _ = io.Copy(io.Discard, resp.Body)
90
- _ = resp.Body.Close()
91
- }
92
-}
src/go/plugin/go.d/modules/scaleio/client/client.go
+4
-11
@@ -105,7 +105,7 @@ func (c *Client) Login() error {
105
}
106
req := c.createLoginRequest()
107
resp, err := c.doOK(req)
108
- defer closeBody(resp)
108
+ defer web.CloseBody(resp)
109
if err != nil {
110
return err
111
}
@@ -128,7 +128,7 @@ func (c *Client) Logout() error {
128
c.token.unset()
129
130
resp, err := c.do(req)
131
- defer closeBody(resp)
131
+ defer web.CloseBody(resp)
132
return err
133
}
134
@@ -136,7 +136,7 @@ func (c *Client) Logout() error {
136
func (c *Client) APIVersion() (Version, error) {
137
req := c.createAPIVersionRequest()
138
resp, err := c.doOK(req)
139
- defer closeBody(resp)
139
+ defer web.CloseBody(resp)
140
if err != nil {
141
return Version{}, err
142
}
@@ -248,20 +248,13 @@ func (c *Client) doOKWithRetry(req web.Request) (*http.Response, error) {
248
249
func (c *Client) doJSONWithRetry(dst interface{}, req web.Request) error {
250
resp, err := c.doOKWithRetry(req)
251
- defer closeBody(resp)
251
+ defer web.CloseBody(resp)
252
if err != nil {
253
return err
254
}
255
return json.NewDecoder(resp.Body).Decode(dst)
256
}
257
258
-func closeBody(resp *http.Response) {
259
- if resp != nil && resp.Body != nil {
260
- _, _ = io.Copy(io.Discard, resp.Body)
261
- _ = resp.Body.Close()
262
- }
263
-}
264
-
258
func checkStatusCode(resp *http.Response) error {
259
// For all 4xx and 5xx return codes, the body may contain an apiError
260
// instance with more specifics about the failure.
src/go/plugin/go.d/modules/squid/collect.go
+1
-8
@@ -88,7 +88,7 @@ func (s *Squid) doOK(req *http.Request, parse func(body io.Reader) error) error
88
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
89
}
90
91
- defer closeBody(resp)
91
+ defer web.CloseBody(resp)
92
93
if resp.StatusCode != http.StatusOK {
94
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -96,10 +96,3 @@ func (s *Squid) doOK(req *http.Request, parse func(body io.Reader) error) error
96
97
return parse(resp.Body)
98
}
99
-
100
-func closeBody(resp *http.Response) {
101
- if resp != nil && resp.Body != nil {
102
- _, _ = io.Copy(io.Discard, resp.Body)
103
- _ = resp.Body.Close()
104
- }
105
-}
src/go/plugin/go.d/modules/tengine/apiclient.go
+1
-8
@@ -93,7 +93,7 @@ func (a apiClient) getStatus() (*tengineStatus, error) {
93
}
94
95
resp, err := a.doRequestOK(req)
96
- defer closeBody(resp)
96
+ defer web.CloseBody(resp)
97
if err != nil {
98
return nil, err
99
}
@@ -117,13 +117,6 @@ func (a apiClient) doRequestOK(req *http.Request) (*http.Response, error) {
117
return resp, nil
118
}
119
120
-func closeBody(resp *http.Response) {
121
- if resp != nil && resp.Body != nil {
122
- _, _ = io.Copy(io.Discard, resp.Body)
123
- _ = resp.Body.Close()
124
- }
125
-}
126
-
120
func parseStatus(r io.Reader) (*tengineStatus, error) {
121
var status tengineStatus
122
src/go/plugin/go.d/modules/tomcat/collect.go
+1
-9
@@ -6,7 +6,6 @@ import (
6
"encoding/xml"
7
"errors"
8
"fmt"
9
- "io"
9
"net/http"
10
"net/url"
11
"strings"
@@ -109,7 +108,7 @@ func (t *Tomcat) doOKDecode(req *http.Request, in interface{}) error {
108
if err != nil {
109
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
110
}
112
- defer closeBody(resp)
111
+ defer web.CloseBody(resp)
112
113
if resp.StatusCode != http.StatusOK {
114
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
@@ -121,10 +120,3 @@ func (t *Tomcat) doOKDecode(req *http.Request, in interface{}) error {
120
121
return nil
122
}
124
-
125
-func closeBody(resp *http.Response) {
126
- if resp != nil && resp.Body != nil {
127
- _, _ = io.Copy(io.Discard, resp.Body)
128
- _ = resp.Body.Close()
129
- }
130
-}
src/go/plugin/go.d/modules/typesense/collect.go
+5
-11
@@ -5,7 +5,6 @@ package typesense
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"strings"
10
@@ -121,15 +120,17 @@ func (ts *Typesense) doOKDecode(req *http.Request, in interface{}) error {
120
if err != nil {
121
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err)
122
}
124
- defer closeBody(resp)
123
+
124
+ defer web.CloseBody(resp)
125
126
if resp.StatusCode != http.StatusOK {
127
// {"message": "Forbidden - a valid `x-typesense-api-key` header must be sent."}
128
var msg struct {
129
Msg string `json:"message"`
130
}
131
- if err := json.NewDecoder(resp.Body).Decode(&msg); err == nil {
132
- return fmt.Errorf("'%s' returned HTTP status code: %d (msg: '%s')", req.URL, resp.StatusCode, msg.Msg)
131
+ if err := json.NewDecoder(resp.Body).Decode(&msg); err == nil && msg.Msg != "" {
132
+ return fmt.Errorf("'%s' returned HTTP status code: %d (msg: '%s')",
133
+ req.URL, resp.StatusCode, msg.Msg)
134
}
135
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode)
136
}
@@ -140,13 +141,6 @@ func (ts *Typesense) doOKDecode(req *http.Request, in interface{}) error {
141
return nil
142
}
143
143
-func closeBody(resp *http.Response) {
144
- if resp != nil && resp.Body != nil {
145
- _, _ = io.Copy(io.Discard, resp.Body)
146
- _ = resp.Body.Close()
147
- }
148
-}
149
-
144
func isStatusUnauthorized(err error) bool {
145
return strings.Contains(err.Error(), "code: 401")
146
}
src/go/plugin/go.d/modules/vcsa/client/client.go
+3
-11
@@ -5,7 +5,6 @@ package client
5
import (
6
"encoding/json"
7
"fmt"
8
- "io"
8
"net/http"
9
"sync"
10
@@ -96,7 +95,7 @@ func (c *Client) Logout() error {
95
}
96
97
resp, err := c.doOK(req)
99
- closeBody(resp)
98
+ web.CloseBody(resp)
99
c.token.set("")
100
return err
101
}
@@ -110,7 +109,7 @@ func (c *Client) Ping() error {
109
Headers: map[string]string{apiSessIDKey: c.token.get()},
110
}
111
resp, err := c.doOK(req)
113
- defer closeBody(resp)
112
+ defer web.CloseBody(resp)
113
if resp != nil && resp.StatusCode == http.StatusUnauthorized {
114
return c.Login()
115
}
@@ -193,7 +192,7 @@ func (c *Client) doOK(req web.Request) (*http.Response, error) {
192
193
func (c *Client) doOKWithDecode(req web.Request, dst interface{}) error {
194
resp, err := c.doOK(req)
196
- defer closeBody(resp)
195
+ defer web.CloseBody(resp)
196
if err != nil {
197
return err
198
}
@@ -204,10 +203,3 @@ func (c *Client) doOKWithDecode(req web.Request, dst interface{}) error {
203
}
204
return nil
205
}
207
-
208
-func closeBody(resp *http.Response) {
209
- if resp != nil && resp.Body != nil {
210
- _, _ = io.Copy(io.Discard, resp.Body)
211
- _ = resp.Body.Close()
212
- }
213
-}
src/go/plugin/go.d/pkg/web/client.go
+8
@@ -5,6 +5,7 @@ package web
5
import (
6
"errors"
7
"fmt"
8
+ "io"
9
"net"
10
"net/http"
11
"net/url"
@@ -64,6 +65,13 @@ func NewHTTPClient(cfg Client) (*http.Client, error) {
65
}, nil
66
}
67
68
+func CloseBody(resp *http.Response) {
69
+ if resp != nil && resp.Body != nil {
70
+ _, _ = io.Copy(io.Discard, resp.Body)
71
+ _ = resp.Body.Close()
72
+ }
73
+}
74
+
75
func redirectFunc(notFollowRedirect bool) func(req *http.Request, via []*http.Request) error {
76
if follow := !notFollowRedirect; follow {
77
return nil