Skip to content

Commit

Permalink
Add --instrument flag to regal lint (#1254)
Browse files Browse the repository at this point in the history
Same as for `opa eval`

Unrelated change is increasing the granularity of the memory allocation
collectors, as I found that helped a lot when tracking down allocations

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Nov 11, 2024
1 parent 22ec7cb commit 21aa8d4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type lintCommandParams struct {
enablePrint bool
metrics bool
profile bool
instrument bool
disableAll bool
enableAll bool
}
Expand Down Expand Up @@ -167,6 +168,8 @@ func init() {
"enable metrics reporting (currently supported only for JSON output format)")
lintCommand.Flags().BoolVar(&params.profile, "profile", false,
"enable profiling metrics to be added to reporting (currently supported only for JSON output format)")
lintCommand.Flags().BoolVar(&params.instrument, "instrument", false,
"enable instrumentation metrics to be added to reporting (currently supported only for JSON output format)")

lintCommand.Flags().VarP(&params.disable, "disable", "d",
"disable specific rule(s). This flag can be repeated.")
Expand Down Expand Up @@ -285,6 +288,10 @@ func lint(args []string, params *lintCommandParams) (report.Report, error) {
regal = regal.WithProfiling(true)
}

if params.instrument {
regal = regal.WithInstrumentation(true)
}

var userConfig config.Config

userConfigFile, err := readUserConfig(params, regalDir)
Expand Down
4 changes: 2 additions & 2 deletions cmd/profiling.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func wrapProfiling(f func([]string) error) func(*cobra.Command, []string) error
case "clock":
opts = append(opts, profile.ClockProfile)
case "mem_heap":
opts = append(opts, profile.MemProfileHeap)
opts = append(opts, profile.MemProfileHeap, profile.MemProfileRate(1024))
case "mem_allocs":
opts = append(opts, profile.MemProfileAllocs)
opts = append(opts, profile.MemProfileAllocs, profile.MemProfileRate(1024))
case "trace":
opts = append(opts, profile.TraceProfile)
case "goroutine":
Expand Down
16 changes: 16 additions & 0 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Linter struct {
disableAll bool
enableAll bool
profiling bool
instrumentation bool
}

//nolint:gochecknoglobals
Expand Down Expand Up @@ -199,6 +200,13 @@ func (l Linter) WithProfiling(enabled bool) Linter {
return l
}

// WithInstrumentation enables instrumentation metrics.
func (l Linter) WithInstrumentation(enabled bool) Linter {
l.instrumentation = enabled

return l
}

// WithPathPrefix sets the root path prefix for the linter.
// A root directory prefix can be used to resolve relative paths
// referenced in the linter configuration with absolute file paths or URIs.
Expand Down Expand Up @@ -719,6 +727,10 @@ func (l Linter) prepareRegoArgs(query ast.Body) ([]func(*rego.Rego), error) {
)
}

if l.instrumentation {
regoArgs = append(regoArgs, rego.Instrument(true))
}

if l.dataBundle != nil {
regoArgs = append(regoArgs, rego.ParsedBundle("internal", l.dataBundle))
}
Expand Down Expand Up @@ -864,6 +876,10 @@ func (l Linter) lintWithRegoRules(
evalArgs = append(evalArgs, rego.EvalQueryTracer(prof))
}

if l.instrumentation {
evalArgs = append(evalArgs, rego.EvalInstrument(true))
}

resultSet, err := pq.Eval(ctx, evalArgs...)
if err != nil {
errCh <- fmt.Errorf("error encountered in query evaluation %w", err)
Expand Down

0 comments on commit 21aa8d4

Please sign in to comment.