From 8c03dfaf8ea4f56cd35eb013536619ccf4a5c677 Mon Sep 17 00:00:00 2001 From: Khushi Jain Date: Mon, 20 May 2024 13:47:55 +0530 Subject: [PATCH] otelzap: Implement methods on `objectEncoder` (#5602) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Part of #5191 --------- Co-authored-by: Robert PajÄ…k --- bridges/otelzap/encoder.go | 45 +++++++++++++++++++++++---------- bridges/otelzap/encoder_test.go | 21 +++++++++++++++ 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/bridges/otelzap/encoder.go b/bridges/otelzap/encoder.go index b480095fe47..ac05450b1b6 100644 --- a/bridges/otelzap/encoder.go +++ b/bridges/otelzap/encoder.go @@ -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) { @@ -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) {} diff --git a/bridges/otelzap/encoder_test.go b/bridges/otelzap/encoder_test.go index 24205718ec9..61f3e72ad5c 100644 --- a/bridges/otelzap/encoder_test.go +++ b/bridges/otelzap/encoder_test.go @@ -7,6 +7,7 @@ package otelzap import ( "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -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 {