Skip to content

Commit

Permalink
Fix log.EnabledParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Dec 12, 2024
1 parent 1024029 commit bf65345
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 45 deletions.
3 changes: 1 addition & 2 deletions bridges/otellogr/logsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@ var _ logr.LogSink = (*LogSink)(nil)
// For example, commandline flags might be used to set the logging
// verbosity and disable some info logs.
func (l *LogSink) Enabled(level int) bool {
var param log.EnabledParameters
param.SetSeverity(l.levelSeverity(level))
ctx := context.Background()
param := log.EnabledParameters{Severity: l.levelSeverity(level)}
return l.logger.Enabled(ctx, param)
}

Expand Down
6 changes: 1 addition & 5 deletions bridges/otellogr/logsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,7 @@ func TestLogSink(t *testing.T) {

func TestLogSinkEnabled(t *testing.T) {
enabledFunc := func(ctx context.Context, param log.EnabledParameters) bool {
lvl, ok := param.Severity()
if !ok {
return true
}
return lvl == log.SeverityInfo
return param.Severity == log.SeverityInfo
}

rec := logtest.NewRecorder(logtest.WithEnabledFunc(enabledFunc))
Expand Down
3 changes: 1 addition & 2 deletions bridges/otelslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,8 @@ func (h *Handler) convertRecord(r slog.Record) log.Record {
// Enable returns true if the Handler is enabled to log for the provided
// context and Level. Otherwise, false is returned if it is not enabled.
func (h *Handler) Enabled(ctx context.Context, l slog.Level) bool {
var param log.EnabledParameters
const sevOffset = slog.Level(log.SeverityDebug) - slog.LevelDebug
param.SetSeverity(log.Severity(l + sevOffset))
param := log.EnabledParameters{Severity: log.Severity(l + sevOffset)}
return h.logger.Enabled(ctx, param)
}

Expand Down
6 changes: 1 addition & 5 deletions bridges/otelslog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ type enablerKey uint
var enableKey enablerKey

func (r *recorder) Enabled(ctx context.Context, param log.EnabledParameters) bool {
lvl, ok := param.Severity()
if !ok {
return true
}
return ctx.Value(enableKey) != nil || lvl >= r.MinSeverity
return ctx.Value(enableKey) != nil || param.Severity >= r.MinSeverity
}

func (r *recorder) Emit(_ context.Context, record log.Record) {
Expand Down
6 changes: 2 additions & 4 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ func NewCore(name string, opts ...Option) *Core {

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

Expand Down Expand Up @@ -179,8 +178,7 @@ func (o *Core) Sync() error {
// 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 {
param := log.EnabledParameters{}
param.SetSeverity(convertLevel(ent.Level))
param := log.EnabledParameters{Severity: convertLevel(ent.Level)}

logger := o.logger
if ent.LoggerName != "" {
Expand Down
6 changes: 1 addition & 5 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ func TestCore(t *testing.T) {

func TestCoreEnabled(t *testing.T) {
enabledFunc := func(c context.Context, param log.EnabledParameters) bool {
lvl, ok := param.Severity()
if !ok {
return true
}
return lvl >= log.SeverityInfo
return param.Severity >= log.SeverityInfo
}

r := logtest.NewRecorder(logtest.WithEnabledFunc(enabledFunc))
Expand Down
6 changes: 3 additions & 3 deletions processors/minsev/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func ExampleSeveritier() {

p := NewLogProcessor(&processor{}, EnvSeverity{})

ctx, params := context.Background(), log.EnabledParameters{}
params.SetSeverity(log.SeverityDebug)
ctx := context.Background()
params := log.EnabledParameters{Severity: log.SeverityDebug}
fmt.Println(p.Enabled(ctx, params))

params.SetSeverity(log.SeverityError)
params.Severity = log.SeverityError
fmt.Println(p.Enabled(ctx, params))

// Output:
Expand Down
6 changes: 1 addition & 5 deletions processors/minsev/minsev.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ func (p *LogProcessor) OnEmit(ctx context.Context, record *log.Record) error {
// severity of param is greater than or equal to p.Minimum. Otherwise false is
// returned.
func (p *LogProcessor) Enabled(ctx context.Context, param api.EnabledParameters) bool {
sev, ok := param.Severity()
if !ok {
return true
}

sev := param.Severity
if p.filter != nil {
return sev >= p.sev.Severity() &&
p.filter.Enabled(ctx, param)
Expand Down
25 changes: 11 additions & 14 deletions processors/minsev/minsev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,18 @@ func TestLogProcessorDynamicSeverity(t *testing.T) {
p := NewLogProcessor(wrapped, sev)

ctx := context.Background()
params := &api.EnabledParameters{}
params.SetSeverity(api.SeverityDebug)
params := &api.EnabledParameters{Severity: api.SeverityDebug}
assert.False(t, p.Enabled(ctx, *params), api.SeverityDebug.String())

params.SetSeverity(api.SeverityInfo)
params.Severity = api.SeverityInfo
assert.True(t, p.Enabled(ctx, *params), api.SeverityInfo.String())

sev.Set(SeverityError)

params.SetSeverity(api.SeverityInfo)
params.Severity = api.SeverityInfo
assert.False(t, p.Enabled(ctx, *params), api.SeverityInfo.String())

params.SetSeverity(api.SeverityError)
params.Severity = api.SeverityError
assert.True(t, p.Enabled(ctx, *params), api.SeverityError.String())
}

Expand Down Expand Up @@ -138,7 +137,7 @@ func TestLogProcessorEnabled(t *testing.T) {
ctx := context.Background()
param := api.EnabledParameters{}
for _, sev := range severities {
param.SetSeverity(sev)
param.Severity = sev
assert.True(t, p.Enabled(ctx, param), sev.String())

if assert.Lenf(t, wrapped.EnabledCalls, 1, "Record with severity %s not passed-through", sev) {
Expand All @@ -156,7 +155,7 @@ func TestLogProcessorEnabled(t *testing.T) {
ctx := context.Background()
param := api.EnabledParameters{}
for _, sev := range severities {
param.SetSeverity(sev)
param.Severity = sev
assert.False(t, p.Enabled(ctx, param), sev.String())

if !assert.Emptyf(t, wrapped.EnabledCalls, "Record with severity %s passed-through", sev) {
Expand All @@ -173,13 +172,13 @@ func TestLogProcessorEnabled(t *testing.T) {
ctx := context.Background()
params := &api.EnabledParameters{}

params.SetSeverity(api.SeverityDebug)
params.Severity = api.SeverityDebug
assert.False(t, p.Enabled(ctx, *params))

params.SetSeverity(api.SeverityInfo)
params.Severity = api.SeverityInfo
assert.True(t, p.Enabled(ctx, *params))

params.SetSeverity(api.SeverityError)
params.Severity = api.SeverityError
assert.True(t, p.Enabled(ctx, *params))

assert.Empty(t, wrapped.EnabledCalls)
Expand Down Expand Up @@ -214,8 +213,7 @@ func TestLogProcessorNilDownstream(t *testing.T) {
ctx := context.Background()
r := new(log.Record)
r.SetSeverity(api.SeverityTrace1)
param := api.EnabledParameters{}
param.SetSeverity(api.SeverityTrace1)
param := api.EnabledParameters{Severity: api.SeverityTrace1}
assert.NotPanics(t, func() {
assert.NoError(t, p.OnEmit(ctx, r))
assert.False(t, p.Enabled(ctx, param))
Expand All @@ -227,8 +225,7 @@ func TestLogProcessorNilDownstream(t *testing.T) {
func BenchmarkLogProcessor(b *testing.B) {
r := new(log.Record)
r.SetSeverity(api.SeverityTrace)
param := api.EnabledParameters{}
param.SetSeverity(api.SeverityTrace)
param := api.EnabledParameters{Severity: api.SeverityTrace}
ctx := context.Background()

type combo interface {
Expand Down

0 comments on commit bf65345

Please sign in to comment.