Skip to content

Commit

Permalink
Add option to suppress interceptor logs (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
domgreen authored and devnev committed Nov 30, 2017
1 parent 0e9bafc commit 228a0b0
Show file tree
Hide file tree
Showing 15 changed files with 386 additions and 72 deletions.
20 changes: 10 additions & 10 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 1 addition & 28 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/gogo/protobuf"
version = "0.5.0"
Expand All @@ -29,10 +6,6 @@
branch = "master"
name = "github.com/golang/protobuf"

[[constraint]]
branch = "master"
name = "github.com/grpc-ecosystem/go-grpc-middleware"

[[constraint]]
name = "github.com/opentracing/opentracing-go"
version = "1.0.2"
Expand All @@ -59,4 +32,4 @@

[[constraint]]
name = "google.golang.org/grpc"
version = "1.7.3"
version = "1.8.0"
19 changes: 17 additions & 2 deletions logging/DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,49 @@ See relevant packages below.
- [google.golang.org/grpc/codes](https://godoc.org/google.golang.org/grpc/codes)

## <a name="pkg-index">Index</a>
* [func DefaultDeciderMethod(fullMethodName string, err error) bool](#DefaultDeciderMethod)
* [func DefaultErrorToCode(err error) codes.Code](#DefaultErrorToCode)
* [type ClientPayloadLoggingDecider](#ClientPayloadLoggingDecider)
* [type Decider](#Decider)
* [type ErrorToCode](#ErrorToCode)
* [type ServerPayloadLoggingDecider](#ServerPayloadLoggingDecider)

#### <a name="pkg-files">Package files</a>
[common.go](./common.go) [doc.go](./doc.go)

## <a name="DefaultDeciderMethod">func</a> [DefaultDeciderMethod](./common.go#L25)
``` go
func DefaultDeciderMethod(fullMethodName string, err error) bool
```
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

## <a name="DefaultErrorToCode">func</a> [DefaultErrorToCode](./common.go#L16)
``` go
func DefaultErrorToCode(err error) codes.Code
```

## <a name="ClientPayloadLoggingDecider">type</a> [ClientPayloadLoggingDecider](./common.go#L26)
## <a name="ClientPayloadLoggingDecider">type</a> [ClientPayloadLoggingDecider](./common.go#L35)
``` go
type ClientPayloadLoggingDecider func(ctx context.Context, fullMethodName string) bool
```
ClientPayloadLoggingDecider is a user-provided function for deciding whether to log the client-side
request/response payloads

## <a name="Decider">type</a> [Decider](./common.go#L21)
``` go
type Decider func(fullMethodName string, err error) bool
```
Decider function defines rules for suppressing any interceptor logs

## <a name="ErrorToCode">type</a> [ErrorToCode](./common.go#L14)
``` go
type ErrorToCode func(err error) codes.Code
```
ErrorToCode function determines the error code of an error
This makes using custom errors with grpc middleware easier

## <a name="ServerPayloadLoggingDecider">type</a> [ServerPayloadLoggingDecider](./common.go#L22)
## <a name="ServerPayloadLoggingDecider">type</a> [ServerPayloadLoggingDecider](./common.go#L31)
``` go
type ServerPayloadLoggingDecider func(ctx context.Context, fullMethodName string, servingObject interface{}) bool
```
Expand Down
9 changes: 9 additions & 0 deletions logging/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ func DefaultErrorToCode(err error) codes.Code {
return grpc.Code(err)
}

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

// 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(fullMethodName string, err error) bool {
return true
}

// ServerPayloadLoggingDecider is a user-provided function for deciding whether to log the server-side
// request/response payloads
type ServerPayloadLoggingDecider func(ctx context.Context, fullMethodName string, servingObject interface{}) bool
Expand Down
62 changes: 51 additions & 11 deletions logging/logrus/DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [Overview](#pkg-overview)
* [Imported Packages](#pkg-imports)
* [Index](#pkg-index)
* [Examples](#pkg-examples)

## <a name="pkg-overview">Overview</a>
`grpc_logrus` is a gRPC logging middleware backed by Logrus loggers
Expand Down Expand Up @@ -57,9 +58,13 @@ Please see examples and tests for examples of use.
* [type DurationToField](#DurationToField)
* [type Option](#Option)
* [func WithCodes(f grpc\_logging.ErrorToCode) Option](#WithCodes)
* [func WithDecider(f grpc\_logging.Decider) Option](#WithDecider)
* [func WithDurationField(f DurationToField) Option](#WithDurationField)
* [func WithLevels(f CodeToLevel) Option](#WithLevels)

#### <a name="pkg-examples">Examples</a>
* [WithDecider](#example_WithDecider)

#### <a name="pkg-files">Package files</a>
[client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go)

Expand Down Expand Up @@ -92,25 +97,25 @@ func AddFields(ctx context.Context, fields logrus.Fields)
AddFields adds logrus fields to the logger.
Deprecated: should use the ctx_logrus.Extract instead

## <a name="DefaultClientCodeToLevel">func</a> [DefaultClientCodeToLevel](./options.go#L120)
## <a name="DefaultClientCodeToLevel">func</a> [DefaultClientCodeToLevel](./options.go#L129)
``` go
func DefaultClientCodeToLevel(code codes.Code) logrus.Level
```
DefaultClientCodeToLevel is the default implementation of gRPC return codes to log levels for client side.

## <a name="DefaultCodeToLevel">func</a> [DefaultCodeToLevel](./options.go#L78)
## <a name="DefaultCodeToLevel">func</a> [DefaultCodeToLevel](./options.go#L87)
``` go
func DefaultCodeToLevel(code codes.Code) logrus.Level
```
DefaultCodeToLevel is the default implementation of gRPC return codes to log levels for server side.

## <a name="DurationToDurationField">func</a> [DurationToDurationField](./options.go#L170)
## <a name="DurationToDurationField">func</a> [DurationToDurationField](./options.go#L179)
``` go
func DurationToDurationField(duration time.Duration) (key string, value interface{})
```
DurationToDurationField uses the duration value to log the request duration.

## <a name="DurationToTimeMillisField">func</a> [DurationToTimeMillisField](./options.go#L165)
## <a name="DurationToTimeMillisField">func</a> [DurationToTimeMillisField](./options.go#L174)
``` go
func DurationToTimeMillisField(duration time.Duration) (key string, value interface{})
```
Expand Down Expand Up @@ -166,7 +171,7 @@ func StreamClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamCli
```
StreamServerInterceptor returns a new streaming client interceptor that optionally logs the execution of external gRPC calls.

## <a name="StreamServerInterceptor">func</a> [StreamServerInterceptor](./server_interceptors.go#L48)
## <a name="StreamServerInterceptor">func</a> [StreamServerInterceptor](./server_interceptors.go#L54)
``` go
func StreamServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamServerInterceptor
```
Expand All @@ -184,36 +189,71 @@ func UnaryServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryServe
```
PayloadUnaryServerInterceptor returns a new unary server interceptors that adds logrus.Entry to the context.

## <a name="CodeToLevel">type</a> [CodeToLevel](./options.go#L51)
## <a name="CodeToLevel">type</a> [CodeToLevel](./options.go#L53)
``` go
type CodeToLevel func(code codes.Code) logrus.Level
```
CodeToLevel function defines the mapping between gRPC return codes and interceptor log level.

## <a name="DurationToField">type</a> [DurationToField](./options.go#L54)
## <a name="DurationToField">type</a> [DurationToField](./options.go#L56)
``` go
type DurationToField func(duration time.Duration) (key string, value interface{})
```
DurationToField function defines how to produce duration fields for logging

## <a name="Option">type</a> [Option](./options.go#L48)
## <a name="Option">type</a> [Option](./options.go#L50)
``` go
type Option func(*options)
```

### <a name="WithCodes">func</a> [WithCodes](./options.go#L64)
### <a name="WithCodes">func</a> [WithCodes](./options.go#L73)
``` go
func WithCodes(f grpc_logging.ErrorToCode) Option
```
WithCodes customizes the function for mapping errors to error codes.

### <a name="WithDurationField">func</a> [WithDurationField](./options.go#L71)
### <a name="WithDecider">func</a> [WithDecider](./options.go#L59)
``` go
func WithDecider(f grpc_logging.Decider) Option
```
WithDecider customizes the function for deciding if the gRPC interceptor logs should log.

#### Example:

<details>
<summary>Click to expand code.</summary>

```go
opts := []grpc_logrus.Option{
grpc_logrus.WithDecider(func(methodFullName string, err error) bool {
// will not log gRPC calls if it was a call to healthcheck and no error was raised
if err == nil && methodFullName == "blah.foo.healthcheck" {
return false
}

// by default you will log all calls
return true
}),
}

_ = []grpc.ServerOption{
grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(),
grpc_logrus.StreamServerInterceptor(logrus.NewEntry(logrus.New()), opts...)),
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(),
grpc_logrus.UnaryServerInterceptor(logrus.NewEntry(logrus.New()), opts...)),
}
```

</details>
### <a name="WithDurationField">func</a> [WithDurationField](./options.go#L80)
``` go
func WithDurationField(f DurationToField) Option
```
WithDurationField customizes the function for mapping request durations to log fields.

### <a name="WithLevels">func</a> [WithLevels](./options.go#L57)
### <a name="WithLevels">func</a> [WithLevels](./options.go#L66)
``` go
func WithLevels(f CodeToLevel) Option
```
Expand Down
28 changes: 25 additions & 3 deletions logging/logrus/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package grpc_logrus_test
import (
"time"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/sirupsen/logrus"

"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -75,3 +74,26 @@ func Example_HandlerUsageUnaryPing() {
return &pb_testproto.PingResponse{Value: ping.Value}, nil
}
}

func ExampleWithDecider() {
opts := []grpc_logrus.Option{
grpc_logrus.WithDecider(func(methodFullName string, err error) bool {
// will not log gRPC calls if it was a call to healthcheck and no error was raised
if err == nil && methodFullName == "blah.foo.healthcheck" {
return false
}

// by default you will log all calls
return true
}),
}

_ = []grpc.ServerOption{
grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(),
grpc_logrus.StreamServerInterceptor(logrus.NewEntry(logrus.New()), opts...)),
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(),
grpc_logrus.UnaryServerInterceptor(logrus.NewEntry(logrus.New()), opts...)),
}
}
9 changes: 9 additions & 0 deletions logging/logrus/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
var (
defaultOptions = &options{
levelFunc: nil,
shouldLog: grpc_logging.DefaultDeciderMethod,
codeFunc: grpc_logging.DefaultErrorToCode,
durationFunc: DefaultDurationToField,
}
)

type options struct {
levelFunc CodeToLevel
shouldLog grpc_logging.Decider
codeFunc grpc_logging.ErrorToCode
durationFunc DurationToField
}
Expand Down Expand Up @@ -53,6 +55,13 @@ type CodeToLevel func(code codes.Code) logrus.Level
// DurationToField function defines how to produce duration fields for logging
type DurationToField func(duration time.Duration) (key string, value interface{})

// WithDecider customizes the function for deciding if the gRPC interceptor logs should log.
func WithDecider(f grpc_logging.Decider) Option {
return func(o *options) {
o.shouldLog = f
}
}

// WithLevels customizes the function for mapping gRPC return codes and interceptor log level statements.
func WithLevels(f CodeToLevel) Option {
return func(o *options) {
Expand Down
Loading

0 comments on commit 228a0b0

Please sign in to comment.