Improved logging and metrics. #227

Merged
telackey merged 11 commits from telackey/logging into v4 2023-01-21 01:39:26 +00:00
Showing only changes of commit 1986fee751 - Show all commits

View File

@ -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())
}
}) })
} }