Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set user agent on scrape requests to nginx. #70

Merged
merged 4 commits into from
Nov 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,15 @@ func main() {
scrapeURI = &newScrapeURI
}

userAgent := fmt.Sprintf("NGINX-Prometheus-Exporter/v%v", version)
userAgentRT := &userAgentRoundTripper{
agent: userAgent,
rt: transport,
}

httpClient := &http.Client{
Timeout: timeout.Duration,
Transport: transport,
Transport: userAgentRT,
}

srv := http.Server{}
Expand Down Expand Up @@ -290,3 +296,28 @@ func main() {
log.Printf("NGINX Prometheus Exporter has successfully started")
log.Fatal(srv.Serve(listener))
}

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)
}
asherf marked this conversation as resolved.
Show resolved Hide resolved

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
}