ch19.4
This commit is contained in:
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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)))))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user