MM-23131 Include HTTP status code in the metrics (#14240)

* ResponseWriter wrapper to get status code

For our metrics, we need the status code returned by a request
so this wrapper includes a new method StatusCode() that includes
the desired code

* Shadow the responsewriter variable in the handlers

In order to avoid confusion to people deciding what variable to use.
I've also changed the tests to reflect this change and added a new
one that checks the Flush method works
Этот коммит содержится в:
Mario de Frutos Dieguez
2020-04-14 14:15:00 +02:00
коммит произвёл GitHub
родитель 53c1c0d4b4
Коммит aafea55976
5 изменённых файлов: 157 добавлений и 5 удалений

Просмотреть файл

@@ -10,6 +10,7 @@ import (
"net/http"
"reflect"
"runtime"
"strconv"
"strings"
"time"
@@ -79,6 +80,7 @@ type Handler struct {
}
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w = newWrappedWriter(w)
now := time.Now()
requestID := model.NewId()
@@ -257,8 +259,9 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != model.API_URL_SUFFIX+"/websocket" {
elapsed := float64(time.Since(now)) / float64(time.Second)
statusCode := strconv.Itoa(w.(*responseWriterWrapper).StatusCode())
c.App.Metrics().ObserveHttpRequestDuration(elapsed)
c.App.Metrics().ObserveApiEndpointDuration(h.HandlerName, r.Method, elapsed)
c.App.Metrics().ObserveApiEndpointDuration(h.HandlerName, r.Method, statusCode, elapsed)
}
}
}