Include label in prom metric to distinguish ws connection latencies.

This commit is contained in:
Tom Wilkie
2016-05-23 15:02:41 +01:00
parent 5079a9eba4
commit 3335361ae9
2 changed files with 9 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package middleware
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
@@ -17,10 +18,16 @@ type Instrument struct {
Duration *prometheus.SummaryVec
}
func isWSHandshakeRequest(req *http.Request) bool {
return strings.ToLower(req.Header.Get("Upgrade")) == "websocket" &&
strings.ToLower(req.Header.Get("Connection")) == "upgrade"
}
// Wrap implements middleware.Interface
func (i Instrument) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
begin := time.Now()
isWS := strconv.FormatBool(isWSHandshakeRequest(r))
interceptor := &interceptor{ResponseWriter: w, statusCode: http.StatusOK}
next.ServeHTTP(interceptor, r)
var (
@@ -28,7 +35,7 @@ func (i Instrument) Wrap(next http.Handler) http.Handler {
status = strconv.Itoa(interceptor.statusCode)
took = time.Since(begin)
)
i.Duration.WithLabelValues(r.Method, route, status).Observe(float64(took.Nanoseconds()))
i.Duration.WithLabelValues(r.Method, route, status, isWS).Observe(float64(took.Nanoseconds()))
})
}

View File

@@ -32,7 +32,7 @@ var (
Namespace: "scope",
Name: "request_duration_nanoseconds",
Help: "Time spent serving HTTP requests.",
}, []string{"method", "route", "status_code"})
}, []string{"method", "route", "status_code", "ws"})
)
func init() {