Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor custom collector unit test #85680

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion staging/src/k8s.io/component-base/metrics/BUILD
Expand Up @@ -53,7 +53,6 @@ go_test(
"//vendor/github.com/blang/semver:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus/testutil:go_default_library",
"//vendor/github.com/prometheus/client_model/go:go_default_library",
"//vendor/github.com/prometheus/common/expfmt:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
],
Expand Down
151 changes: 46 additions & 105 deletions staging/src/k8s.io/component-base/metrics/collector_test.go
Expand Up @@ -17,137 +17,78 @@ limitations under the License.
package metrics

import (
"strings"
"testing"

dto "github.com/prometheus/client_model/go"

"github.com/prometheus/client_golang/prometheus/testutil"
apimachineryversion "k8s.io/apimachinery/pkg/version"
)

type testCustomCollector struct {
BaseStableCollector
}

var (
currentVersion = apimachineryversion.Info{
Major: "1",
Minor: "17",
GitVersion: "v1.17.0-alpha-1.12345",
}
alphaDesc = NewDesc("metric_alpha", "alpha metric", []string{"name"}, nil,
ALPHA, "")
stableDesc = NewDesc("metric_stable", "stable metrics", []string{"name"}, nil,
STABLE, "")
deprecatedDesc = NewDesc("metric_deprecated", "stable deprecated metrics", []string{"name"}, nil,
STABLE, "1.17.0")
hiddenDesc = NewDesc("metric_hidden", "stable hidden metrics", []string{"name"}, nil,
STABLE, "1.16.0")
)

func (tc *testCustomCollector) DescribeWithStability(ch chan<- *Desc) {
ch <- alphaDesc
ch <- stableDesc
ch <- deprecatedDesc
ch <- hiddenDesc
descriptors []*Desc
}

func (tc *testCustomCollector) CollectWithStability(ch chan<- Metric) {
ch <- NewLazyConstMetric(
alphaDesc,
GaugeValue,
1,
"value",
)
ch <- NewLazyConstMetric(
stableDesc,
GaugeValue,
1,
"value",
)
ch <- NewLazyConstMetric(
deprecatedDesc,
GaugeValue,
1,
"value",
)
ch <- NewLazyConstMetric(
hiddenDesc,
GaugeValue,
1,
"value",
)
func newTestCustomCollector(ds ...*Desc) *testCustomCollector {
c := &testCustomCollector{}
c.descriptors = append(c.descriptors, ds...)

return c
}

func getMetric(metrics []*dto.MetricFamily, fqName string) *dto.MetricFamily {
for _, m := range metrics {
if *m.Name == fqName {
return m
}
func (tc *testCustomCollector) DescribeWithStability(ch chan<- *Desc) {
for i := range tc.descriptors {
ch <- tc.descriptors[i]
}
}

return nil
func (tc *testCustomCollector) CollectWithStability(ch chan<- Metric) {
for i := range tc.descriptors {
ch <- NewLazyConstMetric(tc.descriptors[i], GaugeValue, 1, "value")
}
}

func TestBaseCustomCollector(t *testing.T) {
var tests = []struct {
name string
d *Desc
shouldHidden bool
expectedHelp string
}{
{
name: "alpha metric should contains stability metadata",
d: alphaDesc,
shouldHidden: false,
expectedHelp: "[ALPHA] alpha metric",
},
{
name: "stable metric should contains stability metadata",
d: stableDesc,
shouldHidden: false,
expectedHelp: "[STABLE] stable metrics",
},
{
name: "deprecated metric should contains stability metadata",
d: deprecatedDesc,
shouldHidden: false,
expectedHelp: "[STABLE] (Deprecated since 1.17.0) stable deprecated metrics",
},
{
name: "hidden metric should be ignored",
d: hiddenDesc,
shouldHidden: true,
expectedHelp: "[STABLE] stable hidden metrics",
},
var currentVersion = apimachineryversion.Info{
Major: "1",
Minor: "17",
GitVersion: "v1.17.0-alpha-1.12345",
}

var (
alphaDesc = NewDesc("metric_alpha", "alpha metric", []string{"name"}, nil,
ALPHA, "")
stableDesc = NewDesc("metric_stable", "stable metrics", []string{"name"}, nil,
STABLE, "")
deprecatedDesc = NewDesc("metric_deprecated", "stable deprecated metrics", []string{"name"}, nil,
STABLE, "1.17.0")
hiddenDesc = NewDesc("metric_hidden", "stable hidden metrics", []string{"name"}, nil,
STABLE, "1.16.0")
)

registry := newKubeRegistry(currentVersion)
customCollector := &testCustomCollector{}
customCollector := newTestCustomCollector(alphaDesc, stableDesc, deprecatedDesc, hiddenDesc)

if err := registry.CustomRegister(customCollector); err != nil {
t.Fatalf("register collector failed with err: %v", err)
}

metrics, err := registry.Gather()
expectedMetrics := `
# HELP metric_alpha [ALPHA] alpha metric
# TYPE metric_alpha gauge
metric_alpha{name="value"} 1
# HELP metric_stable [STABLE] stable metrics
# TYPE metric_stable gauge
metric_stable{name="value"} 1
# HELP metric_deprecated [STABLE] (Deprecated since 1.17.0) stable deprecated metrics
# TYPE metric_deprecated gauge
metric_deprecated{name="value"} 1
`

err := testutil.GatherAndCompare(registry, strings.NewReader(expectedMetrics), alphaDesc.fqName,
stableDesc.fqName, deprecatedDesc.fqName, hiddenDesc.fqName)
if err != nil {
t.Fatalf("failed to get metrics from collector, %v", err)
}

for _, test := range tests {
tc := test
t.Run(tc.name, func(t *testing.T) {
m := getMetric(metrics, tc.d.fqName)
if m == nil {
if !tc.shouldHidden {
t.Fatalf("Want metric: %s", tc.d.fqName)
}
} else {
if m.GetHelp() != tc.expectedHelp {
t.Fatalf("Metric(%s) HELP(%s) not contains: %s", tc.d.fqName, *m.Help, tc.expectedHelp)
}
}

})
t.Fatal(err)
}
}