Skip to content

Commit

Permalink
Add support for custom validations in promlint
Browse files Browse the repository at this point in the history
Signed-off-by: João Vilaça <jvilaca@redhat.com>
  • Loading branch information
machadovilaca committed Jul 13, 2023
1 parent c39535e commit 83b0350
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
23 changes: 20 additions & 3 deletions prometheus/testutil/promlint/promlint.go
Expand Up @@ -35,6 +35,8 @@ type Linter struct {
// of them.
r io.Reader
mfs []*dto.MetricFamily

customValidations []validations.Validation
}

// New creates a new Linter that reads an input stream of Prometheus metrics in
Expand All @@ -53,6 +55,14 @@ func NewWithMetricFamilies(mfs []*dto.MetricFamily) *Linter {
}
}

// AddCustomValidations adds custom validations to the linter.
func (l *Linter) AddCustomValidations(vs ...validations.Validation) {
if l.customValidations == nil {
l.customValidations = make([]validations.Validation, 0, len(vs))
}
l.customValidations = append(l.customValidations, vs...)
}

// Lint performs a linting pass, returning a slice of Problems indicating any
// issues found in the metrics stream. The slice is sorted by metric name
// and issue description.
Expand All @@ -72,11 +82,11 @@ func (l *Linter) Lint() ([]validations.Problem, error) {
return nil, err
}

problems = append(problems, lint(mf)...)
problems = append(problems, l.lint(mf)...)
}
}
for _, mf := range l.mfs {
problems = append(problems, lint(mf)...)
problems = append(problems, l.lint(mf)...)
}

// Ensure deterministic output.
Expand All @@ -91,12 +101,19 @@ func (l *Linter) Lint() ([]validations.Problem, error) {
}

// lint is the entry point for linting a single metric.
func lint(mf *dto.MetricFamily) []validations.Problem {
func (l *Linter) lint(mf *dto.MetricFamily) []validations.Problem {
var problems []validations.Problem

for _, fn := range validations.DefaultValidations {
problems = append(problems, fn(mf)...)
}

if l.customValidations != nil {
for _, fn := range l.customValidations {
problems = append(problems, fn(mf)...)
}
}

// TODO(mdlayher): lint rules for specific metrics types.
return problems
}
59 changes: 59 additions & 0 deletions prometheus/testutil/promlint/promlint_test.go
Expand Up @@ -19,6 +19,8 @@ import (
"strings"
"testing"

dto "github.com/prometheus/client_model/go"

"github.com/prometheus/client_golang/prometheus/testutil/promlint"
"github.com/prometheus/client_golang/prometheus/testutil/promlint/validations"
)
Expand Down Expand Up @@ -787,3 +789,60 @@ func runTests(t *testing.T, tests []test) {
})
}
}

func TestCustomValidations(t *testing.T) {
lintAndVerify := func(l *promlint.Linter, cv test) {
problems, err := l.Lint()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if want, got := cv.problems, problems; !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected problems:\n- want: %v\n- got: %v",
want, got)
}
}

prob := []validations.Problem{
{
Metric: "mc_something_total",
Text: "expected metric name to start with 'memcached_'",
},
}

cv := test{
name: "metric without necessary prefix",
in: `
# HELP mc_something_total Test metric.
# TYPE mc_something_total counter
mc_something_total 10
`,
problems: nil,
}

prefixValidation := func(mf *dto.MetricFamily) []validations.Problem {
if !strings.HasPrefix(mf.GetName(), "memcached_") {
return []validations.Problem{
{
Metric: mf.GetName(),
Text: "expected metric name to start with 'memcached_'",
},
}
}
return []validations.Problem{}
}

t.Helper()
t.Run(cv.name, func(t *testing.T) {
// no problems
l1 := promlint.New(strings.NewReader(cv.in))
lintAndVerify(l1, cv)
})
t.Run(cv.name, func(t *testing.T) {
// prefix problems
l2 := promlint.New(strings.NewReader(cv.in))
l2.AddCustomValidations(prefixValidation)
cv.problems = prob
lintAndVerify(l2, cv)
})
}

0 comments on commit 83b0350

Please sign in to comment.