Skip to content

Commit 870bbef

Browse files
committed
Add collector UT, and it's also an example for custom collector
1 parent 0b348ec commit 870bbef

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import (
20+
"testing"
21+
22+
dto "github.com/prometheus/client_model/go"
23+
24+
apimachineryversion "k8s.io/apimachinery/pkg/version"
25+
)
26+
27+
type testCustomCollector struct {
28+
BaseStableCollector
29+
}
30+
31+
var (
32+
currentVersion = apimachineryversion.Info{
33+
Major: "1",
34+
Minor: "17",
35+
GitVersion: "v1.17.0-alpha-1.12345",
36+
}
37+
alphaDesc = NewDesc("metric_alpha", "alpha metric", []string{"name"}, nil,
38+
ALPHA, "")
39+
stableDesc = NewDesc("metric_stable", "stable metrics", []string{"name"}, nil,
40+
STABLE, "")
41+
deprecatedDesc = NewDesc("metric_deprecated", "stable deprecated metrics", []string{"name"}, nil,
42+
STABLE, "1.17.0")
43+
hiddenDesc = NewDesc("metric_hidden", "stable hidden metrics", []string{"name"}, nil,
44+
STABLE, "1.16.0")
45+
)
46+
47+
func (tc *testCustomCollector) DescribeWithStability(ch chan<- *Desc) {
48+
ch <- alphaDesc
49+
ch <- stableDesc
50+
ch <- deprecatedDesc
51+
ch <- hiddenDesc
52+
}
53+
54+
func (tc *testCustomCollector) CollectWithStability(ch chan<- Metric) {
55+
ch <- MustNewConstMetric(
56+
alphaDesc,
57+
GaugeValue,
58+
1,
59+
"value",
60+
)
61+
ch <- MustNewConstMetric(
62+
stableDesc,
63+
GaugeValue,
64+
1,
65+
"value",
66+
)
67+
ch <- MustNewConstMetric(
68+
deprecatedDesc,
69+
GaugeValue,
70+
1,
71+
"value",
72+
)
73+
ch <- MustNewConstMetric(
74+
hiddenDesc,
75+
GaugeValue,
76+
1,
77+
"value",
78+
)
79+
80+
}
81+
82+
func getMetric(metrics []*dto.MetricFamily, fqName string) *dto.MetricFamily {
83+
for _, m := range metrics {
84+
if *m.Name == fqName {
85+
return m
86+
}
87+
}
88+
89+
return nil
90+
}
91+
92+
func TestBaseCustomCollector(t *testing.T) {
93+
var tests = []struct {
94+
name string
95+
d *Desc
96+
shouldHidden bool
97+
expectedHelp string
98+
}{
99+
{
100+
name: "alpha metric should contains stability metadata",
101+
d: alphaDesc,
102+
shouldHidden: false,
103+
expectedHelp: "[ALPHA] alpha metric",
104+
},
105+
{
106+
name: "stable metric should contains stability metadata",
107+
d: stableDesc,
108+
shouldHidden: false,
109+
expectedHelp: "[STABLE] stable metrics",
110+
},
111+
{
112+
name: "deprecated metric should contains stability metadata",
113+
d: deprecatedDesc,
114+
shouldHidden: false,
115+
expectedHelp: "[STABLE] (Deprecated since 1.17.0) stable deprecated metrics",
116+
},
117+
{
118+
name: "hidden metric should be ignored",
119+
d: hiddenDesc,
120+
shouldHidden: true,
121+
expectedHelp: "[STABLE] stable hidden metrics",
122+
},
123+
}
124+
125+
registry := newKubeRegistry(currentVersion)
126+
customCollector := &testCustomCollector{}
127+
128+
if err := registry.CustomRegister(customCollector); err != nil {
129+
t.Fatalf("register collector failed with err: %v", err)
130+
}
131+
132+
metrics, err := registry.Gather()
133+
if err != nil {
134+
t.Fatalf("failed to get metrics from collector, %v", err)
135+
}
136+
137+
for _, test := range tests {
138+
tc := test
139+
t.Run(tc.name, func(t *testing.T) {
140+
m := getMetric(metrics, tc.d.fqName)
141+
if m == nil {
142+
if !tc.shouldHidden {
143+
t.Fatalf("Want metric: %s", tc.d.fqName)
144+
}
145+
} else {
146+
if m.GetHelp() != tc.expectedHelp {
147+
t.Fatalf("Metric(%s) HELP(%s) not contains: %s", tc.d.fqName, *m.Help, tc.expectedHelp)
148+
}
149+
}
150+
151+
})
152+
}
153+
}

0 commit comments

Comments
 (0)