forked from cloudflare/pint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
324 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp/syntax" | ||
|
||
"github.com/cloudflare/pint/internal/parser" | ||
) | ||
|
||
const ( | ||
RegexpCheckName = "promql/regexp" | ||
) | ||
|
||
func NewRegexpCheck() RegexpCheck { | ||
return RegexpCheck{} | ||
} | ||
|
||
type RegexpCheck struct{} | ||
|
||
func (c RegexpCheck) String() string { | ||
return RegexpCheckName | ||
} | ||
|
||
func (c RegexpCheck) Reporter() string { | ||
return RegexpCheckName | ||
} | ||
|
||
func (c RegexpCheck) Check(ctx context.Context, rule parser.Rule) (problems []Problem) { | ||
expr := rule.Expr() | ||
if expr.SyntaxError != nil { | ||
return nil | ||
} | ||
|
||
for _, selector := range getSelectors(expr.Query) { | ||
for _, lm := range selector.LabelMatchers { | ||
if s := lm.GetRegexString(); s != "" { | ||
var isUseful, isEmpty bool | ||
r, _ := syntax.Parse(s, syntax.Perl) | ||
for _, s := range r.Sub { | ||
switch s.Op { | ||
case syntax.OpBeginText: | ||
continue | ||
case syntax.OpEndText: | ||
continue | ||
case syntax.OpLiteral: | ||
isEmpty = false | ||
case syntax.OpEmptyMatch: | ||
isEmpty = true | ||
default: | ||
isUseful = true | ||
} | ||
} | ||
if !isUseful { | ||
var text string | ||
if isEmpty { | ||
text = fmt.Sprintf("unnecessary regexp match on empty string: %s", lm) | ||
} else { | ||
text = fmt.Sprintf("unnecessary regexp match on static string: %s", lm) | ||
} | ||
problems = append(problems, Problem{ | ||
Fragment: selector.String(), | ||
Lines: expr.Lines(), | ||
Reporter: c.Reporter(), | ||
Text: text, | ||
Severity: Warning, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package checks_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cloudflare/pint/internal/checks" | ||
) | ||
|
||
func TestRegexpCheck(t *testing.T) { | ||
testCases := []checkTest{ | ||
{ | ||
description: "ignores rules with syntax errors", | ||
content: "- alert: foo\n expr: sum(foo) without(\n", | ||
checker: checks.NewRegexpCheck(), | ||
}, | ||
{ | ||
description: "static match", | ||
content: "- record: foo\n expr: foo{job=\"bar\"}\n", | ||
checker: checks.NewRegexpCheck(), | ||
}, | ||
{ | ||
description: "valid regexp", | ||
content: "- record: foo\n expr: foo{job=~\"bar.+\"}\n", | ||
checker: checks.NewRegexpCheck(), | ||
}, | ||
{ | ||
description: "valid regexp", | ||
content: "- record: foo\n expr: foo{job!~\"(.*)\"}\n", | ||
checker: checks.NewRegexpCheck(), | ||
}, | ||
{ | ||
description: "valid regexp", | ||
content: "- record: foo\n expr: foo{job=~\"a|b|c\"}\n", | ||
checker: checks.NewRegexpCheck(), | ||
}, | ||
{ | ||
description: "unnecessary regexp", | ||
content: "- record: foo\n expr: foo{job=~\"bar\"}\n", | ||
checker: checks.NewRegexpCheck(), | ||
problems: []checks.Problem{ | ||
{ | ||
Fragment: `foo{job=~"bar"}`, | ||
Lines: []int{2}, | ||
Reporter: "promql/regexp", | ||
Text: `unnecessary regexp match on static string: job=~"bar"`, | ||
Severity: checks.Warning, | ||
}, | ||
}, | ||
}, | ||
{ | ||
description: "empty regexp", | ||
content: "- record: foo\n expr: foo{job=~\"\"}\n", | ||
checker: checks.NewRegexpCheck(), | ||
problems: []checks.Problem{ | ||
{ | ||
Fragment: `foo{job=~""}`, | ||
Lines: []int{2}, | ||
Reporter: "promql/regexp", | ||
Text: `unnecessary regexp match on empty string: job=~""`, | ||
Severity: checks.Warning, | ||
}, | ||
}, | ||
}, | ||
} | ||
runTests(t, testCases) | ||
} |
Oops, something went wrong.