123456789101112131415161718192021222324 |
- package handler
- import (
- "net/http"
- "git.i2edu.net/i2/go-zero/core/stat"
- "git.i2edu.net/i2/go-zero/core/timex"
- )
- // MetricHandler returns a middleware that stat the metrics.
- func MetricHandler(metrics *stat.Metrics) func(http.Handler) http.Handler {
- return func(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- startTime := timex.Now()
- defer func() {
- metrics.Add(stat.Task{
- Duration: timex.Since(startTime),
- })
- }()
- next.ServeHTTP(w, r)
- })
- }
- }
|