Skip to content

Commit

Permalink
Move diags to a package
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Mar 7, 2025
1 parent 6608b01 commit 3002aa5
Show file tree
Hide file tree
Showing 78 changed files with 1,075 additions and 1,073 deletions.
6 changes: 3 additions & 3 deletions cmd/pint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

"github.com/cloudflare/pint/internal/checks"
"github.com/cloudflare/pint/internal/config"
"github.com/cloudflare/pint/internal/diags"
"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/git"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/reporter"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -202,7 +202,7 @@ func verifyOwners(entries []discovery.Entry, allowedOwners []*regexp.Regexp) (re
Summary: "missing owner",
Details: "",
Severity: checks.Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
checks.WholeRuleDiag(entry.Rule, fmt.Sprintf("`%s` comments are required in all files, please add a `# pint %s $owner` somewhere in this file and/or `# pint %s $owner` on top of each rule.",
discovery.RuleOwnerComment, discovery.FileOwnerComment, discovery.RuleOwnerComment)),
},
Expand All @@ -227,7 +227,7 @@ func verifyOwners(entries []discovery.Entry, allowedOwners []*regexp.Regexp) (re
Summary: "invalid owner",
Details: "",
Severity: checks.Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
checks.WholeRuleDiag(entry.Rule, fmt.Sprintf("This rule is set as owned by `%s` but `%s` doesn't match any of the allowed owner values.", entry.Owner, entry.Owner)),
},
},
Expand Down
9 changes: 5 additions & 4 deletions internal/checks/alerts_absent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/cloudflare/pint/internal/diags"
"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/parser"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (c AlertsAbsentCheck) Check(ctx context.Context, _ discovery.Path, rule par
Summary: "unable to run checks",
Details: "",
Severity: severity,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: text,
Pos: rule.AlertingRule.Expr.Value.Pos,
Expand All @@ -113,7 +114,7 @@ func (c AlertsAbsentCheck) Check(ctx context.Context, _ discovery.Path, rule par

for _, s := range absentSources {
var summary string
diags := []output.Diagnostic{
dgs := []diags.Diagnostic{
{
Message: "Using `absent()` might cause false positive alerts when Prometheus restarts.",
Pos: rule.AlertingRule.Expr.Value.Pos,
Expand All @@ -123,7 +124,7 @@ func (c AlertsAbsentCheck) Check(ctx context.Context, _ discovery.Path, rule par
}
if forVal > 0 {
summary = "absent() based alert with insufficient for"
diags = append(diags, output.Diagnostic{
dgs = append(dgs, diags.Diagnostic{
Message: fmt.Sprintf("Use a value that's at least twice Prometheus scrape interval (`%s`).",
output.HumanizeDuration(cfg.Config.Global.ScrapeInterval)),
Pos: rule.AlertingRule.For.Pos,
Expand All @@ -141,7 +142,7 @@ func (c AlertsAbsentCheck) Check(ctx context.Context, _ discovery.Path, rule par
Summary: summary,
Details: AlertsAbsentCheckDetails,
Severity: Warning,
Diagnostics: diags,
Diagnostics: dgs,
})
}

Expand Down
20 changes: 10 additions & 10 deletions internal/checks/alerts_absent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/cloudflare/pint/internal/checks"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/diags"
"github.com/cloudflare/pint/internal/promapi"
)

Expand Down Expand Up @@ -63,7 +63,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Summary: "absent() based alert without for",
Details: checks.AlertsAbsentCheckDetails,
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "Using `absent()` might cause false positive alerts when Prometheus restarts.",
},
Expand All @@ -90,7 +90,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Summary: "absent() based alert without for",
Details: checks.AlertsAbsentCheckDetails,
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "Using `absent()` might cause false positive alerts when Prometheus restarts.",
},
Expand All @@ -117,7 +117,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Summary: "absent() based alert with insufficient for",
Details: checks.AlertsAbsentCheckDetails,
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "Using `absent()` might cause false positive alerts when Prometheus restarts.",
},
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Summary: "absent() based alert with insufficient for",
Details: checks.AlertsAbsentCheckDetails,
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "Using `absent()` might cause false positive alerts when Prometheus restarts.",
},
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Summary: "absent() based alert with insufficient for",
Details: checks.AlertsAbsentCheckDetails,
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "Using `absent()` might cause false positive alerts when Prometheus restarts.",
},
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Reporter: checks.AlertsAbsentCheckName,
Summary: "unable to run checks",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: checkErrorBadData("prom", uri, "bad_data: bad input data"),
},
Expand All @@ -245,7 +245,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Reporter: checks.AlertsAbsentCheckName,
Summary: "unable to run checks",
Severity: checks.Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: checkErrorUnableToRun(checks.AlertsAbsentCheckName, "prom", uri, fmt.Sprintf("failed to decode config data in %s response: yaml: line 2: could not find expected ':'", uri)),
},
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Reporter: checks.AlertsAbsentCheckName,
Summary: "unable to run checks",
Severity: checks.Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: checkErrorUnableToRun(checks.AlertsAbsentCheckName, "prom", "http://127.0.0.1:1111", "connection refused"),
},
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestAlertsAbsentCheck(t *testing.T) {
Reporter: checks.AlertsAbsentCheckName,
Summary: "unable to run checks",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: checkErrorBadData("prom", s, "bad_response: 600 status code 600"),
},
Expand Down
12 changes: 6 additions & 6 deletions internal/checks/alerts_annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strconv"
"strings"

"github.com/cloudflare/pint/internal/diags"
"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/parser"
)

Expand Down Expand Up @@ -76,7 +76,7 @@ func (c AnnotationCheck) Check(_ context.Context, _ discovery.Path, rule parser.
Summary: "required annotation not set",
Details: maybeComment(c.comment),
Severity: c.severity,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
WholeRuleDiag(rule, fmt.Sprintf("`%s` annotation is required.", c.keyRe.original)),
},
})
Expand All @@ -100,7 +100,7 @@ func (c AnnotationCheck) Check(_ context.Context, _ discovery.Path, rule parser.
Summary: "required annotation not set",
Details: maybeComment(c.comment),
Severity: c.severity,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: fmt.Sprintf("`%s` annotation is required.", c.keyRe.original),
Pos: rule.AlertingRule.Annotations.Key.Pos,
Expand All @@ -121,7 +121,7 @@ func (c AnnotationCheck) Check(_ context.Context, _ discovery.Path, rule parser.
Summary: "required annotation not set",
Details: maybeComment(c.comment),
Severity: c.severity,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: fmt.Sprintf("`%s` annotation is required.", c.keyRe.original),
Pos: ann.Key.Pos,
Expand Down Expand Up @@ -153,7 +153,7 @@ func (c AnnotationCheck) checkValue(rule parser.Rule, value string, ann *parser.
Summary: "invalid annotation value",
Details: maybeComment(c.comment),
Severity: c.severity,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: fmt.Sprintf("`%s` annotation value `%s` must match `%s`.", c.keyRe.original, value, c.valueRe.anchored),
Pos: ann.Pos,
Expand Down Expand Up @@ -190,7 +190,7 @@ func (c AnnotationCheck) checkValue(rule parser.Rule, value string, ann *parser.
Summary: "invalid annotation value",
Details: details.String(),
Severity: c.severity,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: fmt.Sprintf("`%s` annotation value `%s` is not one of valid values.", c.keyRe.original, value),
Pos: ann.Pos,
Expand Down
24 changes: 12 additions & 12 deletions internal/checks/alerts_annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/cloudflare/pint/internal/checks"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/diags"
"github.com/cloudflare/pint/internal/promapi"
)

Expand Down Expand Up @@ -32,7 +32,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "required annotation not set",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`severity` annotation is required.",
},
Expand All @@ -54,7 +54,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "required annotation not set",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`severity` annotation is required.",
},
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "required annotation not set",
Severity: checks.Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`severity` annotation is required.",
},
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "required annotation not set",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`severity` annotation is required.",
},
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "invalid annotation value",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`severity` annotation value `bar` must match `^critical$`.",
},
Expand All @@ -167,7 +167,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "invalid annotation value",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`severity` annotation value `bar` must match `^critical$`.",
},
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "invalid annotation value",
Severity: checks.Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`for` annotation value `4m` must match `^{{ $for }}$`.",
},
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "invalid annotation value",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`annotation_.*` annotation value `bar` must match `^critical$`.",
},
Expand All @@ -278,7 +278,7 @@ func TestAnnotationCheck(t *testing.T) {
Reporter: checks.AnnotationCheckName,
Summary: "invalid annotation value",
Severity: checks.Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`annotation_.*` annotation value `bar` must match `^critical$`.",
},
Expand All @@ -301,7 +301,7 @@ func TestAnnotationCheck(t *testing.T) {
Summary: "invalid annotation value",
Details: "Rule comment: rule comment",
Severity: checks.Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`components` annotation value `db` must match `^api|memcached$`.",
},
Expand Down Expand Up @@ -332,7 +332,7 @@ func TestAnnotationCheck(t *testing.T) {
Summary: "invalid annotation value",
Details: "List of allowed values:\n\n- `api`\n- `memcached`\n- `storage`\n- `prometheus`\n- `kvm`\n- `mysql`\n\nAnd 3 other value(s).\nRule comment: rule comment",
Severity: checks.Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "`components` annotation value `db` is not one of valid values.",
},
Expand Down
8 changes: 4 additions & 4 deletions internal/checks/alerts_comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package checks
import (
"context"

"github.com/cloudflare/pint/internal/diags"
"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/parser"
"github.com/cloudflare/pint/internal/parser/utils"

Expand Down Expand Up @@ -67,7 +67,7 @@ func (c ComparisonCheck) Check(_ context.Context, _ discovery.Path, rule parser.
Summary: "always firing alert",
Details: ComparisonCheckDetails,
Severity: rewriteSeverity(Warning, n.LHS, n.RHS),
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "Alert query uses `or` operator with one side of the query that will always return a result, this alert will always fire.",
Pos: expr.Value.Pos,
Expand All @@ -88,7 +88,7 @@ func (c ComparisonCheck) Check(_ context.Context, _ discovery.Path, rule parser.
Summary: "always firing alert",
Details: ComparisonCheckDetails,
Severity: Bug,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "Alert query uses `bool` modifier for comparison, this means it will always return a result and the alert will always fire.",
Pos: expr.Value.Pos,
Expand All @@ -112,7 +112,7 @@ func (c ComparisonCheck) Check(_ context.Context, _ discovery.Path, rule parser.
Summary: "always firing alert",
Details: ComparisonCheckDetails,
Severity: Warning,
Diagnostics: []output.Diagnostic{
Diagnostics: []diags.Diagnostic{
{
Message: "Alert query doesn't have any condition, it will always fire if the metric exists.",
Pos: expr.Value.Pos,
Expand Down
Loading

0 comments on commit 3002aa5

Please sign in to comment.