fix metric naming test (#4017)

Signed-off-by: Ben Ye <yb532204897@gmail.com>
This commit is contained in:
Ben Ye 2020-07-21 08:15:55 +00:00 committed by GitHub
parent 0c9e2e280c
commit 7f0ad57635
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,20 +1,20 @@
package test package test
import ( import (
"bytes"
"go/ast" "go/ast"
"go/parser" "go/parser"
"go/token" "go/token"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"testing" "testing"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil/promlint"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/prometheus/util/promlint"
) )
func TestMetricNaming(t *testing.T) { func TestMetricNaming(t *testing.T) {
@ -27,16 +27,7 @@ func TestMetricNaming(t *testing.T) {
} }
if len(walker.Metrics) > 0 { if len(walker.Metrics) > 0 {
l := promlint.NewWithMetricFamilies(walker.Metrics)
buf := &bytes.Buffer{}
encoder := expfmt.NewEncoder(buf, expfmt.FmtText)
for _, mf := range walker.Metrics {
if err := encoder.Encode(mf); err != nil {
t.Fatalf("Encoding and sending metric family: %s", err)
}
}
l := promlint.New(buf)
problems, err := l.Lint() problems, err := l.Lint()
if err != nil { if err != nil {
t.Fatalf("Link found error: %s", err) t.Fatalf("Link found error: %s", err)
@ -86,7 +77,7 @@ type metric struct {
Metric *dto.MetricFamily Metric *dto.MetricFamily
} }
func (l metric) Visit(n ast.Node) ast.Visitor { func (l *metric) Visit(n ast.Node) ast.Visitor {
if n == nil { if n == nil {
return nil return nil
} }
@ -142,13 +133,20 @@ func (l metric) Visit(n ast.Node) ast.Visitor {
if !ok { if !ok {
continue continue
} }
// remove quotes
stringLiteral, err := strconv.Unquote(value.Value)
if err != nil {
return l
}
switch object.Name { switch object.Name {
case "Subsystem": case "Subsystem":
subsystem = value.Value subsystem = stringLiteral
case "Name": case "Name":
name = value.Value name = stringLiteral
case "Help": case "Help":
help = value.Value help = stringLiteral
} }
} }
@ -157,12 +155,7 @@ func (l metric) Visit(n ast.Node) ast.Visitor {
return l return l
} }
var metricName string metricName := prometheus.BuildFQName(plugin.Namespace, subsystem, name)
if len(subsystem) > 0 {
metricName = strings.Join([]string{plugin.Namespace, subsystem, name}, "_")
} else {
metricName = strings.Join([]string{plugin.Namespace, name}, "_")
}
l.Metric = &dto.MetricFamily{ l.Metric = &dto.MetricFamily{
Name: &metricName, Name: &metricName,
Help: &help, Help: &help,