Skip to content

Commit

Permalink
set user agent on scrape requests to nginx.
Browse files Browse the repository at this point in the history
  • Loading branch information
asherf committed Oct 31, 2019
1 parent 330108c commit e215887
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### DEV

FEATURES:
* Set User-Agent header when calling the nginx stats endpoint.

### 0.4.2

BUGFIXES:
Expand Down
12 changes: 10 additions & 2 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type NginxClient struct {
apiEndpoint string
httpClient *http.Client
appName string
}

// StubStats represents NGINX stub_status metrics.
Expand All @@ -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()
Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e215887

Please sign in to comment.