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

provide functionality for custom collector testing #84919

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
14 changes: 14 additions & 0 deletions staging/src/k8s.io/component-base/metrics/desc.go
Expand Up @@ -18,6 +18,7 @@ package metrics

import (
"fmt"
"strings"
"sync"

"github.com/blang/semver"
Expand Down Expand Up @@ -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)
}
10 changes: 10 additions & 0 deletions staging/src/k8s.io/component-base/metrics/testutil/testutil.go
Expand Up @@ -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...)
}