mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Merge pull request #112907 from logicalhan/stable-metric-metric
add meta-metrics for metrics framework
This commit is contained in:
commit
609bf91c95
@ -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
|
||||
|
@ -47,6 +47,7 @@ var (
|
||||
func init() {
|
||||
RawMustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
|
||||
RawMustRegister(collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.MetricsAll)))
|
||||
defaultRegistry.RegisterMetaMetrics()
|
||||
}
|
||||
|
||||
// Handler returns an HTTP handler for the DefaultGatherer. It is
|
||||
|
@ -72,6 +72,7 @@ type lazyMetric struct {
|
||||
markDeprecationOnce sync.Once
|
||||
createOnce sync.Once
|
||||
self kubeCollector
|
||||
stabilityLevel StabilityLevel
|
||||
}
|
||||
|
||||
func (r *lazyMetric) IsCreated() bool {
|
||||
@ -149,6 +150,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 +161,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()
|
||||
}
|
||||
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
|
||||
@ -129,6 +155,8 @@ type KubeRegistry interface {
|
||||
// Reset invokes the Reset() function on all items in the registry
|
||||
// which are added as resettables.
|
||||
Reset()
|
||||
// RegisterMetaMetrics registers metrics about the number of registered metrics.
|
||||
RegisterMetaMetrics()
|
||||
}
|
||||
|
||||
// kubeRegistry is a wrapper around a prometheus registry-type object. Upon initialization
|
||||
@ -250,6 +278,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 +358,14 @@ 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())
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *kubeRegistry) RegisterMetaMetrics() {
|
||||
r.MustRegister(registeredMetrics)
|
||||
r.MustRegister(disabledMetricsTotal)
|
||||
r.MustRegister(hiddenMetricsTotal)
|
||||
}
|
||||
|
@ -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