diff --git a/go.mod b/go.mod index 688be67..cd66291 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/google/uuid v1.3.0 github.com/pelletier/go-toml v1.2.0 // indirect github.com/prometheus/client_golang v1.12.1 + github.com/sirupsen/logrus v1.8.1 go.uber.org/zap v1.21.0 golang.org/x/net v0.0.0-20220420153159-1850ba15e1be golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect diff --git a/go.sum b/go.sum index 8983b09..1723e3f 100644 --- a/go.sum +++ b/go.sum @@ -203,6 +203,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= @@ -326,6 +328,7 @@ golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/xrequestid/README.md b/xrequestid/README.md index 33ea25d..8344ed8 100644 --- a/xrequestid/README.md +++ b/xrequestid/README.md @@ -8,6 +8,7 @@ xrequestid is an grpc interceptor which receives request id from metadata and se - Fixed the Compilation errors due to the depricated `metadata.FromContext` function - Added a `go.mod` and replaced imports with the go mod equivilent - Add an option `persistRequestID` if you would like to add the request ID to the outgoing context +- Add interceptor option to Log the incoming request along with the Request ID (logrus package) ## Usage diff --git a/xrequestid/handler.go b/xrequestid/handler.go index d6d0838..d25b242 100644 --- a/xrequestid/handler.go +++ b/xrequestid/handler.go @@ -28,6 +28,9 @@ func UnaryServerInterceptor(opt ...Option) grpc.UnaryServerInterceptor { if opts.persistRequestID { ctx = metadata.AppendToOutgoingContext(ctx, DefaultXRequestIDKey, requestID) } + if opts.logRequest { + logRequestWithID(req, requestID, info.FullMethod) + } ctx = context.WithValue(ctx, requestIDKey{}, requestID) return handler(ctx, req) } @@ -51,6 +54,9 @@ func StreamServerInterceptor(opt ...Option) grpc.StreamServerInterceptor { if opts.persistRequestID { ctx = metadata.AppendToOutgoingContext(ctx, DefaultXRequestIDKey, requestID) } + if opts.logRequest { + logRequestWithID("Contains StreamData", requestID, info.FullMethod) + } ctx = context.WithValue(ctx, requestIDKey{}, requestID) stream = multiint.NewServerStreamWithContext(stream, ctx) return handler(srv, stream) diff --git a/xrequestid/option.go b/xrequestid/option.go index c5f059d..ea01f15 100644 --- a/xrequestid/option.go +++ b/xrequestid/option.go @@ -12,7 +12,8 @@ func (a optionApplyer) apply(opt *options) { type options struct { chainRequestID bool - persistRequestID bool // if true, attach request id to outgoing context + persistRequestID bool + logRequest bool validator requestIDValidator } @@ -22,12 +23,20 @@ func ChainRequestID() Option { }) } +// Attach the request id to the outgoing context func PersistRequestID() Option { return optionApplyer(func(opt *options) { opt.persistRequestID = true }) } +// Logs the incoming request with the request id and the method destination +func LogRequest() Option { + return optionApplyer(func(opt *options) { + opt.logRequest = true + }) +} + type requestIDValidator func(string) bool // RequestIDValidator is validator function that returns true if diff --git a/xrequestid/requestid.go b/xrequestid/requestid.go index 317b3d2..0340c82 100644 --- a/xrequestid/requestid.go +++ b/xrequestid/requestid.go @@ -2,8 +2,10 @@ package xrequestid import ( "fmt" + "strings" "github.com/google/uuid" + "github.com/sirupsen/logrus" "golang.org/x/net/context" "google.golang.org/grpc/metadata" ) @@ -60,3 +62,23 @@ func HandleRequestIDChain(ctx context.Context, validator requestIDValidator) str func newRequestID() string { return uuid.NewString() } + +// Logs the incoming request with the new request id. full.const +// FullMethod is the full RPC method string, i.e., /package.service/method. +func logRequestWithID(requestData interface{}, requestID, fullMethod string) { + methodPath := strings.Split(fullMethod, "/") + if len(methodPath) > 2 { + logrus.WithFields(logrus.Fields{ + "Request Data": fmt.Sprintf("%+v", requestData), + "Request ID": requestID, + "Package.Service": methodPath[1], + "Method Name": methodPath[2], + }).Infof("Request ID appended to request") + } else { + logrus.WithFields(logrus.Fields{ + "Request Data": fmt.Sprintf("%+v", requestData), + "Request ID": requestID, + "Full Method Path": fullMethod, + }).Infof("Request ID appended to request") + } +}