From 3335361ae935a0d48d59d25d6478203f599edcd8 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 23 May 2016 15:02:41 +0100 Subject: [PATCH] Include label in prom metric to distinguish ws connection latencies. --- common/middleware/instrument.go | 9 ++++++++- prog/app.go | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/common/middleware/instrument.go b/common/middleware/instrument.go index 018aed52a..23d7de699 100644 --- a/common/middleware/instrument.go +++ b/common/middleware/instrument.go @@ -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())) }) } diff --git a/prog/app.go b/prog/app.go index 3d58e0e7c..325b5cb13 100644 --- a/prog/app.go +++ b/prog/app.go @@ -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() {