cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
cmd/pint/scan.go
272lines · modecode
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "log/slog" |
| 8 | "sync" |
| 9 | "time" |
| 10 | |
| 11 | "go.uber.org/atomic" |
| 12 | |
| 13 | "github.com/cloudflare/pint/internal/checks" |
| 14 | "github.com/cloudflare/pint/internal/comments" |
| 15 | "github.com/cloudflare/pint/internal/config" |
| 16 | "github.com/cloudflare/pint/internal/discovery" |
| 17 | "github.com/cloudflare/pint/internal/parser" |
| 18 | "github.com/cloudflare/pint/internal/promapi" |
| 19 | "github.com/cloudflare/pint/internal/reporter" |
| 20 | ) |
| 21 | |
| 22 | const ( |
| 23 | yamlParseReporter = "yaml/parse" |
| 24 | ignoreFileReporter = "ignore/file" |
| 25 | pintCommentReporter = "pint/comment" |
| 26 | |
| 27 | yamlDetails = `This Prometheus rule is not valid. |
| 28 | This usually means that it's missing some required fields.` |
| 29 | ) |
| 30 | |
| 31 | func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.PrometheusGenerator, cfg config.Config, entries []discovery.Entry) (summary reporter.Summary, err error) { |
| 32 | if isOffline { |
| 33 | slog.Info("Offline mode, skipping Prometheus discovery") |
| 34 | } else { |
| 35 | if len(entries) > 0 { |
| 36 | if err = gen.GenerateDynamic(ctx); err != nil { |
| 37 | return summary, err |
| 38 | } |
| 39 | slog.Debug("Generated all Prometheus servers", slog.Int("count", gen.Count())) |
| 40 | } else { |
| 41 | slog.Info("No rules found, skipping Prometheus discovery") |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | checkIterationChecks.Set(0) |
| 46 | checkIterationChecksDone.Set(0) |
| 47 | |
| 48 | start := time.Now() |
| 49 | defer func() { |
| 50 | lastRunDuration.Set(time.Since(start).Seconds()) |
| 51 | }() |
| 52 | |
| 53 | jobs := make(chan scanJob, workers*5) |
| 54 | results := make(chan reporter.Report, workers*5) |
| 55 | wg := sync.WaitGroup{} |
| 56 | |
| 57 | ctx = context.WithValue(ctx, promapi.AllPrometheusServers, gen.Servers()) |
| 58 | for _, s := range cfg.Check { |
| 59 | settings, _ := s.Decode() |
| 60 | key := checks.SettingsKey(s.Name) |
| 61 | ctx = context.WithValue(ctx, key, settings) |
| 62 | } |
| 63 | |
| 64 | for w := 1; w <= workers; w++ { |
| 65 | wg.Add(1) |
| 66 | go func() { |
| 67 | defer wg.Done() |
| 68 | scanWorker(ctx, jobs, results) |
| 69 | }() |
| 70 | } |
| 71 | |
| 72 | go func() { |
| 73 | defer close(results) |
| 74 | wg.Wait() |
| 75 | }() |
| 76 | |
| 77 | var onlineChecksCount, offlineChecksCount, checkedEntriesCount atomic.Int64 |
| 78 | go func() { |
| 79 | for _, entry := range entries { |
| 80 | switch { |
| 81 | case entry.State == discovery.Excluded: |
| 82 | continue |
| 83 | case entry.PathError != nil && entry.State == discovery.Removed: |
| 84 | continue |
| 85 | case entry.Rule.Error.Err != nil && entry.State == discovery.Removed: |
| 86 | continue |
| 87 | case entry.PathError == nil && entry.Rule.Error.Err == nil: |
| 88 | if entry.Rule.RecordingRule != nil { |
| 89 | rulesParsedTotal.WithLabelValues(config.RecordingRuleType).Inc() |
| 90 | slog.Debug("Found recording rule", |
| 91 | slog.String("path", entry.Path.Name), |
| 92 | slog.String("record", entry.Rule.RecordingRule.Record.Value), |
| 93 | slog.String("lines", entry.Rule.Lines.String()), |
| 94 | ) |
| 95 | } |
| 96 | if entry.Rule.AlertingRule != nil { |
| 97 | rulesParsedTotal.WithLabelValues(config.AlertingRuleType).Inc() |
| 98 | slog.Debug("Found alerting rule", |
| 99 | slog.String("path", entry.Path.Name), |
| 100 | slog.String("alert", entry.Rule.AlertingRule.Alert.Value), |
| 101 | slog.String("lines", entry.Rule.Lines.String()), |
| 102 | ) |
| 103 | } |
| 104 | |
| 105 | checkedEntriesCount.Inc() |
| 106 | checkList := cfg.GetChecksForRule(ctx, gen, entry, entry.DisabledChecks) |
| 107 | for _, check := range checkList { |
| 108 | checkIterationChecks.Inc() |
| 109 | if check.Meta().IsOnline { |
| 110 | onlineChecksCount.Inc() |
| 111 | } else { |
| 112 | offlineChecksCount.Inc() |
| 113 | } |
| 114 | jobs <- scanJob{entry: entry, allEntries: entries, check: check} |
| 115 | } |
| 116 | default: |
| 117 | if entry.Rule.Error.Err != nil { |
| 118 | slog.Debug("Found invalid rule", |
| 119 | slog.String("path", entry.Path.Name), |
| 120 | slog.String("lines", entry.Rule.Lines.String()), |
| 121 | ) |
| 122 | rulesParsedTotal.WithLabelValues(config.InvalidRuleType).Inc() |
| 123 | } |
| 124 | jobs <- scanJob{entry: entry, allEntries: entries, check: nil} |
| 125 | } |
| 126 | } |
| 127 | defer close(jobs) |
| 128 | }() |
| 129 | |
| 130 | for result := range results { |
| 131 | summary.Report(result) |
| 132 | } |
| 133 | summary.SortReports() |
| 134 | summary.Duration = time.Since(start) |
| 135 | summary.TotalEntries = len(entries) |
| 136 | summary.CheckedEntries = checkedEntriesCount.Load() |
| 137 | summary.OnlineChecks = onlineChecksCount.Load() |
| 138 | summary.OfflineChecks = offlineChecksCount.Load() |
| 139 | |
| 140 | lastRunTime.SetToCurrentTime() |
| 141 | |
| 142 | return summary, nil |
| 143 | } |
| 144 | |
| 145 | type scanJob struct { |
| 146 | check checks.RuleChecker |
| 147 | allEntries []discovery.Entry |
| 148 | entry discovery.Entry |
| 149 | } |
| 150 | |
| 151 | func scanWorker(ctx context.Context, jobs <-chan scanJob, results chan<- reporter.Report) { |
| 152 | for job := range jobs { |
| 153 | select { |
| 154 | case <-ctx.Done(): |
| 155 | return |
| 156 | default: |
| 157 | var commentErr comments.CommentError |
| 158 | var ignoreErr discovery.FileIgnoreError |
| 159 | switch { |
| 160 | case errors.As(job.entry.PathError, &ignoreErr): |
| 161 | results <- reporter.Report{ |
| 162 | Path: discovery.Path{ |
| 163 | Name: job.entry.Path.Name, |
| 164 | SymlinkTarget: job.entry.Path.SymlinkTarget, |
| 165 | }, |
| 166 | ModifiedLines: job.entry.ModifiedLines, |
| 167 | Problem: checks.Problem{ |
| 168 | Lines: parser.LineRange{ |
| 169 | First: ignoreErr.Line, |
| 170 | Last: ignoreErr.Line, |
| 171 | }, |
| 172 | Reporter: ignoreFileReporter, |
| 173 | Text: ignoreErr.Error(), |
| 174 | Severity: checks.Information, |
| 175 | }, |
| 176 | Owner: job.entry.Owner, |
| 177 | } |
| 178 | case errors.As(job.entry.PathError, &commentErr): |
| 179 | results <- reporter.Report{ |
| 180 | Path: discovery.Path{ |
| 181 | Name: job.entry.Path.Name, |
| 182 | SymlinkTarget: job.entry.Path.SymlinkTarget, |
| 183 | }, |
| 184 | ModifiedLines: job.entry.ModifiedLines, |
| 185 | Problem: checks.Problem{ |
| 186 | Lines: parser.LineRange{ |
| 187 | First: commentErr.Line, |
| 188 | Last: commentErr.Line, |
| 189 | }, |
| 190 | Reporter: pintCommentReporter, |
| 191 | Text: "This comment is not a valid pint control comment: " + commentErr.Error(), |
| 192 | Severity: checks.Warning, |
| 193 | }, |
| 194 | Owner: job.entry.Owner, |
| 195 | } |
| 196 | case job.entry.PathError != nil: |
| 197 | results <- reporter.Report{ |
| 198 | Path: discovery.Path{ |
| 199 | Name: job.entry.Path.Name, |
| 200 | SymlinkTarget: job.entry.Path.SymlinkTarget, |
| 201 | }, |
| 202 | ModifiedLines: job.entry.ModifiedLines, |
| 203 | Problem: checks.Problem{ |
| 204 | Lines: parser.LineRange{ |
| 205 | First: 1, |
| 206 | Last: 1, |
| 207 | }, |
| 208 | Reporter: yamlParseReporter, |
| 209 | Text: fmt.Sprintf("YAML parser returned an error when reading this file: `%s`.", job.entry.PathError), |
| 210 | Details: `pint cannot read this file because YAML parser returned an error. |
| 211 | This usually means that you have an indention error or the file doesn't have the YAML structure required by Prometheus for [recording](https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/) and [alerting](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/) rules. |
| 212 | If this file is a template that will be rendered into valid YAML then you can instruct pint to ignore some lines using comments, see [pint docs](https://cloudflare.github.io/pint/ignoring.html). |
| 213 | `, |
| 214 | Severity: checks.Fatal, |
| 215 | }, |
| 216 | Owner: job.entry.Owner, |
| 217 | } |
| 218 | case job.entry.Rule.Error.Err != nil: |
| 219 | details := yamlDetails |
| 220 | if job.entry.Rule.Error.Details != "" { |
| 221 | details = job.entry.Rule.Error.Details |
| 222 | } |
| 223 | results <- reporter.Report{ |
| 224 | Path: discovery.Path{ |
| 225 | Name: job.entry.Path.Name, |
| 226 | SymlinkTarget: job.entry.Path.SymlinkTarget, |
| 227 | }, |
| 228 | ModifiedLines: job.entry.ModifiedLines, |
| 229 | Rule: job.entry.Rule, |
| 230 | Problem: checks.Problem{ |
| 231 | Lines: parser.LineRange{ |
| 232 | First: job.entry.Rule.Error.Line, |
| 233 | Last: job.entry.Rule.Error.Line, |
| 234 | }, |
| 235 | Reporter: yamlParseReporter, |
| 236 | Text: fmt.Sprintf("This rule is not a valid Prometheus rule: `%s`.", job.entry.Rule.Error.Err.Error()), |
| 237 | Details: details, |
| 238 | Severity: checks.Fatal, |
| 239 | }, |
| 240 | Owner: job.entry.Owner, |
| 241 | } |
| 242 | default: |
| 243 | if job.entry.State == discovery.Unknown { |
| 244 | slog.Warn( |
| 245 | "Bug: unknown rule state", |
| 246 | slog.String("path", job.entry.Path.String()), |
| 247 | slog.Int("line", job.entry.Rule.Lines.First), |
| 248 | slog.String("name", job.entry.Rule.Name()), |
| 249 | ) |
| 250 | } |
| 251 | |
| 252 | start := time.Now() |
| 253 | problems := job.check.Check(ctx, job.entry.Path, job.entry.Rule, job.allEntries) |
| 254 | checkDuration.WithLabelValues(job.check.Reporter()).Observe(time.Since(start).Seconds()) |
| 255 | for _, problem := range problems { |
| 256 | results <- reporter.Report{ |
| 257 | Path: discovery.Path{ |
| 258 | Name: job.entry.Path.Name, |
| 259 | SymlinkTarget: job.entry.Path.SymlinkTarget, |
| 260 | }, |
| 261 | ModifiedLines: job.entry.ModifiedLines, |
| 262 | Rule: job.entry.Rule, |
| 263 | Problem: problem, |
| 264 | Owner: job.entry.Owner, |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | checkIterationChecksDone.Inc() |
| 271 | } |
| 272 | } |
| 273 | |