Skip to content
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

Don't return an error from Handler.ServeHTTP #35

Closed
wants to merge 1 commit into from

Conversation

tgeoghegan
Copy link
Contributor

Caddy can be set up to export Prometheus metrics from HTTP servers, and middleware automatically gets wrapped in a metricsInstrumentedHandler which will emit histograms of requests times, labeled by the HTTP status. However, this only happens if a handler's ServeHTTP method doesn't return an error (source). The rate limiter's ServeHTTP was doing just that, and so the metrics showed no trace of the 429 responses.

Caddy can be set up to export Prometheus metrics from HTTP servers, and
middleware automatically gets wrapped in a `metricsInstrumentedHandler`
which will emit histograms of requests times, labeled by the HTTP
status. However, this only happens if a handler's `ServeHTTP` method
doesn't return an error ([source][1]). The rate limiter's `ServeHTTP`
was doing just that, and so the metrics showed no trace of the 429
responses.

[1]: https://github.com/caddyserver/caddy/blob/7d919af01b31aff6eb1086e87784bf59c52419bb/modules/caddyhttp/metrics.go#L140
@francislavoie
Copy link

Hmm, could we fix metricsInstrumentedHandler to check for caddyhttp.Error instead? That way it's an actual recoverable error that can be handled with handle_errors routes in Caddy.

Technically this is a breaking change because it means error routes can no longer be used to implement custom behaviour on rate limiting (e.g. render an error page or w/e).

@tgeoghegan
Copy link
Contributor Author

Ah, breaking changes are no good. I'll see if I can put together a Caddy PR that does as you suggest

@mholt
Copy link
Owner

mholt commented Dec 12, 2023

The change should be fairly simple, let me know if you have questions!

(Thanks as always Francis)

tgeoghegan added a commit to tgeoghegan/caddy that referenced this pull request Dec 13, 2023
If the error returned by the wrapped handler is a `Caddy.HandlerError`,
that indicates a problem that still yielded a successful HTTP
transaction, so it makes sense to observe request duration and size
metrics for it.

See mholt/caddy-ratelimit#35 for some context.
@tgeoghegan
Copy link
Contributor Author

I think this change is obsoleted by caddyserver/caddy#5979, which I've verified solves my problem. I'm going to close this PR for the sake of tidiness and we can re-open this discussion should the linked Caddy PR not go forward for some reason.

@tgeoghegan tgeoghegan closed this Dec 13, 2023
@mholt
Copy link
Owner

mholt commented Dec 14, 2023

Sounds good! :)

francislavoie pushed a commit to tgeoghegan/caddy that referenced this pull request Dec 15, 2023
If the error returned by the wrapped handler is a `Caddy.HandlerError`,
that indicates a problem that still yielded a successful HTTP
transaction, so it makes sense to observe request duration and size
metrics for it.

See mholt/caddy-ratelimit#35 for some context.
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.

3 participants