Skip to content

Commit

Permalink
otelzap: Implement methods on objectEncoder (#5595)
Browse files Browse the repository at this point in the history
Part of #5191
---------
Co-authored-by: Robert Pająk <[email protected]>
  • Loading branch information
khushijain21 authored May 17, 2024
1 parent 2ea2299 commit 6d343e9
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 27 deletions.
41 changes: 14 additions & 27 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ zapcore.ObjectEncoder = (*objectEncoder)(nil)
// objectEncoder implements zapcore.ObjectEncoder.
// It encodes given fields to OTel key-values.
type objectEncoder struct {
kv []log.KeyValue // nolint:unused
kv []log.KeyValue
}

// nolint:unused
Expand All @@ -28,65 +28,50 @@ func newObjectEncoder(len int) *objectEncoder {
}
}

// AddArray converts array to log.Slice using ArrayEncoder.
func (m *objectEncoder) AddArray(key string, v zapcore.ArrayMarshaler) error {
// TODO
return nil
}

// AddObject converts object to log.Map using ObjectEncoder.
func (m *objectEncoder) AddObject(k string, v zapcore.ObjectMarshaler) error {
// TODO
return nil
}

// AddBinary converts binary to log.Bytes.
func (m *objectEncoder) AddBinary(k string, v []byte) {
// TODO
m.kv = append(m.kv, log.Bytes(k, v))
}

// AddByteString converts byte to log.String.
func (m *objectEncoder) AddByteString(k string, v []byte) {
// TODO
m.kv = append(m.kv, log.String(k, string(v)))
}

// AddBool converts bool to log.Bool.
func (m *objectEncoder) AddBool(k string, v bool) {
// TODO
m.kv = append(m.kv, log.Bool(k, v))
}

// AddDuration converts duration to log.Int.
func (m *objectEncoder) AddDuration(k string, v time.Duration) {
// TODO
}

// TODO.
func (m *objectEncoder) AddComplex128(k string, v complex128) {
// TODO.
}

// AddFloat64 converts float64 to log.Float64.
func (m *objectEncoder) AddFloat64(k string, v float64) {
// TODO
}

// AddFloat32 converts float32 to log.Float64.
func (m *objectEncoder) AddFloat32(k string, v float32) {
// TODO
m.kv = append(m.kv, log.Float64(k, v))
}

// AddInt64 converts int64 to log.Int64.
func (m *objectEncoder) AddInt64(k string, v int64) {
// TODO
m.kv = append(m.kv, log.Int64(k, v))
}

// AddInt converts int to log.Int.
func (m *objectEncoder) AddInt(k string, v int) {
// TODO
m.kv = append(m.kv, log.Int(k, v))
}

// AddString converts string to log.String.
func (m *objectEncoder) AddString(k string, v string) {
// TODO
m.kv = append(m.kv, log.String(k, v))
}

// TODO.
Expand All @@ -104,11 +89,13 @@ func (m *objectEncoder) OpenNamespace(k string) {
// TODO
}

func (m *objectEncoder) AddFloat32(k string, v float32) { m.AddFloat64(k, float64(v)) }
func (m *objectEncoder) AddInt32(k string, v int32) { m.AddInt64(k, int64(v)) }
func (m *objectEncoder) AddInt16(k string, v int16) { m.AddInt64(k, int64(v)) }
func (m *objectEncoder) AddInt8(k string, v int8) { m.AddInt64(k, int64(v)) }

// TODO.
func (m *objectEncoder) AddComplex64(k string, v complex64) {}
func (m *objectEncoder) AddInt32(k string, v int32) {}
func (m *objectEncoder) AddInt16(k string, v int16) {}
func (m *objectEncoder) AddInt8(k string, v int8) {}
func (m *objectEncoder) AddTime(k string, v time.Time) {}
func (m *objectEncoder) AddUint(k string, v uint) {}
func (m *objectEncoder) AddUint32(k string, v uint32) {}
Expand Down
118 changes: 118 additions & 0 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Copyright (c) 2016-2017 Uber Technologies, Inc.

package otelzap

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"go.opentelemetry.io/otel/log"
)

// Copied from https://github.com/uber-go/zap/blob/b39f8b6b6a44d8371a87610be50cce58eeeaabcb/zapcore/memory_encoder_test.go.
func TestObjectEncoder(t *testing.T) {
tests := []struct {
desc string
f func(zapcore.ObjectEncoder)
expected interface{}
}{
{
desc: "AddBinary",
f: func(e zapcore.ObjectEncoder) { e.AddBinary("k", []byte("foo")) },
expected: []byte("foo"),
},
{
desc: "AddByteString",
f: func(e zapcore.ObjectEncoder) { e.AddByteString("k", []byte("foo")) },
expected: "foo",
},
{
desc: "AddBool",
f: func(e zapcore.ObjectEncoder) { e.AddBool("k", true) },
expected: true,
},
{
desc: "AddFloat64",
f: func(e zapcore.ObjectEncoder) { e.AddFloat64("k", 3.14) },
expected: 3.14,
},
{
desc: "AddFloat32",
f: func(e zapcore.ObjectEncoder) { e.AddFloat32("k", 3.14) },
expected: float64(float32(3.14)),
},
{
desc: "AddInt",
f: func(e zapcore.ObjectEncoder) { e.AddInt("k", 42) },
expected: int64(42),
},
{
desc: "AddInt64",
f: func(e zapcore.ObjectEncoder) { e.AddInt64("k", 42) },
expected: int64(42),
},
{
desc: "AddInt32",
f: func(e zapcore.ObjectEncoder) { e.AddInt32("k", 42) },
expected: int64(42),
},
{
desc: "AddInt16",
f: func(e zapcore.ObjectEncoder) { e.AddInt16("k", 42) },
expected: int64(42),
},
{
desc: "AddInt8",
f: func(e zapcore.ObjectEncoder) { e.AddInt8("k", 42) },
expected: int64(42),
},
{
desc: "AddString",
f: func(e zapcore.ObjectEncoder) { e.AddString("k", "v") },
expected: "v",
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
enc := newObjectEncoder(1)
tt.f(enc)
require.Len(t, enc.kv, 1)
assert.Equal(t, tt.expected, value2Result((enc.kv[0].Value)), "Unexpected encoder output.")
})
}
}

func value2Result(v log.Value) any {
switch v.Kind() {
case log.KindBool:
return v.AsBool()
case log.KindFloat64:
return v.AsFloat64()
case log.KindInt64:
return v.AsInt64()
case log.KindString:
return v.AsString()
case log.KindBytes:
return v.AsBytes()
case log.KindSlice:
var s []any
for _, val := range v.AsSlice() {
s = append(s, value2Result(val))
}
return s
case log.KindMap:
m := make(map[string]any)
for _, val := range v.AsMap() {
m[val.Key] = value2Result(val.Value)
}
return m
}
return nil
}

0 comments on commit 6d343e9

Please sign in to comment.