Skip to content

Commit 4abdee6

Browse files
committed
add promlinter to lint metrics name
Signed-off-by: Ben Ye <[email protected]>
1 parent 75bc80d commit 4abdee6

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

go.sum

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ type LintersSettings struct {
277277
Cyclop Cyclop
278278
ImportAs ImportAsSettings
279279
GoModDirectives GoModDirectivesSettings
280+
Promlinter PromlinterSettings
280281

281282
Custom map[string]CustomLinterSettings
282283
}
@@ -457,6 +458,10 @@ type PredeclaredSettings struct {
457458
Qualified bool `mapstructure:"q"`
458459
}
459460

461+
type PromlinterSettings struct {
462+
Strict bool `mapstructure:"strict"`
463+
}
464+
460465
type Cyclop struct {
461466
MaxComplexity int `mapstructure:"max-complexity"`
462467
PackageAverage float64 `mapstructure:"package-average"`

pkg/golinters/promlinter.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package golinters
2+
3+
import (
4+
"fmt"
5+
"go/ast"
6+
"strings"
7+
"sync"
8+
9+
"github.com/yeya24/promlinter"
10+
"golang.org/x/tools/go/analysis"
11+
12+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
13+
"github.com/golangci/golangci-lint/pkg/lint/linter"
14+
"github.com/golangci/golangci-lint/pkg/result"
15+
)
16+
17+
func NewPromlinter() *goanalysis.Linter {
18+
var mu sync.Mutex
19+
var resIssues []goanalysis.Issue
20+
21+
const linterName = "promlinter"
22+
analyzer := &analysis.Analyzer{
23+
Name: linterName,
24+
Doc: goanalysis.TheOnlyanalyzerDoc,
25+
}
26+
return goanalysis.NewLinter(
27+
linterName,
28+
"Check Prometheus metrics naming via promlint",
29+
[]*analysis.Analyzer{analyzer},
30+
nil,
31+
).WithContextSetter(func(lintCtx *linter.Context) {
32+
strict := lintCtx.Cfg.LintersSettings.Promlinter.Strict
33+
34+
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
35+
files := make([]*ast.File, 0)
36+
37+
for _, f := range pass.Files {
38+
if strings.HasSuffix(pass.Fset.Position(f.Pos()).Filename, "_test.go") {
39+
continue
40+
}
41+
42+
files = append(files, f)
43+
}
44+
issues := promlinter.Run(pass.Fset, files, strict)
45+
46+
if len(issues) == 0 {
47+
return nil, nil
48+
}
49+
50+
res := make([]goanalysis.Issue, len(issues))
51+
for k, i := range issues {
52+
issue := result.Issue{
53+
Pos: i.Pos,
54+
Text: fmt.Sprintf("Metric: %s Error: %s", i.Metric, i.Text),
55+
FromLinter: linterName,
56+
}
57+
58+
res[k] = goanalysis.NewIssue(&issue, pass)
59+
}
60+
61+
mu.Lock()
62+
resIssues = append(resIssues, res...)
63+
mu.Unlock()
64+
65+
return nil, nil
66+
}
67+
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
68+
return resIssues
69+
}).WithLoadMode(goanalysis.LoadModeSyntax)
70+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
479479
WithSince("v1.39.0").
480480
WithPresets(linter.PresetStyle, linter.PresetModule).
481481
WithURL("https://github.com/ldez/gomoddirectives"),
482-
482+
linter.NewConfig(golinters.NewPromlinter()).
483+
WithPresets(linter.PresetStyle).
484+
WithAutoFix().
485+
WithURL("https://github.com/yeya24/promlinter"),
483486
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
484487
linter.NewConfig(golinters.NewNoLintLint()).
485488
WithSince("v1.26.0").

0 commit comments

Comments
 (0)