Fix stable metric finder for NewDesc with custom import name

This commit is contained in:
Marek Siarkowicz 2023-06-23 15:32:00 +02:00
parent baefd08919
commit 22258a263f
3 changed files with 21 additions and 4 deletions

View File

@ -144,7 +144,7 @@ func (c *metricDecoder) decodeDesc(ce *ast.CallExpr) (metric, error) {
return *m, newDecodeErrorf(ce, "can't decode const labels")
}
m.ConstLabels = cLabels
sl, err := decodeStabilityLevel(ce.Args[4], "metrics")
sl, err := decodeStabilityLevel(ce.Args[4], c.kubeMetricsImportName)
if err != nil {
return *m, newDecodeErrorf(ce, "can't decode stability level")
}

View File

@ -66,7 +66,7 @@ func (f *stableMetricFinder) Visit(node ast.Node) (w ast.Visitor) {
case *ast.CallExpr:
if se, ok := opts.Fun.(*ast.SelectorExpr); ok {
if se.Sel.Name == "NewDesc" {
sl, _ := decodeStabilityLevel(opts.Args[4], "metrics")
sl, _ := decodeStabilityLevel(opts.Args[4], f.metricsImportName)
if sl != nil {
classes := []metrics.StabilityLevel{metrics.STABLE, metrics.BETA}
if ALL_STABILITY_CLASSES {

View File

@ -23,6 +23,8 @@ import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"k8s.io/component-base/metrics"
)
@ -306,6 +308,21 @@ var _ = custom.NewCounter(
StabilityLevel: custom.STABLE,
},
)
`},
{
testName: "Custom import NewDesc",
metric: metric{
Name: "apiserver_storage_size_bytes",
Help: "Size of the storage database file physically allocated in bytes.",
Labels: []string{"server"},
StabilityLevel: "STABLE",
Type: customType,
ConstLabels: map[string]string{},
},
src: `
package test
import custom "k8s.io/component-base/metrics"
var _ = custom.NewDesc("apiserver_storage_size_bytes", "Size of the storage database file physically allocated in bytes.", []string{"server"}, nil, custom.STABLE, "")
`},
{
testName: "Const",
@ -526,8 +543,8 @@ var _ = compbasemetrics.NewCounter(
if test.metric.Labels == nil {
test.metric.Labels = []string{}
}
if !reflect.DeepEqual(metrics[0], test.metric) {
t.Errorf("metric:\ngot %v\nwant %v", metrics[0], test.metric)
if diff := cmp.Diff(metrics[0], test.metric); diff != "" {
t.Errorf("metric diff: %s", diff)
}
})
}