Correctly instrument websocket handshakes

Part of weaveworks/monitoring#82
This commit is contained in:
Jonathan Lange
2016-12-08 17:36:45 +00:00
parent 5687054126
commit 813e6468bc
2 changed files with 14 additions and 5 deletions

View File

@@ -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)

2
vendor/manifest vendored
View File

@@ -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
},