Skip to content

Commit

Permalink
Add request failed metric
Browse files Browse the repository at this point in the history
This metric is predefined in k6. When a request completes we want to
measure how many of these failed. This is based on the status code of
the response. If the code is outside of the 200-399 range, it is deemed
to have failed. The default status code range was taken from k6.

The opposite is also possible to determine, we can work out which
requests succeeded since the value of the metric will be 0.
  • Loading branch information
ankur22 committed May 23, 2023
1 parent 7d8cc6c commit e124756
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
fromCache, fromPreCache, fromSvcWrk bool
url = req.url.String()
wallTime = time.Now()
failed float64
)
if resp != nil {
status = resp.status
Expand All @@ -189,6 +190,11 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
fromSvcWrk = resp.fromServiceWorker
wallTime = resp.wallTime
url = resp.url
// Assuming that a failure is when status
// is between 200 and 399 (inclusive).
if status < 200 || status > 399 {
failed = 1
}
} else {
m.logger.Debugf("NetworkManager:emitResponseMetrics",
"response is nil url:%s method:%s", req.url, req.method)
Expand Down Expand Up @@ -258,6 +264,11 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
Value: k6metrics.D(time.Duration(resp.timing.ReceiveHeadersEnd-resp.timing.SendEnd) * time.Millisecond),
Time: wallTime,
},
{
TimeSeries: k6metrics.TimeSeries{Metric: state.BuiltinMetrics.HTTPReqFailed, Tags: tags},
Value: failed,
Time: wallTime,
},
},
})
}
Expand Down

0 comments on commit e124756

Please sign in to comment.