add prometheus-middlewares for http and ws endpoint

This commit is contained in:
Ilnur Galiev
2020-10-19 18:00:55 +03:00
parent 1043df9156
commit f627c2edfa
7 changed files with 261 additions and 5 deletions
+35
View File
@@ -0,0 +1,35 @@
package prom
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
func HTTPMiddleware(next http.Handler) http.Handler {
if !metrics {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
httpCount.Inc()
timer := prometheus.NewTimer(httpDuration)
next.ServeHTTP(w, r)
timer.ObserveDuration()
})
}
func WSMiddleware(next http.Handler) http.Handler {
if !metrics {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wsCount.Inc()
timer := prometheus.NewTimer(wsDuration)
next.ServeHTTP(w, r)
timer.ObserveDuration()
})
}