go.d fix url path overwrite (#18132)
Ilya Mashchenko committed
Jul 12, 2024 at 22:44 UTC
28de8a8c71925b63782ad41ab1f3bda176cb89fd
16 files changed
+99
-92
src/go/plugin/go.d/modules/consul/collect.go
+1
-2
@@ -69,12 +69,11 @@ func (c *Consul) isServer() bool {
69
}
70
71
func (c *Consul) doOKDecode(urlPath string, in interface{}, statusCodes ...int) error {
72
- req, err := web.NewHTTPRequest(c.Request.Copy())
72
+ req, err := web.NewHTTPRequestWithPath(c.Request, urlPath)
73
if err != nil {
74
return fmt.Errorf("error on creating request: %v", err)
75
}
76
77
- req.URL.Path = urlPath
77
if c.ACLToken != "" {
78
req.Header.Set("X-Consul-Token", c.ACLToken)
79
}
src/go/plugin/go.d/modules/couchbase/collect.go
+5
-3
@@ -112,11 +112,13 @@ func (cb *Couchbase) addDimToChart(chartID string, dim *module.Dim) {
112
}
113
114
func (cb *Couchbase) scrapeCouchbase() (*cbMetrics, error) {
115
- ms := &cbMetrics{}
116
- req, _ := web.NewHTTPRequest(cb.Request)
117
- req.URL.Path = urlPathBucketsStats
115
+ req, err := web.NewHTTPRequestWithPath(cb.Request, urlPathBucketsStats)
116
+ if err != nil {
117
+ return nil, err
118
+ }
119
req.URL.RawQuery = url.Values{"skipMap": []string{"true"}}.Encode()
120
121
+ ms := &cbMetrics{}
122
if err := cb.doOKDecode(req, &ms.BucketsBasicStats); err != nil {
123
return nil, err
124
}
src/go/plugin/go.d/modules/couchdb/collect.go
+8
-12
@@ -120,8 +120,7 @@ func (cdb *CouchDB) scrapeCouchDB() *cdbMetrics {
120
}
121
122
func (cdb *CouchDB) scrapeNodeStats(ms *cdbMetrics) {
123
- req, _ := web.NewHTTPRequest(cdb.Request)
124
- req.URL.Path = fmt.Sprintf(urlPathOverviewStats, cdb.Config.Node)
123
+ req, _ := web.NewHTTPRequestWithPath(cdb.Request, fmt.Sprintf(urlPathOverviewStats, cdb.Config.Node))
124
125
var stats cdbNodeStats
126
if err := cdb.doOKDecode(req, &stats); err != nil {
@@ -132,8 +131,7 @@ func (cdb *CouchDB) scrapeNodeStats(ms *cdbMetrics) {
131
}
132
133
func (cdb *CouchDB) scrapeSystemStats(ms *cdbMetrics) {
135
- req, _ := web.NewHTTPRequest(cdb.Request)
136
- req.URL.Path = fmt.Sprintf(urlPathSystemStats, cdb.Config.Node)
134
+ req, _ := web.NewHTTPRequestWithPath(cdb.Request, fmt.Sprintf(urlPathSystemStats, cdb.Config.Node))
135
136
var stats cdbNodeSystem
137
if err := cdb.doOKDecode(req, &stats); err != nil {
@@ -144,8 +142,7 @@ func (cdb *CouchDB) scrapeSystemStats(ms *cdbMetrics) {
142
}
143
144
func (cdb *CouchDB) scrapeActiveTasks(ms *cdbMetrics) {
147
- req, _ := web.NewHTTPRequest(cdb.Request)
148
- req.URL.Path = urlPathActiveTasks
145
+ req, _ := web.NewHTTPRequestWithPath(cdb.Request, urlPathActiveTasks)
146
147
var stats []cdbActiveTask
148
if err := cdb.doOKDecode(req, &stats); err != nil {
@@ -156,8 +153,7 @@ func (cdb *CouchDB) scrapeActiveTasks(ms *cdbMetrics) {
153
}
154
155
func (cdb *CouchDB) scrapeDBStats(ms *cdbMetrics) {
159
- req, _ := web.NewHTTPRequest(cdb.Request)
160
- req.URL.Path = urlPathDatabases
156
+ req, _ := web.NewHTTPRequestWithPath(cdb.Request, urlPathDatabases)
157
req.Method = http.MethodPost
158
req.Header.Add("Accept", "application/json")
159
req.Header.Add("Content-Type", "application/json")
@@ -182,18 +178,18 @@ func (cdb *CouchDB) scrapeDBStats(ms *cdbMetrics) {
178
}
179
180
func findMaxMQSize(MessageQueues map[string]interface{}) int64 {
185
- var max float64
181
+ var maxSize float64
182
for _, mq := range MessageQueues {
183
switch mqSize := mq.(type) {
184
case float64:
189
- max = math.Max(max, mqSize)
185
+ maxSize = math.Max(maxSize, mqSize)
186
case map[string]interface{}:
187
if v, ok := mqSize["count"].(float64); ok {
192
- max = math.Max(max, v)
188
+ maxSize = math.Max(maxSize, v)
189
}
190
}
191
}
196
- return int64(max)
192
+ return int64(maxSize)
193
}
194
195
func (cdb *CouchDB) pingCouchDB() error {
src/go/plugin/go.d/modules/dnsdist/collect.go
+4
-2
@@ -36,8 +36,10 @@ func (d *DNSdist) collectStatistic(collected map[string]int64, statistics *stati
36
}
37
38
func (d *DNSdist) scrapeStatistics() (*statisticMetrics, error) {
39
- req, _ := web.NewHTTPRequest(d.Request)
40
- req.URL.Path = urlPathJSONStat
39
+ req, err := web.NewHTTPRequestWithPath(d.Request, urlPathJSONStat)
40
+ if err != nil {
41
+ return nil, err
42
+ }
43
req.URL.RawQuery = url.Values{"command": []string{"stats"}}.Encode()
44
45
var statistics statisticMetrics
src/go/plugin/go.d/modules/elasticsearch/collect.go
+8
-9
@@ -158,13 +158,15 @@ func (es *Elasticsearch) scrapeElasticsearch() *esMetrics {
158
}
159
160
func (es *Elasticsearch) scrapeNodesStats(ms *esMetrics) {
161
- req, _ := web.NewHTTPRequest(es.Request)
161
+ var p string
162
if es.ClusterMode {
163
- req.URL.Path = urlPathNodesStats
163
+ p = urlPathNodesStats
164
} else {
165
- req.URL.Path = urlPathLocalNodeStats
165
+ p = urlPathLocalNodeStats
166
}
167
168
+ req, _ := web.NewHTTPRequestWithPath(es.Request, p)
169
+
170
var stats esNodesStats
171
if err := es.doOKDecode(req, &stats); err != nil {
172
es.Warning(err)
@@ -175,8 +177,7 @@ func (es *Elasticsearch) scrapeNodesStats(ms *esMetrics) {
177
}
178
179
func (es *Elasticsearch) scrapeClusterHealth(ms *esMetrics) {
178
- req, _ := web.NewHTTPRequest(es.Request)
179
- req.URL.Path = urlPathClusterHealth
180
+ req, _ := web.NewHTTPRequestWithPath(es.Request, urlPathClusterHealth)
181
182
var health esClusterHealth
183
if err := es.doOKDecode(req, &health); err != nil {
@@ -188,8 +189,7 @@ func (es *Elasticsearch) scrapeClusterHealth(ms *esMetrics) {
189
}
190
191
func (es *Elasticsearch) scrapeClusterStats(ms *esMetrics) {
191
- req, _ := web.NewHTTPRequest(es.Request)
192
- req.URL.Path = urlPathClusterStats
192
+ req, _ := web.NewHTTPRequestWithPath(es.Request, urlPathClusterStats)
193
194
var stats esClusterStats
195
if err := es.doOKDecode(req, &stats); err != nil {
@@ -201,8 +201,7 @@ func (es *Elasticsearch) scrapeClusterStats(ms *esMetrics) {
201
}
202
203
func (es *Elasticsearch) scrapeLocalIndicesStats(ms *esMetrics) {
204
- req, _ := web.NewHTTPRequest(es.Request)
205
- req.URL.Path = urlPathIndicesStats
204
+ req, _ := web.NewHTTPRequestWithPath(es.Request, urlPathIndicesStats)
205
req.URL.RawQuery = "local=true&format=json"
206
207
var stats []esIndexStats
src/go/plugin/go.d/modules/ipfs/collect.go
+4
-12
@@ -125,13 +125,11 @@ func (ip *IPFS) collectPinLs(mx map[string]int64) error {
125
}
126
127
func (ip *IPFS) queryStatsBandwidth() (*ipfsStatsBw, error) {
128
- req, err := web.NewHTTPRequest(ip.Request)
128
+ req, err := web.NewHTTPRequestWithPath(ip.Request, urlPathStatsBandwidth)
129
if err != nil {
130
return nil, err
131
}
132
133
- req.URL.Path = urlPathStatsBandwidth
134
-
133
var stats ipfsStatsBw
134
if err := ip.doOKDecode(req, &stats); err != nil {
135
return nil, err
@@ -145,13 +143,11 @@ func (ip *IPFS) queryStatsBandwidth() (*ipfsStatsBw, error) {
143
}
144
145
func (ip *IPFS) querySwarmPeers() (*ipfsSwarmPeers, error) {
148
- req, err := web.NewHTTPRequest(ip.Request)
146
+ req, err := web.NewHTTPRequestWithPath(ip.Request, urlPathSwarmPeers)
147
if err != nil {
148
return nil, err
149
}
150
153
- req.URL.Path = urlPathSwarmPeers
154
-
151
var stats ipfsSwarmPeers
152
if err := ip.doOKDecode(req, &stats); err != nil {
153
return nil, err
@@ -161,13 +157,11 @@ func (ip *IPFS) querySwarmPeers() (*ipfsSwarmPeers, error) {
157
}
158
159
func (ip *IPFS) queryStatsRepo() (*ipfsStatsRepo, error) {
164
- req, err := web.NewHTTPRequest(ip.Request)
160
+ req, err := web.NewHTTPRequestWithPath(ip.Request, urlPathStatsRepo)
161
if err != nil {
162
return nil, err
163
}
164
169
- req.URL.Path = urlPathStatsRepo
170
-
165
var stats ipfsStatsRepo
166
if err := ip.doOKDecode(req, &stats); err != nil {
167
return nil, err
@@ -177,13 +171,11 @@ func (ip *IPFS) queryStatsRepo() (*ipfsStatsRepo, error) {
171
}
172
173
func (ip *IPFS) queryPinLs() (*ipfsPinsLs, error) {
180
- req, err := web.NewHTTPRequest(ip.Request)
174
+ req, err := web.NewHTTPRequestWithPath(ip.Request, urlPathPinLs)
175
if err != nil {
176
return nil, err
177
}
178
185
- req.URL.Path = urlPathPinLs
186
-
179
var stats ipfsPinsLs
180
if err := ip.doOKDecode(req, &stats); err != nil {
181
return nil, err
src/go/plugin/go.d/modules/logstash/collect.go
+4
-2
@@ -45,8 +45,10 @@ func (l *Logstash) updateCharts(pipelines map[string]pipelineStats) {
45
}
46
47
func (l *Logstash) queryNodeStats() (*nodeStats, error) {
48
- req, _ := web.NewHTTPRequest(l.Request.Copy())
49
- req.URL.Path = urlPathNodeStatsAPI
48
+ req, err := web.NewHTTPRequestWithPath(l.Request, urlPathNodeStatsAPI)
49
+ if err != nil {
50
+ return nil, err
51
+ }
52
53
var stats nodeStats
54
src/go/plugin/go.d/modules/nginxplus/nginx_http_api_query.go
+15
-30
@@ -46,8 +46,7 @@ type nginxMetrics struct {
46
}
47
48
func (n *NginxPlus) queryAPIVersion() (int64, error) {
49
- req, _ := web.NewHTTPRequest(n.Request.Copy())
50
- req.URL.Path = urlPathAPIVersions
49
+ req, _ := web.NewHTTPRequestWithPath(n.Request, urlPathAPIVersions)
50
51
var versions nginxAPIVersions
52
if err := n.doWithDecode(&versions, req); err != nil {
@@ -62,8 +61,7 @@ func (n *NginxPlus) queryAPIVersion() (int64, error) {
61
}
62
63
func (n *NginxPlus) queryAvailableEndpoints() error {
65
- req, _ := web.NewHTTPRequest(n.Request.Copy())
66
- req.URL.Path = fmt.Sprintf(urlPathAPIEndpointsRoot, n.apiVersion)
64
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIEndpointsRoot, n.apiVersion))
65
66
var endpoints []string
67
if err := n.doWithDecode(&endpoints, req); err != nil {
@@ -91,8 +89,7 @@ func (n *NginxPlus) queryAvailableEndpoints() error {
89
90
if hasHTTP {
91
endpoints = endpoints[:0]
94
- req, _ = web.NewHTTPRequest(n.Request.Copy())
95
- req.URL.Path = fmt.Sprintf(urlPathAPIEndpointsHTTP, n.apiVersion)
92
+ req, _ = web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIEndpointsHTTP, n.apiVersion))
93
94
if err := n.doWithDecode(&endpoints, req); err != nil {
95
return err
@@ -117,8 +114,7 @@ func (n *NginxPlus) queryAvailableEndpoints() error {
114
115
if hasStream {
116
endpoints = endpoints[:0]
120
- req, _ = web.NewHTTPRequest(n.Request.Copy())
121
- req.URL.Path = fmt.Sprintf(urlPathAPIEndpointsStream, n.apiVersion)
117
+ req, _ = web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIEndpointsStream, n.apiVersion))
118
119
if err := n.doWithDecode(&endpoints, req); err != nil {
120
return err
@@ -171,8 +167,7 @@ func (n *NginxPlus) queryMetrics() *nginxMetrics {
167
}
168
169
func (n *NginxPlus) queryNginxInfo(ms *nginxMetrics) {
174
- req, _ := web.NewHTTPRequest(n.Request.Copy())
175
- req.URL.Path = fmt.Sprintf(urlPathAPINginx, n.apiVersion)
170
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPINginx, n.apiVersion))
171
172
var v nginxInfo
173
@@ -186,8 +181,7 @@ func (n *NginxPlus) queryNginxInfo(ms *nginxMetrics) {
181
}
182
183
func (n *NginxPlus) queryConnections(ms *nginxMetrics) {
189
- req, _ := web.NewHTTPRequest(n.Request.Copy())
190
- req.URL.Path = fmt.Sprintf(urlPathAPIConnections, n.apiVersion)
184
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIConnections, n.apiVersion))
185
186
var v nginxConnections
187
@@ -201,8 +195,7 @@ func (n *NginxPlus) queryConnections(ms *nginxMetrics) {
195
}
196
197
func (n *NginxPlus) querySSL(ms *nginxMetrics) {
204
- req, _ := web.NewHTTPRequest(n.Request.Copy())
205
- req.URL.Path = fmt.Sprintf(urlPathAPISSL, n.apiVersion)
198
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPISSL, n.apiVersion))
199
200
var v nginxSSL
201
@@ -216,8 +209,7 @@ func (n *NginxPlus) querySSL(ms *nginxMetrics) {
209
}
210
211
func (n *NginxPlus) queryHTTPRequests(ms *nginxMetrics) {
219
- req, _ := web.NewHTTPRequest(n.Request.Copy())
220
- req.URL.Path = fmt.Sprintf(urlPathAPIHTTPRequests, n.apiVersion)
212
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPRequests, n.apiVersion))
213
214
var v nginxHTTPRequests
215
@@ -231,8 +223,7 @@ func (n *NginxPlus) queryHTTPRequests(ms *nginxMetrics) {
223
}
224
225
func (n *NginxPlus) queryHTTPServerZones(ms *nginxMetrics) {
234
- req, _ := web.NewHTTPRequest(n.Request.Copy())
235
- req.URL.Path = fmt.Sprintf(urlPathAPIHTTPServerZones, n.apiVersion)
226
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPServerZones, n.apiVersion))
227
228
var v nginxHTTPServerZones
229
@@ -246,8 +237,7 @@ func (n *NginxPlus) queryHTTPServerZones(ms *nginxMetrics) {
237
}
238
239
func (n *NginxPlus) queryHTTPLocationZones(ms *nginxMetrics) {
249
- req, _ := web.NewHTTPRequest(n.Request.Copy())
250
- req.URL.Path = fmt.Sprintf(urlPathAPIHTTPLocationZones, n.apiVersion)
240
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPLocationZones, n.apiVersion))
241
242
var v nginxHTTPLocationZones
243
@@ -261,8 +251,7 @@ func (n *NginxPlus) queryHTTPLocationZones(ms *nginxMetrics) {
251
}
252
253
func (n *NginxPlus) queryHTTPUpstreams(ms *nginxMetrics) {
264
- req, _ := web.NewHTTPRequest(n.Request.Copy())
265
- req.URL.Path = fmt.Sprintf(urlPathAPIHTTPUpstreams, n.apiVersion)
254
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPUpstreams, n.apiVersion))
255
256
var v nginxHTTPUpstreams
257
@@ -276,8 +265,7 @@ func (n *NginxPlus) queryHTTPUpstreams(ms *nginxMetrics) {
265
}
266
267
func (n *NginxPlus) queryHTTPCaches(ms *nginxMetrics) {
279
- req, _ := web.NewHTTPRequest(n.Request.Copy())
280
- req.URL.Path = fmt.Sprintf(urlPathAPIHTTPCaches, n.apiVersion)
268
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIHTTPCaches, n.apiVersion))
269
270
var v nginxHTTPCaches
271
@@ -291,8 +279,7 @@ func (n *NginxPlus) queryHTTPCaches(ms *nginxMetrics) {
279
}
280
281
func (n *NginxPlus) queryStreamServerZones(ms *nginxMetrics) {
294
- req, _ := web.NewHTTPRequest(n.Request.Copy())
295
- req.URL.Path = fmt.Sprintf(urlPathAPIStreamServerZones, n.apiVersion)
282
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIStreamServerZones, n.apiVersion))
283
284
var v nginxStreamServerZones
285
@@ -306,8 +293,7 @@ func (n *NginxPlus) queryStreamServerZones(ms *nginxMetrics) {
293
}
294
295
func (n *NginxPlus) queryStreamUpstreams(ms *nginxMetrics) {
309
- req, _ := web.NewHTTPRequest(n.Request.Copy())
310
- req.URL.Path = fmt.Sprintf(urlPathAPIStreamUpstreams, n.apiVersion)
296
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIStreamUpstreams, n.apiVersion))
297
298
var v nginxStreamUpstreams
299
@@ -321,8 +307,7 @@ func (n *NginxPlus) queryStreamUpstreams(ms *nginxMetrics) {
307
}
308
309
func (n *NginxPlus) queryResolvers(ms *nginxMetrics) {
324
- req, _ := web.NewHTTPRequest(n.Request.Copy())
325
- req.URL.Path = fmt.Sprintf(urlPathAPIResolvers, n.apiVersion)
310
+ req, _ := web.NewHTTPRequestWithPath(n.Request, fmt.Sprintf(urlPathAPIResolvers, n.apiVersion))
311
312
var v nginxResolvers
313
src/go/plugin/go.d/modules/pihole/collect.go
+4
-8
@@ -131,13 +131,12 @@ func (p *Pihole) queryMetrics(pmx *piholeMetrics, doConcurrently bool) {
131
}
132
133
func (p *Pihole) querySummary(pmx *piholeMetrics) {
134
- req, err := web.NewHTTPRequest(p.Request)
134
+ req, err := web.NewHTTPRequestWithPath(p.Request, urlPathAPI)
135
if err != nil {
136
p.Error(err)
137
return
138
}
139
140
- req.URL.Path = urlPathAPI
140
req.URL.RawQuery = url.Values{
141
urlQueryKeyAuth: []string{p.Password},
142
urlQueryKeySummaryRaw: []string{"true"},
@@ -153,13 +152,12 @@ func (p *Pihole) querySummary(pmx *piholeMetrics) {
152
}
153
154
func (p *Pihole) queryQueryTypes(pmx *piholeMetrics) {
156
- req, err := web.NewHTTPRequest(p.Request)
155
+ req, err := web.NewHTTPRequestWithPath(p.Request, urlPathAPI)
156
if err != nil {
157
p.Error(err)
158
return
159
}
160
162
- req.URL.Path = urlPathAPI
161
req.URL.RawQuery = url.Values{
162
urlQueryKeyAuth: []string{p.Password},
163
urlQueryKeyGetQueryTypes: []string{"true"},
@@ -176,13 +174,12 @@ func (p *Pihole) queryQueryTypes(pmx *piholeMetrics) {
174
}
175
176
func (p *Pihole) queryForwardedDestinations(pmx *piholeMetrics) {
179
- req, err := web.NewHTTPRequest(p.Request)
177
+ req, err := web.NewHTTPRequestWithPath(p.Request, urlPathAPI)
178
if err != nil {
179
p.Error(err)
180
return
181
}
182
185
- req.URL.Path = urlPathAPI
183
req.URL.RawQuery = url.Values{
184
urlQueryKeyAuth: []string{p.Password},
185
urlQueryKeyGetForwardDestinations: []string{"true"},
@@ -199,12 +196,11 @@ func (p *Pihole) queryForwardedDestinations(pmx *piholeMetrics) {
196
}
197
198
func (p *Pihole) queryAPIVersion() (int, error) {
202
- req, err := web.NewHTTPRequest(p.Request)
199
+ req, err := web.NewHTTPRequestWithPath(p.Request, urlPathAPI)
200
if err != nil {
201
return 0, err
202
}
203
207
- req.URL.Path = urlPathAPI
204
req.URL.RawQuery = url.Values{
205
urlQueryKeyAuth: []string{p.Password},
206
urlQueryKeyAPIVersion: []string{"true"},
src/go/plugin/go.d/modules/powerdns/collect.go
+1
-2
@@ -65,8 +65,7 @@ func (ns *AuthoritativeNS) collectStatistics(collected map[string]int64, statist
65
}
66
67
func (ns *AuthoritativeNS) scrapeStatistics() ([]statisticMetric, error) {
68
- req, _ := web.NewHTTPRequest(ns.Request)
69
- req.URL.Path = urlPathLocalStatistics
68
+ req, _ := web.NewHTTPRequestWithPath(ns.Request, urlPathLocalStatistics)
69
70
var statistics statisticMetrics
71
if err := ns.doOKDecode(req, &statistics); err != nil {
src/go/plugin/go.d/modules/powerdns_recursor/collect.go
+1
-2
@@ -65,8 +65,7 @@ func (r *Recursor) collectStatistics(collected map[string]int64, statistics stat
65
}
66
67
func (r *Recursor) scrapeStatistics() ([]statisticMetric, error) {
68
- req, _ := web.NewHTTPRequest(r.Request)
69
- req.URL.Path = urlPathLocalStatistics
68
+ req, _ := web.NewHTTPRequestWithPath(r.Request, urlPathLocalStatistics)
69
70
var statistics statisticMetrics
71
if err := r.doOKDecode(req, &statistics); err != nil {
src/go/plugin/go.d/modules/puppet/collect.go
+1
-2
@@ -31,12 +31,11 @@ func (p *Puppet) collect() (map[string]int64, error) {
31
}
32
33
func (p *Puppet) queryStatsService() (*statusServiceResponse, error) {
34
- req, err := web.NewHTTPRequest(p.Request)
34
+ req, err := web.NewHTTPRequestWithPath(p.Request, urlPathStatusService)
35
if err != nil {
36
return nil, err
37
}
38
39
- req.URL.Path = urlPathStatusService
39
req.URL.RawQuery = urlQueryStatusService
40
41
var stats statusServiceResponse
src/go/plugin/go.d/modules/rabbitmq/collect.go
+1
-3
@@ -145,13 +145,11 @@ func (r *RabbitMQ) collectQueuesStats(mx map[string]int64) error {
145
}
146
147
func (r *RabbitMQ) doOKDecode(urlPath string, in interface{}) error {
148
- req, err := web.NewHTTPRequest(r.Request.Copy())
148
+ req, err := web.NewHTTPRequestWithPath(r.Request, urlPath)
149
if err != nil {
150
return fmt.Errorf("error on creating request: %v", err)
151
}
152
153
- req.URL.Path = urlPath
154
-
153
r.Debugf("doing HTTP %s to '%s'", req.Method, req.URL)
154
resp, err := r.httpClient.Do(req)
155
if err != nil {
src/go/plugin/go.d/modules/rspamd/collect.go
+1
-3
@@ -50,13 +50,11 @@ func (r *Rspamd) collect() (map[string]int64, error) {
50
}
51
52
func (r *Rspamd) queryRspamdStats() (*rspamdStats, error) {
53
- req, err := web.NewHTTPRequest(r.Request)
53
+ req, err := web.NewHTTPRequestWithPath(r.Request, "/stat")
54
if err != nil {
55
return nil, err
56
}
57
58
- req.URL.Path = "/stat"
59
-
58
var stats rspamdStats
59
if err := r.doOKDecode(req, &stats); err != nil {
60
return nil, err
src/go/plugin/go.d/pkg/web/request.go
+13
@@ -7,6 +7,7 @@ import (
7
"fmt"
8
"io"
9
"net/http"
10
+ "net/url"
11
"strings"
12
13
"github.com/netdata/netdata/go/plugins/pkg/buildinfo"
@@ -90,3 +91,15 @@ func NewHTTPRequest(cfg Request) (*http.Request, error) {
91
92
return req, nil
93
}
94
+
95
+func NewHTTPRequestWithPath(cfg Request, urlPath string) (*http.Request, error) {
96
+ cfg = cfg.Copy()
97
+
98
+ v, err := url.JoinPath(cfg.URL, urlPath)
99
+ if err != nil {
100
+ return nil, fmt.Errorf("failed to join URL path: %v", err)
101
+ }
102
+ cfg.URL = v
103
+
104
+ return NewHTTPRequest(cfg)
105
+}
src/go/plugin/go.d/pkg/web/request_test.go
+28
@@ -159,6 +159,34 @@ func TestNewHTTPRequest(t *testing.T) {
159
}
160
}
161
162
+func TestNewRequest(t *testing.T) {
163
+ tests := map[string]struct {
164
+ url string
165
+ path string
166
+ wantURL string
167
+ }{
168
+ "base url": {
169
+ url: "http://127.0.0.1:65535",
170
+ path: "/bar",
171
+ wantURL: "http://127.0.0.1:65535/bar",
172
+ },
173
+ "with path": {
174
+ url: "http://127.0.0.1:65535/foo/",
175
+ path: "/bar",
176
+ wantURL: "http://127.0.0.1:65535/foo/bar",
177
+ },
178
+ }
179
+
180
+ for name, test := range tests {
181
+ t.Run(name, func(t *testing.T) {
182
+ req, err := NewHTTPRequestWithPath(Request{URL: test.url}.Copy(), test.path)
183
+ require.NoError(t, err)
184
+
185
+ assert.Equal(t, test.wantURL, req.URL.String())
186
+ })
187
+ }
188
+}
189
+
190
func parseBasicAuth(auth string) (username, password string, ok bool) {
191
const prefix = "Basic "
192
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {