Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otelzap: Allow context injection via fields #5707

Merged
merged 13 commits into from
Jun 4, 2024
22 changes: 16 additions & 6 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
type Core struct {
logger log.Logger
attr []log.KeyValue
ctx context.Context
}

// Compile-time check *Core implements zapcore.Core.
Expand All @@ -100,6 +101,7 @@
cfg := newConfig(opts)
return &Core{
logger: cfg.logger(name),
ctx: context.Background(),
}
}

Expand All @@ -114,7 +116,9 @@
func (o *Core) With(fields []zapcore.Field) zapcore.Core {
cloned := o.clone()
if len(fields) > 0 {
cloned.attr = append(cloned.attr, convertField(fields)...)
var attrbuf []log.KeyValue
cloned.ctx, attrbuf = convertField(cloned.ctx, fields)
cloned.attr = append(cloned.attr, attrbuf...)
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
}
return cloned
}
Expand All @@ -123,6 +127,7 @@
return &Core{
logger: o.logger,
attr: slices.Clone(o.attr),
ctx: o.ctx,
}
}

Expand All @@ -149,27 +154,32 @@
r.SetSeverity(convertLevel(ent.Level))

// TODO: Handle attributes passed via With (exceptions: context.Context and zap.Namespace).
// TODO: Handle context.Context containing trace context.
// TODO: Handle zap.Namespace.
// TODO: Handle ent.LoggerName.

r.AddAttributes(o.attr...)
if len(fields) > 0 {
r.AddAttributes(convertField(fields)...)
var attrbuf []log.KeyValue
o.ctx, attrbuf = convertField(o.ctx, fields)
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
r.AddAttributes(attrbuf...)
}

o.logger.Emit(context.Background(), r)
o.logger.Emit(o.ctx, r)
return nil
}

func convertField(fields []zapcore.Field) []log.KeyValue {
func convertField(ctx context.Context, fields []zapcore.Field) (context.Context, []log.KeyValue) {
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Use objectEncoder from a pool instead of newObjectEncoder.
enc := newObjectEncoder(len(fields))
for _, field := range fields {
if ctxFld, ok := field.Interface.(context.Context); ok {
ctx = ctxFld
continue

Check warning on line 177 in bridges/otelzap/core.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/core.go#L176-L177

Added lines #L176 - L177 were not covered by tests
}
field.AddTo(enc)
}

return enc.kv
return ctx, enc.kv
}

func convertLevel(level zapcore.Level) log.Severity {
Expand Down
3 changes: 3 additions & 0 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func TestCore(t *testing.T) {

rec.Reset()

// TODO WriteContext
// TOOD WithContext
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved

// test child logger with accumulated fields
t.Run("With", func(t *testing.T) {
testCases := [][]string{{"test1", "value1"}, {"test2", "value2"}}
Expand Down
Loading