Skip to content

Commit

Permalink
Merge branch 'main' into unconvert-linter
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared authored Jun 26, 2024
2 parents 9dba81d + ff65757 commit bc693c6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
1 change: 0 additions & 1 deletion bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
r.SetSeverity(convertLevel(ent.Level))
r.SetSeverityText(ent.Level.String())

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

r.AddAttributes(o.attr...)
Expand Down
102 changes: 102 additions & 0 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 Down Expand Up @@ -178,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)
}
}
})
})
}
}

0 comments on commit bc693c6

Please sign in to comment.