Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otelzap: Implement methods on objectEncoder #5595

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Copyright (c) 2016-2017 Uber Technologies, Inc.
package otelzap
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved

import (
Expand Down Expand Up @@ -93,3 +94,66 @@ func TestConvertLevel(t *testing.T) {
}
}
}

// Copied from field_test.go. https://github.com/uber-go/zap/blob/b39f8b6b6a44d8371a87610be50cce58eeeaabcb/zapcore/memory_encoder_test.go
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
func TestObjectEncoder(t *testing.T) {
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved

khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
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,
},
}

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
}
6 changes: 3 additions & 3 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ func (m *objectEncoder) AddObject(k string, v zapcore.ObjectMarshaler) error {

// AddBinary converts binary to log.Bytes.
func (m *objectEncoder) AddBinary(k string, v []byte) {
// TODO
m.kv = append(m.kv, log.Bytes(k, v))
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}

// 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.
Expand Down
Loading