Skip to content

Commit

Permalink
zap reflect changes
Browse files Browse the repository at this point in the history
  • Loading branch information
khushijain21 committed Jun 5, 2024
1 parent 474b05f commit 046b3e4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
72 changes: 62 additions & 10 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap"

import (
"encoding/json"
"fmt"
"reflect"
"time"

"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -95,14 +96,12 @@ func (m *objectEncoder) AddUint64(k string, v uint64) {
})
}

// AddReflected converts all non-primitive type to JSON string.
func (m *objectEncoder) AddReflected(k string, v interface{}) error {
// TODO use json Encoder from a pool.
enc := json.NewEncoder(m)
if err := enc.Encode(v); err != nil {
return err
}
m.AddString(k, m.reflectval.AsString())
m.kv = append(m.kv,
log.KeyValue{
Key: k,
Value: convertValue(v),
})
return nil
}

Expand Down Expand Up @@ -193,8 +192,8 @@ func (a *arrayEncoder) AppendObject(v zapcore.ObjectMarshaler) error {
}

func (a *arrayEncoder) AppendReflected(v interface{}) error {
enc := json.NewEncoder(a)
return enc.Encode(v)
a.elems = append(a.elems, convertValue(v))
return nil
}

// Implements io.Writer to which json encoder writes to.
Expand Down Expand Up @@ -253,3 +252,56 @@ func (a *arrayEncoder) AppendUint32(v uint32) { a.AppendInt64(int64(v))
func (a *arrayEncoder) AppendUint16(v uint16) { a.AppendInt64(int64(v)) }
func (a *arrayEncoder) AppendUint8(v uint8) { a.AppendInt64(int64(v)) }
func (a *arrayEncoder) AppendUintptr(v uintptr) { a.AppendUint64(uint64(v)) }

func convertValue(v interface{}) log.Value {
switch v := v.(type) {
case bool:
return log.BoolValue(v)
case []byte:
return log.BytesValue(v)
case float64:
return log.Float64Value(v)
case int:
return log.IntValue(v)
case int64:
return log.Int64Value(v)
case string:
return log.StringValue(v)
}

t := reflect.TypeOf(v)
if t == nil {
return log.Value{}
}
val := reflect.ValueOf(v)
switch t.Kind() {
case reflect.Struct:
return log.StringValue(fmt.Sprintf("%+v", v))
case reflect.Slice, reflect.Array:
items := make([]log.Value, 0, val.Len())
for i := 0; i < val.Len(); i++ {
items = append(items, convertValue(val.Index(i).Interface()))
}
return log.SliceValue(items...)
case reflect.Map:
kvs := make([]log.KeyValue, 0, val.Len())
for _, k := range val.MapKeys() {
var key string
// If the key is a struct, use %+v to print the struct fields.
if k.Kind() == reflect.Struct {
key = fmt.Sprintf("%+v", k.Interface())
} else {
key = fmt.Sprintf("%v", k.Interface())
}
kvs = append(kvs, log.KeyValue{
Key: key,
Value: convertValue(val.MapIndex(k).Interface()),
})
}
return log.MapValue(kvs...)
case reflect.Ptr, reflect.Interface:
return convertValue(val.Elem().Interface())
}

return log.StringValue(fmt.Sprintf("unhandled attribute type: (%s) %+v", t, v))
}
4 changes: 2 additions & 2 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestObjectEncoder(t *testing.T) {
f: func(e zapcore.ObjectEncoder) {
assert.NoError(t, e.AddReflected("k", map[string]interface{}{"foo": 5}), "Expected AddReflected to succeed.")
},
expected: "{\"foo\":5}\n",
expected: map[string]interface{}{"foo": int64(5)},
},
{
desc: "AddBinary",
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestArrayEncoder(t *testing.T) {
f: func(e zapcore.ArrayEncoder) {
assert.NoError(t, e.AppendReflected(map[string]interface{}{"foo": 5}))
},
expected: "{\"foo\":5}\n",
expected: map[string]interface{}{"foo": int64(5)},
},
{"AppendBool", func(e zapcore.ArrayEncoder) { e.AppendBool(true) }, true},
{"AppendByteString", func(e zapcore.ArrayEncoder) { e.AppendByteString([]byte("foo")) }, "foo"},
Expand Down

0 comments on commit 046b3e4

Please sign in to comment.