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 Enabled method #5594

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ func NewCore(opts ...Option) *Core {
}
}

// TODO
// LevelEnabler decides whether a given logging level is enabled when logging a message.
func (o *Core) Enabled(level zapcore.Level) bool {
return true
r := log.Record{}
r.SetSeverity(convertLevel(level))
return o.logger.Enabled(context.Background(), r)
}

// TODO
Expand Down
27 changes: 26 additions & 1 deletion bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package otelzap

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -17,8 +18,9 @@ import (
"go.opentelemetry.io/otel/sdk/instrumentation"
)

var testMessage = "log message"

func TestCore(t *testing.T) {
testMessage := "log message"
rec := logtest.NewRecorder()
zc := NewCore(WithLoggerProvider(rec))
logger := zap.New(zc)
Expand All @@ -31,6 +33,29 @@ func TestCore(t *testing.T) {
assert.Equal(t, log.SeverityInfo, got.Severity())
}

func TestCoreEnabled(t *testing.T) {
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
enabledFunc := func(c context.Context, r log.Record) bool {
return r.Severity() >= log.SeverityInfo
}

r := logtest.NewRecorder(logtest.WithEnabledFunc(enabledFunc))
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
logger := zap.New(NewCore(WithLoggerProvider(r)))

if ce := logger.Check(zap.DebugLevel, testMessage); ce != nil {
ce.Write()
}

assert.Nil(t, r.Result()[1].Records)
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved

if ce := logger.Check(zap.InfoLevel, testMessage); ce != nil {
ce.Write()
}

got := r.Result()[1].Records[0]
khushijain21 marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
}

func TestNewCoreConfiguration(t *testing.T) {
t.Run("Default", func(t *testing.T) {
r := logtest.NewRecorder()
Expand Down