2020-04-25 00:44:21 +03:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/testutil/promlint"
|
|
|
|
)
|
|
|
|
|
2020-04-25 16:59:53 +03:00
|
|
|
// CollectAndLint registers the provided Collector with a newly created pedantic
|
|
|
|
// Registry. It then calls GatherAndLint with that Registry and with the
|
|
|
|
// provided metricNames.
|
2020-04-25 00:44:21 +03:00
|
|
|
func CollectAndLint(c prometheus.Collector, metricNames ...string) ([]promlint.Problem, error) {
|
|
|
|
reg := prometheus.NewPedanticRegistry()
|
|
|
|
if err := reg.Register(c); err != nil {
|
2022-08-03 07:30:51 +03:00
|
|
|
return nil, fmt.Errorf("registering collector failed: %w", err)
|
2020-04-25 00:44:21 +03:00
|
|
|
}
|
|
|
|
return GatherAndLint(reg, metricNames...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GatherAndLint gathers all metrics from the provided Gatherer and checks them
|
|
|
|
// with the linter in the promlint package. If any metricNames are provided,
|
|
|
|
// only metrics with those names are checked.
|
|
|
|
func GatherAndLint(g prometheus.Gatherer, metricNames ...string) ([]promlint.Problem, error) {
|
|
|
|
got, err := g.Gather()
|
|
|
|
if err != nil {
|
2022-08-03 07:30:51 +03:00
|
|
|
return nil, fmt.Errorf("gathering metrics failed: %w", err)
|
2020-04-25 00:44:21 +03:00
|
|
|
}
|
|
|
|
if metricNames != nil {
|
|
|
|
got = filterMetrics(got, metricNames)
|
|
|
|
}
|
|
|
|
return promlint.NewWithMetricFamilies(got).Lint()
|
|
|
|
}
|