Skip to content

Commit 5c5a1ab

Browse files
committed
logging: log when request is rate-limited
Currently there is no indication in the logs that a request has been rate-limited. Signed-off-by: crozzy <[email protected]>
1 parent 4e44f7e commit 5c5a1ab

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

httptransport/concurrentlimit.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/prometheus/client_golang/prometheus"
77
"github.com/prometheus/client_golang/prometheus/promauto"
8+
"github.com/quay/zlog"
89
"golang.org/x/sync/semaphore"
910
)
1011

@@ -37,6 +38,13 @@ func (l *limitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3738
if sem != nil {
3839
if !sem.TryAcquire(1) {
3940
concurrentLimitedCounter.WithLabelValues(endpt, r.Method).Add(1)
41+
zlog.Info(r.Context()).
42+
Str("remote_addr", r.RemoteAddr).
43+
Str("method", r.Method).
44+
Str("request_uri", r.RequestURI).
45+
Int("status", http.StatusTooManyRequests).
46+
Msg("rate limited HTTP request")
47+
4048
apiError(w, http.StatusTooManyRequests, "server handling too many requests")
4149
return
4250
}

httptransport/concurrentlimit_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package httptransport
22

33
import (
44
"context"
5+
"net"
56
"net/http"
67
"net/http/httptest"
78
"sync"
89
"sync/atomic"
910
"testing"
1011

12+
"github.com/quay/zlog"
1113
"golang.org/x/sync/semaphore"
1214
)
1315

1416
func TestConcurrentRequests(t *testing.T) {
17+
ctx := context.Background()
18+
ctx = zlog.Test(ctx, t)
1519
sem := semaphore.NewWeighted(1)
1620
// Ret controls when the http server returns.
1721
// Ready is strobed once the first request is seen.
@@ -29,10 +33,10 @@ func TestConcurrentRequests(t *testing.T) {
2933
w.WriteHeader(http.StatusNoContent)
3034
}),
3135
})
36+
srv.Config.BaseContext = func(_ net.Listener) context.Context { return ctx }
3237
defer srv.Close()
3338
c := srv.Client()
3439

35-
ctx := context.Background()
3640
done := make(chan struct{})
3741
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
3842
if err != nil {

0 commit comments

Comments
 (0)