Skip to content

Commit

Permalink
otelzap: Implement methods on objectEncoder (#5602)
Browse files Browse the repository at this point in the history
Part of #5191

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
  • Loading branch information
khushijain21 and pellared authored May 20, 2024
1 parent 040472f commit 8c03dfa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
45 changes: 32 additions & 13 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ func (m *objectEncoder) AddBool(k string, v bool) {
}

func (m *objectEncoder) AddDuration(k string, v time.Duration) {
// TODO
m.AddInt64(k, v.Nanoseconds())
}

func (m *objectEncoder) AddComplex128(k string, v complex128) {
// TODO.
r := log.Float64("r", real(v))
i := log.Float64("i", imag(v))
m.kv = append(m.kv, log.Map(k, r, i))
}

func (m *objectEncoder) AddFloat64(k string, v float64) {
Expand Down Expand Up @@ -89,16 +91,33 @@ 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)) }
func (m *objectEncoder) AddComplex64(k string, v complex64) {
m.AddComplex128(k, complex128(v))
}

func (m *objectEncoder) AddTime(k string, v time.Time) {
m.AddInt64(k, v.UnixNano())
}

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) AddTime(k string, v time.Time) {}
func (m *objectEncoder) AddUint(k string, v uint) {}
func (m *objectEncoder) AddUint32(k string, v uint32) {}
func (m *objectEncoder) AddUint16(k string, v uint16) {}
func (m *objectEncoder) AddUint8(k string, v uint8) {}
func (m *objectEncoder) AddUintptr(k string, v uintptr) {}
func (m *objectEncoder) AddUint(k string, v uint) {}
func (m *objectEncoder) AddUint32(k string, v uint32) {}
func (m *objectEncoder) AddUint16(k string, v uint16) {}
func (m *objectEncoder) AddUint8(k string, v uint8) {}
func (m *objectEncoder) AddUintptr(k string, v uintptr) {}
21 changes: 21 additions & 0 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package otelzap

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -77,6 +78,26 @@ func TestObjectEncoder(t *testing.T) {
f: func(e zapcore.ObjectEncoder) { e.AddString("k", "v") },
expected: "v",
},
{
desc: "AddDuration",
f: func(e zapcore.ObjectEncoder) { e.AddDuration("k", time.Millisecond) },
expected: int64(1000000),
},
{
desc: "AddTime",
f: func(e zapcore.ObjectEncoder) { e.AddTime("k", time.Unix(0, 100)) },
expected: time.Unix(0, 100).UnixNano(),
},
{
desc: "AddComplex128",
f: func(e zapcore.ObjectEncoder) { e.AddComplex128("k", 1+2i) },
expected: map[string]interface{}{"i": float64(2), "r": float64(1)},
},
{
desc: "AddComplex64",
f: func(e zapcore.ObjectEncoder) { e.AddComplex64("k", 1+2i) },
expected: map[string]interface{}{"i": float64(2), "r": float64(1)},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 8c03dfa

Please sign in to comment.