Skip to content

Commit

Permalink
otelzap: Implement Reflect method
Browse files Browse the repository at this point in the history
  • Loading branch information
khushijain21 committed Jun 1, 2024
1 parent 48ab4e2 commit c49a31d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
28 changes: 25 additions & 3 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap"

import (
"encoding/json"
"time"

"go.uber.org/zap/zapcore"
Expand All @@ -19,7 +20,8 @@ var (
// objectEncoder implements zapcore.ObjectEncoder.
// It encodes given fields to OTel key-values.
type objectEncoder struct {
kv []log.KeyValue
kv []log.KeyValue
reflectval log.Value
}

// nolint:unused
Expand Down Expand Up @@ -93,11 +95,23 @@ func (m *objectEncoder) AddUint64(k string, v uint64) {
})
}

// TODO.
// AddReflected converts all non-primitive type to JSON string.
func (m *objectEncoder) AddReflected(k string, v interface{}) error {
enc := json.NewEncoder(m)
if err := enc.Encode(v); err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

bridges/otelzap/encoder.go#L102

Added line #L102 was not covered by tests
}
m.AddString(k, m.reflectval.AsString())
return nil
}

// Implements io.Writer to which json encoder writes to.
// Used by [AddReflected].
func (m *objectEncoder) Write(p []byte) (n int, err error) {
m.reflectval = log.StringValue(string(p))
return len(p), nil
}

// OpenNamespace opens an isolated namespace where all subsequent fields will
// be added.
func (m *objectEncoder) OpenNamespace(k string) {
Expand Down Expand Up @@ -179,7 +193,15 @@ func (a *arrayEncoder) AppendObject(v zapcore.ObjectMarshaler) error {

// TODO.
func (a *arrayEncoder) AppendReflected(v interface{}) error {
return nil
enc := json.NewEncoder(a)
return enc.Encode(v)
}

// Implements io.Writer to which json encoder writes to.
// Used by [AppendReflected].
func (a *arrayEncoder) Write(p []byte) (n int, err error) {
a.elems = append(a.elems, log.StringValue(string(p)))
return len(p), nil
}

func (a *arrayEncoder) AppendByteString(v []byte) {
Expand Down
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: "{\"foo\":5}\n",
},
{
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: "{\"foo\":5}\n",
},
{"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 c49a31d

Please sign in to comment.