Skip to content

Commit

Permalink
Merge pull request #3 from eltorocorp/RequestLogger
Browse files Browse the repository at this point in the history
add logging option for requestID
  • Loading branch information
dsegundo2 authored Apr 25, 2022
2 parents a939f51 + decc32a commit 4e83132
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions xrequestid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions xrequestid/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion xrequestid/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

0 comments on commit 4e83132

Please sign in to comment.