Skip to content

Commit afdb188

Browse files
committed
feat(zapencoder): improve ctx field detection
1 parent 4879ffa commit afdb188

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

internal/zapencoder/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
This package is partially vendored from `zapcore`
44
Probably we can use `jx` here in the future.
55

6-
Changed code is located in `json_encoder_patch.go` file.
6+
Changed code is located in `patch.go` file.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{"level":"info","ts":"2024-01-02T15:04:05Z","caller":"caller.go","msg":"With context"}
2-
{"level":"info","ts":"2024-01-02T15:04:05Z","caller":"caller.go","msg":"With span","trace_id":"4bf92f3577b34da6a3ce929d0e0e4736","span_id":"00f067aa0ba902b7"}
1+
{"level":"info","ts":"2024-01-02T15:04:05Z","msg":"With context"}
2+
{"level":"info","ts":"2024-01-02T15:04:05Z","msg":"With span","trace_id":"4bf92f3577b34da6a3ce929d0e0e4736","span_id":"00f067aa0ba902b7"}

internal/zapencoder/json_encoder_patch.go renamed to internal/zapencoder/patch.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const (
2323

2424
func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
2525
for _, f := range fields {
26-
if f.Interface != nil && f.Type == zapcore.StringerType {
27-
// Test for ctx.
26+
if f.Interface != nil && f.Type == zapcore.StringerType && f.Key == "ctx" {
27+
// Test for context.Context.
2828
ctx, ok := f.Interface.(context.Context)
2929
if ok {
3030
spanCtx := trace.SpanContextFromContext(ctx)
@@ -37,6 +37,7 @@ func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
3737
}
3838
}
3939

40+
// Do not print ctx field.
4041
continue
4142
}
4243
}

0 commit comments

Comments
 (0)