Skip to content

Add metricsAddr flag for separate metrics listener - #93

Closed
lewispb wants to merge 7 commits into
heyfrom
lb/metrics-addr
Closed

Add metricsAddr flag for separate metrics listener#93
lewispb wants to merge 7 commits into
heyfrom
lb/metrics-addr

Conversation

@lewispb

@lewispb lewispb commented Apr 30, 2026

Copy link
Copy Markdown
Member

Summary

  • New -metricsAddr flag (auto-mapped to IMAGEPROXY_METRICSADDR via the existing envy setup) that serves Prometheus metrics on a separate HTTP listener.
  • When set, /metrics is removed from the main -addr and served only on the metrics address. When unset, behavior is unchanged.

Test plan

  • go build ./cmd/imageproxy succeeds
  • With IMAGEPROXY_METRICSADDR set, /metrics on main addr returns 404 and /metrics on the metrics addr returns Prometheus output
  • With IMAGEPROXY_METRICSADDR unset, /metrics on main addr still works (default behavior preserved)
  • imageproxy -help lists the new flag and env var

When set (via -metricsAddr or IMAGEPROXY_METRICSADDR), Prometheus
metrics are served on a separate HTTP listener and removed from the
main address. Default behavior is unchanged.
Copilot AI review requested due to automatic review settings April 30, 2026 10:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds an optional dedicated metrics listener to the cmd/imageproxy binary so Prometheus scraping can be served on a separate address without exposing /metrics on the main proxy listener.

Changes:

  • Introduces -metricsAddr (and corresponding IMAGEPROXY_METRICSADDR) to enable a standalone metrics HTTP server.
  • When -metricsAddr is set, /metrics on the main -addr is replaced with a 404 handler and served only on the metrics listener.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/imageproxy/main.go Outdated
Comment thread cmd/imageproxy/main.go
lewispb added 2 commits April 30, 2026 11:08
Move /metrics routing into cmd/imageproxy so the library only
generates metrics and the cmd is the sole place that serves them.
…moval

Bind the metrics listener in the main goroutine via net.Listen so a
bind failure (e.g. address already in use) fails fast before the main
proxy starts and before logging that the metrics server is listening.
Also add a test asserting the proxy library no longer special-cases
/metrics.
Copilot AI review requested due to automatic review settings April 30, 2026 10:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/imageproxy/main.go
Comment thread cmd/imageproxy/main.go Outdated
Comment thread cmd/imageproxy/main.go
lewispb added 2 commits April 30, 2026 11:21
actions/cache@v2 is hard-deprecated by GitHub and auto-fails the run.
golangci-lint v1.31 cannot parse the Go 1.25 stdlib (generics).
setup-go@v1 hangs on modern runners (can't fetch the Go version index).
Copilot AI review requested due to automatic review settings April 30, 2026 10:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/imageproxy/main.go
Comment thread cmd/imageproxy/main.go Outdated
lewispb added 2 commits April 30, 2026 11:47
- /metrics on the main listener now returns 404 (not 400) when
  metricsAddr is set, matching the documented behavior.
- Metrics serve errors are logged instead of fatal, so a transient
  metrics-listener failure does not bring down the proxy.
- Use a real *http.Server with ReadHeaderTimeout for the metrics
  listener, and drop the unused server := &http.Server{...} above.
- data.go: apply Go 1.19+ doc-comment heading format (goimports).
- .golangci.yml: exclude pre-existing staticcheck/gosec findings on
  legacy transport fallback code and the main listener's
  http.ListenAndServe usage.
- cmd/imageproxy-sign: restore os.Stdout after TestMainFunc swaps it,
  otherwise Go 1.25's runtime hits the closed pipe when writing the
  coverage profile.
Copilot AI review requested due to automatic review settings April 30, 2026 11:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread imageproxy.go
Comment on lines 133 to 137
fmt.Fprint(w, "OK")
return
}

if r.URL.Path == "/metrics" {
var h http.Handler = promhttp.Handler()
h.ServeHTTP(w, r)
return
}

var h http.Handler = http.HandlerFunc(p.serveImage)

Copilot AI Apr 30, 2026

Copy link

Choose a reason for hiding this comment

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

Proxy.ServeHTTP no longer special-cases /metrics, which changes the behavior of the imageproxy library for any consumers that mount Proxy directly (previously /metrics returned Prometheus output; now it becomes a 400 via request parsing). If this is intentional, it should be called out in docs/README and/or the PR description as a behavior/API change for library users; otherwise consider keeping /metrics handling in the library and letting the cmd/imageproxy router override it when -metricsAddr is set.

Copilot uses AI. Check for mistakes.
Comment thread .golangci.yml
Comment on lines +31 to +35

# The main proxy listener uses http.ListenAndServe; pre-existing.
- path: cmd/imageproxy/main.go
linters: gosec
text: G114

Copilot AI Apr 30, 2026

Copy link

Choose a reason for hiding this comment

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

The exclude-rule path: cmd/imageproxy/main.go + text: G114 suppresses all G114 findings in that file. Since this PR adds a second HTTP server to the same file, this rule can accidentally mask future (or new) timeout-related findings beyond the pre-existing http.ListenAndServe call. Consider removing this exclude-rule and using a targeted inline suppression (e.g., //nolint:gosec on the specific ListenAndServe line) so new servers in the file still get checked.

Suggested change
# The main proxy listener uses http.ListenAndServe; pre-existing.
- path: cmd/imageproxy/main.go
linters: gosec
text: G114

Copilot uses AI. Check for mistakes.
@lewispb lewispb closed this Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants