Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions metrics/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var msgCounters [4]uint64
var msgLast [4]uint64
var msgCurrent [4]uint64

var httpCounters [8]uint64
var httpLast [8]uint64
var httpCurrent [8]uint64
var httpCounters [9]uint64
var httpLast [9]uint64
var httpCurrent [9]uint64

// A MsgCounters records message counters.
type MsgCounters struct {
Expand Down Expand Up @@ -55,6 +55,9 @@ type HttpCounters struct {
// StatusServiceUnavailable is the count of http 503 responses.
StatusServiceUnavailable uint64

// StatusTooManyRequests is the count of http 429 responses.
StatusTooManyRequests uint64

// Written is the number of bytes written.
Written uint64

Expand Down Expand Up @@ -97,7 +100,8 @@ func ReadHttpCounters(m *HttpCounters) {
m.StatusNotFound = httpCurrent[4] - httpLast[4]
m.StatusInternalServerError = httpCurrent[5] - httpLast[5]
m.StatusServiceUnavailable = httpCurrent[6] - httpLast[6]
m.Written = httpCurrent[7] - httpLast[7]
m.StatusTooManyRequests = httpCurrent[7] - httpLast[7]
m.Written = httpCurrent[8] - httpLast[8]

for i := range httpCounters {
httpLast[i] = httpCurrent[i]
Expand Down Expand Up @@ -159,7 +163,12 @@ func StatusServiceUnavailable() {
atomic.AddUint64(&httpCounters[6], 1)
}

// StatusTooManyRequests increments the http response 429 counter. It is safe for concurrent access.
func StatusTooManyRequests() {
atomic.AddUint64(&httpCounters[7], 1)
}

// Written increments the bytes sent counter by n.
func Written(n int64) {
atomic.AddUint64(&httpCounters[7], uint64(n)) //nolint:gosec
atomic.AddUint64(&httpCounters[8], uint64(n)) //nolint:gosec
}
8 changes: 7 additions & 1 deletion metrics/counters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestHttpCounters(t *testing.T) {
{i: l(), f: metrics.StatusNotFound, e: metrics.HttpCounters{StatusNotFound: 1}},
{i: l(), f: metrics.StatusInternalServerError, e: metrics.HttpCounters{StatusInternalServerError: 1}},
{i: l(), f: metrics.StatusServiceUnavailable, e: metrics.HttpCounters{StatusServiceUnavailable: 1}},
{i: l(), f: metrics.StatusTooManyRequests, e: metrics.HttpCounters{StatusTooManyRequests: 1}},
}

var m metrics.HttpCounters
Expand Down Expand Up @@ -108,6 +109,9 @@ func TestHttpCounters(t *testing.T) {
if m.StatusServiceUnavailable != 0 {
t.Errorf("expected 0 got %d", m.StatusServiceUnavailable)
}
if m.StatusTooManyRequests != 0 {
t.Errorf("expected 0 got %d", m.StatusTooManyRequests)
}

// increment one counter
// and check we incremented the correct counter
Expand Down Expand Up @@ -136,7 +140,9 @@ func TestHttpCounters(t *testing.T) {
if m.StatusServiceUnavailable != v.e.StatusServiceUnavailable {
t.Errorf("%s StatusServiceUnavailable expected %d got %d", v.i, v.e.StatusServiceUnavailable, m.StatusServiceUnavailable)
}

if m.StatusTooManyRequests != v.e.StatusTooManyRequests {
t.Errorf("%s StatusTooManyRequests expected %d got %d", v.i, v.e.StatusTooManyRequests, m.StatusTooManyRequests)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions weft/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ func writeResponseAndLogMetrics(err error, w http.ResponseWriter, r *http.Reques
case http.StatusServiceUnavailable:
metrics.StatusServiceUnavailable()
logger.Printf("%d %s %s %s %s", status, r.Method, r.RequestURI, name, err.Error())
case http.StatusTooManyRequests:
metrics.StatusTooManyRequests()
logger.Printf("%d %s %s %s %s", status, r.Method, r.RequestURI, name, err.Error())
}
}

Expand Down
Loading