Skip to content

fix(echoprometheus): don't panic when push gateway is unreachable - #152

Open
sarankumar1325 wants to merge 1 commit into
labstack:masterfrom
sarankumar1325:fix/pushgateway-nil-response
Open

sarankumar1325 wants to merge 1 commit into
labstack:masterfrom
sarankumar1325:fix/pushgateway-nil-response

Conversation

@sarankumar1325

@sarankumar1325 sarankumar1325 commented Oct 31, 2026 •

Copy link
Copy Markdown

RunPushGatewayGatherer panics with a nil pointer dereference whenever the push gateway can't be reached (connection refused, DNS failure, timeout) and the ErrorHandler returns nil, which is what the default ErrorHandler does:

res, err := client.Do(req)
if err != nil {
	if hErr := config.ErrorHandler(...); hErr != nil {
		return hErr
	}
}
if res.StatusCode != http.StatusOK { // res is nil here

So with the default config, a push gateway that goes down for a moment crashes the application's push goroutine (and the process). The response body was also never closed, which leaks a connection on every push.

Changes:

  • continue to the next tick after a client.Do error has been passed to the ErrorHandler.
  • Drain and close the response body after each push.
  • New test TestRunPushGatewayGatherer_unreachableGatewayDoesNotPanic: it points at a closed httptest server with an ErrorHandler that returns nil. On master it panics at prometheus.go:448. With the fix it returns context.DeadlineExceeded, and the handler sees "error sending to push gateway".

Tests: go test ./echoprometheus/, go test -race ./echoprometheus/, go vet, gofmt and staticcheck -tests=false ./echoprometheus/ are clean.

The same code was copied into labstack/echo-prometheus, where labstack/echo-prometheus#4 proposes the same fix.

This change was prepared with AI assistance and reviewed by me.

When client.Do failed and the ErrorHandler returned nil (the default),
RunPushGatewayGatherer fell through to res.StatusCode with a nil
response and panicked. Skip to the next tick instead, and drain and
close the response body so connections are reused.

Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
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.

1 participant