fix: suppress canceled rate limit logs - #21
Conversation
|
| Filename | Overview |
|---|---|
| internal/middleware/ratelimit.go | Adds early-return for context.Canceled from the KV store, recording a 'canceled' span attribute instead of logging a 503; non-cancellation errors retain fail-closed behavior. context.DeadlineExceeded is not similarly suppressed. |
| internal/middleware/ratelimit_test.go | Adds regression test verifying that a pre-canceled context does not produce error-level slog records; introduces a minimal countingErrorHandler slog.Handler helper. |
| internal/observability/attrs.go | Adds RateLimitOutcomeCanceled constant to the existing RateLimitOutcome set. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Incoming request to /mcp] --> B{store.Increment}
B -->|success| C{count > limit?}
B -->|context.Canceled| D[Set span: ratelimit.outcome=canceled]
B -->|other error| E[Set span: ratelimit.outcome=store_error - HTTP 503]
C -->|yes| F[HTTP 429 + Retry-After]
C -->|no| G[ratelimit.outcome=allowed - next.ServeHTTP]
Reviews (1): Last reviewed commit: "fix: suppress canceled rate limit logs" | Re-trigger Greptile
| if errors.Is(err, context.Canceled) { | ||
| trace.SpanFromContext(r.Context()).SetAttributes(observability.RateLimitAttrs{Outcome: observability.RateLimitOutcomeCanceled, Limit: cfg.Requests, Window: cfg.Window}.Attributes()...) | ||
| return | ||
| } |
There was a problem hiding this comment.
The cancellation guard only covers
context.Canceled, but context.DeadlineExceeded surfaces through the same path when a client-set deadline expires before the store responds. Both represent the client's request lifecycle ending, not a store malfunction. Without this, a deadline-exceeded error from store.Increment would still be logged at error level and return a 503 to a client that has already gone. Consider broadening the check to errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded).
context.CanceledfromRateLimitstore checks as a canceled request instead of a store failure503behavior and error logging for non-cancellationkv.Storeerrorsratelimit.outcomecoverage for canceled checksslogrecords