Skip to content

Commit

Permalink
add decider for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
linhbkhn95 committed Sep 22, 2023
1 parent 47ca7d6 commit e74468e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions interceptors/logging/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ func (c *reporter) PostMsgReceive(payload any, err error, duration time.Duration

func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc {
return func(ctx context.Context, c interceptors.CallMeta) (interceptors.Reporter, context.Context) {
if !opts.shouldReport(c.FullMethod()) {
return interceptors.NoopReporter{}, ctx
}
kind := KindServerFieldValue
if c.IsClient {
kind = KindClientFieldValue
Expand Down
44 changes: 44 additions & 0 deletions interceptors/logging/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,44 @@ func (s *loggingCustomGrpcLogFieldsSuite) TestCustomGrpcLogFieldsWithPingList()
AssertField(s.T(), logging.ServiceFieldKey, testpb.TestServiceFullName).AssertNoMoreTags(s.T())
}

type loggingWithDeciderSuite struct {
*baseLoggingSuite
}

func TestWithDeciderSuite(t *testing.T) {
if strings.HasPrefix(runtime.Version(), "go1.7") {
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
return
}
s := &loggingWithDeciderSuite{
baseLoggingSuite: &baseLoggingSuite{
logger: newMockLogger(),
InterceptorTestSuite: &testpb.InterceptorTestSuite{
TestService: &testpb.TestPingService{},
},
},
}
s.InterceptorTestSuite.ClientOpts = []grpc.DialOption{
grpc.WithUnaryInterceptor(logging.UnaryClientInterceptor(s.logger, logging.WithDecider(ignoreLogging()))),
grpc.WithStreamInterceptor(logging.StreamClientInterceptor(s.logger, logging.WithDecider(ignoreLogging()))),
}
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
grpc.StreamInterceptor(logging.StreamServerInterceptor(s.logger, logging.WithDecider(ignoreLogging()))),
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(s.logger, logging.WithDecider(ignoreLogging()))),
}
suite.Run(t, s)
}

// Test that do not have logs with using withDecider.
func (s *loggingWithDeciderSuite) TestDeciderWithPing() {
_, err := s.Client.Ping(s.SimpleCtx(), testpb.GoodPing)
assert.NoError(s.T(), err, "there must be not be an on a successful call")

lines := s.logger.o.Lines()
sort.Sort(lines)
require.Len(s.T(), lines, 0)
}

// waitUntil executes f every interval seconds until timeout or no error is returned from f.
func waitUntil(interval time.Duration, stopc <-chan struct{}, f func() error) error {
tick := time.NewTicker(interval)
Expand All @@ -790,3 +828,9 @@ func waitUntil(interval time.Duration, stopc <-chan struct{}, f func() error) er
}
}
}

func ignoreLogging() logging.Decider {
return func(fullMethodName string) bool {
return fullMethodName == testpb.TestServiceFullName
}
}
18 changes: 18 additions & 0 deletions interceptors/logging/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
levelFunc: nil,
timestampFormat: time.RFC3339,
disableGrpcLogFields: nil,
shouldReport: DefaultDeciderMethod,
}
)

Expand All @@ -62,6 +63,7 @@ type options struct {
timestampFormat string
fieldsFromCtxCallMetaFn fieldsFromCtxCallMetaFn
disableGrpcLogFields []string
shouldReport Decider
}

type Option func(*options)
Expand Down Expand Up @@ -213,3 +215,19 @@ func WithDisableLoggingFields(disableGrpcLogFields ...string) Option {
o.disableGrpcLogFields = disableGrpcLogFields
}
}

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

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

// 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) bool {
return true
}

0 comments on commit e74468e

Please sign in to comment.