mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
add meta-metrics for metrics framework
Change-Id: I15c2947e910419e6cb07db22a40fc0667fcd128b
This commit is contained in:
parent
44a0b4e145
commit
1b74ea0f29
@ -44,7 +44,7 @@ func NewCounter(opts *CounterOpts) *Counter {
|
|||||||
|
|
||||||
kc := &Counter{
|
kc := &Counter{
|
||||||
CounterOpts: opts,
|
CounterOpts: opts,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
kc.setPrometheusCounter(noop)
|
kc.setPrometheusCounter(noop)
|
||||||
kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||||
@ -129,7 +129,7 @@ func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
|
|||||||
CounterVec: noopCounterVec,
|
CounterVec: noopCounterVec,
|
||||||
CounterOpts: opts,
|
CounterOpts: opts,
|
||||||
originalLabels: labels,
|
originalLabels: labels,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
cv.lazyInit(cv, fqName)
|
cv.lazyInit(cv, fqName)
|
||||||
return cv
|
return cv
|
||||||
|
@ -46,7 +46,7 @@ func NewGauge(opts *GaugeOpts) *Gauge {
|
|||||||
|
|
||||||
kc := &Gauge{
|
kc := &Gauge{
|
||||||
GaugeOpts: opts,
|
GaugeOpts: opts,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
kc.setPrometheusGauge(noop)
|
kc.setPrometheusGauge(noop)
|
||||||
kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||||
@ -115,7 +115,7 @@ func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec {
|
|||||||
GaugeVec: noopGaugeVec,
|
GaugeVec: noopGaugeVec,
|
||||||
GaugeOpts: opts,
|
GaugeOpts: opts,
|
||||||
originalLabels: labels,
|
originalLabels: labels,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
cv.lazyInit(cv, fqName)
|
cv.lazyInit(cv, fqName)
|
||||||
return cv
|
return cv
|
||||||
|
@ -52,7 +52,7 @@ func NewHistogram(opts *HistogramOpts) *Histogram {
|
|||||||
|
|
||||||
h := &Histogram{
|
h := &Histogram{
|
||||||
HistogramOpts: opts,
|
HistogramOpts: opts,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
h.setPrometheusHistogram(noopMetric{})
|
h.setPrometheusHistogram(noopMetric{})
|
||||||
h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||||
@ -119,7 +119,7 @@ func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec {
|
|||||||
HistogramVec: noopHistogramVec,
|
HistogramVec: noopHistogramVec,
|
||||||
HistogramOpts: opts,
|
HistogramOpts: opts,
|
||||||
originalLabels: labels,
|
originalLabels: labels,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
v.lazyInit(v, fqName)
|
v.lazyInit(v, fqName)
|
||||||
return v
|
return v
|
||||||
|
@ -72,6 +72,7 @@ type lazyMetric struct {
|
|||||||
markDeprecationOnce sync.Once
|
markDeprecationOnce sync.Once
|
||||||
createOnce sync.Once
|
createOnce sync.Once
|
||||||
self kubeCollector
|
self kubeCollector
|
||||||
|
stabilityLevel StabilityLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *lazyMetric) IsCreated() bool {
|
func (r *lazyMetric) IsCreated() bool {
|
||||||
@ -88,6 +89,14 @@ func (r *lazyMetric) lazyInit(self kubeCollector, fqName string) {
|
|||||||
r.self = self
|
r.self = self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stabilityLevelOrDefault(sl string) string {
|
||||||
|
if sl == "" {
|
||||||
|
return string(INTERNAL)
|
||||||
|
} else {
|
||||||
|
return sl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// preprocessMetric figures out whether the lazy metric should be hidden or not.
|
// preprocessMetric figures out whether the lazy metric should be hidden or not.
|
||||||
// This method takes a Version argument which should be the version of the binary in which
|
// This method takes a Version argument which should be the version of the binary in which
|
||||||
// this code is currently being executed. A metric can be hidden under two conditions:
|
// this code is currently being executed. A metric can be hidden under two conditions:
|
||||||
@ -149,6 +158,7 @@ func (r *lazyMetric) Create(version *semver.Version) bool {
|
|||||||
if r.IsHidden() {
|
if r.IsHidden() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
r.createOnce.Do(func() {
|
r.createOnce.Do(func() {
|
||||||
r.createLock.Lock()
|
r.createLock.Lock()
|
||||||
defer r.createLock.Unlock()
|
defer r.createLock.Unlock()
|
||||||
@ -159,6 +169,13 @@ func (r *lazyMetric) Create(version *semver.Version) bool {
|
|||||||
r.self.initializeMetric()
|
r.self.initializeMetric()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
sl := r.stabilityLevel
|
||||||
|
deprecatedV := r.self.DeprecatedVersion()
|
||||||
|
dv := ""
|
||||||
|
if deprecatedV != nil {
|
||||||
|
dv = deprecatedV.String()
|
||||||
|
}
|
||||||
|
defer registeredMetrics.WithLabelValues(string(sl), dv).Inc()
|
||||||
return r.IsCreated()
|
return r.IsCreated()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +36,31 @@ var (
|
|||||||
registries []*kubeRegistry // stores all registries created by NewKubeRegistry()
|
registries []*kubeRegistry // stores all registries created by NewKubeRegistry()
|
||||||
registriesLock sync.RWMutex
|
registriesLock sync.RWMutex
|
||||||
disabledMetrics = map[string]struct{}{}
|
disabledMetrics = map[string]struct{}{}
|
||||||
|
|
||||||
|
registeredMetrics = NewCounterVec(
|
||||||
|
&CounterOpts{
|
||||||
|
Name: "registered_metric_total",
|
||||||
|
Help: "The count of registered metrics broken by stability level and deprecation version.",
|
||||||
|
StabilityLevel: ALPHA,
|
||||||
|
},
|
||||||
|
[]string{"stability_level", "deprecated_version"},
|
||||||
|
)
|
||||||
|
|
||||||
|
disabledMetricsTotal = NewCounter(
|
||||||
|
&CounterOpts{
|
||||||
|
Name: "disabled_metric_total",
|
||||||
|
Help: "The count of disabled metrics.",
|
||||||
|
StabilityLevel: ALPHA,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
hiddenMetricsTotal = NewCounter(
|
||||||
|
&CounterOpts{
|
||||||
|
Name: "hidden_metric_total",
|
||||||
|
Help: "The count of hidden metrics.",
|
||||||
|
StabilityLevel: ALPHA,
|
||||||
|
},
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
// shouldHide be used to check if a specific metric with deprecated version should be hidden
|
// shouldHide be used to check if a specific metric with deprecated version should be hidden
|
||||||
@ -67,6 +92,7 @@ func SetDisabledMetric(name string) {
|
|||||||
disabledMetricsLock.Lock()
|
disabledMetricsLock.Lock()
|
||||||
defer disabledMetricsLock.Unlock()
|
defer disabledMetricsLock.Unlock()
|
||||||
disabledMetrics[name] = struct{}{}
|
disabledMetrics[name] = struct{}{}
|
||||||
|
disabledMetricsTotal.Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetShowHidden will enable showing hidden metrics. This will no-opt
|
// SetShowHidden will enable showing hidden metrics. This will no-opt
|
||||||
@ -250,6 +276,7 @@ func (kr *kubeRegistry) trackHiddenCollector(c Registerable) {
|
|||||||
defer kr.hiddenCollectorsLock.Unlock()
|
defer kr.hiddenCollectorsLock.Unlock()
|
||||||
|
|
||||||
kr.hiddenCollectors[c.FQName()] = c
|
kr.hiddenCollectors[c.FQName()] = c
|
||||||
|
hiddenMetricsTotal.Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
// trackStableCollectors stores all custom collectors.
|
// trackStableCollectors stores all custom collectors.
|
||||||
@ -329,9 +356,11 @@ func newKubeRegistry(v apimachineryversion.Info) *kubeRegistry {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewKubeRegistry creates a new vanilla Registry without any Collectors
|
// NewKubeRegistry creates a new vanilla Registry
|
||||||
// pre-registered.
|
|
||||||
func NewKubeRegistry() KubeRegistry {
|
func NewKubeRegistry() KubeRegistry {
|
||||||
r := newKubeRegistry(BuildVersion())
|
r := newKubeRegistry(BuildVersion())
|
||||||
|
r.MustRegister(registeredMetrics)
|
||||||
|
r.MustRegister(disabledMetricsTotal)
|
||||||
|
r.MustRegister(hiddenMetricsTotal)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ func NewSummary(opts *SummaryOpts) *Summary {
|
|||||||
|
|
||||||
s := &Summary{
|
s := &Summary{
|
||||||
SummaryOpts: opts,
|
SummaryOpts: opts,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
s.setPrometheusSummary(noopMetric{})
|
s.setPrometheusSummary(noopMetric{})
|
||||||
s.lazyInit(s, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
s.lazyInit(s, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||||
@ -118,7 +118,7 @@ func NewSummaryVec(opts *SummaryOpts, labels []string) *SummaryVec {
|
|||||||
v := &SummaryVec{
|
v := &SummaryVec{
|
||||||
SummaryOpts: opts,
|
SummaryOpts: opts,
|
||||||
originalLabels: labels,
|
originalLabels: labels,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
v.lazyInit(v, fqName)
|
v.lazyInit(v, fqName)
|
||||||
return v
|
return v
|
||||||
|
@ -57,7 +57,7 @@ func NewTestableTimingHistogram(nowFunc func() time.Time, opts *TimingHistogramO
|
|||||||
h := &TimingHistogram{
|
h := &TimingHistogram{
|
||||||
TimingHistogramOpts: opts,
|
TimingHistogramOpts: opts,
|
||||||
nowFunc: nowFunc,
|
nowFunc: nowFunc,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
h.setPrometheusHistogram(noopMetric{})
|
h.setPrometheusHistogram(noopMetric{})
|
||||||
h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||||
@ -136,7 +136,7 @@ func NewTestableTimingHistogramVec(nowFunc func() time.Time, opts *TimingHistogr
|
|||||||
TimingHistogramOpts: opts,
|
TimingHistogramOpts: opts,
|
||||||
nowFunc: nowFunc,
|
nowFunc: nowFunc,
|
||||||
originalLabels: labels,
|
originalLabels: labels,
|
||||||
lazyMetric: lazyMetric{},
|
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||||
}
|
}
|
||||||
v.lazyInit(v, fqName)
|
v.lazyInit(v, fqName)
|
||||||
return v
|
return v
|
||||||
|
Loading…
Reference in New Issue
Block a user