Skip to content

Commit 806664f

Browse files
committed
add testdata for promlinter
Signed-off-by: yeya24 <[email protected]>
1 parent 0917a92 commit 806664f

File tree

7 files changed

+60
-5
lines changed

7 files changed

+60
-5
lines changed

.golangci.example.yml

+14
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ linters-settings:
339339
simple: true
340340
range-loops: true # Report preallocation suggestions on range loops, true by default
341341
for-loops: false # Report preallocation suggestions on for loops, false by default
342+
promlinter:
343+
# Promlinter cannot infer all metrics name in static analysis. Enable strict mode
344+
# will also include the errors caused by failing to parse the args.
345+
strict: false
346+
disabled-linters:
347+
# Please refer to https://github.com/yeya24/promlinter#usage for detailed usage.
348+
# - "Help"
349+
# - "MetricUnits"
350+
# - "Counter"
351+
# - "HistogramSummaryReserved"
352+
# - "MetricTypeInName"
353+
# - "ReservedChars"
354+
# - "CamelCase"
355+
# - "lintUnitAbbreviations"
342356
predeclared:
343357
# comma-separated list of predeclared identifiers to not report on
344358
ignore: ""

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ require (
7373
github.com/ultraware/whitespace v0.0.4
7474
github.com/uudashr/gocognit v1.0.1
7575
github.com/valyala/quicktemplate v1.6.3
76-
github.com/yeya24/promlinter v0.0.0-20201120174540-eec9e2ee3b40
76+
github.com/yeya24/promlinter v0.0.0-20210328235706-000c7d74ddb3
7777
golang.org/x/text v0.3.4 // indirect
7878
golang.org/x/tools v0.1.0
7979
gopkg.in/yaml.v2 v2.4.0

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,11 @@ type ForbidigoSettings struct {
452452
type PredeclaredSettings struct {
453453
Ignore string `mapstructure:"ignore"`
454454
Qualified bool `mapstructure:"q"`
455+
}
455456

456457
type PromlinterSettings struct {
457-
Strict bool `mapstructure:"strict"`
458+
Strict bool `mapstructure:"strict"`
459+
DisabledLinters []string `mapstructure:"disabled-linters"`
458460
}
459461

460462
var defaultLintersSettings = LintersSettings{

pkg/golinters/promlinter.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func NewPromlinter() *goanalysis.Linter {
3030
nil,
3131
).WithContextSetter(func(lintCtx *linter.Context) {
3232
strict := lintCtx.Cfg.LintersSettings.Promlinter.Strict
33+
disabledLinters := lintCtx.Cfg.LintersSettings.Promlinter.DisabledLinters
3334

3435
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
3536
files := make([]*ast.File, 0)
@@ -41,7 +42,10 @@ func NewPromlinter() *goanalysis.Linter {
4142

4243
files = append(files, f)
4344
}
44-
issues := promlinter.Run(pass.Fset, files, strict)
45+
issues := promlinter.RunLint(pass.Fset, files, promlinter.Setting{
46+
Strict: strict,
47+
DisabledLintFuncs: disabledLinters,
48+
})
4549

4650
if len(issues) == 0 {
4751
return nil, nil
@@ -66,5 +70,5 @@ func NewPromlinter() *goanalysis.Linter {
6670
}
6771
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
6872
return resIssues
69-
}).WithLoadMode(goanalysis.LoadModeSyntax)
73+
}).WithLoadMode(goanalysis.LoadModeNone)
7074
}

pkg/lint/lintersdb/manager.go

-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
371371
WithURL("https://github.com/charithe/durationcheck"),
372372
linter.NewConfig(golinters.NewPromlinter()).
373373
WithPresets(linter.PresetStyle).
374-
WithAutoFix().
375374
WithURL("https://github.com/yeya24/promlinter"),
376375
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
377376
linter.NewConfig(golinters.NewNoLintLint()).

test/testdata/promlinter.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//args: -Epromlinter
2+
package testdata
3+
4+
import (
5+
"github.com/prometheus/client_golang/prometheus"
6+
"github.com/prometheus/client_golang/prometheus/promauto"
7+
)
8+
9+
var (
10+
_ = promauto.NewCounterVec(
11+
prometheus.CounterOpts{ // Metric: test_metric_name Error: counter metrics should have "_total" suffix
12+
Name: "test_metric_name",
13+
Help: "test help text",
14+
}, []string{},
15+
)
16+
17+
_ = promauto.NewCounterVec(
18+
prometheus.CounterOpts{ // Metric: test_metric_total Error: no help text
19+
Name: "test_metric_total",
20+
}, []string{},
21+
)
22+
23+
_ = promauto.NewCounterVec(
24+
prometheus.CounterOpts{ // Metric: metric_type_in_name_counter_total Error: metric name should not include type 'counter'
25+
Name: "metric_type_in_name_counter_total",
26+
Help: "foo",
27+
}, []string{},
28+
)
29+
30+
_ = prometheus.NewHistogram(prometheus.HistogramOpts{ // Metric: test_duration_milliseconds Error: use base unit "seconds" instead of "milliseconds"
31+
Name: "test_duration_milliseconds",
32+
Help: "",
33+
})
34+
)

0 commit comments

Comments
 (0)