Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add correlation Ids #25

Merged
merged 6 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
48 changes: 48 additions & 0 deletions logging/correlation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package logging

import (
"context"

"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
"go.uber.org/zap"
)

type ctxKey string

const (
CorrelationIDName ctxKey = "correlation_id"
0xmuralik marked this conversation as resolved.
Show resolved Hide resolved
IntCorrelationIDName ctxKey = "int_correlation_id"
ExternalCorrelationIDName string = "X-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(ExternalCorrelationIDName)

if correlationID != "" {
ctx = context.WithValue(ctx, CorrelationIDName, correlationID)
c.Writer.Header().Set(ExternalCorrelationIDName, correlationID)
l = l.With(CorrelationIDName, correlationID)
}

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)

c.Set("logger", l)

c.Request = c.Request.WithContext(ctx)
0xmuralik marked this conversation as resolved.
Show resolved Hide resolved

c.Next()
}
18 changes: 17 additions & 1 deletion logging/rest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logging

import (
"fmt"
"time"

"github.com/gin-gonic/gin"
Expand All @@ -17,6 +18,9 @@ func LogRequest(logger *zap.Logger) gin.HandlerFunc {

func log(c *gin.Context) {
start := time.Now()

c.Next()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move c.Next() at the beginning of the function?

Copy link
Contributor Author

@0xmuralik 0xmuralik Feb 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even incase of an error, the "status" field in log info says 200. This is because we are setting this before we execute the endpoint logic function. So moved c.Next() to the first line so that the we get the correct status after the endpoint logic function is executed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gsora
You can view in the image attached here

If the logging middleware is not executed last, it cannot pick any errors that have happened in the meantime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe start := time.Now() should be above c.Next() though


// some evil middlewares modify this values
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
Expand All @@ -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, error) {
value, ok := c.Get("logger")
if !ok {
return nil, fmt.Errorf("logger does not exists in context")
}

l, ok := value.(*zap.SugaredLogger)
if !ok {
0xmuralik marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("invalid logger format in context")
}

return l, nil
}