From 813e6468bc5605eae554c6835d67d6a9ae4b84a3 Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Thu, 8 Dec 2016 17:36:45 +0000 Subject: [PATCH] Correctly instrument websocket handshakes Part of weaveworks/monitoring#82 --- .../weaveworks/common/middleware/instrument.go | 17 +++++++++++++---- vendor/manifest | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/vendor/github.com/weaveworks/common/middleware/instrument.go b/vendor/github.com/weaveworks/common/middleware/instrument.go index 676a59f6ab..6997920a20 100644 --- a/vendor/github.com/weaveworks/common/middleware/instrument.go +++ b/vendor/github.com/weaveworks/common/middleware/instrument.go @@ -19,16 +19,25 @@ type Instrument struct { Duration *prometheus.HistogramVec } -func isWSHandshakeRequest(req *http.Request) bool { - return strings.ToLower(req.Header.Get("Upgrade")) == "websocket" && - strings.ToLower(req.Header.Get("Connection")) == "upgrade" +// IsWSHandshakeRequest returns true if the given request is a websocket handshake request. +func IsWSHandshakeRequest(req *http.Request) bool { + if strings.ToLower(req.Header.Get("Upgrade")) == "websocket" { + // Connection header values can be of form "foo, bar, ..." + parts := strings.Split(strings.ToLower(req.Header.Get("Connection")), ",") + for _, part := range parts { + if strings.TrimSpace(part) == "upgrade" { + return true + } + } + } + return false } // 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)) + isWS := strconv.FormatBool(IsWSHandshakeRequest(r)) interceptor := &interceptor{ResponseWriter: w, statusCode: http.StatusOK} route := i.getRouteName(r) next.ServeHTTP(interceptor, r) diff --git a/vendor/manifest b/vendor/manifest index f184d1c858..b3b8f980f7 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -1361,7 +1361,7 @@ "importpath": "github.com/weaveworks/common", "repository": "https://github.com/weaveworks/common", "vcs": "git", - "revision": "5edbff9f695b090e16bbb90dd89b83b70178c49e", + "revision": "cc20acf03ebf74be0facaae4259dff6cde01ce77", "branch": "master", "notests": true },