From e21588702f83bffa4181b59a477b73c4c5ec1a35 Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Thu, 31 Oct 2019 10:11:41 -0700 Subject: [PATCH] set user agent on scrape requests to nginx. --- CHANGELOG.md | 5 +++++ client/nginx.go | 12 ++++++++++-- exporter.go | 3 ++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d9fac4c..b02d55bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +### DEV + +FEATURES: +* Set User-Agent header when calling the nginx stats endpoint. + ### 0.4.2 BUGFIXES: diff --git a/client/nginx.go b/client/nginx.go index c186634b..9d76929c 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -12,6 +12,7 @@ import ( type NginxClient struct { apiEndpoint string httpClient *http.Client + appName string } // StubStats represents NGINX stub_status metrics. @@ -31,10 +32,11 @@ type StubConnections struct { } // NewNginxClient creates an NginxClient. -func NewNginxClient(httpClient *http.Client, apiEndpoint string) (*NginxClient, error) { +func NewNginxClient(httpClient *http.Client, apiEndpoint string, appName string) (*NginxClient, error) { client := &NginxClient{ apiEndpoint: apiEndpoint, httpClient: httpClient, + appName: appName, } _, err := client.GetStubStats() @@ -43,7 +45,13 @@ func NewNginxClient(httpClient *http.Client, apiEndpoint string) (*NginxClient, // GetStubStats fetches the stub_status metrics. func (client *NginxClient) GetStubStats() (*StubStats, error) { - resp, err := client.httpClient.Get(client.apiEndpoint) + + req, err := http.NewRequest("GET", client.apiEndpoint, nil) + if err != nil { + return nil, fmt.Errorf("failed to create get request %v: %v", client.apiEndpoint, err) + } + req.Header.Set("User-Agent", client.appName) + resp, err := client.httpClient.Do(req) if err != nil { return nil, fmt.Errorf("failed to get %v: %v", client.apiEndpoint, err) } diff --git a/exporter.go b/exporter.go index 30629da7..0ba0aca9 100644 --- a/exporter.go +++ b/exporter.go @@ -200,8 +200,9 @@ func main() { } registry.MustRegister(collector.NewNginxPlusCollector(plusClient.(*plusclient.NginxClient), "nginxplus")) } else { + appName := fmt.Sprintf("NGINX-Prometheus-Exporter/v%v", version) ossClient, err := createClientWithRetries(func() (interface{}, error) { - return client.NewNginxClient(httpClient, *scrapeURI) + return client.NewNginxClient(httpClient, *scrapeURI, appName) }, *nginxRetries, nginxRetryInterval.Duration) if err != nil { log.Fatalf("Could not create Nginx Client: %v", err)