Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ func RunPushGatewayGatherer(ctx context.Context, config PushGatewayConfig) error
if hErr := config.ErrorHandler(fmt.Errorf("error sending to push gateway: %w", err)); hErr != nil {
return hErr
}
continue
}
_ = res.Body.Close()
if res.StatusCode != http.StatusOK {
if hErr := config.ErrorHandler(echo.NewHTTPError(res.StatusCode, "post metrics request did not succeed")); hErr != nil {
return hErr
Expand Down
29 changes: 29 additions & 0 deletions prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -387,3 +388,31 @@ func unregisterDefaults(subsystem string) {
Help: "The HTTP request sizes in bytes.",
})
}

func TestRunPushGatewayGatherer_UnreachableGateway(t *testing.T) {
// Start and immediately close a server so the URL refuses connections.
srv := httptest.NewServer(http.NotFoundHandler())
url := srv.URL
srv.Close()

ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()

var errCount int32
config := PushGatewayConfig{
PushGatewayURL: url,
PushInterval: 10 * time.Millisecond,
ErrorHandler: func(err error) error {
atomic.AddInt32(&errCount, 1)
return nil // keep running
},
}

err := RunPushGatewayGatherer(ctx, config)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context deadline exceeded, got: %v", err)
}
if atomic.LoadInt32(&errCount) == 0 {
t.Fatal("expected ErrorHandler to be called for connection errors")
}
}