From b9ef1ce87ee2c341c3a962a4ffe7d0251ecb6fff Mon Sep 17 00:00:00 2001 From: RainbowMango Date: Mon, 18 Nov 2019 10:20:29 +0800 Subject: [PATCH 1/2] lazyInit accepts fqName when init. --- staging/src/k8s.io/component-base/metrics/counter.go | 4 ++-- staging/src/k8s.io/component-base/metrics/gauge.go | 4 ++-- staging/src/k8s.io/component-base/metrics/histogram.go | 4 ++-- staging/src/k8s.io/component-base/metrics/metric.go | 6 ++++-- staging/src/k8s.io/component-base/metrics/summary.go | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/staging/src/k8s.io/component-base/metrics/counter.go b/staging/src/k8s.io/component-base/metrics/counter.go index d641b078299..404ffcefc90 100644 --- a/staging/src/k8s.io/component-base/metrics/counter.go +++ b/staging/src/k8s.io/component-base/metrics/counter.go @@ -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 } diff --git a/staging/src/k8s.io/component-base/metrics/gauge.go b/staging/src/k8s.io/component-base/metrics/gauge.go index c9c187b7b00..7b4469fcb87 100644 --- a/staging/src/k8s.io/component-base/metrics/gauge.go +++ b/staging/src/k8s.io/component-base/metrics/gauge.go @@ -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 } diff --git a/staging/src/k8s.io/component-base/metrics/histogram.go b/staging/src/k8s.io/component-base/metrics/histogram.go index 1a4bbda813e..d233e12b92b 100644 --- a/staging/src/k8s.io/component-base/metrics/histogram.go +++ b/staging/src/k8s.io/component-base/metrics/histogram.go @@ -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 } diff --git a/staging/src/k8s.io/component-base/metrics/metric.go b/staging/src/k8s.io/component-base/metrics/metric.go index 015627b5990..3c62434e101 100644 --- a/staging/src/k8s.io/component-base/metrics/metric.go +++ b/staging/src/k8s.io/component-base/metrics/metric.go @@ -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) { diff --git a/staging/src/k8s.io/component-base/metrics/summary.go b/staging/src/k8s.io/component-base/metrics/summary.go index 3d8da006459..726bcc48940 100644 --- a/staging/src/k8s.io/component-base/metrics/summary.go +++ b/staging/src/k8s.io/component-base/metrics/summary.go @@ -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 } From 4d028a7903ac66db5795056f654c8b0c03b7c90a Mon Sep 17 00:00:00 2001 From: RainbowMango Date: Tue, 26 Nov 2019 21:45:26 +0800 Subject: [PATCH 2/2] Extend Registerable interface with FQName() and track collector by name. --- staging/src/k8s.io/component-base/metrics/metric.go | 5 +++++ .../src/k8s.io/component-base/metrics/registry.go | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/staging/src/k8s.io/component-base/metrics/metric.go b/staging/src/k8s.io/component-base/metrics/metric.go index 3c62434e101..c0383d01218 100644 --- a/staging/src/k8s.io/component-base/metrics/metric.go +++ b/staging/src/k8s.io/component-base/metrics/metric.go @@ -158,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. diff --git a/staging/src/k8s.io/component-base/metrics/registry.go b/staging/src/k8s.io/component-base/metrics/registry.go index ed018332c73..a2fd8a06cdc 100644 --- a/staging/src/k8s.io/component-base/metrics/registry.go +++ b/staging/src/k8s.io/component-base/metrics/registry.go @@ -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()