From 539dab8680310ba4be738f44717de5a458d65879 Mon Sep 17 00:00:00 2001 From: Roman Loginov Date: Fri, 25 Oct 2024 04:39:59 +0300 Subject: [PATCH] [#501] Add the trace id to the logs of middlware Signed-off-by: Roman Loginov --- api/handler/util.go | 6 +----- api/middleware/reqinfo.go | 13 ++++++++++--- api/middleware/response.go | 5 ----- api/middleware/tracing.go | 14 -------------- api/router.go | 1 - 5 files changed, 11 insertions(+), 28 deletions(-) diff --git a/api/handler/util.go b/api/handler/util.go index abc8acb..625b1b0 100644 --- a/api/handler/util.go +++ b/api/handler/util.go @@ -15,7 +15,6 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs" cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" - "go.opentelemetry.io/otel/trace" "go.uber.org/zap" ) @@ -42,10 +41,7 @@ func (h *handler) logAndSendError(ctx context.Context, w http.ResponseWriter, lo zap.String("user", reqInfo.User), zap.Error(err)} fields = append(fields, additional...) - if traceID, err := trace.TraceIDFromHex(reqInfo.TraceID); err == nil && traceID.IsValid() { - fields = append(fields, zap.String("trace_id", reqInfo.TraceID)) - } - h.reqLogger(ctx).Error(logs.RequestFailed, fields...) // consider using h.reqLogger (it requires accept context.Context or http.Request) + h.reqLogger(ctx).Error(logs.RequestFailed, fields...) } func handleDeleteMarker(w http.ResponseWriter, err error) error { diff --git a/api/middleware/reqinfo.go b/api/middleware/reqinfo.go index 15a22af..9d3cdef 100644 --- a/api/middleware/reqinfo.go +++ b/api/middleware/reqinfo.go @@ -187,14 +187,21 @@ func Request(log *zap.Logger, settings RequestSettings) Func { r = r.WithContext(treepool.SetRequestID(r.Context(), reqInfo.RequestID)) - reqLogger := log.With(zap.String("request_id", reqInfo.RequestID)) - r = r.WithContext(SetReqLogger(r.Context(), reqLogger)) + fields := []zap.Field{zap.String("request_id", reqInfo.RequestID)} + ctx, span := StartHTTPServerSpan(r, "REQUEST S3") + if traceID := span.SpanContext().TraceID(); traceID.IsValid() { + fields = append(fields, zap.String("trace_id", traceID.String())) + } + lw := &traceResponseWriter{ResponseWriter: w, ctx: ctx, span: span} + + reqLogger := log.With(fields...) + r = r.WithContext(SetReqLogger(ctx, reqLogger)) reqLogger.Info(logs.RequestStart, zap.String("host", r.Host), zap.String("remote_host", reqInfo.RemoteHost), zap.String("namespace", reqInfo.Namespace)) // continue execution - h.ServeHTTP(w, r) + h.ServeHTTP(lw, r) }) } } diff --git a/api/middleware/response.go b/api/middleware/response.go index 94b633d..e86151f 100644 --- a/api/middleware/response.go +++ b/api/middleware/response.go @@ -11,7 +11,6 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/version" - "go.opentelemetry.io/otel/trace" "go.uber.org/zap" ) @@ -332,10 +331,6 @@ func LogSuccessResponse(l *zap.Logger) Func { fields = append(fields, zap.String("user", reqInfo.User)) } - if traceID, err := trace.TraceIDFromHex(reqInfo.TraceID); err == nil && traceID.IsValid() { - fields = append(fields, zap.String("trace_id", reqInfo.TraceID)) - } - reqLogger.Info(logs.RequestEnd, fields...) }) } diff --git a/api/middleware/tracing.go b/api/middleware/tracing.go index 7597700..bae847b 100644 --- a/api/middleware/tracing.go +++ b/api/middleware/tracing.go @@ -11,20 +11,6 @@ import ( "go.opentelemetry.io/otel/trace" ) -// Tracing adds tracing support for requests. -// Must be placed after prepareRequest middleware. -func Tracing() Func { - return func(h http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - appCtx, span := StartHTTPServerSpan(r, "REQUEST S3") - reqInfo := GetReqInfo(r.Context()) - reqInfo.TraceID = span.SpanContext().TraceID().String() - lw := &traceResponseWriter{ResponseWriter: w, ctx: appCtx, span: span} - h.ServeHTTP(lw, r.WithContext(appCtx)) - }) - } -} - type traceResponseWriter struct { sync.Once http.ResponseWriter diff --git a/api/router.go b/api/router.go index ec8c27b..765bec3 100644 --- a/api/router.go +++ b/api/router.go @@ -134,7 +134,6 @@ func NewRouter(cfg Config) *chi.Mux { s3middleware.Request(cfg.Log, cfg.MiddlewareSettings), middleware.ThrottleWithOpts(cfg.Throttle), middleware.Recoverer, - s3middleware.Tracing(), s3middleware.Metrics(cfg.Log, cfg.Handler.ResolveCID, cfg.Metrics, cfg.MiddlewareSettings), s3middleware.LogSuccessResponse(cfg.Log), s3middleware.Auth(cfg.Center, cfg.Log),