Add request_duration_seconds metric

This commit is contained in:
Daan Sprenkels 2021-05-16 20:24:00 +02:00
parent a26894dac8
commit c4ff0ab1b7
2 changed files with 28 additions and 1 deletions

View File

@ -17,7 +17,7 @@ const metricNamespace = "rushlink"
// metricURLsTotalGauge counts the number of requests that are handled by // metricURLsTotalGauge counts the number of requests that are handled by
// the application, partitioned by status code and HTTP method. // the application, partitioned by status code and HTTP method.
// //
// This counter is updated by throughout the application. // This counter is updated by the router.
var metricRequestsTotalCounter = prometheus.NewCounterVec( var metricRequestsTotalCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Namespace: metricNamespace, Namespace: metricNamespace,
@ -26,6 +26,27 @@ var metricRequestsTotalCounter = prometheus.NewCounterVec(
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.", Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
}, []string{"code", "method"}) }, []string{"code", "method"})
// metricRequestsLatencyNanoSeconds keeps track of the request latencies for
// each http request.
//
// This historogram is updated by the router.
var metricRequestsLatencyNanoSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: metricNamespace,
Subsystem: "http",
Name: "request_duration_seconds",
Buckets: []float64{
float64(500e-6),
float64(1e-3),
float64(2e-3),
float64(5e-3),
float64(10e-3),
float64(20e-3),
float64(50e-3),
},
Help: "The latency of each HTTP request, partitioned by status code and HTTP method.",
}, []string{"code", "method"})
// metricURLsTotalGauge measures the amount of pastes stored in the database, // metricURLsTotalGauge measures the amount of pastes stored in the database,
// partitioned by type and state. // partitioned by type and state.
// //
@ -41,6 +62,7 @@ var metricURLsTotalGauge = prometheus.NewGaugeVec(
// StartMetricsServer starts sering Prometheus metrics exports on addr // StartMetricsServer starts sering Prometheus metrics exports on addr
func StartMetricsServer(addr string, database *db.Database, fs *db.FileStore) { func StartMetricsServer(addr string, database *db.Database, fs *db.FileStore) {
prometheus.MustRegister(metricRequestsTotalCounter) prometheus.MustRegister(metricRequestsTotalCounter)
prometheus.MustRegister(metricRequestsLatencyNanoSeconds)
prometheus.MustRegister(metricURLsTotalGauge) prometheus.MustRegister(metricURLsTotalGauge)
router := mux.NewRouter() router := mux.NewRouter()

View File

@ -58,13 +58,18 @@ func (rl *rushlink) recoveryMiddleware(next http.Handler) http.Handler {
func (rl *rushlink) metricsMiddleware(next http.Handler) http.Handler { func (rl *rushlink) metricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tick := time.Now()
srw := statusResponseWriter{Inner: w} srw := statusResponseWriter{Inner: w}
next.ServeHTTP(&srw, r) next.ServeHTTP(&srw, r)
tock := time.Now()
status := strconv.Itoa(srw.StatusCode) status := strconv.Itoa(srw.StatusCode)
labels := map[string]string{"code": status, "method": r.Method} labels := map[string]string{"code": status, "method": r.Method}
// Update requests counter metric // Update requests counter metric
metricRequestsTotalCounter.With(labels).Inc() metricRequestsTotalCounter.With(labels).Inc()
// Update request latency metric
elapsed := tock.Sub(tick)
metricRequestsLatencyNanoSeconds.With(labels).Observe(elapsed.Seconds())
}) })
} }