Skip to content

Commit

Permalink
otelzap: Implement Enabled method (#5594)
Browse files Browse the repository at this point in the history
Part of #5191
---------
Co-authored-by: Robert Pająk <[email protected]>
  • Loading branch information
khushijain21 authored May 17, 2024
1 parent 6d343e9 commit 6646038
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
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
29 changes: 28 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,31 @@ func TestCore(t *testing.T) {
assert.Equal(t, log.SeverityInfo, got.Severity())
}

func TestCoreEnabled(t *testing.T) {
enabledFunc := func(c context.Context, r log.Record) bool {
return r.Severity() >= log.SeverityInfo
}

r := logtest.NewRecorder(logtest.WithEnabledFunc(enabledFunc))
logger := zap.New(NewCore(WithLoggerProvider(r)))

logger.Debug(testMessage)
assert.Empty(t, r.Result()[1].Records)

if ce := logger.Check(zap.DebugLevel, testMessage); ce != nil {
ce.Write()
}
assert.Empty(t, r.Result()[1].Records)

if ce := logger.Check(zap.InfoLevel, testMessage); ce != nil {
ce.Write()
}
require.Len(t, r.Result()[1].Records, 1)
got := r.Result()[1].Records[0]
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

0 comments on commit 6646038

Please sign in to comment.