Skip to content

Commit

Permalink
Merge pull request #25 from yeya24/lint-check-metric-name
Browse files Browse the repository at this point in the history
Linter skip empty metrics name
  • Loading branch information
yeya24 authored Mar 28, 2021
2 parents f355d20 + 8a96367 commit 000c7d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions promlinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: &currentMetric, Pos: optsPosition})
Expand Down
5 changes: 5 additions & 0 deletions testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 000c7d7

Please sign in to comment.