From fae477590ed1b3c42fb86220d999801f6f416cb2 Mon Sep 17 00:00:00 2001 From: Coleen Iona Quadros Date: Tue, 12 Sep 2023 13:25:44 +0200 Subject: [PATCH] change to disable logging fields Signed-off-by: Coleen Iona Quadros --- interceptors/logging/interceptors.go | 4 ++-- interceptors/logging/interceptors_test.go | 8 ++++---- interceptors/logging/logging.go | 13 +++++++++---- interceptors/logging/options.go | 14 +++++++------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/interceptors/logging/interceptors.go b/interceptors/logging/interceptors.go index 28c88991d..435467220 100644 --- a/interceptors/logging/interceptors.go +++ b/interceptors/logging/interceptors.go @@ -142,8 +142,8 @@ func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc } fields := Fields{} - if opts.grpcLogFields != nil { - fields = fields.WithUnique(customCommonFields(kind, c, opts.grpcLogFields)) + if opts.disableGrpcLogFields != nil { + fields = disableCommonLoggingFields(kind, c, opts.disableGrpcLogFields) } else { // Field dups from context override the common fields. fields = newCommonFields(kind, c) diff --git a/interceptors/logging/interceptors_test.go b/interceptors/logging/interceptors_test.go index 48b7eae85..85eb8a6ac 100644 --- a/interceptors/logging/interceptors_test.go +++ b/interceptors/logging/interceptors_test.go @@ -648,12 +648,12 @@ func TestCustomGrpcLogFieldsSuite(t *testing.T) { }, } s.InterceptorTestSuite.ClientOpts = []grpc.DialOption{ - grpc.WithUnaryInterceptor(logging.UnaryClientInterceptor(s.logger, logging.WithGrpcLogFields(logging.MethodFieldKey, logging.ServiceFieldKey, "custom-field-should-not-be-added"))), - grpc.WithStreamInterceptor(logging.StreamClientInterceptor(s.logger, logging.WithGrpcLogFields(logging.MethodFieldKey, logging.ServiceFieldKey, "custom-field-should-not-be-added"))), + grpc.WithUnaryInterceptor(logging.UnaryClientInterceptor(s.logger, logging.WithDisableLoggingFields(logging.ComponentFieldKey, logging.MethodTypeFieldKey, logging.SystemTag[0], "custom-field-should-be-ignored"))), + grpc.WithStreamInterceptor(logging.StreamClientInterceptor(s.logger, logging.WithDisableLoggingFields(logging.ComponentFieldKey, logging.MethodTypeFieldKey, logging.SystemTag[0], "custom-field-should-be-ignored"))), } s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{ - grpc.StreamInterceptor(logging.StreamServerInterceptor(s.logger, logging.WithGrpcLogFields(logging.MethodFieldKey, logging.ServiceFieldKey, "custom-field-should-not-be-added"))), - grpc.UnaryInterceptor(logging.UnaryServerInterceptor(s.logger, logging.WithGrpcLogFields(logging.MethodFieldKey, logging.ServiceFieldKey, "custom-field-should-not-be-added"))), + grpc.StreamInterceptor(logging.StreamServerInterceptor(s.logger, logging.WithDisableLoggingFields(logging.ComponentFieldKey, logging.MethodTypeFieldKey, logging.SystemTag[0], "custom-field-should-be-ignored"))), + grpc.UnaryInterceptor(logging.UnaryServerInterceptor(s.logger, logging.WithDisableLoggingFields(logging.ComponentFieldKey, logging.MethodTypeFieldKey, logging.SystemTag[0], "custom-field-should-be-ignore"))), } suite.Run(t, s) } diff --git a/interceptors/logging/logging.go b/interceptors/logging/logging.go index 5f0ce05f1..ebc6e532a 100644 --- a/interceptors/logging/logging.go +++ b/interceptors/logging/logging.go @@ -38,7 +38,8 @@ func newCommonFields(kind string, c interceptors.CallMeta) Fields { } } -func customCommonFields(kind string, c interceptors.CallMeta, customFields []string) Fields { +// disableCommonLoggingFields returns copy of common fields with disabled fields removed. +func disableCommonLoggingFields(kind string, c interceptors.CallMeta, disableFields []string) Fields { commonFields := newCommonFields(kind, c) existing := map[any]any{} @@ -48,12 +49,16 @@ func customCommonFields(kind string, c interceptors.CallMeta, customFields []str existing[k] = v } - newFields := Fields{} - for _, key := range customFields { + for _, key := range disableFields { if _, ok := existing[key]; ok { - newFields = append(newFields, key, existing[key]) + delete(existing, key) } } + + newFields := make(Fields, 0, len(existing)) + for k, v := range existing { + newFields = append(newFields, k, v) + } return newFields } diff --git a/interceptors/logging/options.go b/interceptors/logging/options.go index c1ba1d0d6..5308d8b3f 100644 --- a/interceptors/logging/options.go +++ b/interceptors/logging/options.go @@ -48,9 +48,9 @@ var ( codeFunc: DefaultErrorToCode, durationFieldFunc: DefaultDurationToFields, // levelFunc depends if it's client or server. - levelFunc: nil, - timestampFormat: time.RFC3339, - grpcLogFields: nil, + levelFunc: nil, + timestampFormat: time.RFC3339, + disableGrpcLogFields: nil, } ) @@ -61,7 +61,7 @@ type options struct { durationFieldFunc DurationToFields timestampFormat string fieldsFromCtxCallMetaFn fieldsFromCtxCallMetaFn - grpcLogFields []string + disableGrpcLogFields []string } type Option func(*options) @@ -207,9 +207,9 @@ func WithTimestampFormat(format string) Option { } } -// WithAddGrpcLogFields customizes the function for adding gRPC fields to the log entry. -func WithGrpcLogFields(enableGrpcLogFields ...string) Option { +// WithDisableLoggingFields disables logging of gRPC fields provided. +func WithDisableLoggingFields(disableGrpcLogFields ...string) Option { return func(o *options) { - o.grpcLogFields = enableGrpcLogFields + o.disableGrpcLogFields = disableGrpcLogFields } }