Skip to content

Commit

Permalink
Merge branch 'main' into cirilla/fix_b3_propagator_panic
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias authored Jun 26, 2024
2 parents 59ee3a0 + d9e0bf2 commit 56cfb6d
Show file tree
Hide file tree
Showing 66 changed files with 524 additions and 337 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ jobs:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
file: ./coverage.txt
fail_ci_if_error: true
verbose: true
- name: Store coverage test output
uses: actions/upload-artifact@v4
Expand Down
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ linters:
- misspell
- revive
- staticcheck
- tenv
- typecheck
- unconvert
- unparam
- unused

issues:
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ See the [public meeting notes](https://docs.google.com/document/d/1E5e7Ld0NuU1iV

Approvers:

- [Evan Torrie](https://github.com/evantorrie), Verizon Media
- [Josh MacDonald](https://github.com/jmacd), LightStep
- [Sam Xie](https://github.com/XSAM), Cisco/AppDynamics
- [Chester Cheung](https://github.com/hanyuancheung), Tencent
- [Damien Mathieu](https://github.com/dmathieu), Elastic
Expand All @@ -141,7 +139,9 @@ Maintainers:
Emeritus:

- [Gustavo Silva Paiva](https://github.com/paivagustavo), LightStep
- [Josh MacDonald](https://github.com/jmacd), LightStep
- [Anthony Mirabella](https://github.com/Aneurysm9), Amazon
- [Evan Torrie](https://github.com/evantorrie), Yahoo

### Become an Approver or a Maintainer

Expand Down
5 changes: 3 additions & 2 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
r.SetTimestamp(ent.Time)
r.SetBody(log.StringValue(ent.Message))
r.SetSeverity(convertLevel(ent.Level))
r.SetSeverityText(ent.Level.String())

// TODO: Handle zap.Namespace.
// TODO: Handle ent.LoggerName.

r.AddAttributes(o.attr...)
Expand Down Expand Up @@ -183,7 +183,8 @@ func convertField(fields []zapcore.Field) (context.Context, []log.KeyValue) {
field.AddTo(enc)
}

return ctx, enc.kv
enc.calculate(enc.root)
return ctx, enc.root.attrs
}

func convertLevel(level zapcore.Level) log.Severity {
Expand Down
112 changes: 109 additions & 3 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package otelzap

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -22,6 +24,10 @@ var (
loggerName = "name"
testKey = "key"
testValue = "value"
testEntry = zapcore.Entry{
Level: zap.InfoLevel,
Message: testMessage,
}
)

func TestCore(t *testing.T) {
Expand All @@ -34,9 +40,10 @@ func TestCore(t *testing.T) {
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, zap.InfoLevel.String(), got.SeverityText())
assert.Equal(t, 1, got.AttributesLen())
got.WalkAttributes(func(kv log.KeyValue) bool {
assert.Equal(t, testKey, string(kv.Key))
assert.Equal(t, testKey, kv.Key)
assert.Equal(t, testValue, value2Result(kv.Value))
return true
})
Expand All @@ -56,11 +63,12 @@ func TestCore(t *testing.T) {
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, zap.InfoLevel.String(), got.SeverityText())
assert.Equal(t, 2, got.AttributesLen())

index := 0
got.WalkAttributes(func(kv log.KeyValue) bool {
assert.Equal(t, testCases[index][0], string(kv.Key))
assert.Equal(t, testCases[index][0], kv.Key)
assert.Equal(t, testCases[index][1], value2Result(kv.Value))
index++
return true
Expand All @@ -78,11 +86,12 @@ func TestCore(t *testing.T) {
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, zap.InfoLevel.String(), got.SeverityText())
assert.Equal(t, 3, got.AttributesLen())

index := 0
got.WalkAttributes(func(kv log.KeyValue) bool {
assert.Equal(t, testCases[index][0], string(kv.Key))
assert.Equal(t, testCases[index][0], kv.Key)
assert.Equal(t, testCases[index][1], value2Result(kv.Value))
index++
return true
Expand Down Expand Up @@ -113,6 +122,7 @@ func TestCoreEnabled(t *testing.T) {
got := r.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, zap.InfoLevel.String(), got.SeverityText())
}

func TestNewCoreConfiguration(t *testing.T) {
Expand Down Expand Up @@ -174,3 +184,99 @@ func TestConvertLevel(t *testing.T) {
}
}
}

func BenchmarkCoreWrite(b *testing.B) {
benchmarks := []struct {
name string
fields []zapcore.Field
}{
{
name: "10 fields",
fields: []zapcore.Field{
zap.Int16("a", 1),
zap.String("k", "a"),
zap.Bool("k", true),
zap.Time("k", time.Unix(1000, 1000)),
zap.Binary("k", []byte{1, 2}),
zap.ByteString("k", []byte{1, 2}),
zap.Object("k", loggable{true}),
zap.Array("k", loggable{true}),
zap.String("k", "a"),
zap.Ints("k", []int{1, 2}),
},
},
{
name: "20 fields",
fields: []zapcore.Field{
zap.Int16("a", 1),
zap.String("k", "a"),
zap.Bool("k", true),
zap.Time("k", time.Unix(1000, 1000)),
zap.Binary("k", []byte{1, 2}),
zap.ByteString("k", []byte{1, 2}),
zap.Object("k", loggable{true}),
zap.String("k", "a"),
zap.Array("k", loggable{true}),
zap.Ints("k", []int{1, 2}),
zap.Int16("a", 1),
zap.String("k", "a"),
zap.Bool("k", true),
zap.Time("k", time.Unix(1000, 1000)),
zap.Binary("k", []byte{1, 2}),
zap.ByteString("k", []byte{1, 2}),
zap.Object("k", loggable{true}),
zap.Array("k", loggable{true}),
zap.String("k", "a"),
zap.Ints("k", []int{1, 2}),
},
},
{ // Benchmark with nested namespace
name: "Namespace",
fields: []zapcore.Field{
zap.Namespace("a"),
zap.Int16("a", 1),
zap.String("k", "a"),
zap.Bool("k", true),
zap.Time("k", time.Unix(1000, 1000)),
zap.Binary("k", []byte{1, 2}),
zap.Namespace("b"),
zap.Binary("k", []byte{1, 2}),
zap.Object("k", loggable{true}),
zap.String("k", "a"),
zap.Array("k", loggable{true}),
zap.Ints("k", []int{1, 2}),
},
},
}

for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
zc := NewCore(loggerName)
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := zc.Write(testEntry, bm.fields)
if err != nil {
b.Errorf("Unexpected error: %v", err)
}
}
})
})
}

for _, bm := range benchmarks {
b.Run(fmt.Sprint("With", bm.name), func(b *testing.B) {
zc := NewCore(loggerName)
zc1 := zc.With(bm.fields)
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := zc1.Write(testEntry, []zapcore.Field{})
if err != nil {
b.Errorf("Unexpected error: %v", err)
}
}
})
})
}
}
67 changes: 48 additions & 19 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,69 @@ var (
_ zapcore.ArrayEncoder = (*arrayEncoder)(nil)
)

type namespace struct {
name string
attrs []log.KeyValue
next *namespace
}

// 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 *namespace
// cur is a pointer to the namespace we're currently writing to.
cur *namespace
}

// nolint:unused
func newObjectEncoder(len int) *objectEncoder {
keyval := make([]log.KeyValue, 0, len)

m := &namespace{
attrs: keyval,
}
return &objectEncoder{
kv: keyval,
root: m,
cur: m,
}
}

// It iterates to the end of the linked list and appends namespace data.
// Run this function before accessing complete result.
func (m *objectEncoder) calculate(o *namespace) {
if o.next == nil {
return
}
m.calculate(o.next)
o.attrs = append(o.attrs, log.Map(o.next.name, o.next.attrs...))
}

func (m *objectEncoder) AddArray(key string, v zapcore.ArrayMarshaler) error {
// TODO: Use arrayEncoder from a pool.
arr := &arrayEncoder{}
err := v.MarshalLogArray(arr)
m.kv = append(m.kv, log.Slice(key, arr.elems...))
m.cur.attrs = append(m.cur.attrs, log.Slice(key, arr.elems...))
return err
}

func (m *objectEncoder) AddObject(k string, v zapcore.ObjectMarshaler) error {
// TODO: Use objectEncoder from a pool.
newobj := newObjectEncoder(2)
err := v.MarshalLogObject(newobj)
m.kv = append(m.kv, log.Map(k, newobj.kv...))
newobj.calculate(newobj.root)
m.cur.attrs = append(m.cur.attrs, log.Map(k, newobj.root.attrs...))
return err
}

func (m *objectEncoder) AddBinary(k string, v []byte) {
m.kv = append(m.kv, log.Bytes(k, v))
m.cur.attrs = append(m.cur.attrs, log.Bytes(k, v))
}

func (m *objectEncoder) AddByteString(k string, v []byte) {
m.kv = append(m.kv, log.String(k, string(v)))
m.cur.attrs = append(m.cur.attrs, log.String(k, string(v)))
}

func (m *objectEncoder) AddBool(k string, v bool) {
m.kv = append(m.kv, log.Bool(k, v))
m.cur.attrs = append(m.cur.attrs, log.Bool(k, v))
}

func (m *objectEncoder) AddDuration(k string, v time.Duration) {
Expand All @@ -68,35 +90,35 @@ func (m *objectEncoder) AddDuration(k string, v time.Duration) {
func (m *objectEncoder) AddComplex128(k string, v complex128) {
r := log.Float64("r", real(v))
i := log.Float64("i", imag(v))
m.kv = append(m.kv, log.Map(k, r, i))
m.cur.attrs = append(m.cur.attrs, log.Map(k, r, i))
}

func (m *objectEncoder) AddFloat64(k string, v float64) {
m.kv = append(m.kv, log.Float64(k, v))
m.cur.attrs = append(m.cur.attrs, log.Float64(k, v))
}

func (m *objectEncoder) AddInt64(k string, v int64) {
m.kv = append(m.kv, log.Int64(k, v))
m.cur.attrs = append(m.cur.attrs, log.Int64(k, v))
}

func (m *objectEncoder) AddInt(k string, v int) {
m.kv = append(m.kv, log.Int(k, v))
m.cur.attrs = append(m.cur.attrs, log.Int(k, v))
}

func (m *objectEncoder) AddString(k string, v string) {
m.kv = append(m.kv, log.String(k, v))
m.cur.attrs = append(m.cur.attrs, log.String(k, v))
}

func (m *objectEncoder) AddUint64(k string, v uint64) {
m.kv = append(m.kv,
m.cur.attrs = append(m.cur.attrs,
log.KeyValue{
Key: k,
Value: assignUintValue(v),
})
}

func (m *objectEncoder) AddReflected(k string, v interface{}) error {
m.kv = append(m.kv,
m.cur.attrs = append(m.cur.attrs,
log.KeyValue{
Key: k,
Value: convertValue(v),
Expand All @@ -107,7 +129,13 @@ 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 := &namespace{
name: k,
attrs: keyValue,
}
m.cur.next = s
m.cur = s
}

func (m *objectEncoder) AddComplex64(k string, v complex64) {
Expand Down Expand Up @@ -179,7 +207,8 @@ func (a *arrayEncoder) AppendObject(v zapcore.ObjectMarshaler) error {
// TODO: Use objectEncoder from a pool.
m := newObjectEncoder(2)
err := v.MarshalLogObject(m)
a.elems = append(a.elems, log.MapValue(m.kv...))
m.calculate(m.root)
a.elems = append(a.elems, log.MapValue(m.root.attrs...))
return err
}

Expand Down Expand Up @@ -231,7 +260,7 @@ func (a *arrayEncoder) AppendDuration(v time.Duration) { a.AppendInt64(v.Nanosec
func (a *arrayEncoder) AppendInt32(v int32) { a.AppendInt64(int64(v)) }
func (a *arrayEncoder) AppendInt16(v int16) { a.AppendInt64(int64(v)) }
func (a *arrayEncoder) AppendInt8(v int8) { a.AppendInt64(int64(v)) }
func (a *arrayEncoder) AppendTime(v time.Time) { a.AppendInt64(int64(v.UnixNano())) }
func (a *arrayEncoder) AppendTime(v time.Time) { a.AppendInt64(v.UnixNano()) }
func (a *arrayEncoder) AppendUint(v uint) { a.AppendUint64(uint64(v)) }
func (a *arrayEncoder) AppendUint32(v uint32) { a.AppendInt64(int64(v)) }
func (a *arrayEncoder) AppendUint16(v uint16) { a.AppendInt64(int64(v)) }
Expand Down
Loading

0 comments on commit 56cfb6d

Please sign in to comment.