Improved logging and metrics. #227
@ -96,10 +96,6 @@ func prepareRequest(r *http.Request) (*http.Request, error) {
|
|||||||
|
|
||||||
// HTTPMiddleware http connection metric reader
|
// HTTPMiddleware http connection metric reader
|
||||||
func HTTPMiddleware(next http.Handler) http.Handler {
|
func HTTPMiddleware(next http.Handler) http.Handler {
|
||||||
if !metrics {
|
|
||||||
return next
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
r, err := prepareRequest(r)
|
r, err := prepareRequest(r)
|
||||||
@ -109,14 +105,19 @@ func HTTPMiddleware(next http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
apiMethod := fmt.Sprintf("%s", ctx.Value(log.CtxKeyApiMethod))
|
apiMethod := fmt.Sprintf("%s", ctx.Value(log.CtxKeyApiMethod))
|
||||||
|
|
||||||
|
if metrics {
|
||||||
httpCount.WithLabelValues(apiMethod).Inc()
|
httpCount.WithLabelValues(apiMethod).Inc()
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugx(ctx, "START")
|
log.Debugx(ctx, "START")
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
|
||||||
duration := time.Now().Sub(start)
|
duration := time.Now().Sub(start)
|
||||||
log.Debugxf(context.WithValue(ctx, log.CtxKeyDuration, duration.Milliseconds()), "END")
|
log.Debugxf(context.WithValue(ctx, log.CtxKeyDuration, duration.Milliseconds()), "END")
|
||||||
|
|
||||||
|
if metrics {
|
||||||
httpDuration.WithLabelValues(apiMethod).Observe(duration.Seconds())
|
httpDuration.WithLabelValues(apiMethod).Observe(duration.Seconds())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user