@@ -233,9 +233,15 @@ func main() {
233
233
scrapeURI = & newScrapeURI
234
234
}
235
235
236
+ userAgent := fmt .Sprintf ("NGINX-Prometheus-Exporter/v%v" , version )
237
+ userAgentRT := & userAgentRoundTripper {
238
+ agent : userAgent ,
239
+ rt : transport ,
240
+ }
241
+
236
242
httpClient := & http.Client {
237
243
Timeout : timeout .Duration ,
238
- Transport : transport ,
244
+ Transport : userAgentRT ,
239
245
}
240
246
241
247
srv := http.Server {}
@@ -290,3 +296,28 @@ func main() {
290
296
log .Printf ("NGINX Prometheus Exporter has successfully started" )
291
297
log .Fatal (srv .Serve (listener ))
292
298
}
299
+
300
+ type userAgentRoundTripper struct {
301
+ agent string
302
+ rt http.RoundTripper
303
+ }
304
+
305
+ func (rt * userAgentRoundTripper ) RoundTrip (req * http.Request ) (* http.Response , error ) {
306
+ req = cloneRequest (req )
307
+ req .Header .Set ("User-Agent" , rt .agent )
308
+ return rt .rt .RoundTrip (req )
309
+ }
310
+
311
+ func cloneRequest (req * http.Request ) * http.Request {
312
+ r := new (http.Request )
313
+ * r = * req // shallow clone
314
+
315
+ // deep copy headers
316
+ r .Header = make (http.Header , len (req .Header ))
317
+ for key , values := range req .Header {
318
+ newValues := make ([]string , len (values ))
319
+ copy (newValues , values )
320
+ r .Header [key ] = newValues
321
+ }
322
+ return r
323
+ }
0 commit comments