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 Nov 10, 2019
1 parent e215887 commit 5ce43a2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
12 changes: 2 additions & 10 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
type NginxClient struct {
apiEndpoint string
httpClient *http.Client
appName string
}

// StubStats represents NGINX stub_status metrics.
Expand All @@ -32,11 +31,10 @@ type StubConnections struct {
}

// NewNginxClient creates an NginxClient.
func NewNginxClient(httpClient *http.Client, apiEndpoint string, appName string) (*NginxClient, error) {
func NewNginxClient(httpClient *http.Client, apiEndpoint string) (*NginxClient, error) {
client := &NginxClient{
apiEndpoint: apiEndpoint,
httpClient: httpClient,
appName: appName,
}

_, err := client.GetStubStats()
Expand All @@ -45,13 +43,7 @@ func NewNginxClient(httpClient *http.Client, apiEndpoint string, appName string)

// GetStubStats fetches the stub_status metrics.
func (client *NginxClient) GetStubStats() (*StubStats, error) {

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)
resp, err := client.httpClient.Get(client.apiEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to get %v: %v", client.apiEndpoint, err)
}
Expand Down
35 changes: 29 additions & 6 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ func main() {

registry.MustRegister(buildInfoMetric)

userAgent := fmt.Sprintf("NGINX-Prometheus-Exporter/v%v", version)
userAgentRT := &userAgentRoundTripper{agent: userAgent, rt: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !*sslVerify}}}
httpClient := &http.Client{
Timeout: timeout.Duration,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !*sslVerify},
},
Timeout: timeout.Duration,
Transport: userAgentRT,
}

signalChan := make(chan os.Signal, 1)
Expand All @@ -200,9 +200,8 @@ 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, appName)
return client.NewNginxClient(httpClient, *scrapeURI)
}, *nginxRetries, nginxRetryInterval.Duration)
if err != nil {
log.Fatalf("Could not create Nginx Client: %v", err)
Expand All @@ -225,3 +224,27 @@ func main() {
log.Printf("NGINX Prometheus Exporter has successfully started")
log.Fatal(http.ListenAndServe(*listenAddr, nil))
}

type userAgentRoundTripper struct {
agent string
rt http.RoundTripper
}

func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = cloneRequest(req)
req.Header.Set("User-Agent", rt.agent)
return rt.rt.RoundTrip(req)
}
func cloneRequest(req *http.Request) *http.Request {
r := new(http.Request)
*r = *req // shallow clone

// deep copy headers
r.Header = make(http.Header, len(req.Header))
for key, values := range req.Header {
newValues := make([]string, len(values))
copy(newValues, values)
r.Header[key] = newValues
}
return r
}

0 comments on commit 5ce43a2

Please sign in to comment.