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: remove TODO comments for using pooled objects #5895

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func (o *Core) clone() *Core {
}
}

// TODO
// Sync flushes buffered logs (if any).
func (o *Core) Sync() error {
return nil
Expand Down Expand Up @@ -172,7 +171,6 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
}

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 {
Expand Down
20 changes: 14 additions & 6 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ func (m *objectEncoder) calculate(o *namespace) {
}

func (m *objectEncoder) AddArray(key string, v zapcore.ArrayMarshaler) error {
// TODO: Use arrayEncoder from a pool.
arr := &arrayEncoder{}
arr := newArrayEncoder()
err := v.MarshalLogArray(arr)
m.cur.attrs = append(m.cur.attrs, log.Slice(key, arr.elems...))
return err
}

func (m *objectEncoder) AddObject(k string, v zapcore.ObjectMarshaler) error {
// TODO: Use objectEncoder from a pool.
// Similar to console_encoder which uses capacity of 2:
// https://github.com/uber-go/zap/blob/bd0cf0447951b77aa98dcfc1ac19e6f58d3ee64f/zapcore/console_encoder.go#L33.
newobj := newObjectEncoder(2)
pellared marked this conversation as resolved.
Show resolved Hide resolved
err := v.MarshalLogObject(newobj)
newobj.calculate(newobj.root)
Expand Down Expand Up @@ -195,16 +195,24 @@ type arrayEncoder struct {
elems []log.Value
}

func newArrayEncoder() *arrayEncoder {
return &arrayEncoder{
// Similar to console_encoder which uses capacity of 2:
// https://github.com/uber-go/zap/blob/bd0cf0447951b77aa98dcfc1ac19e6f58d3ee64f/zapcore/console_encoder.go#L33.
elems: make([]log.Value, 0, 2),
}
}

func (a *arrayEncoder) AppendArray(v zapcore.ArrayMarshaler) error {
// TODO: Use arrayEncoder from a pool.
arr := &arrayEncoder{}
arr := newArrayEncoder()
err := v.MarshalLogArray(arr)
a.elems = append(a.elems, log.SliceValue(arr.elems...))
return err
}

func (a *arrayEncoder) AppendObject(v zapcore.ObjectMarshaler) error {
// TODO: Use objectEncoder from a pool.
// Similar to console_encoder which uses capacity of 2:
// https://github.com/uber-go/zap/blob/bd0cf0447951b77aa98dcfc1ac19e6f58d3ee64f/zapcore/console_encoder.go#L33.
m := newObjectEncoder(2)
pellared marked this conversation as resolved.
Show resolved Hide resolved
err := v.MarshalLogObject(m)
m.calculate(m.root)
Expand Down