From 52ee3c48b98bdcfe040e37087b45a807a0211752 Mon Sep 17 00:00:00 2001 From: Khushi Jain Date: Tue, 21 May 2024 19:52:38 +0530 Subject: [PATCH] otelzap: Implement Uint methods on encoder (#5609) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Part of https://github.com/open-telemetry/opentelemetry-go-contrib/issues/5191 Pre-work https://github.com/open-telemetry/opentelemetry-go-contrib/pull/5279 This PR implements `uInt` methods `objectEncoder` --------- Co-authored-by: Robert PajÄ…k --- bridges/otelzap/encoder.go | 39 +++++++++++++++++++++++++++------ bridges/otelzap/encoder_test.go | 35 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/bridges/otelzap/encoder.go b/bridges/otelzap/encoder.go index ac05450b1b6..6c6b82f4626 100644 --- a/bridges/otelzap/encoder.go +++ b/bridges/otelzap/encoder.go @@ -76,8 +76,12 @@ func (m *objectEncoder) AddString(k string, v string) { m.kv = append(m.kv, log.String(k, v)) } -// TODO. func (m *objectEncoder) AddUint64(k string, v uint64) { + m.kv = append(m.kv, + log.KeyValue{ + Key: k, + Value: assignUintValue(v), + }) } // TODO. @@ -115,9 +119,30 @@ func (m *objectEncoder) AddInt8(k string, v int8) { m.AddInt64(k, int64(v)) } -// TODO. -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) { + m.AddUint64(k, uint64(v)) +} + +func (m *objectEncoder) AddUint32(k string, v uint32) { + m.AddInt64(k, int64(v)) +} + +func (m *objectEncoder) AddUint16(k string, v uint16) { + m.AddInt64(k, int64(v)) +} + +func (m *objectEncoder) AddUint8(k string, v uint8) { + m.AddInt64(k, int64(v)) +} + +func (m *objectEncoder) AddUintptr(k string, v uintptr) { + m.AddUint64(k, uint64(v)) +} + +func assignUintValue(v uint64) log.Value { + const maxInt64 = ^uint64(0) >> 1 + if v > maxInt64 { + return log.Float64Value(float64(v)) + } + return log.Int64Value(int64(v)) +} diff --git a/bridges/otelzap/encoder_test.go b/bridges/otelzap/encoder_test.go index 61f3e72ad5c..81b342e7b24 100644 --- a/bridges/otelzap/encoder_test.go +++ b/bridges/otelzap/encoder_test.go @@ -78,6 +78,41 @@ func TestObjectEncoder(t *testing.T) { f: func(e zapcore.ObjectEncoder) { e.AddString("k", "v") }, expected: "v", }, + { + desc: "AddUint64", + f: func(e zapcore.ObjectEncoder) { e.AddUint64("k", 42) }, + expected: int64(42), + }, + { + desc: "AddUint64-Overflow", + f: func(e zapcore.ObjectEncoder) { e.AddUint64("k", ^uint64(0)) }, + expected: float64(^uint64(0)), + }, + { + desc: "AddUint", + f: func(e zapcore.ObjectEncoder) { e.AddUint("k", 42) }, + expected: int64(42), + }, + { + desc: "AddUint32", + f: func(e zapcore.ObjectEncoder) { e.AddUint32("k", 42) }, + expected: int64(42), + }, + { + desc: "AddUint16", + f: func(e zapcore.ObjectEncoder) { e.AddUint16("k", 42) }, + expected: int64(42), + }, + { + desc: "AddUint8", + f: func(e zapcore.ObjectEncoder) { e.AddUint8("k", 42) }, + expected: int64(42), + }, + { + desc: "AddUintptr", + f: func(e zapcore.ObjectEncoder) { e.AddUintptr("k", 42) }, + expected: int64(42), + }, { desc: "AddDuration", f: func(e zapcore.ObjectEncoder) { e.AddDuration("k", time.Millisecond) },