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 1 commit
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
9 changes: 5 additions & 4 deletions logging/correlation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
type ctxKey string

const (
CorrelationIDName ctxKey = "correlation_id"
IntCorrelationIDName ctxKey = "int_correlation_id"
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
Expand All @@ -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)
}

Expand Down
2 changes: 2 additions & 0 deletions logging/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
0xmuralik marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

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

return l
Expand Down