Merge pull request #85640 from RainbowMango/pr_track_collector_by_name

Track collectors by fqName
This commit is contained in:
Kubernetes Prow Robot 2019-11-26 21:45:20 -08:00 committed by GitHub
commit 98abd32ee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 14 deletions

View File

@ -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
}
@ -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
}

View File

@ -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
}
@ -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
}

View File

@ -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
}
@ -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
}

View File

@ -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
@ -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
}
@ -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) {
@ -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.

View File

@ -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
@ -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
}
@ -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.
@ -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()

View File

@ -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
}
@ -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
}