This commit is contained in:
Miroslav Šedivý 2022-07-04 18:23:35 +02:00
parent 8f83089c8e
commit bf47e5a8d0
3 changed files with 2 additions and 60 deletions

View File

@ -1,57 +0,0 @@
package http
import (
"net/http"
"time"
"github.com/go-chi/chi/middleware"
"github.com/prometheus/client_golang/prometheus"
)
// Middleware is a handler that exposes prometheus metrics for the number of requests,
// the latency and the response size, partitioned by status code, method and HTTP path.
type metrics struct {
reqs *prometheus.CounterVec
latency *prometheus.HistogramVec
}
// NewMiddleware returns a new prometheus Middleware handler.
func middlewareMetrics(next http.Handler) http.Handler {
var m metrics
m.reqs = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "requests_total",
Namespace: "neko",
Subsystem: "http",
Help: "How many HTTP requests processed, partitioned by status code, method and HTTP path.",
},
[]string{"code", "method", "path"},
)
prometheus.MustRegister(m.reqs)
m.latency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "request_duration_milliseconds",
Namespace: "neko",
Subsystem: "http",
Help: "How long it took to process the request, partitioned by status code, method and HTTP path.",
Buckets: []float64{300, 1200, 5000},
},
[]string{"code", "method", "path"},
)
prometheus.MustRegister(m.latency)
return m.handler(next)
}
func (c metrics) handler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(ww, r)
c.reqs.WithLabelValues(http.StatusText(ww.Status()), r.Method, r.URL.Path).Inc()
c.latency.WithLabelValues(http.StatusText(ww.Status()), r.Method, r.URL.Path).Observe(float64(time.Since(start).Nanoseconds()) / 1000000)
}
return http.HandlerFunc(fn)
}

View File

@ -20,7 +20,6 @@ func newRouter(logger zerolog.Logger) *router {
r := chi.NewRouter()
r.Use(middleware.RequestID) // Create a request ID for each request
r.Use(middleware.RequestLogger(&logFormatter{logger}))
r.Use(middlewareMetrics)
r.Use(middleware.Recoverer) // Recover from panics without crashing server
return &router{r}
}

View File

@ -248,10 +248,10 @@ func (m *metricsCtx) SetVideoID(session types.Session, videoId string) {
if _, found := met.videoIds[videoId]; !found {
met.videoIds[videoId] = promauto.NewGauge(prometheus.GaugeOpts{
Name: "video_id",
Name: "video_listeners",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Current Video ID of a session.",
Help: "Listeners for Video pipelines by a session.",
ConstLabels: map[string]string{
"session_id": session.ID(),
"video_id": videoId,