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 error param to the decider method of logging middleware #372

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions interceptors/logging/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *reporter) logMessage(logger Logger, err error, msg string, duration tim
}

func (c *reporter) PostCall(err error, duration time.Duration) {
switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method)) {
switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method), err) {
case LogFinishCall, LogStartAndFinishCall:
if err == io.EOF {
err = nil
Expand All @@ -57,7 +57,7 @@ func (c *reporter) PostMsgSend(_ interface{}, err error, duration time.Duration)
if c.startCallLogged {
return
}
switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method)) {
switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method), err) {
case LogStartAndFinishCall:
c.startCallLogged = true
c.logMessage(c.logger, err, "started call", duration)
Expand All @@ -68,7 +68,7 @@ func (c *reporter) PostMsgReceive(_ interface{}, err error, duration time.Durati
if c.startCallLogged {
return
}
switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method)) {
switch c.opts.shouldLog(interceptors.FullMethod(c.service, c.method), err) {
case LogStartAndFinishCall:
c.startCallLogged = true
c.logMessage(c.logger, err, "started call", duration)
Expand Down
2 changes: 1 addition & 1 deletion interceptors/logging/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func TestCustomDeciderSuite(t *testing.T) {
t.Skip("Skipping due to json.RawMessage incompatibility with go1.7")
return
}
opts := logging.WithDecider(func(method string) logging.Decision {
opts := logging.WithDecider(func(method string, _ error) logging.Decision {
if method == "/grpc_middleware.testpb.TestService/PingError" {
return logging.LogStartAndFinishCall
}
Expand Down
4 changes: 2 additions & 2 deletions interceptors/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func DefaultErrorToCode(err error) codes.Code {
}

// Decider function defines rules for suppressing any interceptor logs
type Decider func(fullMethodName string) Decision
type Decider func(fullMethodName string, err error) Decision

// DefaultDeciderMethod is the default implementation of decider to see if you should log the call
// by default this if always true so all calls are logged
func DefaultDeciderMethod(_ string) Decision {
func DefaultDeciderMethod(_ string, _ error) Decision {
return LogStartAndFinishCall
}

Expand Down