Skip to content

Commit b81999a

Browse files
authored
Support customization of timestamp format (v2 branch) (#399)
This ports #398 to the v2 branch.
1 parent f60016a commit b81999a

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

interceptors/logging/interceptors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ func (r *reportable) ClientReporter(ctx context.Context, _ interface{}, typ inte
9090

9191
func (r *reportable) reporter(ctx context.Context, typ interceptors.GRPCType, service string, method string, kind string) (interceptors.Reporter, context.Context) {
9292
fields := commonFields(kind, typ, service, method)
93-
fields = append(fields, "grpc.start_time", time.Now().Format(time.RFC3339))
93+
fields = append(fields, "grpc.start_time", time.Now().Format(r.opts.timestampFormat))
9494
if d, ok := ctx.Deadline(); ok {
95-
fields = append(fields, "grpc.request.deadline", d.Format(time.RFC3339))
95+
fields = append(fields, "grpc.request.deadline", d.Format(r.opts.timestampFormat))
9696
}
9797
return &reporter{
9898
ctx: ctx,

interceptors/logging/options.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var (
1111
codeFunc: DefaultErrorToCode,
1212
durationFieldFunc: DefaultDurationToFields,
1313
// levelFunc depends if it's client or server.
14-
levelFunc: nil,
14+
levelFunc: nil,
15+
timestampFormat: time.RFC3339,
1516
}
1617
)
1718

@@ -20,6 +21,7 @@ type options struct {
2021
shouldLog Decider
2122
codeFunc ErrorToCode
2223
durationFieldFunc DurationToFields
24+
timestampFormat string
2325
}
2426

2527
type Option func(*options)
@@ -92,3 +94,10 @@ func DurationToDurationField(duration time.Duration) Fields {
9294
func durationToMilliseconds(duration time.Duration) float32 {
9395
return float32(duration.Nanoseconds()/1000) / 1000
9496
}
97+
98+
// WithTimestampFormat customizes the timestamps emitted in the log fields.
99+
func WithTimestampFormat(format string) Option {
100+
return func(o *options) {
101+
o.timestampFormat = format
102+
}
103+
}

interceptors/logging/payload.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,20 @@ func (c *clientPayloadReporter) PostMsgReceive(reply interface{}, err error, dur
8989
}
9090

9191
type payloadReportable struct {
92-
clientDecider ClientPayloadLoggingDecider
93-
serverDecider ServerPayloadLoggingDecider
94-
logger Logger
92+
clientDecider ClientPayloadLoggingDecider
93+
serverDecider ServerPayloadLoggingDecider
94+
logger Logger
95+
timestampFormat string
9596
}
9697

9798
func (r *payloadReportable) ServerReporter(ctx context.Context, req interface{}, typ interceptors.GRPCType, service string, method string) (interceptors.Reporter, context.Context) {
9899
if !r.serverDecider(ctx, interceptors.FullMethod(service, method), req) {
99100
return interceptors.NoopReporter{}, ctx
100101
}
101102
fields := commonFields(KindServerFieldValue, typ, service, method)
102-
fields = append(fields, "grpc.start_time", time.Now().Format(time.RFC3339))
103+
fields = append(fields, "grpc.start_time", time.Now().Format(r.timestampFormat))
103104
if d, ok := ctx.Deadline(); ok {
104-
fields = append(fields, "grpc.request.deadline", d.Format(time.RFC3339))
105+
fields = append(fields, "grpc.request.deadline", d.Format(r.timestampFormat))
105106
}
106107
return &serverPayloadReporter{
107108
ctx: ctx,
@@ -113,9 +114,9 @@ func (r *payloadReportable) ClientReporter(ctx context.Context, _ interface{}, t
113114
return interceptors.NoopReporter{}, ctx
114115
}
115116
fields := commonFields(KindClientFieldValue, typ, service, method)
116-
fields = append(fields, "grpc.start_time", time.Now().Format(time.RFC3339))
117+
fields = append(fields, "grpc.start_time", time.Now().Format(r.timestampFormat))
117118
if d, ok := ctx.Deadline(); ok {
118-
fields = append(fields, "grpc.request.deadline", d.Format(time.RFC3339))
119+
fields = append(fields, "grpc.request.deadline", d.Format(r.timestampFormat))
119120
}
120121
return &clientPayloadReporter{
121122
ctx: ctx,
@@ -125,26 +126,26 @@ func (r *payloadReportable) ClientReporter(ctx context.Context, _ interface{}, t
125126

126127
// PayloadUnaryServerInterceptor returns a new unary server interceptors that logs the payloads of requests on INFO level.
127128
// Logger tags will be used from tags context.
128-
func PayloadUnaryServerInterceptor(logger Logger, decider ServerPayloadLoggingDecider) grpc.UnaryServerInterceptor {
129-
return interceptors.UnaryServerInterceptor(&payloadReportable{logger: logger, serverDecider: decider})
129+
func PayloadUnaryServerInterceptor(logger Logger, decider ServerPayloadLoggingDecider, timestampFormat string) grpc.UnaryServerInterceptor {
130+
return interceptors.UnaryServerInterceptor(&payloadReportable{logger: logger, serverDecider: decider, timestampFormat: timestampFormat})
130131
}
131132

132133
// PayloadStreamServerInterceptor returns a new server server interceptors that logs the payloads of requests on INFO level.
133134
// Logger tags will be used from tags context.
134-
func PayloadStreamServerInterceptor(logger Logger, decider ServerPayloadLoggingDecider) grpc.StreamServerInterceptor {
135-
return interceptors.StreamServerInterceptor(&payloadReportable{logger: logger, serverDecider: decider})
135+
func PayloadStreamServerInterceptor(logger Logger, decider ServerPayloadLoggingDecider, timestampFormat string) grpc.StreamServerInterceptor {
136+
return interceptors.StreamServerInterceptor(&payloadReportable{logger: logger, serverDecider: decider, timestampFormat: timestampFormat})
136137
}
137138

138-
// PayloadUnaryClientInterceptor returns a new unary client interceptor that logs the paylods of requests and responses on INFO level.
139+
// PayloadUnaryClientInterceptor returns a new unary client interceptor that logs the payloads of requests and responses on INFO level.
139140
// Logger tags will be used from tags context.
140-
func PayloadUnaryClientInterceptor(logger Logger, decider ClientPayloadLoggingDecider) grpc.UnaryClientInterceptor {
141-
return interceptors.UnaryClientInterceptor(&payloadReportable{logger: logger, clientDecider: decider})
141+
func PayloadUnaryClientInterceptor(logger Logger, decider ClientPayloadLoggingDecider, timestampFormat string) grpc.UnaryClientInterceptor {
142+
return interceptors.UnaryClientInterceptor(&payloadReportable{logger: logger, clientDecider: decider, timestampFormat: timestampFormat})
142143
}
143144

144145
// PayloadStreamClientInterceptor returns a new streaming client interceptor that logs the paylods of requests and responses on INFO level.
145146
// Logger tags will be used from tags context.
146-
func PayloadStreamClientInterceptor(logger Logger, decider ClientPayloadLoggingDecider) grpc.StreamClientInterceptor {
147-
return interceptors.StreamClientInterceptor(&payloadReportable{logger: logger, clientDecider: decider})
147+
func PayloadStreamClientInterceptor(logger Logger, decider ClientPayloadLoggingDecider, timestampFormat string) grpc.StreamClientInterceptor {
148+
return interceptors.StreamClientInterceptor(&payloadReportable{logger: logger, clientDecider: decider, timestampFormat: timestampFormat})
148149
}
149150

150151
func logProtoMessageAsJson(logger Logger, pbMsg proto.Message, key string, msg string) {

interceptors/logging/payload_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ func TestPayloadSuite(t *testing.T) {
3636
},
3737
}
3838
s.InterceptorTestSuite.ClientOpts = []grpc.DialOption{
39-
grpc.WithUnaryInterceptor(logging.PayloadUnaryClientInterceptor(s.logger, alwaysLoggingDeciderClient)),
40-
grpc.WithStreamInterceptor(logging.PayloadStreamClientInterceptor(s.logger, alwaysLoggingDeciderClient)),
39+
grpc.WithUnaryInterceptor(logging.PayloadUnaryClientInterceptor(s.logger, alwaysLoggingDeciderClient, time.RFC3339)),
40+
grpc.WithStreamInterceptor(logging.PayloadStreamClientInterceptor(s.logger, alwaysLoggingDeciderClient, time.RFC3339)),
4141
}
4242
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
4343
grpc.ChainStreamInterceptor(
4444
tags.StreamServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor)),
45-
logging.PayloadStreamServerInterceptor(s.logger, alwaysLoggingDeciderServer)),
45+
logging.PayloadStreamServerInterceptor(s.logger, alwaysLoggingDeciderServer, time.RFC3339)),
4646
grpc.ChainUnaryInterceptor(
4747
tags.UnaryServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor)),
48-
logging.PayloadUnaryServerInterceptor(s.logger, alwaysLoggingDeciderServer)),
48+
logging.PayloadUnaryServerInterceptor(s.logger, alwaysLoggingDeciderServer, time.RFC3339)),
4949
}
5050
suite.Run(t, s)
5151
}

0 commit comments

Comments
 (0)