Skip to content

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

Closed
tgeoghegan wants to merge 1 commit intomholt:masterfrom
tgeoghegan:429s-are-not-errors
Closed

Don't return an error from Handler.ServeHTTP#35
tgeoghegan wants to merge 1 commit intomholt:masterfrom
tgeoghegan:429s-are-not-errors

Conversation

@tgeoghegan
Copy link
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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