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

Track collectors by fqName #85640

Merged
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
4 changes: 2 additions & 2 deletions staging/src/k8s.io/component-base/metrics/counter.go
Expand Up @@ -41,7 +41,7 @@ func NewCounter(opts *CounterOpts) *Counter {
lazyMetric: lazyMetric{},
}
kc.setPrometheusCounter(noop)
kc.lazyInit(kc)
kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return kc
}

Expand Down Expand Up @@ -92,7 +92,7 @@ func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
originalLabels: labels,
lazyMetric: lazyMetric{},
}
cv.lazyInit(cv)
cv.lazyInit(cv, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return cv
}

Expand Down
4 changes: 2 additions & 2 deletions staging/src/k8s.io/component-base/metrics/gauge.go
Expand Up @@ -43,7 +43,7 @@ func NewGauge(opts *GaugeOpts) *Gauge {
lazyMetric: lazyMetric{},
}
kc.setPrometheusGauge(noop)
kc.lazyInit(kc)
kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return kc
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec {
originalLabels: labels,
lazyMetric: lazyMetric{},
}
cv.lazyInit(cv)
cv.lazyInit(cv, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return cv
}

Expand Down
4 changes: 2 additions & 2 deletions staging/src/k8s.io/component-base/metrics/histogram.go
Expand Up @@ -53,7 +53,7 @@ func NewHistogram(opts *HistogramOpts) *Histogram {
lazyMetric: lazyMetric{},
}
h.setPrometheusHistogram(noopMetric{})
h.lazyInit(h)
h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return h
}

Expand Down Expand Up @@ -104,7 +104,7 @@ func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec {
originalLabels: labels,
lazyMetric: lazyMetric{},
}
v.lazyInit(v)
v.lazyInit(v, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return v
}

Expand Down
11 changes: 9 additions & 2 deletions staging/src/k8s.io/component-base/metrics/metric.go
Expand Up @@ -63,6 +63,7 @@ implements kubeCollector to get deferred registration behavior. You must call la
with the kubeCollector itself as an argument.
*/
type lazyMetric struct {
fqName string
isDeprecated bool
isHidden bool
isCreated bool
Expand All @@ -81,7 +82,8 @@ func (r *lazyMetric) IsCreated() bool {
// lazyInit provides the lazyMetric with a reference to the kubeCollector it is supposed
// to allow lazy initialization for. It should be invoked in the factory function which creates new
// kubeCollector type objects.
func (r *lazyMetric) lazyInit(self kubeCollector) {
func (r *lazyMetric) lazyInit(self kubeCollector, fqName string) {
r.fqName = fqName
r.self = self
}

Expand All @@ -98,7 +100,7 @@ func (r *lazyMetric) determineDeprecationStatus(version semver.Version) {
r.isDeprecated = true
}
if ShouldShowHidden() {
klog.Warningf("Hidden metrics have been manually overridden, showing this very deprecated metric.")
klog.Warningf("Hidden metrics (%s) have been manually overridden, showing this very deprecated metric.", r.fqName)
return
}
if shouldHide(&version, selfVersion) {
Expand Down Expand Up @@ -156,6 +158,11 @@ func (r *lazyMetric) ClearState() {
r.createOnce = *(new(sync.Once))
}

// FQName returns the fully-qualified metric name of the collector.
func (r *lazyMetric) FQName() string {
return r.fqName
}

/*
This code is directly lifted from the prometheus codebase. It's a convenience struct which
allows you satisfy the Collector interface automatically if you already satisfy the Metric interface.
Expand Down
12 changes: 8 additions & 4 deletions staging/src/k8s.io/component-base/metrics/registry.go
Expand Up @@ -104,6 +104,9 @@ type Registerable interface {

// ClearState will clear all the states marked by Create.
ClearState()

// FQName returns the fully-qualified metric name of the collector.
FQName() string
}

// KubeRegistry is an interface which implements a subset of prometheus.Registerer and
Expand All @@ -127,7 +130,7 @@ type KubeRegistry interface {
type kubeRegistry struct {
PromRegistry
version semver.Version
hiddenCollectors []Registerable // stores all collectors that has been hidden
hiddenCollectors map[string]Registerable // stores all collectors that has been hidden
hiddenCollectorsLock sync.RWMutex
}

Expand Down Expand Up @@ -228,7 +231,7 @@ func (kr *kubeRegistry) trackHiddenCollector(c Registerable) {
kr.hiddenCollectorsLock.Lock()
defer kr.hiddenCollectorsLock.Unlock()

kr.hiddenCollectors = append(kr.hiddenCollectors, c)
kr.hiddenCollectors[c.FQName()] = c
}

// enableHiddenCollectors will re-register all of the hidden collectors.
Expand All @@ -245,8 +248,9 @@ func (kr *kubeRegistry) enableHiddenCollectors() {

func newKubeRegistry(v apimachineryversion.Info) *kubeRegistry {
r := &kubeRegistry{
PromRegistry: prometheus.NewRegistry(),
version: parseVersion(v),
PromRegistry: prometheus.NewRegistry(),
version: parseVersion(v),
hiddenCollectors: make(map[string]Registerable),
}

registriesLock.Lock()
Expand Down
4 changes: 2 additions & 2 deletions staging/src/k8s.io/component-base/metrics/summary.go
Expand Up @@ -44,7 +44,7 @@ func NewSummary(opts *SummaryOpts) *Summary {
lazyMetric: lazyMetric{},
}
s.setPrometheusSummary(noopMetric{})
s.lazyInit(s)
s.lazyInit(s, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return s
}

Expand Down Expand Up @@ -98,7 +98,7 @@ func NewSummaryVec(opts *SummaryOpts, labels []string) *SummaryVec {
originalLabels: labels,
lazyMetric: lazyMetric{},
}
v.lazyInit(v)
v.lazyInit(v, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return v
}

Expand Down