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

Conversation

asherf
Copy link
Contributor

@asherf asherf commented Oct 31, 2019

Proposed changes

This changes the User-Agent header that is being sent when calling nginx stats endpoint.
Currently, the User-Agent is the default Go Http client user agent string (Go-http-client/1.1), which is not very useful when looking at nginx logs.
This will change it to be NGINX-Prometheus-Exporter/v0.5.0 (with the version we get when building the docker image)
I have tested locally (with a custom docker image) in my k8s cluster (faked the version number when calling docker build).

Screenshot 2019-10-31 11 29 23

(ignore the red color, this is just kubetail choosing random colors)

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING guide
  • I have proven my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have ensured the README is up to date
  • I have rebased my branch onto master
  • I will ensure my PR is targeting the master branch and pulling from my branch on my own fork

@pleshakov pleshakov added the enhancement Pull requests for new features/feature enhancements label Oct 31, 2019
@pleshakov
Copy link
Contributor

@asherf thanks for the PR!

I suggest implementing this feature using a custom RoundTripper -- https://godoc.org/net/http#RoundTripper.

There is an example implementation of a similar feature here https://github.com/kubernetes/kubernetes/blob/bc1d8c6d61078269540f807248a455cba09e672c/staging/src/k8s.io/client-go/transport/round_trippers.go#L144 Note that it is important to clone requests by doing a deep copy to adhere of the contract of the RoundTripper.

With a RoundTripper, we will only have to change the httpClient in exporter.go:

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

where userAgentRT is a variable that holds a RoundTripper for setting the user-agent header

As the result, both clients for NGINX and NGINX Plus will set the custom user-agent header.

@asherf
Copy link
Contributor Author

asherf commented Nov 10, 2019

@pleshakov Thanks for that useful feedback! It took me a while (busy at work + I'm new to Go)
But I got this working.
Let me know if you have any further feedback since I am a Go noob.
I am happy to incorporate any feedback you have to this PR.

Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asherf thanks! looks good! just a couple of small suggestions

I also requested a review from @Rulox

CHANGELOG.md Outdated Show resolved Hide resolved
exporter.go Outdated Show resolved Hide resolved
exporter.go Show resolved Hide resolved
@pleshakov pleshakov requested a review from Rulox November 12, 2019 04:41
Copy link

@Rulox Rulox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

+1 to @pleshakov suggestions about readability and best practices.

Additionally, please rebase against master, there are conflicts in exporter.go due to a PR merged recently.

Thanks for the contribution :shipit: !

Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asherf thanks for making the changes. there a bug appeared after the PR was rebased against the master branch. otherwise, looks good!

exporter.go Outdated Show resolved Hide resolved
Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@pleshakov pleshakov merged commit 73a7537 into nginxinc:master Nov 13, 2019
@asherf asherf deleted the user-agent branch November 13, 2019 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Pull requests for new features/feature enhancements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants