diff --git a/promlinter.go b/promlinter.go index 07aa303..898336a 100644 --- a/promlinter.go +++ b/promlinter.go @@ -266,6 +266,12 @@ func (v *visitor) parseOpts(optArg ast.Node, metricType dto.MetricType) ast.Visi } metricName := prometheus.BuildFQName(opts.namespace, opts.subsystem, opts.name) + // We skip the invalid metric if the name is an empty string. + // This kind of metric declaration might be used as a stud metric + // https://github.com/thanos-io/thanos/blob/main/cmd/thanos/tools_bucket.go#L538. + if metricName == "" { + return v + } currentMetric.Name = &metricName v.metrics = append(v.metrics, MetricFamilyWithPos{MetricFamily: ¤tMetric, Pos: optsPosition}) diff --git a/testdata/testdata.go b/testdata/testdata.go index a769a30..274e015 100644 --- a/testdata/testdata.go +++ b/testdata/testdata.go @@ -3,6 +3,7 @@ package testdata import ( + "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "k8s.io/kube-state-metrics/v2/pkg/metric" @@ -122,6 +123,10 @@ func main() { nil, ), } + + // We skip linting these case when metric name is not set. + promauto.With(nil).NewCounter(prometheus.CounterOpts{}) + promauto.With(nil).NewCounterVec(prometheus.CounterOpts{}, nil) } func newDesc(subsystem, name, help string) *prometheus.Desc {