From 4d6b61e6bb79f46feeec271826847fa6eb1cce8b Mon Sep 17 00:00:00 2001 From: SpideyPool192 Date: Wed, 2 Feb 2022 12:08:43 +0530 Subject: [PATCH 1/6] fix logger info status and move correlation middleware and getLoggerFromContext from api-server --- go.mod | 1 + go.sum | 3 ++- logging/correlation.go | 44 ++++++++++++++++++++++++++++++++++++++++++ logging/rest.go | 18 ++++++++++++++++- 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 logging/correlation.go diff --git a/go.mod b/go.mod index d0472c5..7a70409 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/go-playground/universal-translator v0.18.0 github.com/go-playground/validator/v10 v10.9.0 github.com/go-redis/redis/v8 v8.8.3 + github.com/gofrs/uuid v4.2.0+incompatible github.com/iamolegga/enviper v1.2.1 github.com/jackc/pgx/v4 v4.10.1 github.com/jmoiron/sqlx v1.3.3 diff --git a/go.sum b/go.sum index b5f2971..fdf5c3f 100644 --- a/go.sum +++ b/go.sum @@ -545,8 +545,9 @@ github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+ github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= -github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84= github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= +github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/gateway v1.1.0 h1:u0SuhL9+Il+UbjM9VIE3ntfRujKbvVpFvNB4HbjeVQ0= github.com/gogo/gateway v1.1.0/go.mod h1:S7rR8FRQyG3QFESeSv4l2WnsyzlCLG0CzBbUUo/mbic= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= diff --git a/logging/correlation.go b/logging/correlation.go new file mode 100644 index 0000000..ae86d47 --- /dev/null +++ b/logging/correlation.go @@ -0,0 +1,44 @@ +package logging + +import ( + "context" + + "github.com/gin-gonic/gin" + "github.com/gofrs/uuid" + "go.uber.org/zap" +) + +const ( + CorrelationIDName = "correlation_id" + IntCorrelationIDName = "int_correlation_id" +) + +// CorrelationIDMiddleware adds correlationID if it's not specified in HTTP request +func CorrelationIDMiddleware(l *zap.SugaredLogger) gin.HandlerFunc { + return func(c *gin.Context) { + addCorrelationID(c, l) + } +} + +func addCorrelationID(c *gin.Context, l *zap.SugaredLogger) { + ctx := c.Request.Context() + + correlationID := c.Request.Header.Get("X-Correlation-id") + + if correlationID != "" { + ctx = context.WithValue(ctx, CorrelationIDName, correlationID) + c.Writer.Header().Set("X-Correlation-Id", correlationID) + l = l.With(CorrelationIDName, correlationID) + } + + id, _ := uuid.NewV4() + + ctx = context.WithValue(ctx, IntCorrelationIDName, id.String()) + l = l.With(IntCorrelationIDName, id) + + c.Set("logger", l) + + c.Request = c.Request.WithContext(ctx) + + c.Next() +} diff --git a/logging/rest.go b/logging/rest.go index 2262e49..a916b9c 100644 --- a/logging/rest.go +++ b/logging/rest.go @@ -1,6 +1,7 @@ package logging import ( + "net/http" "time" "github.com/gin-gonic/gin" @@ -16,6 +17,9 @@ func LogRequest(logger *zap.Logger) gin.HandlerFunc { } func log(c *gin.Context) { + + c.Next() + start := time.Now() // some evil middlewares modify this values path := c.Request.URL.Path @@ -30,6 +34,18 @@ func log(c *gin.Context) { zap.String("user-agent", c.Request.UserAgent()), zap.String("time", start.Format(time.RFC3339)), ) +} - c.Next() +func GetLoggerFromContext(c *gin.Context) *zap.SugaredLogger { + value, ok := c.Get("logger") + if !ok { + c.AbortWithStatusJSON(http.StatusInternalServerError, "logger does not exists in context") + } + + l, ok := value.(*zap.SugaredLogger) + if !ok { + c.AbortWithStatusJSON(http.StatusInternalServerError, "invalid logger format in context") + } + + return l } From 2292183a137ad5a67c173c5a4c1c66ee8df1ad93 Mon Sep 17 00:00:00 2001 From: SpideyPool192 Date: Wed, 2 Feb 2022 12:30:41 +0530 Subject: [PATCH 2/6] fix lint issue --- logging/correlation.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/logging/correlation.go b/logging/correlation.go index ae86d47..40562d4 100644 --- a/logging/correlation.go +++ b/logging/correlation.go @@ -8,9 +8,11 @@ import ( "go.uber.org/zap" ) +type ctxKey string + const ( - CorrelationIDName = "correlation_id" - IntCorrelationIDName = "int_correlation_id" + CorrelationIDName ctxKey = "correlation_id" + IntCorrelationIDName ctxKey = "int_correlation_id" ) // CorrelationIDMiddleware adds correlationID if it's not specified in HTTP request From 987a6db299c2a8027585b2582d5f1be76159f442 Mon Sep 17 00:00:00 2001 From: SpideyPool192 Date: Thu, 3 Feb 2022 10:30:20 +0530 Subject: [PATCH 3/6] resolve --- logging/correlation.go | 9 +++++---- logging/rest.go | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/logging/correlation.go b/logging/correlation.go index 40562d4..21b25e9 100644 --- a/logging/correlation.go +++ b/logging/correlation.go @@ -11,8 +11,9 @@ import ( type ctxKey string const ( - CorrelationIDName ctxKey = "correlation_id" - IntCorrelationIDName ctxKey = "int_correlation_id" + CorrelationIDName ctxKey = "correlation_id" + IntCorrelationIDName ctxKey = "int_correlation_id" + ExternalCorrelationIDName string = "X-Correlation-Id" ) // CorrelationIDMiddleware adds correlationID if it's not specified in HTTP request @@ -25,11 +26,11 @@ func CorrelationIDMiddleware(l *zap.SugaredLogger) gin.HandlerFunc { func addCorrelationID(c *gin.Context, l *zap.SugaredLogger) { ctx := c.Request.Context() - correlationID := c.Request.Header.Get("X-Correlation-id") + correlationID := c.Request.Header.Get(ExternalCorrelationIDName) if correlationID != "" { ctx = context.WithValue(ctx, CorrelationIDName, correlationID) - c.Writer.Header().Set("X-Correlation-Id", correlationID) + c.Writer.Header().Set(ExternalCorrelationIDName, correlationID) l = l.With(CorrelationIDName, correlationID) } diff --git a/logging/rest.go b/logging/rest.go index a916b9c..5780bda 100644 --- a/logging/rest.go +++ b/logging/rest.go @@ -40,11 +40,13 @@ func GetLoggerFromContext(c *gin.Context) *zap.SugaredLogger { value, ok := c.Get("logger") if !ok { c.AbortWithStatusJSON(http.StatusInternalServerError, "logger does not exists in context") + return nil } l, ok := value.(*zap.SugaredLogger) if !ok { c.AbortWithStatusJSON(http.StatusInternalServerError, "invalid logger format in context") + return nil } return l From ecb9ab4dfc5afe3d2afc0c7266cc627f63287a16 Mon Sep 17 00:00:00 2001 From: SpideyPool192 Date: Thu, 3 Feb 2022 14:16:18 +0530 Subject: [PATCH 4/6] resolve --- logging/rest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logging/rest.go b/logging/rest.go index 5780bda..f25b58b 100644 --- a/logging/rest.go +++ b/logging/rest.go @@ -17,10 +17,10 @@ func LogRequest(logger *zap.Logger) gin.HandlerFunc { } func log(c *gin.Context) { + start := time.Now() c.Next() - start := time.Now() // some evil middlewares modify this values path := c.Request.URL.Path query := c.Request.URL.RawQuery From fdf8e53de63f032c56460f71b8cfc172c24d77e0 Mon Sep 17 00:00:00 2001 From: SpideyPool192 Date: Thu, 3 Feb 2022 16:07:48 +0530 Subject: [PATCH 5/6] resolve --- logging/correlation.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/logging/correlation.go b/logging/correlation.go index 21b25e9..f13e556 100644 --- a/logging/correlation.go +++ b/logging/correlation.go @@ -34,7 +34,8 @@ func addCorrelationID(c *gin.Context, l *zap.SugaredLogger) { l = l.With(CorrelationIDName, correlationID) } - id, _ := uuid.NewV4() + id, err := uuid.NewV4() + l.Errorf("Error while creating new internal correlation id error: %w", err) ctx = context.WithValue(ctx, IntCorrelationIDName, id.String()) l = l.With(IntCorrelationIDName, id) From 648d96fcdceb8de610b9e0ea70cb724c6e612ecb Mon Sep 17 00:00:00 2001 From: SpideyPool192 Date: Mon, 7 Feb 2022 15:19:32 +0530 Subject: [PATCH 6/6] add errors in getlogger --- logging/rest.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/logging/rest.go b/logging/rest.go index f25b58b..51d7f09 100644 --- a/logging/rest.go +++ b/logging/rest.go @@ -1,7 +1,7 @@ package logging import ( - "net/http" + "fmt" "time" "github.com/gin-gonic/gin" @@ -36,18 +36,16 @@ func log(c *gin.Context) { ) } -func GetLoggerFromContext(c *gin.Context) *zap.SugaredLogger { +func GetLoggerFromContext(c *gin.Context) (*zap.SugaredLogger, error) { value, ok := c.Get("logger") if !ok { - c.AbortWithStatusJSON(http.StatusInternalServerError, "logger does not exists in context") - return nil + return nil, fmt.Errorf("logger does not exists in context") } l, ok := value.(*zap.SugaredLogger) if !ok { - c.AbortWithStatusJSON(http.StatusInternalServerError, "invalid logger format in context") - return nil + return nil, fmt.Errorf("invalid logger format in context") } - return l + return l, nil }