Skip to content

Commit

Permalink
requestid middeware: add Requst-Id to logger (#51)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
kumaya authored and johnbelamaric committed Jun 5, 2018
1 parent cde266f commit df1ac3d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions requestid/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
}()

reqID := handleRequestID(ctx)
// add request id to logger
addRequestIDToLogger(ctx, reqID)

ctx = NewContext(ctx, reqID)

Expand Down
6 changes: 6 additions & 0 deletions requestid/requestid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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})
}

0 comments on commit df1ac3d

Please sign in to comment.