From d289d042982a2017f8cddf43e8bc1e98f7aefa2b Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Tue, 12 Nov 2024 10:31:30 +0100 Subject: [PATCH] fix: only log on ignore --- .golangci.yml | 2 ++ integration/watermill/gtag/publishermiddleware.go | 9 +++++++-- integration/watermill/gtag/subscribermiddleware.go | 4 ++++ integration/watermill/mpv2/publishermiddleware.go | 9 +++++++-- integration/watermill/mpv2/subscribermiddleware.go | 5 +++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 7514120..e64ec42 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -70,6 +70,8 @@ linters-settings: disabled: true - name: comment-spacings disabled: true + - name: max-public-structs + disabled: true - name: struct-tag arguments: - "json,inline" diff --git a/integration/watermill/gtag/publishermiddleware.go b/integration/watermill/gtag/publishermiddleware.go index 5d2afba..4a64a48 100644 --- a/integration/watermill/gtag/publishermiddleware.go +++ b/integration/watermill/gtag/publishermiddleware.go @@ -2,13 +2,18 @@ package gtag import ( "github.com/ThreeDotsLabs/watermill/message" + "go.opentelemetry.io/otel/trace" "go.uber.org/zap" ) func PublisherMiddlewareIgnoreError(next PublisherHandler) PublisherHandler { return func(l *zap.Logger, msg *message.Message) error { - err := next(l, msg) - l.With(zap.Error(err)).Warn("ignoring error") + if err := next(l, msg); err != nil { + if spanCtx := trace.SpanContextFromContext(msg.Context()); spanCtx.IsValid() && spanCtx.IsSampled() { + l = l.With(zap.String("trace_id", spanCtx.TraceID().String()), zap.String("span_id", spanCtx.SpanID().String())) + } + l.With(zap.Error(err)).Warn("ignoring error") + } return nil } } diff --git a/integration/watermill/gtag/subscribermiddleware.go b/integration/watermill/gtag/subscribermiddleware.go index 866840e..71fbc0f 100644 --- a/integration/watermill/gtag/subscribermiddleware.go +++ b/integration/watermill/gtag/subscribermiddleware.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/foomo/sesamy-go/pkg/encoding/gtag" + "go.opentelemetry.io/otel/trace" "go.uber.org/zap" ) @@ -20,6 +21,9 @@ func SubscriberMiddlewareUserID(cookieName string) SubscriberMiddleware { func SubscriberMiddlewareLogger(next SubscriberHandler) SubscriberHandler { return func(l *zap.Logger, r *http.Request, payload *gtag.Payload) error { + if spanCtx := trace.SpanContextFromContext(r.Context()); spanCtx.IsValid() && spanCtx.IsSampled() { + l = l.With(zap.String("trace_id", spanCtx.TraceID().String()), zap.String("span_id", spanCtx.SpanID().String())) + } l = l.With( zap.String("event_name", gtag.GetDefault(payload.EventName, "-").String()), zap.String("event_user_id", gtag.GetDefault(payload.UserID, "-")), diff --git a/integration/watermill/mpv2/publishermiddleware.go b/integration/watermill/mpv2/publishermiddleware.go index 79eae95..72f8de7 100644 --- a/integration/watermill/mpv2/publishermiddleware.go +++ b/integration/watermill/mpv2/publishermiddleware.go @@ -5,14 +5,19 @@ import ( "github.com/ThreeDotsLabs/watermill/message" "github.com/foomo/sesamy-go/pkg/encoding/mpv2" + "go.opentelemetry.io/otel/trace" "go.uber.org/zap" ) // PublisherMiddlewareIgnoreError ignores error responses from the gtm endpoint to prevent retries. func PublisherMiddlewareIgnoreError(next PublisherHandler) PublisherHandler { return func(l *zap.Logger, msg *message.Message) error { - err := next(l, msg) - l.With(zap.Error(err)).Warn("ignoring error") + if err := next(l, msg); err != nil { + if spanCtx := trace.SpanContextFromContext(msg.Context()); spanCtx.IsValid() && spanCtx.IsSampled() { + l = l.With(zap.String("trace_id", spanCtx.TraceID().String()), zap.String("span_id", spanCtx.SpanID().String())) + } + l.With(zap.Error(err)).Warn("ignoring error") + } return nil } } diff --git a/integration/watermill/mpv2/subscribermiddleware.go b/integration/watermill/mpv2/subscribermiddleware.go index 6267415..1e4c1da 100644 --- a/integration/watermill/mpv2/subscribermiddleware.go +++ b/integration/watermill/mpv2/subscribermiddleware.go @@ -8,6 +8,7 @@ import ( "github.com/foomo/sesamy-go/pkg/encoding/mpv2" "github.com/foomo/sesamy-go/pkg/session" "github.com/pkg/errors" + "go.opentelemetry.io/otel/trace" "go.uber.org/zap" ) @@ -80,6 +81,10 @@ func SubscriberMiddlewareLogger(next SubscriberHandler) SubscriberHandler { eventNames[i] = event.Name.String() } + if spanCtx := trace.SpanContextFromContext(r.Context()); spanCtx.IsValid() && spanCtx.IsSampled() { + l = l.With(zap.String("trace_id", spanCtx.TraceID().String()), zap.String("span_id", spanCtx.SpanID().String())) + } + l = l.With( zap.String("event_names", strings.Join(eventNames, ",")), zap.String("event_user_id", payload.UserID),