From 311a4d0077708c64d290eeadae9401a181f79a04 Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Tue, 4 Jun 2024 15:27:39 +0530 Subject: [PATCH] otelzap: Implement namespace method --- bridges/otelzap/encoder.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/bridges/otelzap/encoder.go b/bridges/otelzap/encoder.go index eed14b0798f..d32e6b4e170 100644 --- a/bridges/otelzap/encoder.go +++ b/bridges/otelzap/encoder.go @@ -16,10 +16,19 @@ var ( _ zapcore.ArrayEncoder = (*arrayEncoder)(nil) ) +type newNameSpace struct { + ns string + kv []log.KeyValue + next *newNameSpace +} + // objectEncoder implements zapcore.ObjectEncoder. // It encodes given fields to OTel key-values. type objectEncoder struct { - kv []log.KeyValue + // root is a pointer to the default namespace + root *newNameSpace + // cur is a pointer to the namespace we're currently writing to. + cur *newNameSpace } // nolint:unused @@ -31,6 +40,15 @@ func newObjectEncoder(len int) *objectEncoder { } } +// It iterates to the end of the linked list and appends namespace data. +func (m *objectEncoder) getObjValue(o *newNameSpace) { + if o.next == nil { + return + } + m.getObjValue(o.next) + o.kv = append(o.kv, log.Map(o.next.ns, o.next.kv...)) +} + func (m *objectEncoder) AddArray(key string, v zapcore.ArrayMarshaler) error { // TODO: Use arrayEncoder from a pool. arr := &arrayEncoder{} @@ -101,7 +119,14 @@ func (m *objectEncoder) AddReflected(k string, v interface{}) error { // OpenNamespace opens an isolated namespace where all subsequent fields will // be added. func (m *objectEncoder) OpenNamespace(k string) { - // TODO + + keyValue := make([]log.KeyValue, 0, 5) + s := &newNameSpace{ + ns: k, + kv: keyValue, + } + m.cur.next = s + m.cur = s } func (m *objectEncoder) AddComplex64(k string, v complex64) {