Skip to content

Commit 686d758

Browse files
committed
trace2: use 'zap.Duration' to log elapsed times
Rather than log durations as 'Float64' of the duration's '.Seconds()', use 'zap.Duration()' directly and configure the logger to report durations in units of seconds. Signed-off-by: Victoria Dye <[email protected]>
1 parent 95fdf1d commit 686d758

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

internal/log/trace2.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func createTrace2ZapLogger() *zap.Logger {
8888
},
8989
)
9090

91+
// Ensure durations are logged in units of seconds
92+
loggerConfig.EncoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
93+
9194
// Re-purpose the "message" to represent the (always-present) "event" key
9295
loggerConfig.EncoderConfig.MessageKey = "event"
9396

@@ -108,13 +111,13 @@ func NewTrace2() traceLoggerInternal {
108111
type fieldList []zap.Field
109112

110113
func (l fieldList) withTime() fieldList {
111-
return append(l, zap.Float64("t_abs", time.Since(globalStart).Seconds()))
114+
return append(l, zap.Duration("t_abs", time.Since(globalStart)))
112115
}
113116

114117
func (l fieldList) withNesting(r trace2Region, includeTRel bool) fieldList {
115118
l = append(l, zap.Int("nesting", r.level))
116119
if includeTRel {
117-
l = append(l, zap.Float64("t_rel", time.Since(r.tStart).Seconds()))
120+
l = append(l, zap.Duration("t_rel", time.Since(r.tStart)))
118121
}
119122
return l
120123
}

0 commit comments

Comments
 (0)