Skip to content

Commit 48c13f0

Browse files
feat: Support extracting fields from CallMeta (#628)
Extend options.WithFieldsFromContext via a new options.WithFieldsFromContextAndCallMeta in a backward compatible fashion such that it supports extracting fields not only from the Context but also from the CallMeta.
1 parent 44626be commit 48c13f0

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

interceptors/logging/interceptors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc
148148
fields = append(fields, "peer.address", peer.Addr.String())
149149
}
150150
}
151-
if opts.fieldsFromCtxFn != nil {
151+
if opts.fieldsFromCtxCallMetaFn != nil {
152152
// fieldsFromCtxFn dups override the existing fields.
153-
fields = opts.fieldsFromCtxFn(ctx).AppendUnique(fields)
153+
fields = opts.fieldsFromCtxCallMetaFn(ctx, c).AppendUnique(fields)
154154
}
155155

156156
singleUseFields := Fields{"grpc.start_time", time.Now().Format(opts.timestampFormat)}

interceptors/logging/interceptors_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ type loggingClientServerSuite struct {
173173
}
174174

175175
func customFields(_ context.Context) logging.Fields {
176-
// Add custom fields, one new and one that should be ignored as it duplicates the standard field.
177-
return logging.Fields{"custom-field", "yolo", logging.ServiceFieldKey, "something different"}
176+
return logging.Fields{"custom-field", "yolo"}
178177
}
179178

180179
func TestSuite(t *testing.T) {

interceptors/logging/options.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"time"
1010

11+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
1112
"google.golang.org/grpc/codes"
1213
"google.golang.org/grpc/status"
1314
)
@@ -53,12 +54,12 @@ var (
5354
)
5455

5556
type options struct {
56-
levelFunc CodeToLevel
57-
loggableEvents []LoggableEvent
58-
codeFunc ErrorToCode
59-
durationFieldFunc DurationToFields
60-
timestampFormat string
61-
fieldsFromCtxFn fieldsFromCtxFn
57+
levelFunc CodeToLevel
58+
loggableEvents []LoggableEvent
59+
codeFunc ErrorToCode
60+
durationFieldFunc DurationToFields
61+
timestampFormat string
62+
fieldsFromCtxCallMetaFn fieldsFromCtxCallMetaFn
6263
}
6364

6465
type Option func(*options)
@@ -130,12 +131,24 @@ func DefaultClientCodeToLevel(code codes.Code) Level {
130131
}
131132
}
132133

133-
type fieldsFromCtxFn func(ctx context.Context) Fields
134+
type (
135+
fieldsFromCtxFn func(ctx context.Context) Fields
136+
fieldsFromCtxCallMetaFn func(ctx context.Context, c interceptors.CallMeta) Fields
137+
)
134138

135-
// WithFieldsFromContext allows overriding existing or adding extra fields to all log messages per given request.
139+
// WithFieldsFromContext allows overriding existing or adding extra fields to all log messages per given context
136140
func WithFieldsFromContext(f fieldsFromCtxFn) Option {
137141
return func(o *options) {
138-
o.fieldsFromCtxFn = f
142+
o.fieldsFromCtxCallMetaFn = func(ctx context.Context, _ interceptors.CallMeta) Fields {
143+
return f(ctx)
144+
}
145+
}
146+
}
147+
148+
// WithFieldsFromContextAndCallMeta allows overriding existing or adding extra fields to all log messages per given context and interceptor.CallMeta
149+
func WithFieldsFromContextAndCallMeta(f fieldsFromCtxCallMetaFn) Option {
150+
return func(o *options) {
151+
o.fieldsFromCtxCallMetaFn = f
139152
}
140153
}
141154

0 commit comments

Comments
 (0)