-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add promlinter to lint metrics name #1265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 golinters | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/yeya24/promlinter" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
func NewPromlinter() *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
const linterName = "promlinter" | ||
analyzer := &analysis.Analyzer{ | ||
Name: linterName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
return goanalysis.NewLinter( | ||
linterName, | ||
"Check Prometheus metrics naming via promlint", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
strict := lintCtx.Cfg.LintersSettings.Promlinter.Strict | ||
disabledLinters := lintCtx.Cfg.LintersSettings.Promlinter.DisabledLinters | ||
|
||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
files := make([]*ast.File, 0) | ||
|
||
for _, f := range pass.Files { | ||
if strings.HasSuffix(pass.Fset.Position(f.Pos()).Filename, "_test.go") { | ||
ldez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
} | ||
|
||
files = append(files, f) | ||
} | ||
issues := promlinter.RunLint(pass.Fset, files, promlinter.Setting{ | ||
Strict: strict, | ||
DisabledLintFuncs: disabledLinters, | ||
}) | ||
|
||
if len(issues) == 0 { | ||
return nil, nil | ||
} | ||
|
||
res := make([]goanalysis.Issue, len(issues)) | ||
for k, i := range issues { | ||
issue := result.Issue{ | ||
Pos: i.Pos, | ||
Text: fmt.Sprintf("Metric: %s Error: %s", i.Metric, i.Text), | ||
FromLinter: linterName, | ||
} | ||
|
||
res[k] = goanalysis.NewIssue(&issue, pass) | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, res...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeNone) | ||
ldez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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 hidden or 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,34 @@ | ||
//args: -Epromlinter | ||
package testdata | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
var ( | ||
_ = promauto.NewCounterVec( | ||
prometheus.CounterOpts{ // Metric: test_metric_name Error: counter metrics should have "_total" suffix | ||
Name: "test_metric_name", | ||
Help: "test help text", | ||
}, []string{}, | ||
) | ||
|
||
_ = promauto.NewCounterVec( | ||
prometheus.CounterOpts{ // Metric: test_metric_total Error: no help text | ||
Name: "test_metric_total", | ||
}, []string{}, | ||
) | ||
|
||
_ = promauto.NewCounterVec( | ||
prometheus.CounterOpts{ // Metric: metric_type_in_name_counter_total Error: metric name should not include type 'counter' | ||
Name: "metric_type_in_name_counter_total", | ||
Help: "foo", | ||
}, []string{}, | ||
) | ||
|
||
_ = prometheus.NewHistogram(prometheus.HistogramOpts{ // Metric: test_duration_milliseconds Error: use base unit "seconds" instead of "milliseconds" | ||
Name: "test_duration_milliseconds", | ||
Help: "", | ||
}) | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.