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
3 changes: 3 additions & 0 deletions echoprometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,10 @@ 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 is nil when client.Do returns an error
}
_, _ = io.Copy(io.Discard, res.Body)
_ = 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
26 changes: 26 additions & 0 deletions echoprometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,32 @@ func TestRunPushGatewayGatherer(t *testing.T) {
unregisterDefaults("myapp")
}

func TestRunPushGatewayGatherer_unreachableGatewayDoesNotPanic(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
unreachableURL := svr.URL
svr.Close() // connections to this URL now fail, so client.Do returns an error and a nil response

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

var handledErrs []error
config := PushGatewayConfig{
PushGatewayURL: unreachableURL,
PushInterval: 10 * time.Millisecond,
Gatherer: prometheus.NewRegistry(),
ErrorHandler: func(err error) error {
handledErrs = append(handledErrs, err)
return nil // keep pushing, like the default ErrorHandler
},
}
err := RunPushGatewayGatherer(ctx, config)

assert.ErrorIs(t, err, context.DeadlineExceeded)
if assert.NotEmpty(t, handledErrs) {
assert.ErrorContains(t, handledErrs[0], "error sending to push gateway")
}
}

// TestSetPathFor404NoMatchingRoute tests that the url is not included in the metric when
// the 404 response is due to no matching route
func TestSetPathFor404NoMatchingRoute(t *testing.T) {
Expand Down