feat: tracing improvements (#4988)

This commit is contained in:
Dean Sheather
2022-11-28 13:22:10 -08:00
committed by GitHub
parent d402914eb7
commit 15f8967a8a
10 changed files with 342 additions and 34 deletions

View File

@ -1,6 +1,8 @@
package httpmw
import (
"context"
"fmt"
"net/http"
"time"
@ -11,9 +13,13 @@ import (
func Logger(log slog.Logger) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
start := time.Now()
sw := &tracing.StatusWriter{ResponseWriter: w}
sw, ok := rw.(*tracing.StatusWriter)
if !ok {
panic(fmt.Sprintf("ResponseWriter not a *tracing.StatusWriter; got %T", rw))
}
httplog := log.With(
slog.F("host", httpapi.RequestHost(r)),
@ -51,7 +57,11 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
logLevelFn = httplog.Warn
}
logLevelFn(r.Context(), r.Method)
// We already capture most of this information in the span (minus
// the response body which we don't want to capture anyways).
tracing.RunWithoutSpan(r.Context(), func(ctx context.Context) {
logLevelFn(ctx, r.Method)
})
})
}
}