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: Implement Reflect method #5703

Merged
merged 5 commits into from
Jun 5, 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
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 @@
})
}

// 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 @@
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) 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)

Check warning on line 248 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L243-L248

Added lines #L243 - L248 were not covered by tests
case int:
return log.IntValue(v)
case int64:
return log.Int64Value(v)
case string:
return log.StringValue(v)

Check warning on line 254 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L251-L254

Added lines #L251 - L254 were not covered by tests
}

t := reflect.TypeOf(v)
if t == nil {
return log.Value{}

Check warning on line 259 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L259

Added line #L259 was not covered by tests
}
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()))

Check warning on line 268 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L263-L268

Added lines #L263 - L268 were not covered by tests
}
return log.SliceValue(items...)

Check warning on line 270 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L270

Added line #L270 was not covered by tests
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())

Check warning on line 277 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L277

Added line #L277 was not covered by tests
} 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())

Check warning on line 288 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L287-L288

Added lines #L287 - L288 were not covered by tests
}

return log.StringValue(fmt.Sprintf("unhandled attribute type: (%s) %+v", t, v))

Check warning on line 291 in bridges/otelzap/encoder.go

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L291

Added line #L291 was not covered by tests
}
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