Skip to content

Commit

Permalink
change to disable logging fields
Browse files Browse the repository at this point in the history
Signed-off-by: Coleen Iona Quadros <[email protected]>
  • Loading branch information
coleenquadros committed Sep 12, 2023
1 parent ff52335 commit fae4775
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions interceptors/logging/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc
}

fields := Fields{}

Check failure on line 144 in interceptors/logging/interceptors.go

View workflow job for this annotation

GitHub Actions / Linters (Static Analysis) for Go

SA4006: this value of `fields` is never used (staticcheck)
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)
Expand Down
8 changes: 4 additions & 4 deletions interceptors/logging/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 9 additions & 4 deletions interceptors/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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 {

Check failure on line 53 in interceptors/logging/logging.go

View workflow job for this annotation

GitHub Actions / Linters (Static Analysis) for Go

S1033: unnecessary guard around call to delete (gosimple)
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
}

Expand Down
14 changes: 7 additions & 7 deletions interceptors/logging/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)

Expand All @@ -61,7 +61,7 @@ type options struct {
durationFieldFunc DurationToFields
timestampFormat string
fieldsFromCtxCallMetaFn fieldsFromCtxCallMetaFn
grpcLogFields []string
disableGrpcLogFields []string
}

type Option func(*options)
Expand Down Expand Up @@ -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
}
}

0 comments on commit fae4775

Please sign in to comment.