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

otezap: Add support for zap named loggers #5896

Merged
merged 7 commits into from
Jul 12, 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
66 changes: 42 additions & 24 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ func newConfig(options []Option) config {
return c
}

func (c config) logger(name string) log.Logger {
var opts []log.LoggerOption
if c.version != "" {
opts = append(opts, log.WithInstrumentationVersion(c.version))
}
if c.schemaURL != "" {
opts = append(opts, log.WithSchemaURL(c.schemaURL))
}
return c.provider.Logger(name, opts...)
}

// Option configures a [Core].
type Option interface {
apply(config) config
Expand Down Expand Up @@ -88,20 +77,37 @@ 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
ctx context.Context
provider log.LoggerProvider
logger log.Logger
opts []log.LoggerOption
attr []log.KeyValue
ctx context.Context
}

// Compile-time check *Core implements zapcore.Core.
var _ zapcore.Core = (*Core)(nil)

// NewCore creates a new [zapcore.Core] that can be used with [go.uber.org/zap.New].
// The name should be the package import path that is being logged.
// The name is ignored for named loggers created using [go.uber.org/zap.Logger.Named].
func NewCore(name string, opts ...Option) *Core {
cfg := newConfig(opts)

var loggerOpts []log.LoggerOption
if cfg.version != "" {
loggerOpts = append(loggerOpts, log.WithInstrumentationVersion(cfg.version))
}
if cfg.schemaURL != "" {
loggerOpts = append(loggerOpts, log.WithSchemaURL(cfg.schemaURL))
}

logger := cfg.provider.Logger(name, loggerOpts...)

return &Core{
logger: cfg.logger(name),
ctx: context.Background(),
provider: cfg.provider,
logger: logger,
opts: loggerOpts,
ctx: context.Background(),
}
}

Expand All @@ -127,9 +133,11 @@ func (o *Core) With(fields []zapcore.Field) zapcore.Core {

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

Expand All @@ -138,10 +146,18 @@ func (o *Core) Sync() error {
return nil
}

// Check determines whether the supplied Entry should be logged using core.Enabled method.
// Check determines whether the supplied Entry should be logged.
// If the entry should be logged, the Core adds itself to the CheckedEntry and returns the result.
func (o *Core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if o.Enabled(ent.Level) {
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
r := log.Record{}
r.SetSeverity(convertLevel(ent.Level))

logger := o.logger
if ent.LoggerName != "" {
logger = o.provider.Logger(ent.LoggerName, o.opts...)
}

if logger.Enabled(context.Background(), r) {
return ce.AddCore(ent, o)
}
return ce
Expand All @@ -155,8 +171,6 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
r.SetSeverity(convertLevel(ent.Level))
r.SetSeverityText(ent.Level.String())

// TODO: Handle ent.LoggerName.

r.AddAttributes(o.attr...)
if len(fields) > 0 {
ctx, attrbuf := convertField(fields)
Expand All @@ -166,7 +180,11 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
r.AddAttributes(attrbuf...)
}

o.logger.Emit(o.ctx, r)
logger := o.logger
if ent.LoggerName != "" {
logger = o.provider.Logger(ent.LoggerName, o.opts...)
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}
logger.Emit(o.ctx, r)
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ func TestCore(t *testing.T) {

rec.Reset()

t.Run("Named", func(t *testing.T) {
name := "my/pkg"
childlogger := logger.Named(name)
childlogger.Info(testMessage, zap.String(testKey, testValue))

found := false
for _, got := range rec.Result() {
found = got.Name == name
if found {
break
}
}
assert.True(t, found)
})

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]))
Expand Down