Skip to content

Commit

Permalink
otelzap: Implement Reflect method (#5703)
Browse files Browse the repository at this point in the history
Part of
#5191

Pre-work
#5279

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
  • Loading branch information
khushijain21 and pellared authored Jun 5, 2024
1 parent e8540b2 commit 1ddc6ac
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
63 changes: 61 additions & 2 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap"

import (
"fmt"
"reflect"
"time"

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

// TODO.
func (m *objectEncoder) AddReflected(k string, v interface{}) error {
m.kv = append(m.kv,
log.KeyValue{
Key: k,
Value: convertValue(v),
})
return nil
}

Expand Down Expand Up @@ -177,8 +183,8 @@ func (a *arrayEncoder) AppendObject(v zapcore.ObjectMarshaler) error {
return err
}

// TODO.
func (a *arrayEncoder) AppendReflected(v interface{}) error {
a.elems = append(a.elems, convertValue(v))
return nil
}

Expand Down Expand Up @@ -231,3 +237,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))
}
14 changes: 14 additions & 0 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func TestObjectEncoder(t *testing.T) {
},
expected: []interface{}{wantTurducken, wantTurducken},
},
{
desc: "AddReflected",
f: func(e zapcore.ObjectEncoder) {
assert.NoError(t, e.AddReflected("k", map[string]interface{}{"foo": 5}), "Expected AddReflected to succeed.")
},
expected: map[string]interface{}{"foo": int64(5)},
},
{
desc: "AddBinary",
f: func(e zapcore.ObjectEncoder) { e.AddBinary("k", []byte("foo")) },
Expand Down Expand Up @@ -207,6 +214,13 @@ func TestArrayEncoder(t *testing.T) {
},
expected: []interface{}{true, false},
},
{
desc: "AppendReflected",
f: func(e zapcore.ArrayEncoder) {
assert.NoError(t, e.AppendReflected(map[string]interface{}{"foo": 5}))
},
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"},
{"AppendFloat64", func(e zapcore.ArrayEncoder) { e.AppendFloat64(3.14) }, 3.14},
Expand Down

0 comments on commit 1ddc6ac

Please sign in to comment.