mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-08-23 22:26:29 +00:00
Merge pull request #388 from hansbogert/master
feat(logging): add trace_id to http request debug logs
This commit is contained in:
+12
-2
@@ -3,6 +3,7 @@ package http
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -18,13 +19,22 @@ func NewLoggingMiddleware(logger *zap.Logger) *LoggingMiddleware {
|
||||
|
||||
func (m *LoggingMiddleware) Handler(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
m.logger.Debug(
|
||||
"request started",
|
||||
fields := []zap.Field{
|
||||
zap.String("proto", r.Proto),
|
||||
zap.String("uri", r.RequestURI),
|
||||
zap.String("method", r.Method),
|
||||
zap.String("remote", r.RemoteAddr),
|
||||
zap.String("user-agent", r.UserAgent()),
|
||||
}
|
||||
|
||||
spanCtx := trace.SpanContextFromContext(r.Context())
|
||||
if spanCtx.HasTraceID() {
|
||||
fields = append(fields, zap.String("trace_id", spanCtx.TraceID().String()))
|
||||
}
|
||||
|
||||
m.logger.Debug(
|
||||
"request started",
|
||||
fields...,
|
||||
)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user