Skip to content

Commit

Permalink
otelzap: Implement methods on arrayEncoder (#5632)
Browse files Browse the repository at this point in the history
Part of
#5191

Pre-work
#5279

This PR partially implements `arrayEncoder`
  • Loading branch information
khushijain21 authored May 23, 2024
1 parent 720adbc commit 2d41c29
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
42 changes: 33 additions & 9 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ func newObjectEncoder(len int) *objectEncoder {
}

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

func (m *objectEncoder) AddObject(k string, v zapcore.ObjectMarshaler) error {
Expand Down Expand Up @@ -170,16 +173,37 @@ func (a *arrayEncoder) AppendReflected(v interface{}) error {
return nil
}

func (a *arrayEncoder) AppendByteString(v []byte) {
a.elems = append(a.elems, log.StringValue(string(v)))
}

func (a *arrayEncoder) AppendBool(v bool) {
a.elems = append(a.elems, log.BoolValue(v))
}

func (a *arrayEncoder) AppendFloat64(v float64) {
a.elems = append(a.elems, log.Float64Value(v))
}

func (a *arrayEncoder) AppendFloat32(v float32) {
a.AppendFloat64(float64(v))
}

func (a *arrayEncoder) AppendInt(v int) {
a.elems = append(a.elems, log.IntValue(v))
}

func (a *arrayEncoder) AppendInt64(v int64) {
a.elems = append(a.elems, log.Int64Value(v))
}

func (a *arrayEncoder) AppendString(v string) {
a.elems = append(a.elems, log.StringValue(v))
}

// TODO.
func (a *arrayEncoder) AppendComplex128(v complex128) {}
func (a *arrayEncoder) AppendFloat32(v float32) {}
func (a *arrayEncoder) AppendByteString(v []byte) {}
func (a *arrayEncoder) AppendBool(v bool) {}
func (a *arrayEncoder) AppendUint64(v uint64) {}
func (a *arrayEncoder) AppendFloat64(v float64) {}
func (a *arrayEncoder) AppendInt(v int) {}
func (a *arrayEncoder) AppendInt64(v int64) {}
func (a *arrayEncoder) AppendString(v string) {}
func (a *arrayEncoder) AppendComplex64(v complex64) {}
func (a *arrayEncoder) AppendDuration(v time.Duration) {}
func (a *arrayEncoder) AppendInt32(v int32) {}
Expand Down
42 changes: 42 additions & 0 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ func TestObjectEncoder(t *testing.T) {
f func(zapcore.ObjectEncoder)
expected interface{}
}{
{
desc: "AddArray",
f: func(e zapcore.ObjectEncoder) {
assert.NoError(t, e.AddArray("k", zapcore.ArrayMarshalerFunc(func(arr zapcore.ArrayEncoder) error {
arr.AppendBool(true)
arr.AppendBool(false)
arr.AppendBool(true)
return nil
})), "Expected AddArray to succeed.")
},
expected: []interface{}{true, false, true},
},
{
desc: "AddBinary",
f: func(e zapcore.ObjectEncoder) { e.AddBinary("k", []byte("foo")) },
Expand Down Expand Up @@ -145,6 +157,36 @@ func TestObjectEncoder(t *testing.T) {
}
}

// Copied from https://github.com/uber-go/zap/blob/b39f8b6b6a44d8371a87610be50cce58eeeaabcb/zapcore/memory_encoder_test.go.
func TestArrayEncoder(t *testing.T) {
tests := []struct {
desc string
f func(zapcore.ArrayEncoder)
expected interface{}
}{
{"AppendBool", func(e zapcore.ArrayEncoder) { e.AppendBool(true) }, true},
{"AppendByteString", func(e zapcore.ArrayEncoder) { e.AppendByteString([]byte("foo")) }, "foo"},
{"AppendFloat64", func(e zapcore.ArrayEncoder) { e.AppendFloat64(3.14) }, 3.14},
{"AppendFloat32", func(e zapcore.ArrayEncoder) { e.AppendFloat32(3.14) }, float64(float32(3.14))},
{"AppendInt64", func(e zapcore.ArrayEncoder) { e.AppendInt64(42) }, int64(42)},
{"AppendInt", func(e zapcore.ArrayEncoder) { e.AppendInt(42) }, int64(42)},
{"AppendString", func(e zapcore.ArrayEncoder) { e.AppendString("foo") }, "foo"},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
enc := newObjectEncoder(1)
assert.NoError(t, enc.AddArray("k", zapcore.ArrayMarshalerFunc(func(arr zapcore.ArrayEncoder) error {
tt.f(arr)
tt.f(arr)
return nil
})), "Expected AddArray to succeed.")

assert.Equal(t, []interface{}{tt.expected, tt.expected}, value2Result(enc.kv[0].Value), "Unexpected encoder output.")
})
}
}

func value2Result(v log.Value) any {
switch v.Kind() {
case log.KindBool:
Expand Down

0 comments on commit 2d41c29

Please sign in to comment.