Skip to content

Commit f2b1b3e

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

2 files changed

Lines changed: 17 additions & 5 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
}

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)