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

avoid executing costly With if noop logger #1015

Merged
merged 1 commit into from
Mar 15, 2024
Merged
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
17 changes: 11 additions & 6 deletions internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,14 @@ func (r *Rule) Evaluate(phase types.RulePhase, tx plugintypes.TransactionState,
// collectiveMatchedValues lives across recursive calls of doEvaluate
var collectiveMatchedValues []types.MatchData

var logger debuglog.Logger
logger := tx.DebugLogger()

if r.ID_ == noID {
logger = tx.DebugLogger().With(debuglog.Str("rule_ref", fmt.Sprintf("%s#L%d", r.File_, r.Line_)))
} else {
logger = tx.DebugLogger().With(debuglog.Int("rule_id", r.ID_))
if logger.Debug().IsEnabled() {
if r.ID_ == noID {
logger = logger.With(debuglog.Str("rule_ref", fmt.Sprintf("%s#L%d", r.File_, r.Line_)))
} else {
logger = logger.With(debuglog.Int("rule_id", r.ID_))
}
}

r.doEvaluate(logger, phase, tx.(*Transaction), &collectiveMatchedValues, chainLevelZero, cache)
Expand Down Expand Up @@ -245,7 +247,10 @@ func (r *Rule) doEvaluate(logger debuglog.Logger, phase types.RulePhase, tx *Tra

values = tx.GetField(v)

vLog := logger.With(debuglog.Str("variable", v.Variable.Name()))
vLog := logger
if logger.Debug().IsEnabled() {
vLog = logger.With(debuglog.Str("variable", v.Variable.Name()))
}
vLog.Debug().Msg("Expanding arguments for rule")

for i, arg := range values {
Expand Down
Loading