This commit is contained in:
lbenedar
2026-03-24 16:58:19 +03:00
parent 8b270bbc0e
commit bb8922f429
2 changed files with 21 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"errors"
"expvar"
"fmt"
"net"
"net/http"
@@ -199,3 +200,22 @@ func (app *application) enableCORS(next http.Handler) http.Handler {
next.ServeHTTP(w, r)
})
}
func (app *application) metrics(next http.Handler) http.Handler {
var (
totalRequestsReceived = expvar.NewInt("total_requests_received")
totalResponseSent = expvar.NewInt("total_responses_sent")
totalProcessingTimeMicroseconds = expvar.NewInt("total_processing_time_μs")
)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
totalRequestsReceived.Add(1)
next.ServeHTTP(w, r)
totalResponseSent.Add(1)
duration := time.Since(start).Microseconds()
totalProcessingTimeMicroseconds.Add(duration)
})
}

View File

@@ -28,5 +28,5 @@ func (app *application) routes() http.Handler {
router.Handler(http.MethodGet, "/debug/vars", expvar.Handler())
return app.recoverPanic(app.enableCORS(app.rateLimit(app.authenticate(router))))
return app.metrics(app.recoverPanic(app.enableCORS(app.rateLimit(app.authenticate(router)))))
}