Skip to content

Commit

Permalink
add progress logs for rules
Browse files Browse the repository at this point in the history
Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan committed Jul 14, 2023
1 parent 60dc204 commit cbb29c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"

"go.lsp.dev/uri"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -151,6 +152,11 @@ func (r *ruleEngine) RunRules(ctx context.Context, ruleSets []RuleSet, selectors
// Need a better name for this thing
ret := make(chan response)

var totalRules int32
var matchedRules int32
var unmatchedRules int32
var failedRules int32

wg := &sync.WaitGroup{}
// Handle returns
go func() {
Expand All @@ -161,7 +167,9 @@ func (r *ruleEngine) RunRules(ctx context.Context, ruleSets []RuleSet, selectors
r.logger.Info("rule returned", "rule", response.Rule.RuleID)
defer wg.Done()
if response.Err != nil {
atomic.AddInt32(&failedRules, 1)
r.logger.Error(response.Err, "failed to evaluate rule", "ruleID", response.Rule.RuleID)

if rs, ok := mapRuleSets[response.RuleSetName]; ok {
rs.Errors[response.Rule.RuleID] = response.Err.Error()
}
Expand All @@ -170,18 +178,25 @@ func (r *ruleEngine) RunRules(ctx context.Context, ruleSets []RuleSet, selectors
if err != nil {
r.logger.Error(err, "unable to create violation from response")
}
atomic.AddInt32(&matchedRules, 1)

rs, ok := mapRuleSets[response.RuleSetName]
if !ok {
r.logger.Info("this should never happen that we don't find the ruleset")
}
rs.Violations[response.Rule.RuleID] = violation
} else {
atomic.AddInt32(&unmatchedRules, 1)
// Log that rule did not pass
r.logger.V(5).Info("rule was evaluated, and we did not find a violation", "rule", response.Rule.RuleID)

if rs, ok := mapRuleSets[response.RuleSetName]; ok {
rs.Unmatched = append(rs.Unmatched, response.Rule.RuleID)
}
}
atomic.AddInt32(&totalRules, 1)
r.logger.V(5).Info("rule response received", "total", totalRules, "failed", failedRules, "matched", matchedRules, "unmatched", unmatchedRules)

}()
case <-ctx.Done():
// At this point we should just return the function, we may want to close the wait group too.
Expand Down
3 changes: 3 additions & 0 deletions parser/rule_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (r *RuleParser) LoadRule(filepath string) ([]engine.Rule, map[string]provid
rules := []engine.Rule{}
ruleIDMap := map[string]*struct{}{}
providers := map[string]provider.InternalProviderClient{}
rulesParsed := 0
for _, ruleMap := range ruleMap {
ruleID, ok := ruleMap["ruleID"].(string)
if !ok {
Expand Down Expand Up @@ -419,6 +420,8 @@ func (r *RuleParser) LoadRule(filepath string) ([]engine.Rule, map[string]provid
} else {
rules = append(rules, rule)
}
rulesParsed++
r.Log.V(5).Info("rules parsed", "parsed", rulesParsed)
}

return append(infoRules, rules...), providers, nil
Expand Down

0 comments on commit cbb29c1

Please sign in to comment.