Add CustomCollectAndCompare to testutils which especially for custom collector

Add GetRawDesc() to Desc.
This commit is contained in:
RainbowMango 2019-11-07 18:34:37 +08:00
parent 695c3061dd
commit 1b01a5eb9b
2 changed files with 24 additions and 0 deletions

View File

@ -18,6 +18,7 @@ package metrics
import (
"fmt"
"strings"
"sync"
"github.com/blang/semver"
@ -156,3 +157,16 @@ func (d *Desc) initializeDeprecatedDesc() {
d.markDeprecated()
d.initialize()
}
// GetRawDesc will returns a new *Desc with original parameters provided to NewDesc().
//
// It will be useful in testing scenario that the same Desc be registered to different registry.
// 1. Desc `D` is registered to registry 'A' in TestA (Note: `D` maybe created)
// 2. Desc `D` is registered to registry 'B' in TestB (Note: since 'D' has been created once, thus will be ignored by registry 'B')
func (d *Desc) GetRawDesc() *Desc {
// remove stability from help if any
stabilityStr := fmt.Sprintf("[%v] ", d.stabilityLevel)
rawHelp := strings.Replace(d.help, stabilityStr, "", -1)
return NewDesc(d.fqName, rawHelp, d.variableLabels, d.constLabels, d.stabilityLevel, d.deprecatedVersion)
}

View File

@ -38,3 +38,13 @@ func CollectAndCompare(c metrics.Collector, expected io.Reader, metricNames ...s
func GatherAndCompare(g metrics.Gatherer, expected io.Reader, metricNames ...string) error {
return testutil.GatherAndCompare(g, expected, metricNames...)
}
// CustomCollectAndCompare registers the provided StableCollector with a newly created
// registry. It then does the same as GatherAndCompare, gathering the
// metrics from the pedantic Registry.
func CustomCollectAndCompare(c metrics.StableCollector, expected io.Reader, metricNames ...string) error {
registry := metrics.NewKubeRegistry()
registry.CustomMustRegister(c)
return GatherAndCompare(registry, expected, metricNames...)
}