Skip to content

Commit cee00e9

Browse files
authored
Merge pull request kubernetes#84919 from RainbowMango/pr_functionality_for_customcollector_testing
provide functionality for custom collector testing
2 parents bae8f56 + 1b01a5e commit cee00e9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

staging/src/k8s.io/component-base/metrics/desc.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package metrics
1818

1919
import (
2020
"fmt"
21+
"strings"
2122
"sync"
2223

2324
"github.com/blang/semver"
@@ -156,3 +157,16 @@ func (d *Desc) initializeDeprecatedDesc() {
156157
d.markDeprecated()
157158
d.initialize()
158159
}
160+
161+
// GetRawDesc will returns a new *Desc with original parameters provided to NewDesc().
162+
//
163+
// It will be useful in testing scenario that the same Desc be registered to different registry.
164+
// 1. Desc `D` is registered to registry 'A' in TestA (Note: `D` maybe created)
165+
// 2. Desc `D` is registered to registry 'B' in TestB (Note: since 'D' has been created once, thus will be ignored by registry 'B')
166+
func (d *Desc) GetRawDesc() *Desc {
167+
// remove stability from help if any
168+
stabilityStr := fmt.Sprintf("[%v] ", d.stabilityLevel)
169+
rawHelp := strings.Replace(d.help, stabilityStr, "", -1)
170+
171+
return NewDesc(d.fqName, rawHelp, d.variableLabels, d.constLabels, d.stabilityLevel, d.deprecatedVersion)
172+
}

staging/src/k8s.io/component-base/metrics/testutil/testutil.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ func CollectAndCompare(c metrics.Collector, expected io.Reader, metricNames ...s
3838
func GatherAndCompare(g metrics.Gatherer, expected io.Reader, metricNames ...string) error {
3939
return testutil.GatherAndCompare(g, expected, metricNames...)
4040
}
41+
42+
// CustomCollectAndCompare registers the provided StableCollector with a newly created
43+
// registry. It then does the same as GatherAndCompare, gathering the
44+
// metrics from the pedantic Registry.
45+
func CustomCollectAndCompare(c metrics.StableCollector, expected io.Reader, metricNames ...string) error {
46+
registry := metrics.NewKubeRegistry()
47+
registry.CustomMustRegister(c)
48+
49+
return GatherAndCompare(registry, expected, metricNames...)
50+
}

0 commit comments

Comments
 (0)