mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-20 10:20:51 +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{
|
||||
CounterOpts: opts,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
kc.setPrometheusCounter(noop)
|
||||
kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||
@ -129,7 +129,7 @@ func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
|
||||
CounterVec: noopCounterVec,
|
||||
CounterOpts: opts,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
cv.lazyInit(cv, fqName)
|
||||
return cv
|
||||
|
@ -46,7 +46,7 @@ func NewGauge(opts *GaugeOpts) *Gauge {
|
||||
|
||||
kc := &Gauge{
|
||||
GaugeOpts: opts,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
kc.setPrometheusGauge(noop)
|
||||
kc.lazyInit(kc, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||
@ -115,7 +115,7 @@ func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec {
|
||||
GaugeVec: noopGaugeVec,
|
||||
GaugeOpts: opts,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
cv.lazyInit(cv, fqName)
|
||||
return cv
|
||||
|
@ -52,7 +52,7 @@ func NewHistogram(opts *HistogramOpts) *Histogram {
|
||||
|
||||
h := &Histogram{
|
||||
HistogramOpts: opts,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
h.setPrometheusHistogram(noopMetric{})
|
||||
h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||
@ -119,7 +119,7 @@ func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec {
|
||||
HistogramVec: noopHistogramVec,
|
||||
HistogramOpts: opts,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
v.lazyInit(v, fqName)
|
||||
return v
|
||||
|
@ -72,6 +72,7 @@ type lazyMetric struct {
|
||||
markDeprecationOnce sync.Once
|
||||
createOnce sync.Once
|
||||
self kubeCollector
|
||||
stabilityLevel StabilityLevel
|
||||
}
|
||||
|
||||
func (r *lazyMetric) IsCreated() bool {
|
||||
@ -88,6 +89,14 @@ func (r *lazyMetric) lazyInit(self kubeCollector, fqName string) {
|
||||
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.
|
||||
// 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:
|
||||
@ -149,6 +158,7 @@ func (r *lazyMetric) Create(version *semver.Version) bool {
|
||||
if r.IsHidden() {
|
||||
return false
|
||||
}
|
||||
|
||||
r.createOnce.Do(func() {
|
||||
r.createLock.Lock()
|
||||
defer r.createLock.Unlock()
|
||||
@ -159,6 +169,13 @@ func (r *lazyMetric) Create(version *semver.Version) bool {
|
||||
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()
|
||||
}
|
||||
|
||||
|
@ -36,6 +36,31 @@ var (
|
||||
registries []*kubeRegistry // stores all registries created by NewKubeRegistry()
|
||||
registriesLock sync.RWMutex
|
||||
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
|
||||
@ -67,6 +92,7 @@ func SetDisabledMetric(name string) {
|
||||
disabledMetricsLock.Lock()
|
||||
defer disabledMetricsLock.Unlock()
|
||||
disabledMetrics[name] = struct{}{}
|
||||
disabledMetricsTotal.Inc()
|
||||
}
|
||||
|
||||
// SetShowHidden will enable showing hidden metrics. This will no-opt
|
||||
@ -250,6 +276,7 @@ func (kr *kubeRegistry) trackHiddenCollector(c Registerable) {
|
||||
defer kr.hiddenCollectorsLock.Unlock()
|
||||
|
||||
kr.hiddenCollectors[c.FQName()] = c
|
||||
hiddenMetricsTotal.Inc()
|
||||
}
|
||||
|
||||
// trackStableCollectors stores all custom collectors.
|
||||
@ -329,9 +356,11 @@ func newKubeRegistry(v apimachineryversion.Info) *kubeRegistry {
|
||||
return r
|
||||
}
|
||||
|
||||
// NewKubeRegistry creates a new vanilla Registry without any Collectors
|
||||
// pre-registered.
|
||||
// NewKubeRegistry creates a new vanilla Registry
|
||||
func NewKubeRegistry() KubeRegistry {
|
||||
r := newKubeRegistry(BuildVersion())
|
||||
r.MustRegister(registeredMetrics)
|
||||
r.MustRegister(disabledMetricsTotal)
|
||||
r.MustRegister(hiddenMetricsTotal)
|
||||
return r
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ func NewSummary(opts *SummaryOpts) *Summary {
|
||||
|
||||
s := &Summary{
|
||||
SummaryOpts: opts,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
s.setPrometheusSummary(noopMetric{})
|
||||
s.lazyInit(s, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||
@ -118,7 +118,7 @@ func NewSummaryVec(opts *SummaryOpts, labels []string) *SummaryVec {
|
||||
v := &SummaryVec{
|
||||
SummaryOpts: opts,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
v.lazyInit(v, fqName)
|
||||
return v
|
||||
|
@ -57,7 +57,7 @@ func NewTestableTimingHistogram(nowFunc func() time.Time, opts *TimingHistogramO
|
||||
h := &TimingHistogram{
|
||||
TimingHistogramOpts: opts,
|
||||
nowFunc: nowFunc,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
h.setPrometheusHistogram(noopMetric{})
|
||||
h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
|
||||
@ -136,7 +136,7 @@ func NewTestableTimingHistogramVec(nowFunc func() time.Time, opts *TimingHistogr
|
||||
TimingHistogramOpts: opts,
|
||||
nowFunc: nowFunc,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
lazyMetric: lazyMetric{stabilityLevel: opts.StabilityLevel},
|
||||
}
|
||||
v.lazyInit(v, fqName)
|
||||
return v
|
||||
|
Loading…
Reference in New Issue
Block a user