-
Notifications
You must be signed in to change notification settings - Fork 369
Add proxy protocol v2 client-side support #979
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
base: main
Are you sure you want to change the base?
Conversation
✅ All required contributors have signed the F5 CLA for this PR. Thank you! |
I have hereby read the F5 CLA and agree to its terms |
For the reference, here's the linter output from a platform I've been testing on:
|
@sindhushiv, this PR introduces a new 3rd party dependency ( |
@defanator Could you please resolve conflicts? |
@jjngx sorry for slow response - I'll try to address this ASAP. |
@jjngx re #979 (comment), its Apache 2.0 👍🏼 |
test nginx conf:
start nginx docker container:
cURL:
stub_status:
exporter test with proxy_protocol:
exporter test without proxy_protocol:
exporter test with proxy_protocol using envvar PROXY_PROTOCOL:
|
I have verified the tests @vepatel ran |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds client-side support for proxy protocol v2 to the nginx-prometheus-exporter, enabling it to work with nginx instances behind load balancers that use proxy protocol. The implementation allows the exporter to prepend proxy protocol headers when connecting to nginx endpoints.
- Adds a new command-line flag
--nginx.proxy-protocol
to enable proxy protocol support - Implements proxy protocol v2 header generation for TCP (IPv4/IPv6) and Unix socket connections
- Updates the HTTP transport's DialContext to handle proxy protocol when enabled
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
go.mod | Adds dependency on go-proxyproto library v0.8.1 |
exporter.go | Implements proxy protocol v2 client support with conditional dialing logic |
README.md | Documents the new --nginx.proxy-protocol flag |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
||
switch addr := remoteAddr.(type) { | ||
case *net.TCPAddr: | ||
if addr.IP.To4() == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name 'addr' shadows the outer 'addr' parameter, which could lead to confusion. The switch should use a different variable name like 'tcpAddr' or 'remoteAddrTyped'.
if addr.IP.To4() == nil { | |
switch remoteAddrTyped := remoteAddr.(type) { | |
case *net.TCPAddr: | |
if remoteAddrTyped.IP.To4() == nil { |
Copilot uses AI. Check for mistakes.
_, err = header.WriteTo(conn) | ||
if err != nil { | ||
return nil, fmt.Errorf("writing proxyproto header: %w", err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The number of bytes written is being ignored. Consider logging or validating the bytes written to ensure the complete header was sent, or add a comment explaining why this return value is intentionally ignored.
} | |
n, err := header.WriteTo(conn) | |
if err != nil { | |
return nil, fmt.Errorf("writing proxyproto header: %w", err) | |
} | |
// Validate that the entire header was written | |
var buf bytes.Buffer | |
_, marshalErr := header.WriteTo(&buf) | |
if marshalErr != nil { | |
return nil, fmt.Errorf("marshalling proxyproto header for length check: %w", marshalErr) | |
} | |
expectedLen := buf.Len() | |
if n < expectedLen { | |
return nil, fmt.Errorf("proxyproto header: only %d of %d bytes written", n, expectedLen) | |
} |
Copilot uses AI. Check for mistakes.
Proposed changes
This PR introduces support for proxy protocol v2 which is available in nginx starting from versions 1.13.11 (foss) and R16 (NGINX Plus).
Primary use case is running nginx-prometheus-exporter alongside with nginx or NGINX Plus sitting behind other load balancers like AWS NLB or Google's PNLB with proxy protocol enabled. Sometimes creating dedicated listeners just for stub_status or API could be undesired, and with the proposed changes the exporter will be able to reuse existing listeners without extra effort.
Changes were tested with the following combinations of listeners:
proxy_protocol
, IPv4 withoutproxy_protocol
.proxy_protocol
, IPv6 withoutproxy_protocol
.proxy_protocol
, unix socket withoutproxy_protocol
(not that common scenario, but still supported by standard).Implementation used: https://github.com/pires/go-proxyproto
Checklist
Before creating a PR, run through this checklist and mark each as complete.