From df1ac3d923d1a5fe489ffcba584bc477437140a4 Mon Sep 17 00:00:00 2001 From: Mayank Kumar Date: Tue, 5 Jun 2018 21:12:30 +0530 Subject: [PATCH] requestid middeware: add Requst-Id to logger (#51) - add Request-Id as key value pair additional fields to be used by logger. This value will then be used while logging each requests with Request-Id parameter. --- requestid/interceptor.go | 2 ++ requestid/requestid.go | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/requestid/interceptor.go b/requestid/interceptor.go index 395ce23b..bb389eab 100644 --- a/requestid/interceptor.go +++ b/requestid/interceptor.go @@ -28,6 +28,8 @@ func UnaryServerInterceptor() grpc.UnaryServerInterceptor { }() reqID := handleRequestID(ctx) + // add request id to logger + addRequestIDToLogger(ctx, reqID) ctx = NewContext(ctx, reqID) diff --git a/requestid/requestid.go b/requestid/requestid.go index e577ef4f..9fec8d47 100644 --- a/requestid/requestid.go +++ b/requestid/requestid.go @@ -4,7 +4,9 @@ import ( "context" "github.com/google/uuid" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" "github.com/infobloxopen/atlas-app-toolkit/gateway" + "github.com/sirupsen/logrus" "google.golang.org/grpc/metadata" ) @@ -41,3 +43,7 @@ func NewContext(ctx context.Context, reqID string) context.Context { md := metadata.Pairs(DefaultRequestIDKey, reqID) return metadata.NewOutgoingContext(ctx, md) } + +func addRequestIDToLogger(ctx context.Context, reqID string) { + ctxlogrus.AddFields(ctx, logrus.Fields{DefaultRequestIDKey: reqID}) +}