forked from ory/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
31 lines (26 loc) · 848 Bytes
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package prometheus
import (
"net/http"
"time"
)
type MetricsManager struct {
prometheusMetrics *Metrics
}
func NewMetricsManager(version, hash, buildTime string) *MetricsManager {
return &MetricsManager{
prometheusMetrics: NewMetrics(version, hash, buildTime),
}
}
// Main middleware method to collect metrics for Prometheus.
// Example:
// start := time.Now()
// next(rw, r)
// Request counter metric
// pmm.prometheusMetrics.Counter.WithLabelValues(r.URL.Path).Inc()
// Response time metric
// pmm.prometheusMetrics.ResponseTime.WithLabelValues(r.URL.Path).Observe(time.Since(start).Seconds())
func (pmm *MetricsManager) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
next(rw, r)
pmm.prometheusMetrics.ResponseTime.WithLabelValues(r.URL.Path).Observe(time.Since(start).Seconds())
}