rebase: update kubernetes to v1.25.0

update kubernetes to latest v1.25.0
release.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna
2022-08-24 07:54:25 +05:30
committed by mergify[bot]
parent f47839d73d
commit e3bf375035
645 changed files with 42507 additions and 9219 deletions

View File

@ -18,6 +18,7 @@ package metrics
import (
"context"
"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
@ -106,9 +107,14 @@ type CounterVec struct {
originalLabels []string
}
// NewCounterVec returns an object which satisfies the kubeCollector and CounterVecMetric interfaces.
var _ kubeCollector = &CounterVec{}
// TODO: make this true: var _ CounterVecMetric = &CounterVec{}
// NewCounterVec returns an object which satisfies the kubeCollector and (almost) CounterVecMetric interfaces.
// However, the object returned will not measure anything unless the collector is first
// registered, since the metric is lazily instantiated.
// registered, since the metric is lazily instantiated, and only members extracted after
// registration will actually measure anything.
func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
opts.StabilityLevel.setDefaults()
@ -149,13 +155,16 @@ func (v *CounterVec) initializeDeprecatedMetric() {
v.initializeMetric()
}
// Default Prometheus behavior actually results in the creation of a new metric
// if a metric with the unique label values is not found in the underlying stored metricMap.
// Default Prometheus Vec behavior is that member extraction results in creation of a new element
// if one with the unique label values is not found in the underlying stored metricMap.
// This means that if this function is called but the underlying metric is not registered
// (which means it will never be exposed externally nor consumed), the metric will exist in memory
// for perpetuity (i.e. throughout application lifecycle).
//
// For reference: https://github.com/prometheus/client_golang/blob/v0.9.2/prometheus/counter.go#L179-L197
//
// In contrast, the Vec behavior in this package is that member extraction before registration
// returns a permanent noop object.
// WithLabelValues returns the Counter for the given slice of label
// values (same order as the VariableLabels in Desc). If that combination of

View File

@ -218,8 +218,8 @@ func (d *Desc) initializeDeprecatedDesc() {
// GetRawDesc will returns a new *Desc with original parameters provided to NewDesc().
//
// It will be useful in testing scenario that the same Desc be registered to different registry.
// 1. Desc `D` is registered to registry 'A' in TestA (Note: `D` maybe created)
// 2. Desc `D` is registered to registry 'B' in TestB (Note: since 'D' has been created once, thus will be ignored by registry 'B')
// 1. Desc `D` is registered to registry 'A' in TestA (Note: `D` maybe created)
// 2. Desc `D` is registered to registry 'B' in TestB (Note: since 'D' has been created once, thus will be ignored by registry 'B')
func (d *Desc) GetRawDesc() *Desc {
return NewDesc(d.fqName, d.help, d.variableLabels, d.constLabels, d.stabilityLevel, d.deprecatedVersion)
}

View File

@ -18,6 +18,7 @@ package metrics
import (
"context"
"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
@ -33,7 +34,11 @@ type Gauge struct {
selfCollector
}
// NewGauge returns an object which satisfies the kubeCollector and KubeGauge interfaces.
var _ GaugeMetric = &Gauge{}
var _ Registerable = &Gauge{}
var _ kubeCollector = &Gauge{}
// NewGauge returns an object which satisfies the kubeCollector, Registerable, and Gauge interfaces.
// However, the object returned will not measure anything unless the collector is first
// registered, since the metric is lazily instantiated.
func NewGauge(opts *GaugeOpts) *Gauge {
@ -88,9 +93,14 @@ type GaugeVec struct {
originalLabels []string
}
// NewGaugeVec returns an object which satisfies the kubeCollector and KubeGaugeVec interfaces.
var _ GaugeVecMetric = &GaugeVec{}
var _ Registerable = &GaugeVec{}
var _ kubeCollector = &GaugeVec{}
// NewGaugeVec returns an object which satisfies the kubeCollector, Registerable, and GaugeVecMetric interfaces.
// However, the object returned will not measure anything unless the collector is first
// registered, since the metric is lazily instantiated.
// registered, since the metric is lazily instantiated, and only members extracted after
// registration will actually measure anything.
func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec {
opts.StabilityLevel.setDefaults()
@ -130,26 +140,55 @@ func (v *GaugeVec) initializeDeprecatedMetric() {
v.initializeMetric()
}
// Default Prometheus behavior actually results in the creation of a new metric
// if a metric with the unique label values is not found in the underlying stored metricMap.
func (v *GaugeVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, error) {
if !v.IsCreated() {
if v.IsHidden() {
return noop, nil
}
return noop, errNotRegistered // return no-op gauge
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
}
elt, err := v.GaugeVec.GetMetricWithLabelValues(lvs...)
return elt, err
}
// Default Prometheus Vec behavior is that member extraction results in creation of a new element
// if one with the unique label values is not found in the underlying stored metricMap.
// This means that if this function is called but the underlying metric is not registered
// (which means it will never be exposed externally nor consumed), the metric will exist in memory
// for perpetuity (i.e. throughout application lifecycle).
//
// For reference: https://github.com/prometheus/client_golang/blob/v0.9.2/prometheus/gauge.go#L190-L208
//
// In contrast, the Vec behavior in this package is that member extraction before registration
// returns a permanent noop object.
// WithLabelValues returns the GaugeMetric for the given slice of label
// values (same order as the VariableLabels in Desc). If that combination of
// label values is accessed for the first time, a new GaugeMetric is created IFF the gaugeVec
// has been registered to a metrics registry.
func (v *GaugeVec) WithLabelValues(lvs ...string) GaugeMetric {
ans, err := v.WithLabelValuesChecked(lvs...)
if err == nil || ErrIsNotRegistered(err) {
return ans
}
panic(err)
}
func (v *GaugeVec) WithChecked(labels map[string]string) (GaugeMetric, error) {
if !v.IsCreated() {
return noop // return no-op gauge
if v.IsHidden() {
return noop, nil
}
return noop, errNotRegistered // return no-op gauge
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
v.LabelValueAllowLists.ConstrainLabelMap(labels)
}
return v.GaugeVec.WithLabelValues(lvs...)
elt, err := v.GaugeVec.GetMetricWith(labels)
return elt, err
}
// With returns the GaugeMetric for the given Labels map (the label names
@ -157,13 +196,11 @@ func (v *GaugeVec) WithLabelValues(lvs ...string) GaugeMetric {
// accessed for the first time, a new GaugeMetric is created IFF the gaugeVec has
// been registered to a metrics registry.
func (v *GaugeVec) With(labels map[string]string) GaugeMetric {
if !v.IsCreated() {
return noop // return no-op gauge
ans, err := v.WithChecked(labels)
if err == nil || ErrIsNotRegistered(err) {
return ans
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainLabelMap(labels)
}
return v.GaugeVec.With(labels)
panic(err)
}
// Delete deletes the metric where the variable labels are the same as those
@ -219,6 +256,10 @@ func (v *GaugeVec) WithContext(ctx context.Context) *GaugeVecWithContext {
}
}
func (v *GaugeVec) InterfaceWithContext(ctx context.Context) GaugeVecMetric {
return v.WithContext(ctx)
}
// GaugeVecWithContext is the wrapper of GaugeVec with context.
type GaugeVecWithContext struct {
*GaugeVec

View File

@ -18,6 +18,7 @@ package metrics
import (
"context"
"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
)
@ -100,7 +101,10 @@ type HistogramVec struct {
// NewHistogramVec returns an object which satisfies kubeCollector and wraps the
// prometheus.HistogramVec object. However, the object returned will not measure
// anything unless the collector is first registered, since the metric is lazily instantiated.
// anything unless the collector is first registered, since the metric is lazily instantiated,
// and only members extracted after
// registration will actually measure anything.
func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec {
opts.StabilityLevel.setDefaults()
@ -136,13 +140,16 @@ func (v *HistogramVec) initializeDeprecatedMetric() {
v.initializeMetric()
}
// Default Prometheus behavior actually results in the creation of a new metric
// if a metric with the unique label values is not found in the underlying stored metricMap.
// Default Prometheus Vec behavior is that member extraction results in creation of a new element
// if one with the unique label values is not found in the underlying stored metricMap.
// This means that if this function is called but the underlying metric is not registered
// (which means it will never be exposed externally nor consumed), the metric will exist in memory
// for perpetuity (i.e. throughout application lifecycle).
//
// For reference: https://github.com/prometheus/client_golang/blob/v0.9.2/prometheus/histogram.go#L460-L470
//
// In contrast, the Vec behavior in this package is that member extraction before registration
// returns a permanent noop object.
// WithLabelValues returns the ObserverMetric for the given slice of label
// values (same order as the VariableLabels in Desc). If that combination of

View File

@ -22,6 +22,7 @@ import (
"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
promext "k8s.io/component-base/metrics/prometheusextension"
"k8s.io/klog/v2"
)
@ -90,13 +91,14 @@ func (r *lazyMetric) lazyInit(self kubeCollector, fqName string) {
// 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:
// 1. if the metric is deprecated and is outside the grace period (i.e. has been
// deprecated for more than one release
// 2. if the metric is manually disabled via a CLI flag.
// 1. if the metric is deprecated and is outside the grace period (i.e. has been
// deprecated for more than one release
// 2. if the metric is manually disabled via a CLI flag.
//
// Disclaimer: disabling a metric via a CLI flag has higher precedence than
// deprecation and will override show-hidden-metrics for the explicitly
// disabled metric.
//
// deprecation and will override show-hidden-metrics for the explicitly
// disabled metric.
func (r *lazyMetric) preprocessMetric(version semver.Version) {
disabledMetricsLock.RLock()
defer disabledMetricsLock.RUnlock()
@ -203,6 +205,7 @@ func (c *selfCollector) Collect(ch chan<- prometheus.Metric) {
// no-op vecs for convenience
var noopCounterVec = &prometheus.CounterVec{}
var noopHistogramVec = &prometheus.HistogramVec{}
var noopTimingHistogramVec = &promext.TimingHistogramVec{}
var noopGaugeVec = &prometheus.GaugeVec{}
var noopObserverVec = &noopObserverVector{}
@ -211,17 +214,18 @@ var noop = &noopMetric{}
type noopMetric struct{}
func (noopMetric) Inc() {}
func (noopMetric) Add(float64) {}
func (noopMetric) Dec() {}
func (noopMetric) Set(float64) {}
func (noopMetric) Sub(float64) {}
func (noopMetric) Observe(float64) {}
func (noopMetric) SetToCurrentTime() {}
func (noopMetric) Desc() *prometheus.Desc { return nil }
func (noopMetric) Write(*dto.Metric) error { return nil }
func (noopMetric) Describe(chan<- *prometheus.Desc) {}
func (noopMetric) Collect(chan<- prometheus.Metric) {}
func (noopMetric) Inc() {}
func (noopMetric) Add(float64) {}
func (noopMetric) Dec() {}
func (noopMetric) Set(float64) {}
func (noopMetric) Sub(float64) {}
func (noopMetric) Observe(float64) {}
func (noopMetric) ObserveWithWeight(float64, uint64) {}
func (noopMetric) SetToCurrentTime() {}
func (noopMetric) Desc() *prometheus.Desc { return nil }
func (noopMetric) Write(*dto.Metric) error { return nil }
func (noopMetric) Describe(chan<- *prometheus.Desc) {}
func (noopMetric) Collect(chan<- prometheus.Metric) {}
type noopObserverVector struct{}

View File

@ -24,6 +24,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/util/sets"
promext "k8s.io/component-base/metrics/prometheusextension"
)
var (
@ -189,6 +190,54 @@ func (o *HistogramOpts) toPromHistogramOpts() prometheus.HistogramOpts {
}
}
// TimingHistogramOpts bundles the options for creating a TimingHistogram metric. It is
// mandatory to set Name to a non-empty string. All other fields are optional
// and can safely be left at their zero value, although it is strongly
// encouraged to set a Help string.
type TimingHistogramOpts struct {
Namespace string
Subsystem string
Name string
Help string
ConstLabels map[string]string
Buckets []float64
InitialValue float64
DeprecatedVersion string
deprecateOnce sync.Once
annotateOnce sync.Once
StabilityLevel StabilityLevel
LabelValueAllowLists *MetricLabelAllowList
}
// Modify help description on the metric description.
func (o *TimingHistogramOpts) markDeprecated() {
o.deprecateOnce.Do(func() {
o.Help = fmt.Sprintf("(Deprecated since %v) %v", o.DeprecatedVersion, o.Help)
})
}
// annotateStabilityLevel annotates help description on the metric description with the stability level
// of the metric
func (o *TimingHistogramOpts) annotateStabilityLevel() {
o.annotateOnce.Do(func() {
o.Help = fmt.Sprintf("[%v] %v", o.StabilityLevel, o.Help)
})
}
// convenience function to allow easy transformation to the prometheus
// counterpart. This will do more once we have a proper label abstraction
func (o *TimingHistogramOpts) toPromHistogramOpts() promext.TimingHistogramOpts {
return promext.TimingHistogramOpts{
Namespace: o.Namespace,
Subsystem: o.Subsystem,
Name: o.Name,
Help: o.Help,
ConstLabels: o.ConstLabels,
Buckets: o.Buckets,
InitialValue: o.InitialValue,
}
}
// SummaryOpts bundles the options for creating a Summary metric. It is
// mandatory to set Name to a non-empty string. While all other fields are
// optional and can safely be left at their zero value, it is recommended to set

View File

@ -0,0 +1,189 @@
/*
Copyright 2022 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 prometheusextension
import (
"errors"
"time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
// GaugeOps is the part of `prometheus.Gauge` that is relevant to
// instrumented code.
// This factoring should be in prometheus, analogous to the way
// it already factors out the Observer interface for histograms and summaries.
type GaugeOps interface {
// Set is the same as Gauge.Set
Set(float64)
// Inc is the same as Gauge.inc
Inc()
// Dec is the same as Gauge.Dec
Dec()
// Add is the same as Gauge.Add
Add(float64)
// Sub is the same as Gauge.Sub
Sub(float64)
// SetToCurrentTime the same as Gauge.SetToCurrentTime
SetToCurrentTime()
}
// A TimingHistogram tracks how long a `float64` variable spends in
// ranges defined by buckets. Time is counted in nanoseconds. The
// histogram's sum is the integral over time (in nanoseconds, from
// creation of the histogram) of the variable's value.
type TimingHistogram interface {
prometheus.Metric
prometheus.Collector
GaugeOps
}
// TimingHistogramOpts is the parameters of the TimingHistogram constructor
type TimingHistogramOpts struct {
Namespace string
Subsystem string
Name string
Help string
ConstLabels prometheus.Labels
// Buckets defines the buckets into which observations are
// accumulated. Each element in the slice is the upper
// inclusive bound of a bucket. The values must be sorted in
// strictly increasing order. There is no need to add a
// highest bucket with +Inf bound. The default value is
// prometheus.DefBuckets.
Buckets []float64
// The initial value of the variable.
InitialValue float64
}
// NewTimingHistogram creates a new TimingHistogram
func NewTimingHistogram(opts TimingHistogramOpts) (TimingHistogram, error) {
return NewTestableTimingHistogram(time.Now, opts)
}
// NewTestableTimingHistogram creates a TimingHistogram that uses a mockable clock
func NewTestableTimingHistogram(nowFunc func() time.Time, opts TimingHistogramOpts) (TimingHistogram, error) {
desc := prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
wrapTimingHelp(opts.Help),
nil,
opts.ConstLabels,
)
return newTimingHistogram(nowFunc, desc, opts)
}
func wrapTimingHelp(given string) string {
return "EXPERIMENTAL: " + given
}
func newTimingHistogram(nowFunc func() time.Time, desc *prometheus.Desc, opts TimingHistogramOpts, variableLabelValues ...string) (TimingHistogram, error) {
allLabelsM := prometheus.Labels{}
allLabelsS := prometheus.MakeLabelPairs(desc, variableLabelValues)
for _, pair := range allLabelsS {
if pair == nil || pair.Name == nil || pair.Value == nil {
return nil, errors.New("prometheus.MakeLabelPairs returned a nil")
}
allLabelsM[*pair.Name] = *pair.Value
}
weighted, err := newWeightedHistogram(desc, WeightedHistogramOpts{
Namespace: opts.Namespace,
Subsystem: opts.Subsystem,
Name: opts.Name,
Help: opts.Help,
ConstLabels: allLabelsM,
Buckets: opts.Buckets,
}, variableLabelValues...)
if err != nil {
return nil, err
}
return &timingHistogram{
nowFunc: nowFunc,
weighted: weighted,
lastSetTime: nowFunc(),
value: opts.InitialValue,
}, nil
}
type timingHistogram struct {
nowFunc func() time.Time
weighted *weightedHistogram
// The following fields must only be accessed with weighted's lock held
lastSetTime time.Time // identifies when value was last set
value float64
}
var _ TimingHistogram = &timingHistogram{}
func (th *timingHistogram) Set(newValue float64) {
th.update(func(float64) float64 { return newValue })
}
func (th *timingHistogram) Inc() {
th.update(func(oldValue float64) float64 { return oldValue + 1 })
}
func (th *timingHistogram) Dec() {
th.update(func(oldValue float64) float64 { return oldValue - 1 })
}
func (th *timingHistogram) Add(delta float64) {
th.update(func(oldValue float64) float64 { return oldValue + delta })
}
func (th *timingHistogram) Sub(delta float64) {
th.update(func(oldValue float64) float64 { return oldValue - delta })
}
func (th *timingHistogram) SetToCurrentTime() {
th.update(func(oldValue float64) float64 { return th.nowFunc().Sub(time.Unix(0, 0)).Seconds() })
}
func (th *timingHistogram) update(updateFn func(float64) float64) {
th.weighted.lock.Lock()
defer th.weighted.lock.Unlock()
now := th.nowFunc()
delta := now.Sub(th.lastSetTime)
value := th.value
if delta > 0 {
th.weighted.observeWithWeightLocked(value, uint64(delta))
th.lastSetTime = now
}
th.value = updateFn(value)
}
func (th *timingHistogram) Desc() *prometheus.Desc {
return th.weighted.Desc()
}
func (th *timingHistogram) Write(dest *dto.Metric) error {
th.Add(0) // account for time since last update
return th.weighted.Write(dest)
}
func (th *timingHistogram) Describe(ch chan<- *prometheus.Desc) {
ch <- th.weighted.Desc()
}
func (th *timingHistogram) Collect(ch chan<- prometheus.Metric) {
ch <- th
}

View File

@ -0,0 +1,111 @@
/*
Copyright 2022 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 prometheusextension
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
// GaugeVecOps is a bunch of Gauge that have the same
// Desc and are distinguished by the values for their variable labels.
type GaugeVecOps interface {
GetMetricWith(prometheus.Labels) (GaugeOps, error)
GetMetricWithLabelValues(lvs ...string) (GaugeOps, error)
With(prometheus.Labels) GaugeOps
WithLabelValues(...string) GaugeOps
CurryWith(prometheus.Labels) (GaugeVecOps, error)
MustCurryWith(prometheus.Labels) GaugeVecOps
}
type TimingHistogramVec struct {
*prometheus.MetricVec
}
var _ GaugeVecOps = &TimingHistogramVec{}
var _ prometheus.Collector = &TimingHistogramVec{}
func NewTimingHistogramVec(opts TimingHistogramOpts, labelNames ...string) *TimingHistogramVec {
return NewTestableTimingHistogramVec(time.Now, opts, labelNames...)
}
func NewTestableTimingHistogramVec(nowFunc func() time.Time, opts TimingHistogramOpts, labelNames ...string) *TimingHistogramVec {
desc := prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
wrapTimingHelp(opts.Help),
labelNames,
opts.ConstLabels,
)
return &TimingHistogramVec{
MetricVec: prometheus.NewMetricVec(desc, func(lvs ...string) prometheus.Metric {
metric, err := newTimingHistogram(nowFunc, desc, opts, lvs...)
if err != nil {
panic(err) // like in prometheus.newHistogram
}
return metric
}),
}
}
func (hv *TimingHistogramVec) GetMetricWith(labels prometheus.Labels) (GaugeOps, error) {
metric, err := hv.MetricVec.GetMetricWith(labels)
if metric != nil {
return metric.(GaugeOps), err
}
return nil, err
}
func (hv *TimingHistogramVec) GetMetricWithLabelValues(lvs ...string) (GaugeOps, error) {
metric, err := hv.MetricVec.GetMetricWithLabelValues(lvs...)
if metric != nil {
return metric.(GaugeOps), err
}
return nil, err
}
func (hv *TimingHistogramVec) With(labels prometheus.Labels) GaugeOps {
h, err := hv.GetMetricWith(labels)
if err != nil {
panic(err)
}
return h
}
func (hv *TimingHistogramVec) WithLabelValues(lvs ...string) GaugeOps {
h, err := hv.GetMetricWithLabelValues(lvs...)
if err != nil {
panic(err)
}
return h
}
func (hv *TimingHistogramVec) CurryWith(labels prometheus.Labels) (GaugeVecOps, error) {
vec, err := hv.MetricVec.CurryWith(labels)
if vec != nil {
return &TimingHistogramVec{MetricVec: vec}, err
}
return nil, err
}
func (hv *TimingHistogramVec) MustCurryWith(labels prometheus.Labels) GaugeVecOps {
vec, err := hv.CurryWith(labels)
if err != nil {
panic(err)
}
return vec
}

View File

@ -0,0 +1,203 @@
/*
Copyright 2022 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 prometheusextension
import (
"fmt"
"math"
"sort"
"sync"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
// WeightedHistogram generalizes Histogram: each observation has
// an associated _weight_. For a given `x` and `N`,
// `1` call on `ObserveWithWeight(x, N)` has the same meaning as
// `N` calls on `ObserveWithWeight(x, 1)`.
// The weighted sum might differ slightly due to the use of
// floating point, although the implementation takes some steps
// to mitigate that.
// If every weight were 1,
// this would be the same as the existing Histogram abstraction.
type WeightedHistogram interface {
prometheus.Metric
prometheus.Collector
WeightedObserver
}
// WeightedObserver generalizes the Observer interface.
type WeightedObserver interface {
// Set the variable to the given value with the given weight.
ObserveWithWeight(value float64, weight uint64)
}
// WeightedHistogramOpts is the same as for an ordinary Histogram
type WeightedHistogramOpts = prometheus.HistogramOpts
// NewWeightedHistogram creates a new WeightedHistogram
func NewWeightedHistogram(opts WeightedHistogramOpts) (WeightedHistogram, error) {
desc := prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
wrapWeightedHelp(opts.Help),
nil,
opts.ConstLabels,
)
return newWeightedHistogram(desc, opts)
}
func wrapWeightedHelp(given string) string {
return "EXPERIMENTAL: " + given
}
func newWeightedHistogram(desc *prometheus.Desc, opts WeightedHistogramOpts, variableLabelValues ...string) (*weightedHistogram, error) {
if len(opts.Buckets) == 0 {
opts.Buckets = prometheus.DefBuckets
}
for i, upperBound := range opts.Buckets {
if i < len(opts.Buckets)-1 {
if upperBound >= opts.Buckets[i+1] {
return nil, fmt.Errorf(
"histogram buckets must be in increasing order: %f >= %f",
upperBound, opts.Buckets[i+1],
)
}
} else {
if math.IsInf(upperBound, +1) {
// The +Inf bucket is implicit. Remove it here.
opts.Buckets = opts.Buckets[:i]
}
}
}
upperBounds := make([]float64, len(opts.Buckets))
copy(upperBounds, opts.Buckets)
return &weightedHistogram{
desc: desc,
variableLabelValues: variableLabelValues,
upperBounds: upperBounds,
buckets: make([]uint64, len(upperBounds)+1),
hotCount: initialHotCount,
}, nil
}
type weightedHistogram struct {
desc *prometheus.Desc
variableLabelValues []string
upperBounds []float64 // exclusive of +Inf
lock sync.Mutex // applies to all the following
// buckets is longer by one than upperBounds.
// For 0 <= idx < len(upperBounds), buckets[idx] holds the
// accumulated time.Duration that value has been <=
// upperBounds[idx] but not <= upperBounds[idx-1].
// buckets[len(upperBounds)] holds the accumulated
// time.Duration when value fit in no other bucket.
buckets []uint64
// sumHot + sumCold is the weighted sum of value.
// Rather than risk loss of precision in one
// float64, we do this sum hierarchically. Many successive
// increments are added into sumHot; once in a while
// the magnitude of sumHot is compared to the magnitude
// of sumCold and, if the ratio is high enough,
// sumHot is transferred into sumCold.
sumHot float64
sumCold float64
transferThreshold float64 // = math.Abs(sumCold) / 2^26 (that's about half of the bits of precision in a float64)
// hotCount is used to decide when to consider dumping sumHot into sumCold.
// hotCount counts upward from initialHotCount to zero.
hotCount int
}
// initialHotCount is the negative of the number of terms
// that are summed into sumHot before considering whether
// to transfer to sumCold. This only has to be big enough
// to make the extra floating point operations occur in a
// distinct minority of cases.
const initialHotCount = -15
var _ WeightedHistogram = &weightedHistogram{}
var _ prometheus.Metric = &weightedHistogram{}
var _ prometheus.Collector = &weightedHistogram{}
func (sh *weightedHistogram) ObserveWithWeight(value float64, weight uint64) {
idx := sort.SearchFloat64s(sh.upperBounds, value)
sh.lock.Lock()
defer sh.lock.Unlock()
sh.updateLocked(idx, value, weight)
}
func (sh *weightedHistogram) observeWithWeightLocked(value float64, weight uint64) {
idx := sort.SearchFloat64s(sh.upperBounds, value)
sh.updateLocked(idx, value, weight)
}
func (sh *weightedHistogram) updateLocked(idx int, value float64, weight uint64) {
sh.buckets[idx] += weight
newSumHot := sh.sumHot + float64(weight)*value
sh.hotCount++
if sh.hotCount >= 0 {
sh.hotCount = initialHotCount
if math.Abs(newSumHot) > sh.transferThreshold {
newSumCold := sh.sumCold + newSumHot
sh.sumCold = newSumCold
sh.transferThreshold = math.Abs(newSumCold / 67108864)
sh.sumHot = 0
return
}
}
sh.sumHot = newSumHot
}
func (sh *weightedHistogram) Desc() *prometheus.Desc {
return sh.desc
}
func (sh *weightedHistogram) Write(dest *dto.Metric) error {
count, sum, buckets := func() (uint64, float64, map[float64]uint64) {
sh.lock.Lock()
defer sh.lock.Unlock()
nBounds := len(sh.upperBounds)
buckets := make(map[float64]uint64, nBounds)
var count uint64
for idx, upperBound := range sh.upperBounds {
count += sh.buckets[idx]
buckets[upperBound] = count
}
count += sh.buckets[nBounds]
return count, sh.sumHot + sh.sumCold, buckets
}()
metric, err := prometheus.NewConstHistogram(sh.desc, count, sum, buckets, sh.variableLabelValues...)
if err != nil {
return err
}
return metric.Write(dest)
}
func (sh *weightedHistogram) Describe(ch chan<- *prometheus.Desc) {
ch <- sh.desc
}
func (sh *weightedHistogram) Collect(ch chan<- prometheus.Metric) {
ch <- sh
}

View File

@ -0,0 +1,106 @@
/*
Copyright 2022 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 prometheusextension
import (
"github.com/prometheus/client_golang/prometheus"
)
// WeightedObserverVec is a bunch of WeightedObservers that have the same
// Desc and are distinguished by the values for their variable labels.
type WeightedObserverVec interface {
GetMetricWith(prometheus.Labels) (WeightedObserver, error)
GetMetricWithLabelValues(lvs ...string) (WeightedObserver, error)
With(prometheus.Labels) WeightedObserver
WithLabelValues(...string) WeightedObserver
CurryWith(prometheus.Labels) (WeightedObserverVec, error)
MustCurryWith(prometheus.Labels) WeightedObserverVec
}
// WeightedHistogramVec implements WeightedObserverVec
type WeightedHistogramVec struct {
*prometheus.MetricVec
}
var _ WeightedObserverVec = &WeightedHistogramVec{}
var _ prometheus.Collector = &WeightedHistogramVec{}
func NewWeightedHistogramVec(opts WeightedHistogramOpts, labelNames ...string) *WeightedHistogramVec {
desc := prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
wrapWeightedHelp(opts.Help),
labelNames,
opts.ConstLabels,
)
return &WeightedHistogramVec{
MetricVec: prometheus.NewMetricVec(desc, func(lvs ...string) prometheus.Metric {
metric, err := newWeightedHistogram(desc, opts, lvs...)
if err != nil {
panic(err) // like in prometheus.newHistogram
}
return metric
}),
}
}
func (hv *WeightedHistogramVec) GetMetricWith(labels prometheus.Labels) (WeightedObserver, error) {
metric, err := hv.MetricVec.GetMetricWith(labels)
if metric != nil {
return metric.(WeightedObserver), err
}
return nil, err
}
func (hv *WeightedHistogramVec) GetMetricWithLabelValues(lvs ...string) (WeightedObserver, error) {
metric, err := hv.MetricVec.GetMetricWithLabelValues(lvs...)
if metric != nil {
return metric.(WeightedObserver), err
}
return nil, err
}
func (hv *WeightedHistogramVec) With(labels prometheus.Labels) WeightedObserver {
h, err := hv.GetMetricWith(labels)
if err != nil {
panic(err)
}
return h
}
func (hv *WeightedHistogramVec) WithLabelValues(lvs ...string) WeightedObserver {
h, err := hv.GetMetricWithLabelValues(lvs...)
if err != nil {
panic(err)
}
return h
}
func (hv *WeightedHistogramVec) CurryWith(labels prometheus.Labels) (WeightedObserverVec, error) {
vec, err := hv.MetricVec.CurryWith(labels)
if vec != nil {
return &WeightedHistogramVec{MetricVec: vec}, err
}
return nil, err
}
func (hv *WeightedHistogramVec) MustCurryWith(labels prometheus.Labels) WeightedObserverVec {
vec, err := hv.CurryWith(labels)
if err != nil {
panic(err)
}
return vec
}

View File

@ -18,10 +18,17 @@ package metrics
import (
"context"
"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
)
const (
DefAgeBuckets = prometheus.DefAgeBuckets
DefBufCap = prometheus.DefBufCap
DefMaxAge = prometheus.DefMaxAge
)
// Summary is our internal representation for our wrapping struct around prometheus
// summaries. Summary implements both kubeCollector and ObserverMetric
//
@ -93,7 +100,9 @@ type SummaryVec struct {
// NewSummaryVec returns an object which satisfies kubeCollector and wraps the
// prometheus.SummaryVec object. However, the object returned will not measure
// anything unless the collector is first registered, since the metric is lazily instantiated.
// anything unless the collector is first registered, since the metric is lazily instantiated,
// and only members extracted after
// registration will actually measure anything.
//
// DEPRECATED: as per the metrics overhaul KEP
func NewSummaryVec(opts *SummaryOpts, labels []string) *SummaryVec {
@ -130,13 +139,16 @@ func (v *SummaryVec) initializeDeprecatedMetric() {
v.initializeMetric()
}
// Default Prometheus behavior actually results in the creation of a new metric
// if a metric with the unique label values is not found in the underlying stored metricMap.
// Default Prometheus Vec behavior is that member extraction results in creation of a new element
// if one with the unique label values is not found in the underlying stored metricMap.
// This means that if this function is called but the underlying metric is not registered
// (which means it will never be exposed externally nor consumed), the metric will exist in memory
// for perpetuity (i.e. throughout application lifecycle).
//
// For reference: https://github.com/prometheus/client_golang/blob/v0.9.2/prometheus/summary.go#L485-L495
// For reference: https://github.com/prometheus/client_golang/blob/v0.9.2/prometheus/histogram.go#L460-L470
//
// In contrast, the Vec behavior in this package is that member extraction before registration
// returns a permanent noop object.
// WithLabelValues returns the ObserverMetric for the given slice of label
// values (same order as the VariableLabels in Desc). If that combination of

View File

@ -0,0 +1,267 @@
/*
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 (
"context"
"time"
"github.com/blang/semver/v4"
promext "k8s.io/component-base/metrics/prometheusextension"
)
// PrometheusTimingHistogram is the abstraction of the underlying histogram
// that we want to promote from the wrapper.
type PrometheusTimingHistogram interface {
GaugeMetric
}
// TimingHistogram is our internal representation for our wrapping struct around
// timing histograms. It implements both kubeCollector and GaugeMetric
type TimingHistogram struct {
PrometheusTimingHistogram
*TimingHistogramOpts
nowFunc func() time.Time
lazyMetric
selfCollector
}
var _ GaugeMetric = &TimingHistogram{}
var _ Registerable = &TimingHistogram{}
var _ kubeCollector = &TimingHistogram{}
// NewTimingHistogram returns an object which is TimingHistogram-like. However, nothing
// will be measured until the histogram is registered somewhere.
func NewTimingHistogram(opts *TimingHistogramOpts) *TimingHistogram {
return NewTestableTimingHistogram(time.Now, opts)
}
// NewTestableTimingHistogram adds injection of the clock
func NewTestableTimingHistogram(nowFunc func() time.Time, opts *TimingHistogramOpts) *TimingHistogram {
opts.StabilityLevel.setDefaults()
h := &TimingHistogram{
TimingHistogramOpts: opts,
nowFunc: nowFunc,
lazyMetric: lazyMetric{},
}
h.setPrometheusHistogram(noopMetric{})
h.lazyInit(h, BuildFQName(opts.Namespace, opts.Subsystem, opts.Name))
return h
}
// setPrometheusHistogram sets the underlying KubeGauge object, i.e. the thing that does the measurement.
func (h *TimingHistogram) setPrometheusHistogram(histogram promext.TimingHistogram) {
h.PrometheusTimingHistogram = histogram
h.initSelfCollection(histogram)
}
// DeprecatedVersion returns a pointer to the Version or nil
func (h *TimingHistogram) DeprecatedVersion() *semver.Version {
return parseSemver(h.TimingHistogramOpts.DeprecatedVersion)
}
// initializeMetric invokes the actual prometheus.Histogram object instantiation
// and stores a reference to it
func (h *TimingHistogram) initializeMetric() {
h.TimingHistogramOpts.annotateStabilityLevel()
// this actually creates the underlying prometheus gauge.
histogram, err := promext.NewTestableTimingHistogram(h.nowFunc, h.TimingHistogramOpts.toPromHistogramOpts())
if err != nil {
panic(err) // handle as for regular histograms
}
h.setPrometheusHistogram(histogram)
}
// initializeDeprecatedMetric invokes the actual prometheus.Histogram object instantiation
// but modifies the Help description prior to object instantiation.
func (h *TimingHistogram) initializeDeprecatedMetric() {
h.TimingHistogramOpts.markDeprecated()
h.initializeMetric()
}
// WithContext allows the normal TimingHistogram metric to pass in context. The context is no-op now.
func (h *TimingHistogram) WithContext(ctx context.Context) GaugeMetric {
return h.PrometheusTimingHistogram
}
// TimingHistogramVec is the internal representation of our wrapping struct around prometheus
// TimingHistogramVecs.
type TimingHistogramVec struct {
*promext.TimingHistogramVec
*TimingHistogramOpts
nowFunc func() time.Time
lazyMetric
originalLabels []string
}
var _ GaugeVecMetric = &TimingHistogramVec{}
var _ Registerable = &TimingHistogramVec{}
var _ kubeCollector = &TimingHistogramVec{}
// NewTimingHistogramVec returns an object which satisfies the kubeCollector, Registerable, and GaugeVecMetric interfaces
// and wraps an underlying promext.TimingHistogramVec object. Note well the way that
// behavior depends on registration and whether this is hidden.
func NewTimingHistogramVec(opts *TimingHistogramOpts, labels []string) *TimingHistogramVec {
return NewTestableTimingHistogramVec(time.Now, opts, labels)
}
// NewTestableTimingHistogramVec adds injection of the clock.
func NewTestableTimingHistogramVec(nowFunc func() time.Time, opts *TimingHistogramOpts, labels []string) *TimingHistogramVec {
opts.StabilityLevel.setDefaults()
fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[fqName]; ok {
opts.LabelValueAllowLists = allowList
}
allowListLock.RUnlock()
v := &TimingHistogramVec{
TimingHistogramVec: noopTimingHistogramVec,
TimingHistogramOpts: opts,
nowFunc: nowFunc,
originalLabels: labels,
lazyMetric: lazyMetric{},
}
v.lazyInit(v, fqName)
return v
}
// DeprecatedVersion returns a pointer to the Version or nil
func (v *TimingHistogramVec) DeprecatedVersion() *semver.Version {
return parseSemver(v.TimingHistogramOpts.DeprecatedVersion)
}
func (v *TimingHistogramVec) initializeMetric() {
v.TimingHistogramOpts.annotateStabilityLevel()
v.TimingHistogramVec = promext.NewTestableTimingHistogramVec(v.nowFunc, v.TimingHistogramOpts.toPromHistogramOpts(), v.originalLabels...)
}
func (v *TimingHistogramVec) initializeDeprecatedMetric() {
v.TimingHistogramOpts.markDeprecated()
v.initializeMetric()
}
// WithLabelValuesChecked, if called before this vector has been registered in
// at least one registry, will return a noop gauge and
// an error that passes ErrIsNotRegistered.
// If called on a hidden vector,
// will return a noop gauge and a nil error.
// If called with a syntactic problem in the labels, will
// return a noop gauge and an error about the labels.
// If none of the above apply, this method will return
// the appropriate vector member and a nil error.
func (v *TimingHistogramVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, error) {
if !v.IsCreated() {
if v.IsHidden() {
return noop, nil
}
return noop, errNotRegistered
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
}
ops, err := v.TimingHistogramVec.GetMetricWithLabelValues(lvs...)
return ops.(GaugeMetric), err
}
// WithLabelValues calls WithLabelValuesChecked
// and handles errors as follows.
// An error that passes ErrIsNotRegistered is ignored
// and the noop gauge is returned;
// all other errors cause a panic.
func (v *TimingHistogramVec) WithLabelValues(lvs ...string) GaugeMetric {
ans, err := v.WithLabelValuesChecked(lvs...)
if err == nil || ErrIsNotRegistered(err) {
return ans
}
panic(err)
}
// WithChecked, if called before this vector has been registered in
// at least one registry, will return a noop gauge and
// an error that passes ErrIsNotRegistered.
// If called on a hidden vector,
// will return a noop gauge and a nil error.
// If called with a syntactic problem in the labels, will
// return a noop gauge and an error about the labels.
// If none of the above apply, this method will return
// the appropriate vector member and a nil error.
func (v *TimingHistogramVec) WithChecked(labels map[string]string) (GaugeMetric, error) {
if !v.IsCreated() {
if v.IsHidden() {
return noop, nil
}
return noop, errNotRegistered
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainLabelMap(labels)
}
ops, err := v.TimingHistogramVec.GetMetricWith(labels)
return ops.(GaugeMetric), err
}
// With calls WithChecked and handles errors as follows.
// An error that passes ErrIsNotRegistered is ignored
// and the noop gauge is returned;
// all other errors cause a panic.
func (v *TimingHistogramVec) With(labels map[string]string) GaugeMetric {
ans, err := v.WithChecked(labels)
if err == nil || ErrIsNotRegistered(err) {
return ans
}
panic(err)
}
// Delete deletes the metric where the variable labels are the same as those
// passed in as labels. It returns true if a metric was deleted.
//
// It is not an error if the number and names of the Labels are inconsistent
// with those of the VariableLabels in Desc. However, such inconsistent Labels
// can never match an actual metric, so the method will always return false in
// that case.
func (v *TimingHistogramVec) Delete(labels map[string]string) bool {
if !v.IsCreated() {
return false // since we haven't created the metric, we haven't deleted a metric with the passed in values
}
return v.TimingHistogramVec.Delete(labels)
}
// Reset deletes all metrics in this vector.
func (v *TimingHistogramVec) Reset() {
if !v.IsCreated() {
return
}
v.TimingHistogramVec.Reset()
}
// WithContext returns wrapped TimingHistogramVec with context
func (v *TimingHistogramVec) InterfaceWithContext(ctx context.Context) GaugeVecMetric {
return &TimingHistogramVecWithContext{
ctx: ctx,
TimingHistogramVec: v,
}
}
// TimingHistogramVecWithContext is the wrapper of TimingHistogramVec with context.
// Currently the context is ignored.
type TimingHistogramVecWithContext struct {
*TimingHistogramVec
ctx context.Context
}

View File

@ -50,7 +50,8 @@ func NewLazyConstMetric(desc *Desc, valueType ValueType, value float64, labelVal
// NewLazyMetricWithTimestamp is a helper of NewMetricWithTimestamp.
//
// Warning: the Metric 'm' must be the one created by NewLazyConstMetric(),
// otherwise, no stability guarantees would be offered.
//
// otherwise, no stability guarantees would be offered.
func NewLazyMetricWithTimestamp(t time.Time, m Metric) Metric {
if m == nil {
return nil

View File

@ -17,6 +17,8 @@ limitations under the License.
package metrics
import (
"errors"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
@ -65,6 +67,64 @@ type GaugeMetric interface {
SetToCurrentTime()
}
// GaugeVecMetric is a collection of Gauges that differ only in label values.
type GaugeVecMetric interface {
// Default Prometheus Vec behavior is that member extraction results in creation of a new element
// if one with the unique label values is not found in the underlying stored metricMap.
// This means that if this function is called but the underlying metric is not registered
// (which means it will never be exposed externally nor consumed), the metric would exist in memory
// for perpetuity (i.e. throughout application lifecycle).
//
// For reference: https://github.com/prometheus/client_golang/blob/v0.9.2/prometheus/gauge.go#L190-L208
//
// In contrast, the Vec behavior in this package is that member extraction before registration
// returns a permanent noop object.
// WithLabelValuesChecked, if called before this vector has been registered in
// at least one registry, will return a noop gauge and
// an error that passes ErrIsNotRegistered.
// If called on a hidden vector,
// will return a noop gauge and a nil error.
// If called with a syntactic problem in the labels, will
// return a noop gauge and an error about the labels.
// If none of the above apply, this method will return
// the appropriate vector member and a nil error.
WithLabelValuesChecked(labelValues ...string) (GaugeMetric, error)
// WithLabelValues calls WithLabelValuesChecked
// and handles errors as follows.
// An error that passes ErrIsNotRegistered is ignored
// and the noop gauge is returned;
// all other errors cause a panic.
WithLabelValues(labelValues ...string) GaugeMetric
// WithChecked, if called before this vector has been registered in
// at least one registry, will return a noop gauge and
// an error that passes ErrIsNotRegistered.
// If called on a hidden vector,
// will return a noop gauge and a nil error.
// If called with a syntactic problem in the labels, will
// return a noop gauge and an error about the labels.
// If none of the above apply, this method will return
// the appropriate vector member and a nil error.
WithChecked(labels map[string]string) (GaugeMetric, error)
// With calls WithChecked and handles errors as follows.
// An error that passes ErrIsNotRegistered is ignored
// and the noop gauge is returned;
// all other errors cause a panic.
With(labels map[string]string) GaugeMetric
// Delete asserts that the vec should have no member for the given label set.
// The returned bool indicates whether there was a change.
// The return will certainly be `false` if the given label set has the wrong
// set of label names.
Delete(map[string]string) bool
// Reset removes all the members
Reset()
}
// ObserverMetric captures individual observations.
type ObserverMetric interface {
Observe(float64)
@ -93,3 +153,9 @@ type GaugeFunc interface {
Metric
Collector
}
func ErrIsNotRegistered(err error) bool {
return err == errNotRegistered
}
var errNotRegistered = errors.New("metric vec is not registered yet")