Commit d1522d6a authored by Ben Kochie's avatar Ben Kochie

Extract instrumentDuration function.

Apply `instrumentDuration()` to both `route()` and `wsRoute()`.
parent 98929304
...@@ -63,18 +63,19 @@ func compileRegexp(regexpStr string) *regexp.Regexp { ...@@ -63,18 +63,19 @@ func compileRegexp(regexpStr string) *regexp.Regexp {
return regexp.MustCompile(regexpStr) return regexp.MustCompile(regexpStr)
} }
func instrumentDuration(h http.Handler, method string, regexpStr string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
h.ServeHTTP(w, r)
routeRequestDurations.WithLabelValues(method, regexpStr).Observe(time.Since(start).Seconds())
})
}
func route(method, regexpStr string, handler http.Handler, matchers ...matcherFunc) routeEntry { func route(method, regexpStr string, handler http.Handler, matchers ...matcherFunc) routeEntry {
instr := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
h.ServeHTTP(w, r)
routeRequestDurations.WithLabelValues(method, regexpStr).Observe(time.Since(start).Seconds())
})
}
return routeEntry{ return routeEntry{
method: method, method: method,
regex: compileRegexp(regexpStr), regex: compileRegexp(regexpStr),
handler: instr(denyWebsocket(handler)), handler: instrumentDuration(denyWebsocket(handler), method, regexpStr),
matchers: matchers, matchers: matchers,
} }
} }
...@@ -83,7 +84,7 @@ func wsRoute(regexpStr string, handler http.Handler, matchers ...matcherFunc) ro ...@@ -83,7 +84,7 @@ func wsRoute(regexpStr string, handler http.Handler, matchers ...matcherFunc) ro
return routeEntry{ return routeEntry{
method: "GET", method: "GET",
regex: compileRegexp(regexpStr), regex: compileRegexp(regexpStr),
handler: handler, handler: instrumentDuration(handler, "GET", regexpStr),
matchers: append(matchers, websocket.IsWebSocketUpgrade), matchers: append(matchers, websocket.IsWebSocketUpgrade),
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment