2020-01-14 10:38:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2022-05-05 02:47:06 +00:00
|
|
|
"github.com/blang/semver/v4"
|
2020-01-14 10:38:55 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
|
|
|
|
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
|
|
|
"k8s.io/component-base/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-06-25 04:59:51 +00:00
|
|
|
showHiddenOnce sync.Once
|
|
|
|
disabledMetricsLock sync.RWMutex
|
2023-02-01 17:06:36 +00:00
|
|
|
showHidden atomic.Bool
|
2021-06-25 04:59:51 +00:00
|
|
|
registries []*kubeRegistry // stores all registries created by NewKubeRegistry()
|
|
|
|
registriesLock sync.RWMutex
|
|
|
|
disabledMetrics = map[string]struct{}{}
|
2023-02-01 17:06:36 +00:00
|
|
|
|
2023-12-20 12:23:59 +00:00
|
|
|
registeredMetricsTotal = NewCounterVec(
|
2023-02-01 17:06:36 +00:00
|
|
|
&CounterOpts{
|
2023-08-17 05:15:28 +00:00
|
|
|
Name: "registered_metrics_total",
|
2023-02-01 17:06:36 +00:00
|
|
|
Help: "The count of registered metrics broken by stability level and deprecation version.",
|
2023-08-17 05:15:28 +00:00
|
|
|
StabilityLevel: BETA,
|
2023-02-01 17:06:36 +00:00
|
|
|
},
|
|
|
|
[]string{"stability_level", "deprecated_version"},
|
|
|
|
)
|
|
|
|
|
|
|
|
disabledMetricsTotal = NewCounter(
|
|
|
|
&CounterOpts{
|
2023-08-17 05:15:28 +00:00
|
|
|
Name: "disabled_metrics_total",
|
2023-02-01 17:06:36 +00:00
|
|
|
Help: "The count of disabled metrics.",
|
2023-08-17 05:15:28 +00:00
|
|
|
StabilityLevel: BETA,
|
2023-02-01 17:06:36 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
hiddenMetricsTotal = NewCounter(
|
|
|
|
&CounterOpts{
|
2023-08-17 05:15:28 +00:00
|
|
|
Name: "hidden_metrics_total",
|
2023-02-01 17:06:36 +00:00
|
|
|
Help: "The count of hidden metrics.",
|
2023-08-17 05:15:28 +00:00
|
|
|
StabilityLevel: BETA,
|
2023-02-01 17:06:36 +00:00
|
|
|
},
|
|
|
|
)
|
2023-12-20 12:23:59 +00:00
|
|
|
|
|
|
|
cardinalityEnforcementUnexpectedCategorizationsTotal = NewCounter(
|
|
|
|
&CounterOpts{
|
|
|
|
Name: "cardinality_enforcement_unexpected_categorizations_total",
|
|
|
|
Help: "The count of unexpected categorizations during cardinality enforcement.",
|
|
|
|
StabilityLevel: ALPHA,
|
|
|
|
},
|
|
|
|
)
|
2020-01-14 10:38:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// shouldHide be used to check if a specific metric with deprecated version should be hidden
|
|
|
|
// according to metrics deprecation lifecycle.
|
|
|
|
func shouldHide(currentVersion *semver.Version, deprecatedVersion *semver.Version) bool {
|
|
|
|
guardVersion, err := semver.Make(fmt.Sprintf("%d.%d.0", currentVersion.Major, currentVersion.Minor))
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to make version from current version")
|
|
|
|
}
|
|
|
|
|
|
|
|
if deprecatedVersion.LT(guardVersion) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateShowHiddenMetricsVersion checks invalid version for which show hidden metrics.
|
|
|
|
func ValidateShowHiddenMetricsVersion(v string) []error {
|
|
|
|
err := validateShowHiddenMetricsVersion(parseVersion(version.Get()), v)
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 04:59:51 +00:00
|
|
|
func SetDisabledMetric(name string) {
|
|
|
|
disabledMetricsLock.Lock()
|
|
|
|
defer disabledMetricsLock.Unlock()
|
|
|
|
disabledMetrics[name] = struct{}{}
|
2023-02-01 17:06:36 +00:00
|
|
|
disabledMetricsTotal.Inc()
|
2021-06-25 04:59:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
// SetShowHidden will enable showing hidden metrics. This will no-opt
|
|
|
|
// after the initial call
|
|
|
|
func SetShowHidden() {
|
|
|
|
showHiddenOnce.Do(func() {
|
|
|
|
showHidden.Store(true)
|
|
|
|
|
|
|
|
// re-register collectors that has been hidden in phase of last registry.
|
|
|
|
for _, r := range registries {
|
|
|
|
r.enableHiddenCollectors()
|
2020-04-14 07:04:33 +00:00
|
|
|
r.enableHiddenStableCollectors()
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldShowHidden returns whether showing hidden deprecated metrics
|
|
|
|
// is enabled. While the primary usecase for this is internal (to determine
|
|
|
|
// registration behavior) this can also be used to introspect
|
|
|
|
func ShouldShowHidden() bool {
|
2023-02-01 17:06:36 +00:00
|
|
|
return showHidden.Load()
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registerable is an interface for a collector metric which we
|
|
|
|
// will register with KubeRegistry.
|
|
|
|
type Registerable interface {
|
|
|
|
prometheus.Collector
|
|
|
|
|
|
|
|
// Create will mark deprecated state for the collector
|
|
|
|
Create(version *semver.Version) bool
|
|
|
|
|
|
|
|
// ClearState will clear all the states marked by Create.
|
|
|
|
ClearState()
|
2020-04-14 07:04:33 +00:00
|
|
|
|
|
|
|
// FQName returns the fully-qualified metric name of the collector.
|
|
|
|
FQName() string
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 12:28:29 +00:00
|
|
|
type resettable interface {
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
// KubeRegistry is an interface which implements a subset of prometheus.Registerer and
|
|
|
|
// prometheus.Gatherer interfaces
|
|
|
|
type KubeRegistry interface {
|
|
|
|
// Deprecated
|
|
|
|
RawMustRegister(...prometheus.Collector)
|
2020-12-17 12:28:29 +00:00
|
|
|
// CustomRegister is our internal variant of Prometheus registry.Register
|
2020-01-14 10:38:55 +00:00
|
|
|
CustomRegister(c StableCollector) error
|
2020-12-17 12:28:29 +00:00
|
|
|
// CustomMustRegister is our internal variant of Prometheus registry.MustRegister
|
2020-01-14 10:38:55 +00:00
|
|
|
CustomMustRegister(cs ...StableCollector)
|
2020-12-17 12:28:29 +00:00
|
|
|
// Register conforms to Prometheus registry.Register
|
2020-01-14 10:38:55 +00:00
|
|
|
Register(Registerable) error
|
2020-12-17 12:28:29 +00:00
|
|
|
// MustRegister conforms to Prometheus registry.MustRegister
|
2020-01-14 10:38:55 +00:00
|
|
|
MustRegister(...Registerable)
|
2020-12-17 12:28:29 +00:00
|
|
|
// Unregister conforms to Prometheus registry.Unregister
|
2020-04-14 07:04:33 +00:00
|
|
|
Unregister(collector Collector) bool
|
2020-12-17 12:28:29 +00:00
|
|
|
// Gather conforms to Prometheus gatherer.Gather
|
2020-01-14 10:38:55 +00:00
|
|
|
Gather() ([]*dto.MetricFamily, error)
|
2020-12-17 12:28:29 +00:00
|
|
|
// Reset invokes the Reset() function on all items in the registry
|
|
|
|
// which are added as resettables.
|
|
|
|
Reset()
|
2023-02-01 17:06:36 +00:00
|
|
|
// RegisterMetaMetrics registers metrics about the number of registered metrics.
|
|
|
|
RegisterMetaMetrics()
|
|
|
|
// Registerer exposes the underlying prometheus registerer
|
|
|
|
Registerer() prometheus.Registerer
|
|
|
|
// Gatherer exposes the underlying prometheus gatherer
|
|
|
|
Gatherer() prometheus.Gatherer
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kubeRegistry is a wrapper around a prometheus registry-type object. Upon initialization
|
|
|
|
// the kubernetes binary version information is loaded into the registry object, so that
|
|
|
|
// automatic behavior can be configured for metric versioning.
|
|
|
|
type kubeRegistry struct {
|
|
|
|
PromRegistry
|
|
|
|
version semver.Version
|
2020-04-14 07:04:33 +00:00
|
|
|
hiddenCollectors map[string]Registerable // stores all collectors that has been hidden
|
|
|
|
stableCollectors []StableCollector // stores all stable collector
|
2020-01-14 10:38:55 +00:00
|
|
|
hiddenCollectorsLock sync.RWMutex
|
2020-04-14 07:04:33 +00:00
|
|
|
stableCollectorsLock sync.RWMutex
|
2020-12-17 12:28:29 +00:00
|
|
|
resetLock sync.RWMutex
|
|
|
|
resettables []resettable
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers a new Collector to be included in metrics
|
|
|
|
// collection. It returns an error if the descriptors provided by the
|
|
|
|
// Collector are invalid or if they — in combination with descriptors of
|
|
|
|
// already registered Collectors — do not fulfill the consistency and
|
|
|
|
// uniqueness criteria described in the documentation of metric.Desc.
|
|
|
|
func (kr *kubeRegistry) Register(c Registerable) error {
|
|
|
|
if c.Create(&kr.version) {
|
2020-12-17 12:28:29 +00:00
|
|
|
defer kr.addResettable(c)
|
2020-01-14 10:38:55 +00:00
|
|
|
return kr.PromRegistry.Register(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
kr.trackHiddenCollector(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// Registerer exposes the underlying prometheus.Registerer
|
|
|
|
func (kr *kubeRegistry) Registerer() prometheus.Registerer {
|
|
|
|
return kr.PromRegistry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gatherer exposes the underlying prometheus.Gatherer
|
|
|
|
func (kr *kubeRegistry) Gatherer() prometheus.Gatherer {
|
|
|
|
return kr.PromRegistry
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
// MustRegister works like Register but registers any number of
|
|
|
|
// Collectors and panics upon the first registration that causes an
|
|
|
|
// error.
|
|
|
|
func (kr *kubeRegistry) MustRegister(cs ...Registerable) {
|
|
|
|
metrics := make([]prometheus.Collector, 0, len(cs))
|
|
|
|
for _, c := range cs {
|
|
|
|
if c.Create(&kr.version) {
|
|
|
|
metrics = append(metrics, c)
|
2020-12-17 12:28:29 +00:00
|
|
|
kr.addResettable(c)
|
2020-01-14 10:38:55 +00:00
|
|
|
} else {
|
|
|
|
kr.trackHiddenCollector(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kr.PromRegistry.MustRegister(metrics...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CustomRegister registers a new custom collector.
|
|
|
|
func (kr *kubeRegistry) CustomRegister(c StableCollector) error {
|
2020-04-14 07:04:33 +00:00
|
|
|
kr.trackStableCollectors(c)
|
2020-12-17 12:28:29 +00:00
|
|
|
defer kr.addResettable(c)
|
2020-01-14 10:38:55 +00:00
|
|
|
if c.Create(&kr.version, c) {
|
|
|
|
return kr.PromRegistry.Register(c)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CustomMustRegister works like CustomRegister but registers any number of
|
|
|
|
// StableCollectors and panics upon the first registration that causes an
|
|
|
|
// error.
|
|
|
|
func (kr *kubeRegistry) CustomMustRegister(cs ...StableCollector) {
|
2020-04-14 07:04:33 +00:00
|
|
|
kr.trackStableCollectors(cs...)
|
2020-01-14 10:38:55 +00:00
|
|
|
collectors := make([]prometheus.Collector, 0, len(cs))
|
|
|
|
for _, c := range cs {
|
|
|
|
if c.Create(&kr.version, c) {
|
2020-12-17 12:28:29 +00:00
|
|
|
kr.addResettable(c)
|
2020-01-14 10:38:55 +00:00
|
|
|
collectors = append(collectors, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kr.PromRegistry.MustRegister(collectors...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawMustRegister takes a native prometheus.Collector and registers the collector
|
|
|
|
// to the registry. This bypasses metrics safety checks, so should only be used
|
|
|
|
// to register custom prometheus collectors.
|
|
|
|
//
|
|
|
|
// Deprecated
|
|
|
|
func (kr *kubeRegistry) RawMustRegister(cs ...prometheus.Collector) {
|
|
|
|
kr.PromRegistry.MustRegister(cs...)
|
2020-12-17 12:28:29 +00:00
|
|
|
for _, c := range cs {
|
|
|
|
kr.addResettable(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addResettable will automatically add our metric to our reset
|
|
|
|
// list if it satisfies the interface
|
|
|
|
func (kr *kubeRegistry) addResettable(i interface{}) {
|
|
|
|
kr.resetLock.Lock()
|
|
|
|
defer kr.resetLock.Unlock()
|
|
|
|
if resettable, ok := i.(resettable); ok {
|
|
|
|
kr.resettables = append(kr.resettables, resettable)
|
|
|
|
}
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unregister unregisters the Collector that equals the Collector passed
|
|
|
|
// in as an argument. (Two Collectors are considered equal if their
|
|
|
|
// Describe method yields the same set of descriptors.) The function
|
|
|
|
// returns whether a Collector was unregistered. Note that an unchecked
|
|
|
|
// Collector cannot be unregistered (as its Describe method does not
|
|
|
|
// yield any descriptor).
|
2020-04-14 07:04:33 +00:00
|
|
|
func (kr *kubeRegistry) Unregister(collector Collector) bool {
|
2020-01-14 10:38:55 +00:00
|
|
|
return kr.PromRegistry.Unregister(collector)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather calls the Collect method of the registered Collectors and then
|
|
|
|
// gathers the collected metrics into a lexicographically sorted slice
|
|
|
|
// of uniquely named MetricFamily protobufs. Gather ensures that the
|
|
|
|
// returned slice is valid and self-consistent so that it can be used
|
|
|
|
// for valid exposition. As an exception to the strict consistency
|
|
|
|
// requirements described for metric.Desc, Gather will tolerate
|
|
|
|
// different sets of label names for metrics of the same metric family.
|
|
|
|
func (kr *kubeRegistry) Gather() ([]*dto.MetricFamily, error) {
|
|
|
|
return kr.PromRegistry.Gather()
|
|
|
|
}
|
|
|
|
|
|
|
|
// trackHiddenCollector stores all hidden collectors.
|
|
|
|
func (kr *kubeRegistry) trackHiddenCollector(c Registerable) {
|
|
|
|
kr.hiddenCollectorsLock.Lock()
|
|
|
|
defer kr.hiddenCollectorsLock.Unlock()
|
|
|
|
|
2020-04-14 07:04:33 +00:00
|
|
|
kr.hiddenCollectors[c.FQName()] = c
|
2023-02-01 17:06:36 +00:00
|
|
|
hiddenMetricsTotal.Inc()
|
2020-04-14 07:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// trackStableCollectors stores all custom collectors.
|
|
|
|
func (kr *kubeRegistry) trackStableCollectors(cs ...StableCollector) {
|
|
|
|
kr.stableCollectorsLock.Lock()
|
|
|
|
defer kr.stableCollectorsLock.Unlock()
|
|
|
|
|
|
|
|
kr.stableCollectors = append(kr.stableCollectors, cs...)
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enableHiddenCollectors will re-register all of the hidden collectors.
|
|
|
|
func (kr *kubeRegistry) enableHiddenCollectors() {
|
2020-04-14 07:04:33 +00:00
|
|
|
if len(kr.hiddenCollectors) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
kr.hiddenCollectorsLock.Lock()
|
2020-04-14 07:04:33 +00:00
|
|
|
cs := make([]Registerable, 0, len(kr.hiddenCollectors))
|
2020-01-14 10:38:55 +00:00
|
|
|
|
|
|
|
for _, c := range kr.hiddenCollectors {
|
|
|
|
c.ClearState()
|
2020-04-14 07:04:33 +00:00
|
|
|
cs = append(cs, c)
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
2020-04-14 07:04:33 +00:00
|
|
|
|
2021-12-08 13:50:47 +00:00
|
|
|
kr.hiddenCollectors = make(map[string]Registerable)
|
2020-04-14 07:04:33 +00:00
|
|
|
kr.hiddenCollectorsLock.Unlock()
|
|
|
|
kr.MustRegister(cs...)
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 07:04:33 +00:00
|
|
|
// enableHiddenStableCollectors will re-register the stable collectors if there is one or more hidden metrics in it.
|
|
|
|
// Since we can not register a metrics twice, so we have to unregister first then register again.
|
|
|
|
func (kr *kubeRegistry) enableHiddenStableCollectors() {
|
|
|
|
if len(kr.stableCollectors) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
kr.stableCollectorsLock.Lock()
|
|
|
|
|
|
|
|
cs := make([]StableCollector, 0, len(kr.stableCollectors))
|
|
|
|
for _, c := range kr.stableCollectors {
|
|
|
|
if len(c.HiddenMetrics()) > 0 {
|
|
|
|
kr.Unregister(c) // unregister must happens before clear state, otherwise no metrics would be unregister
|
|
|
|
c.ClearState()
|
|
|
|
cs = append(cs, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kr.stableCollectors = nil
|
|
|
|
kr.stableCollectorsLock.Unlock()
|
|
|
|
kr.CustomMustRegister(cs...)
|
|
|
|
}
|
|
|
|
|
2020-12-17 12:28:29 +00:00
|
|
|
// Reset invokes Reset on all metrics that are resettable.
|
|
|
|
func (kr *kubeRegistry) Reset() {
|
|
|
|
kr.resetLock.RLock()
|
|
|
|
defer kr.resetLock.RUnlock()
|
|
|
|
for _, r := range kr.resettables {
|
|
|
|
r.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 07:04:33 +00:00
|
|
|
// BuildVersion is a helper function that can be easily mocked.
|
|
|
|
var BuildVersion = version.Get
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
func newKubeRegistry(v apimachineryversion.Info) *kubeRegistry {
|
|
|
|
r := &kubeRegistry{
|
2020-04-14 07:04:33 +00:00
|
|
|
PromRegistry: prometheus.NewRegistry(),
|
|
|
|
version: parseVersion(v),
|
|
|
|
hiddenCollectors: make(map[string]Registerable),
|
2020-12-17 12:28:29 +00:00
|
|
|
resettables: make([]resettable, 0),
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
registriesLock.Lock()
|
|
|
|
defer registriesLock.Unlock()
|
|
|
|
registries = append(registries, r)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// NewKubeRegistry creates a new vanilla Registry
|
2020-01-14 10:38:55 +00:00
|
|
|
func NewKubeRegistry() KubeRegistry {
|
2020-04-14 07:04:33 +00:00
|
|
|
r := newKubeRegistry(BuildVersion())
|
2020-01-14 10:38:55 +00:00
|
|
|
return r
|
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
|
|
|
|
func (r *kubeRegistry) RegisterMetaMetrics() {
|
2023-12-20 12:23:59 +00:00
|
|
|
r.MustRegister(registeredMetricsTotal)
|
2023-02-01 17:06:36 +00:00
|
|
|
r.MustRegister(disabledMetricsTotal)
|
|
|
|
r.MustRegister(hiddenMetricsTotal)
|
2023-12-20 12:23:59 +00:00
|
|
|
r.MustRegister(cardinalityEnforcementUnexpectedCategorizationsTotal)
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|