Skip to content

Commit aa311ca

Browse files
committed
feat: add 429 status to metrics and weft
1 parent d71b97b commit aa311ca

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

metrics/counters.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ var msgCounters [4]uint64
1010
var msgLast [4]uint64
1111
var msgCurrent [4]uint64
1212

13-
var httpCounters [8]uint64
14-
var httpLast [8]uint64
15-
var httpCurrent [8]uint64
13+
var httpCounters [9]uint64
14+
var httpLast [9]uint64
15+
var httpCurrent [9]uint64
1616

1717
// A MsgCounters records message counters.
1818
type MsgCounters struct {
@@ -55,6 +55,9 @@ type HttpCounters struct {
5555
// StatusServiceUnavailable is the count of http 503 responses.
5656
StatusServiceUnavailable uint64
5757

58+
// StatusTooManyRequests is the count of http 429 responses.
59+
StatusTooManyRequests uint64
60+
5861
// Written is the number of bytes written.
5962
Written uint64
6063

@@ -97,7 +100,8 @@ func ReadHttpCounters(m *HttpCounters) {
97100
m.StatusNotFound = httpCurrent[4] - httpLast[4]
98101
m.StatusInternalServerError = httpCurrent[5] - httpLast[5]
99102
m.StatusServiceUnavailable = httpCurrent[6] - httpLast[6]
100-
m.Written = httpCurrent[7] - httpLast[7]
103+
m.StatusTooManyRequests = httpCurrent[7] - httpLast[7]
104+
m.Written = httpCurrent[8] - httpLast[8]
101105

102106
for i := range httpCounters {
103107
httpLast[i] = httpCurrent[i]
@@ -159,7 +163,12 @@ func StatusServiceUnavailable() {
159163
atomic.AddUint64(&httpCounters[6], 1)
160164
}
161165

166+
// StatusTooManyRequests increments the http response 429 counter. It is safe for concurrent access.
167+
func StatusTooManyRequests() {
168+
atomic.AddUint64(&httpCounters[7], 1)
169+
}
170+
162171
// Written increments the bytes sent counter by n.
163172
func Written(n int64) {
164-
atomic.AddUint64(&httpCounters[7], uint64(n)) //nolint:gosec
173+
atomic.AddUint64(&httpCounters[8], uint64(n)) //nolint:gosec
165174
}

metrics/counters_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func TestHttpCounters(t *testing.T) {
7979
{i: l(), f: metrics.StatusNotFound, e: metrics.HttpCounters{StatusNotFound: 1}},
8080
{i: l(), f: metrics.StatusInternalServerError, e: metrics.HttpCounters{StatusInternalServerError: 1}},
8181
{i: l(), f: metrics.StatusServiceUnavailable, e: metrics.HttpCounters{StatusServiceUnavailable: 1}},
82+
{i: l(), f: metrics.StatusTooManyRequests, e: metrics.HttpCounters{StatusTooManyRequests: 1}},
8283
}
8384

8485
var m metrics.HttpCounters
@@ -108,6 +109,9 @@ func TestHttpCounters(t *testing.T) {
108109
if m.StatusServiceUnavailable != 0 {
109110
t.Errorf("expected 0 got %d", m.StatusServiceUnavailable)
110111
}
112+
if m.StatusTooManyRequests != 0 {
113+
t.Errorf("expected 0 got %d", m.StatusTooManyRequests)
114+
}
111115

112116
// increment one counter
113117
// and check we incremented the correct counter
@@ -136,7 +140,9 @@ func TestHttpCounters(t *testing.T) {
136140
if m.StatusServiceUnavailable != v.e.StatusServiceUnavailable {
137141
t.Errorf("%s StatusServiceUnavailable expected %d got %d", v.i, v.e.StatusServiceUnavailable, m.StatusServiceUnavailable)
138142
}
139-
143+
if m.StatusTooManyRequests != v.e.StatusTooManyRequests {
144+
t.Errorf("%s StatusTooManyRequests expected %d got %d", v.i, v.e.StatusTooManyRequests, m.StatusTooManyRequests)
145+
}
140146
}
141147
}
142148

weft/handlers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ func writeResponseAndLogMetrics(err error, w http.ResponseWriter, r *http.Reques
304304
case http.StatusServiceUnavailable:
305305
metrics.StatusServiceUnavailable()
306306
logger.Printf("%d %s %s %s %s", status, r.Method, r.RequestURI, name, err.Error())
307+
case http.StatusTooManyRequests:
308+
metrics.StatusTooManyRequests()
309+
logger.Printf("%d %s %s %s %s", status, r.Method, r.RequestURI, name, err.Error())
307310
}
308311
}
309312

0 commit comments

Comments
 (0)