Extend Registerable interface with FQName() and track collector by name.

This commit is contained in:
RainbowMango 2019-11-26 21:45:26 +08:00
parent b9ef1ce87e
commit 4d028a7903
2 changed files with 13 additions and 4 deletions

View File

@ -158,6 +158,11 @@ func (r *lazyMetric) ClearState() {
r.createOnce = *(new(sync.Once)) 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 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. 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 will clear all the states marked by Create.
ClearState() 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 // KubeRegistry is an interface which implements a subset of prometheus.Registerer and
@ -127,7 +130,7 @@ type KubeRegistry interface {
type kubeRegistry struct { type kubeRegistry struct {
PromRegistry PromRegistry
version semver.Version 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 hiddenCollectorsLock sync.RWMutex
} }
@ -228,7 +231,7 @@ func (kr *kubeRegistry) trackHiddenCollector(c Registerable) {
kr.hiddenCollectorsLock.Lock() kr.hiddenCollectorsLock.Lock()
defer kr.hiddenCollectorsLock.Unlock() defer kr.hiddenCollectorsLock.Unlock()
kr.hiddenCollectors = append(kr.hiddenCollectors, c) kr.hiddenCollectors[c.FQName()] = c
} }
// enableHiddenCollectors will re-register all of the hidden collectors. // enableHiddenCollectors will re-register all of the hidden collectors.
@ -245,8 +248,9 @@ func (kr *kubeRegistry) enableHiddenCollectors() {
func newKubeRegistry(v apimachineryversion.Info) *kubeRegistry { func newKubeRegistry(v apimachineryversion.Info) *kubeRegistry {
r := &kubeRegistry{ r := &kubeRegistry{
PromRegistry: prometheus.NewRegistry(), PromRegistry: prometheus.NewRegistry(),
version: parseVersion(v), version: parseVersion(v),
hiddenCollectors: make(map[string]Registerable),
} }
registriesLock.Lock() registriesLock.Lock()