Skip to content

Commit

Permalink
Merge branch 'main' into akats7-owner
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias authored Jun 4, 2024
2 parents dd34b11 + a594aef commit 2bbdd94
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
28 changes: 21 additions & 7 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func WithLoggerProvider(provider log.LoggerProvider) Option {
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 @@ func NewCore(name string, opts ...Option) *Core {
cfg := newConfig(opts)
return &Core{
logger: cfg.logger(name),
ctx: context.Background(),
}
}

Expand All @@ -114,7 +116,11 @@ func (o *Core) Enabled(level zapcore.Level) bool {
func (o *Core) With(fields []zapcore.Field) zapcore.Core {
cloned := o.clone()
if len(fields) > 0 {
cloned.attr = append(cloned.attr, convertField(fields)...)
ctx, attrbuf := convertField(fields)
if ctx != nil {
cloned.ctx = ctx
}
cloned.attr = append(cloned.attr, attrbuf...)
}
return cloned
}
Expand All @@ -123,6 +129,7 @@ func (o *Core) clone() *Core {
return &Core{
logger: o.logger,
attr: slices.Clone(o.attr),
ctx: o.ctx,
}
}

Expand All @@ -148,28 +155,35 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
r.SetBody(log.StringValue(ent.Message))
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)...)
ctx, attrbuf := convertField(fields)
if ctx != nil {
o.ctx = ctx
}
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(fields []zapcore.Field) (context.Context, []log.KeyValue) {
// TODO: Use objectEncoder from a pool instead of newObjectEncoder.
var ctx context.Context
enc := newObjectEncoder(len(fields))
for _, field := range fields {
if ctxFld, ok := field.Interface.(context.Context); ok {
ctx = ctxFld
continue
}
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: Add WriteContext test case.
// TODO: Add WithContext test case.

// test child logger with accumulated fields
t.Run("With", func(t *testing.T) {
testCases := [][]string{{"test1", "value1"}, {"test2", "value2"}}
Expand Down
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
go.opentelemetry.io/build-tools/gotmpl v0.13.0
go.opentelemetry.io/build-tools/multimod v0.13.0
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc
golang.org/x/tools v0.21.1-0.20240603183908-4478db00aae5
golang.org/x/tools v0.21.1-0.20240514024235-59d9797072e7
golang.org/x/vuln v1.1.1
)

Expand Down
4 changes: 2 additions & 2 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,8 @@ golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.21.1-0.20240603183908-4478db00aae5 h1:0341WNhaXj6wZ9byrkZC/PcJ1qH2ePVbZg69NKnh1lE=
golang.org/x/tools v0.21.1-0.20240603183908-4478db00aae5/go.mod h1:bqv7PJ/TtlrzgJKhOAGdDUkUltQapRik/UEHubLVBWo=
golang.org/x/tools v0.21.1-0.20240514024235-59d9797072e7 h1:DnP3aRQn/r68glNGB8/7+3iE77jA+YZZCxpfIXx2MdA=
golang.org/x/tools v0.21.1-0.20240514024235-59d9797072e7/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/vuln v1.1.1 h1:4nYQg4OSr7uYQMtjuuYqLAEVuTjY4k/CPMYqvv5OPcI=
golang.org/x/vuln v1.1.1/go.mod h1:hNgE+SKMSp2wHVUpW0Ow2ejgKpNJePdML+4YjxrVxik=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down

0 comments on commit 2bbdd94

Please sign in to comment.