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 With method #5653

Merged
merged 15 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 16 additions & 4 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap"

import (
"context"
"slices"

"go.uber.org/zap/zapcore"

Expand Down Expand Up @@ -88,6 +89,7 @@ func WithLoggerProvider(provider log.LoggerProvider) Option {
// Core is a [zapcore.Core] that sends logging records to OpenTelemetry.
type Core struct {
logger log.Logger
attr []log.KeyValue
}

// Compile-time check *Core implements zapcore.Core.
Expand All @@ -108,10 +110,20 @@ func (o *Core) Enabled(level zapcore.Level) bool {
return o.logger.Enabled(context.Background(), r)
}

// TODO
// With adds structured context to the Core.
func (o *Core) With(fields []zapcore.Field) zapcore.Core {
return o
cloned := o.clone()
if len(fields) > 0 {
cloned.attr = append(cloned.attr, convertField(fields)...)
}
return cloned
}

func (o *Core) clone() *Core {
return &Core{
logger: o.logger,
attr: slices.Clone(o.attr),
}
}

// TODO
Expand Down Expand Up @@ -143,9 +155,9 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
// TODO: Handle zap.Array.
// TODO: Handle ent.LoggerName.

r.AddAttributes(o.attr...)
if len(fields) > 0 {
attrbuf := convertField(fields)
r.AddAttributes(attrbuf...)
r.AddAttributes(convertField(fields)...)
}

o.logger.Emit(context.Background(), r)
Expand Down
63 changes: 54 additions & 9 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,61 @@ func TestCore(t *testing.T) {
zc := NewCore(loggerName, WithLoggerProvider(rec))
logger := zap.New(zc)

logger.Info(testMessage, zap.String(testKey, testValue))
t.Run("Write", func(t *testing.T) {
logger.Info(testMessage, zap.String(testKey, testValue))
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, 1, got.AttributesLen())
got.WalkAttributes(func(kv log.KeyValue) bool {
assert.Equal(t, testKey, string(kv.Key))
assert.Equal(t, testValue, value2Result(kv.Value))
return true
})
})

got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, 1, got.AttributesLen())
got.WalkAttributes(func(kv log.KeyValue) bool {
assert.Equal(t, testKey, string(kv.Key))
assert.Equal(t, testValue, value2Result(kv.Value))
return true
rec.Reset()

// test child logger with accumulated fields
t.Run("With", func(t *testing.T) {
testCases := [][]string{{"test1", "value1"}, {"test2", "value2"}}
childlogger := logger.With(zap.String(testCases[0][0], testCases[0][1]))
childlogger.Info(testMessage, zap.String(testCases[1][0], testCases[1][1]))

got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
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][1], value2Result(kv.Value))
index++
return true
})
})

rec.Reset()

t.Run("WithMultiple", func(t *testing.T) {
testCases := [][]string{{"test1", "value1"}, {"test2", "value2"}, {"test3", "value3"}}
childlogger := logger.With(zap.String(testCases[0][0], testCases[0][1]))
childlogger2 := childlogger.With(zap.String(testCases[1][0], testCases[1][1]))
childlogger2.Info(testMessage, zap.String(testCases[2][0], testCases[2][1]))

got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
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][1], value2Result(kv.Value))
index++
return true
})
})
}

Expand Down