Update dependencies
Change Kubernetes dependency to v1.17.0 Change csi-lib-utils to v0.7.0
This commit is contained in:
20
vendor/k8s.io/client-go/tools/cache/mutation_detector.go
generated
vendored
20
vendor/k8s.io/client-go/tools/cache/mutation_detector.go
generated
vendored
@@ -48,7 +48,7 @@ func NewCacheMutationDetector(name string) MutationDetector {
|
||||
return dummyMutationDetector{}
|
||||
}
|
||||
klog.Warningln("Mutation detector is enabled, this will result in memory leakage.")
|
||||
return &defaultCacheMutationDetector{name: name, period: 1 * time.Second}
|
||||
return &defaultCacheMutationDetector{name: name, period: 1 * time.Second, retainDuration: 2 * time.Minute}
|
||||
}
|
||||
|
||||
type dummyMutationDetector struct{}
|
||||
@@ -68,6 +68,10 @@ type defaultCacheMutationDetector struct {
|
||||
lock sync.Mutex
|
||||
cachedObjs []cacheObj
|
||||
|
||||
retainDuration time.Duration
|
||||
lastRotated time.Time
|
||||
retainedCachedObjs []cacheObj
|
||||
|
||||
// failureFunc is injectable for unit testing. If you don't have it, the process will panic.
|
||||
// This panic is intentional, since turning on this detection indicates you want a strong
|
||||
// failure signal. This failure is effectively a p0 bug and you can't trust process results
|
||||
@@ -84,6 +88,14 @@ type cacheObj struct {
|
||||
func (d *defaultCacheMutationDetector) Run(stopCh <-chan struct{}) {
|
||||
// we DON'T want protection from panics. If we're running this code, we want to die
|
||||
for {
|
||||
if d.lastRotated.IsZero() {
|
||||
d.lastRotated = time.Now()
|
||||
} else if time.Now().Sub(d.lastRotated) > d.retainDuration {
|
||||
d.retainedCachedObjs = d.cachedObjs
|
||||
d.cachedObjs = nil
|
||||
d.lastRotated = time.Now()
|
||||
}
|
||||
|
||||
d.CompareObjects()
|
||||
|
||||
select {
|
||||
@@ -120,6 +132,12 @@ func (d *defaultCacheMutationDetector) CompareObjects() {
|
||||
altered = true
|
||||
}
|
||||
}
|
||||
for i, obj := range d.retainedCachedObjs {
|
||||
if !reflect.DeepEqual(obj.cached, obj.copied) {
|
||||
fmt.Printf("CACHE %s[%d] ALTERED!\n%v\n", d.name, i, diff.ObjectGoPrintSideBySide(obj.cached, obj.copied))
|
||||
altered = true
|
||||
}
|
||||
}
|
||||
|
||||
if altered {
|
||||
msg := fmt.Sprintf("cache %s modified", d.name)
|
||||
|
202
vendor/k8s.io/component-base/LICENSE
generated
vendored
Normal file
202
vendor/k8s.io/component-base/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
9
vendor/k8s.io/component-base/metrics/OWNERS
generated
vendored
Normal file
9
vendor/k8s.io/component-base/metrics/OWNERS
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
approvers:
|
||||
- sig-instrumentation-approvers
|
||||
- logicalhan
|
||||
reviewers:
|
||||
- sig-instrumentation-reviewers
|
||||
labels:
|
||||
- sig/instrumentation
|
148
vendor/k8s.io/component-base/metrics/collector.go
generated
vendored
Normal file
148
vendor/k8s.io/component-base/metrics/collector.go
generated
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
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"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// StableCollector extends the prometheus.Collector interface to allow customization of the
|
||||
// metric registration process, it's especially intend to be used in scenario of custom collector.
|
||||
type StableCollector interface {
|
||||
prometheus.Collector
|
||||
|
||||
// DescribeWithStability sends the super-set of all possible metrics.Desc collected
|
||||
// by this StableCollector to the provided channel.
|
||||
DescribeWithStability(chan<- *Desc)
|
||||
|
||||
// CollectWithStability sends each collected metrics.Metric via the provide channel.
|
||||
CollectWithStability(chan<- Metric)
|
||||
|
||||
// Create will initialize all Desc and it intends to be called by registry.
|
||||
Create(version *semver.Version, self StableCollector) bool
|
||||
}
|
||||
|
||||
// BaseStableCollector which implements almost all of the methods defined by StableCollector
|
||||
// is a convenient assistant for custom collectors.
|
||||
// It is recommend that inherit BaseStableCollector when implementing custom collectors.
|
||||
type BaseStableCollector struct {
|
||||
descriptors []*Desc // stores all Desc collected from DescribeWithStability().
|
||||
registrable []*Desc // stores registrable Desc(not be hidden), is a subset of descriptors.
|
||||
self StableCollector
|
||||
}
|
||||
|
||||
// DescribeWithStability sends all descriptors to the provided channel.
|
||||
// Every custom collector should over-write this method.
|
||||
func (bsc *BaseStableCollector) DescribeWithStability(ch chan<- *Desc) {
|
||||
panic(fmt.Errorf("custom collector should over-write DescribeWithStability method"))
|
||||
}
|
||||
|
||||
// Describe sends all descriptors to the provided channel.
|
||||
// It intend to be called by prometheus registry.
|
||||
func (bsc *BaseStableCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
for _, d := range bsc.registrable {
|
||||
ch <- d.toPrometheusDesc()
|
||||
}
|
||||
}
|
||||
|
||||
// CollectWithStability sends all metrics to the provided channel.
|
||||
// Every custom collector should over-write this method.
|
||||
func (bsc *BaseStableCollector) CollectWithStability(ch chan<- Metric) {
|
||||
panic(fmt.Errorf("custom collector should over-write CollectWithStability method"))
|
||||
}
|
||||
|
||||
// Collect is called by the Prometheus registry when collecting metrics.
|
||||
func (bsc *BaseStableCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
mch := make(chan Metric)
|
||||
|
||||
go func() {
|
||||
bsc.self.CollectWithStability(mch)
|
||||
close(mch)
|
||||
}()
|
||||
|
||||
for m := range mch {
|
||||
// nil Metric usually means hidden metrics
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ch <- prometheus.Metric(m)
|
||||
}
|
||||
}
|
||||
|
||||
func (bsc *BaseStableCollector) add(d *Desc) {
|
||||
bsc.descriptors = append(bsc.descriptors, d)
|
||||
}
|
||||
|
||||
// Init intends to be called by registry.
|
||||
func (bsc *BaseStableCollector) init(self StableCollector) {
|
||||
bsc.self = self
|
||||
|
||||
dch := make(chan *Desc)
|
||||
|
||||
// collect all possible descriptions from custom side
|
||||
go func() {
|
||||
bsc.self.DescribeWithStability(dch)
|
||||
close(dch)
|
||||
}()
|
||||
|
||||
for d := range dch {
|
||||
bsc.add(d)
|
||||
}
|
||||
}
|
||||
|
||||
// Create intends to be called by registry.
|
||||
// Create will return true as long as there is one or more metrics not be hidden.
|
||||
// Otherwise return false, that means the whole collector will be ignored by registry.
|
||||
func (bsc *BaseStableCollector) Create(version *semver.Version, self StableCollector) bool {
|
||||
bsc.init(self)
|
||||
|
||||
for _, d := range bsc.descriptors {
|
||||
if version != nil {
|
||||
d.determineDeprecationStatus(*version)
|
||||
}
|
||||
|
||||
d.createOnce.Do(func() {
|
||||
d.createLock.Lock()
|
||||
defer d.createLock.Unlock()
|
||||
|
||||
if d.IsHidden() {
|
||||
// do nothing for hidden metrics
|
||||
} else if d.IsDeprecated() {
|
||||
d.initializeDeprecatedDesc()
|
||||
bsc.registrable = append(bsc.registrable, d)
|
||||
d.isCreated = true
|
||||
} else {
|
||||
d.initialize()
|
||||
bsc.registrable = append(bsc.registrable, d)
|
||||
d.isCreated = true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if len(bsc.registrable) > 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if our BaseStableCollector implements necessary interface
|
||||
var _ StableCollector = &BaseStableCollector{}
|
170
vendor/k8s.io/component-base/metrics/counter.go
generated
vendored
Normal file
170
vendor/k8s.io/component-base/metrics/counter.go
generated
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
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 (
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// Counter is our internal representation for our wrapping struct around prometheus
|
||||
// counters. Counter implements both kubeCollector and CounterMetric.
|
||||
type Counter struct {
|
||||
CounterMetric
|
||||
*CounterOpts
|
||||
lazyMetric
|
||||
selfCollector
|
||||
}
|
||||
|
||||
// NewCounter returns an object which satisfies the kubeCollector and CounterMetric interfaces.
|
||||
// However, the object returned will not measure anything unless the collector is first
|
||||
// registered, since the metric is lazily instantiated.
|
||||
func NewCounter(opts *CounterOpts) *Counter {
|
||||
opts.StabilityLevel.setDefaults()
|
||||
|
||||
kc := &Counter{
|
||||
CounterOpts: opts,
|
||||
lazyMetric: lazyMetric{},
|
||||
}
|
||||
kc.setPrometheusCounter(noop)
|
||||
kc.lazyInit(kc)
|
||||
return kc
|
||||
}
|
||||
|
||||
// setPrometheusCounter sets the underlying CounterMetric object, i.e. the thing that does the measurement.
|
||||
func (c *Counter) setPrometheusCounter(counter prometheus.Counter) {
|
||||
c.CounterMetric = counter
|
||||
c.initSelfCollection(counter)
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (c *Counter) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(c.CounterOpts.DeprecatedVersion)
|
||||
}
|
||||
|
||||
// initializeMetric invocation creates the actual underlying Counter. Until this method is called
|
||||
// the underlying counter is a no-op.
|
||||
func (c *Counter) initializeMetric() {
|
||||
c.CounterOpts.annotateStabilityLevel()
|
||||
// this actually creates the underlying prometheus counter.
|
||||
c.setPrometheusCounter(prometheus.NewCounter(c.CounterOpts.toPromCounterOpts()))
|
||||
}
|
||||
|
||||
// initializeDeprecatedMetric invocation creates the actual (but deprecated) Counter. Until this method
|
||||
// is called the underlying counter is a no-op.
|
||||
func (c *Counter) initializeDeprecatedMetric() {
|
||||
c.CounterOpts.markDeprecated()
|
||||
c.initializeMetric()
|
||||
}
|
||||
|
||||
// CounterVec is the internal representation of our wrapping struct around prometheus
|
||||
// counterVecs. CounterVec implements both kubeCollector and CounterVecMetric.
|
||||
type CounterVec struct {
|
||||
*prometheus.CounterVec
|
||||
*CounterOpts
|
||||
lazyMetric
|
||||
originalLabels []string
|
||||
}
|
||||
|
||||
// NewCounterVec returns an object which satisfies the kubeCollector and CounterVecMetric interfaces.
|
||||
// However, the object returned will not measure anything unless the collector is first
|
||||
// registered, since the metric is lazily instantiated.
|
||||
func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
|
||||
opts.StabilityLevel.setDefaults()
|
||||
|
||||
cv := &CounterVec{
|
||||
CounterVec: noopCounterVec,
|
||||
CounterOpts: opts,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
}
|
||||
cv.lazyInit(cv)
|
||||
return cv
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (v *CounterVec) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(v.CounterOpts.DeprecatedVersion)
|
||||
|
||||
}
|
||||
|
||||
// initializeMetric invocation creates the actual underlying CounterVec. Until this method is called
|
||||
// the underlying counterVec is a no-op.
|
||||
func (v *CounterVec) initializeMetric() {
|
||||
v.CounterOpts.annotateStabilityLevel()
|
||||
v.CounterVec = prometheus.NewCounterVec(v.CounterOpts.toPromCounterOpts(), v.originalLabels)
|
||||
}
|
||||
|
||||
// initializeDeprecatedMetric invocation creates the actual (but deprecated) CounterVec. Until this method is called
|
||||
// the underlying counterVec is a no-op.
|
||||
func (v *CounterVec) initializeDeprecatedMetric() {
|
||||
v.CounterOpts.markDeprecated()
|
||||
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.
|
||||
// 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
|
||||
|
||||
// WithLabelValues returns the Counter 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 Counter is created IFF the counterVec
|
||||
// has been registered to a metrics registry.
|
||||
func (v *CounterVec) WithLabelValues(lvs ...string) CounterMetric {
|
||||
if !v.IsCreated() {
|
||||
return noop // return no-op counter
|
||||
}
|
||||
return v.CounterVec.WithLabelValues(lvs...)
|
||||
}
|
||||
|
||||
// With returns the Counter for the given Labels map (the label names
|
||||
// must match those of the VariableLabels in Desc). If that label map is
|
||||
// accessed for the first time, a new Counter is created IFF the counterVec has
|
||||
// been registered to a metrics registry.
|
||||
func (v *CounterVec) With(labels map[string]string) CounterMetric {
|
||||
if !v.IsCreated() {
|
||||
return noop // return no-op counter
|
||||
}
|
||||
return v.CounterVec.With(labels)
|
||||
}
|
||||
|
||||
// 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 *CounterVec) 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.CounterVec.Delete(labels)
|
||||
}
|
||||
|
||||
// Reset deletes all metrics in this vector.
|
||||
func (v *CounterVec) Reset() {
|
||||
if !v.IsCreated() {
|
||||
return
|
||||
}
|
||||
|
||||
v.CounterVec.Reset()
|
||||
}
|
173
vendor/k8s.io/component-base/metrics/desc.go
generated
vendored
Normal file
173
vendor/k8s.io/component-base/metrics/desc.go
generated
vendored
Normal file
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
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"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
"k8s.io/klog"
|
||||
)
|
||||
|
||||
// Desc is a prometheus.Desc extension.
|
||||
//
|
||||
// Use NewDesc to create new Desc instances.
|
||||
type Desc struct {
|
||||
// fqName has been built from Namespace, Subsystem, and Name.
|
||||
fqName string
|
||||
// help provides some helpful information about this metric.
|
||||
help string
|
||||
// constLabels is the label names. Their label values are variable.
|
||||
constLabels Labels
|
||||
// variableLabels contains names of labels for which the metric
|
||||
// maintains variable values.
|
||||
variableLabels []string
|
||||
|
||||
// promDesc is the descriptor used by every Prometheus Metric.
|
||||
promDesc *prometheus.Desc
|
||||
|
||||
// stabilityLevel represents the API guarantees for a given defined metric.
|
||||
stabilityLevel StabilityLevel
|
||||
// deprecatedVersion represents in which version this metric be deprecated.
|
||||
deprecatedVersion string
|
||||
|
||||
isDeprecated bool
|
||||
isHidden bool
|
||||
isCreated bool
|
||||
createLock sync.RWMutex
|
||||
markDeprecationOnce sync.Once
|
||||
createOnce sync.Once
|
||||
deprecateOnce sync.Once
|
||||
hideOnce sync.Once
|
||||
annotateOnce sync.Once
|
||||
}
|
||||
|
||||
// NewDesc extends prometheus.NewDesc with stability support.
|
||||
//
|
||||
// The stabilityLevel should be valid stability label, such as "metrics.ALPHA"
|
||||
// and "metrics.STABLE"(Maybe "metrics.BETA" in future). Default value "metrics.ALPHA"
|
||||
// will be used in case of empty or invalid stability label.
|
||||
//
|
||||
// The deprecatedVersion represents in which version this Metric be deprecated.
|
||||
// The deprecation policy outlined by the control plane metrics stability KEP.
|
||||
func NewDesc(fqName string, help string, variableLabels []string, constLabels Labels,
|
||||
stabilityLevel StabilityLevel, deprecatedVersion string) *Desc {
|
||||
d := &Desc{
|
||||
fqName: fqName,
|
||||
help: help,
|
||||
variableLabels: variableLabels,
|
||||
constLabels: constLabels,
|
||||
stabilityLevel: stabilityLevel,
|
||||
deprecatedVersion: deprecatedVersion,
|
||||
}
|
||||
d.stabilityLevel.setDefaults()
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
// String formats the Desc as a string.
|
||||
// The stability metadata maybe annotated in 'HELP' section if called after registry,
|
||||
// otherwise not.
|
||||
func (d *Desc) String() string {
|
||||
if d.isCreated {
|
||||
return d.promDesc.String()
|
||||
}
|
||||
|
||||
return prometheus.NewDesc(d.fqName, d.help, d.variableLabels, prometheus.Labels(d.constLabels)).String()
|
||||
}
|
||||
|
||||
// toPrometheusDesc transform self to prometheus.Desc
|
||||
func (d *Desc) toPrometheusDesc() *prometheus.Desc {
|
||||
return d.promDesc
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (d *Desc) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(d.deprecatedVersion)
|
||||
|
||||
}
|
||||
|
||||
func (d *Desc) determineDeprecationStatus(version semver.Version) {
|
||||
selfVersion := d.DeprecatedVersion()
|
||||
if selfVersion == nil {
|
||||
return
|
||||
}
|
||||
d.markDeprecationOnce.Do(func() {
|
||||
if selfVersion.LTE(version) {
|
||||
d.isDeprecated = true
|
||||
}
|
||||
if ShouldShowHidden() {
|
||||
klog.Warningf("Hidden metrics(%s) have been manually overridden, showing this very deprecated metric.", d.fqName)
|
||||
return
|
||||
}
|
||||
if shouldHide(&version, selfVersion) {
|
||||
// TODO(RainbowMango): Remove this log temporarily. https://github.com/kubernetes/kubernetes/issues/85369
|
||||
// klog.Warningf("This metric(%s) has been deprecated for more than one release, hiding.", d.fqName)
|
||||
d.isHidden = true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// IsHidden returns if metric will be hidden
|
||||
func (d *Desc) IsHidden() bool {
|
||||
return d.isHidden
|
||||
}
|
||||
|
||||
// IsDeprecated returns if metric has been deprecated
|
||||
func (d *Desc) IsDeprecated() bool {
|
||||
return d.isDeprecated
|
||||
}
|
||||
|
||||
func (d *Desc) markDeprecated() {
|
||||
d.deprecateOnce.Do(func() {
|
||||
d.help = fmt.Sprintf("(Deprecated since %s) %s", d.deprecatedVersion, d.help)
|
||||
})
|
||||
}
|
||||
|
||||
func (d *Desc) annotateStabilityLevel() {
|
||||
d.annotateOnce.Do(func() {
|
||||
d.help = fmt.Sprintf("[%v] %v", d.stabilityLevel, d.help)
|
||||
})
|
||||
}
|
||||
|
||||
func (d *Desc) initialize() {
|
||||
d.annotateStabilityLevel()
|
||||
// this actually creates the underlying prometheus desc.
|
||||
d.promDesc = prometheus.NewDesc(d.fqName, d.help, d.variableLabels, prometheus.Labels(d.constLabels))
|
||||
}
|
||||
|
||||
func (d *Desc) initializeDeprecatedDesc() {
|
||||
d.markDeprecated()
|
||||
d.initialize()
|
||||
}
|
||||
|
||||
// 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')
|
||||
func (d *Desc) GetRawDesc() *Desc {
|
||||
// remove stability from help if any
|
||||
stabilityStr := fmt.Sprintf("[%v] ", d.stabilityLevel)
|
||||
rawHelp := strings.Replace(d.help, stabilityStr, "", -1)
|
||||
|
||||
return NewDesc(d.fqName, rawHelp, d.variableLabels, d.constLabels, d.stabilityLevel, d.deprecatedVersion)
|
||||
}
|
193
vendor/k8s.io/component-base/metrics/gauge.go
generated
vendored
Normal file
193
vendor/k8s.io/component-base/metrics/gauge.go
generated
vendored
Normal file
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
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 (
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
"k8s.io/component-base/version"
|
||||
)
|
||||
|
||||
// Gauge is our internal representation for our wrapping struct around prometheus
|
||||
// gauges. kubeGauge implements both kubeCollector and KubeGauge.
|
||||
type Gauge struct {
|
||||
GaugeMetric
|
||||
*GaugeOpts
|
||||
lazyMetric
|
||||
selfCollector
|
||||
}
|
||||
|
||||
// NewGauge returns an object which satisfies the kubeCollector and KubeGauge 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 {
|
||||
opts.StabilityLevel.setDefaults()
|
||||
|
||||
kc := &Gauge{
|
||||
GaugeOpts: opts,
|
||||
lazyMetric: lazyMetric{},
|
||||
}
|
||||
kc.setPrometheusGauge(noop)
|
||||
kc.lazyInit(kc)
|
||||
return kc
|
||||
}
|
||||
|
||||
// setPrometheusGauge sets the underlying KubeGauge object, i.e. the thing that does the measurement.
|
||||
func (g *Gauge) setPrometheusGauge(gauge prometheus.Gauge) {
|
||||
g.GaugeMetric = gauge
|
||||
g.initSelfCollection(gauge)
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (g *Gauge) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(g.GaugeOpts.DeprecatedVersion)
|
||||
}
|
||||
|
||||
// initializeMetric invocation creates the actual underlying Gauge. Until this method is called
|
||||
// the underlying gauge is a no-op.
|
||||
func (g *Gauge) initializeMetric() {
|
||||
g.GaugeOpts.annotateStabilityLevel()
|
||||
// this actually creates the underlying prometheus gauge.
|
||||
g.setPrometheusGauge(prometheus.NewGauge(g.GaugeOpts.toPromGaugeOpts()))
|
||||
}
|
||||
|
||||
// initializeDeprecatedMetric invocation creates the actual (but deprecated) Gauge. Until this method
|
||||
// is called the underlying gauge is a no-op.
|
||||
func (g *Gauge) initializeDeprecatedMetric() {
|
||||
g.GaugeOpts.markDeprecated()
|
||||
g.initializeMetric()
|
||||
}
|
||||
|
||||
// GaugeVec is the internal representation of our wrapping struct around prometheus
|
||||
// gaugeVecs. kubeGaugeVec implements both kubeCollector and KubeGaugeVec.
|
||||
type GaugeVec struct {
|
||||
*prometheus.GaugeVec
|
||||
*GaugeOpts
|
||||
lazyMetric
|
||||
originalLabels []string
|
||||
}
|
||||
|
||||
// NewGaugeVec returns an object which satisfies the kubeCollector and KubeGaugeVec interfaces.
|
||||
// However, the object returned will not measure anything unless the collector is first
|
||||
// registered, since the metric is lazily instantiated.
|
||||
func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec {
|
||||
opts.StabilityLevel.setDefaults()
|
||||
|
||||
cv := &GaugeVec{
|
||||
GaugeVec: noopGaugeVec,
|
||||
GaugeOpts: opts,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
}
|
||||
cv.lazyInit(cv)
|
||||
return cv
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (v *GaugeVec) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(v.GaugeOpts.DeprecatedVersion)
|
||||
}
|
||||
|
||||
// initializeMetric invocation creates the actual underlying GaugeVec. Until this method is called
|
||||
// the underlying gaugeVec is a no-op.
|
||||
func (v *GaugeVec) initializeMetric() {
|
||||
v.GaugeOpts.annotateStabilityLevel()
|
||||
v.GaugeVec = prometheus.NewGaugeVec(v.GaugeOpts.toPromGaugeOpts(), v.originalLabels)
|
||||
}
|
||||
|
||||
// initializeDeprecatedMetric invocation creates the actual (but deprecated) GaugeVec. Until this method is called
|
||||
// the underlying gaugeVec is a no-op.
|
||||
func (v *GaugeVec) initializeDeprecatedMetric() {
|
||||
v.GaugeOpts.markDeprecated()
|
||||
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.
|
||||
// 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
|
||||
|
||||
// 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 {
|
||||
if !v.IsCreated() {
|
||||
return noop // return no-op gauge
|
||||
}
|
||||
return v.GaugeVec.WithLabelValues(lvs...)
|
||||
}
|
||||
|
||||
// With returns the GaugeMetric for the given Labels map (the label names
|
||||
// must match those of the VariableLabels in Desc). If that label map is
|
||||
// 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
|
||||
}
|
||||
return v.GaugeVec.With(labels)
|
||||
}
|
||||
|
||||
// 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 *GaugeVec) 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.GaugeVec.Delete(labels)
|
||||
}
|
||||
|
||||
// Reset deletes all metrics in this vector.
|
||||
func (v *GaugeVec) Reset() {
|
||||
if !v.IsCreated() {
|
||||
return
|
||||
}
|
||||
|
||||
v.GaugeVec.Reset()
|
||||
}
|
||||
|
||||
func newGaugeFunc(opts GaugeOpts, function func() float64, v semver.Version) GaugeFunc {
|
||||
g := NewGauge(&opts)
|
||||
|
||||
if !g.Create(&v) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return prometheus.NewGaugeFunc(g.GaugeOpts.toPromGaugeOpts(), function)
|
||||
}
|
||||
|
||||
// NewGaugeFunc creates a new GaugeFunc based on the provided GaugeOpts. The
|
||||
// value reported is determined by calling the given function from within the
|
||||
// Write method. Take into account that metric collection may happen
|
||||
// concurrently. If that results in concurrent calls to Write, like in the case
|
||||
// where a GaugeFunc is directly registered with Prometheus, the provided
|
||||
// function must be concurrency-safe.
|
||||
func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc {
|
||||
v := parseVersion(version.Get())
|
||||
|
||||
return newGaugeFunc(opts, function, v)
|
||||
}
|
177
vendor/k8s.io/component-base/metrics/histogram.go
generated
vendored
Normal file
177
vendor/k8s.io/component-base/metrics/histogram.go
generated
vendored
Normal file
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
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 (
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// DefBuckets is a wrapper for prometheus.DefBuckets
|
||||
var DefBuckets = prometheus.DefBuckets
|
||||
|
||||
// LinearBuckets is a wrapper for prometheus.LinearBuckets.
|
||||
func LinearBuckets(start, width float64, count int) []float64 {
|
||||
return prometheus.LinearBuckets(start, width, count)
|
||||
}
|
||||
|
||||
// ExponentialBuckets is a wrapper for prometheus.ExponentialBuckets.
|
||||
func ExponentialBuckets(start, factor float64, count int) []float64 {
|
||||
return prometheus.ExponentialBuckets(start, factor, count)
|
||||
}
|
||||
|
||||
// Histogram is our internal representation for our wrapping struct around prometheus
|
||||
// histograms. Summary implements both kubeCollector and ObserverMetric
|
||||
type Histogram struct {
|
||||
ObserverMetric
|
||||
*HistogramOpts
|
||||
lazyMetric
|
||||
selfCollector
|
||||
}
|
||||
|
||||
// NewHistogram returns an object which is Histogram-like. However, nothing
|
||||
// will be measured until the histogram is registered somewhere.
|
||||
func NewHistogram(opts *HistogramOpts) *Histogram {
|
||||
opts.StabilityLevel.setDefaults()
|
||||
|
||||
h := &Histogram{
|
||||
HistogramOpts: opts,
|
||||
lazyMetric: lazyMetric{},
|
||||
}
|
||||
h.setPrometheusHistogram(noopMetric{})
|
||||
h.lazyInit(h)
|
||||
return h
|
||||
}
|
||||
|
||||
// setPrometheusHistogram sets the underlying KubeGauge object, i.e. the thing that does the measurement.
|
||||
func (h *Histogram) setPrometheusHistogram(histogram prometheus.Histogram) {
|
||||
h.ObserverMetric = histogram
|
||||
h.initSelfCollection(histogram)
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (h *Histogram) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(h.HistogramOpts.DeprecatedVersion)
|
||||
}
|
||||
|
||||
// initializeMetric invokes the actual prometheus.Histogram object instantiation
|
||||
// and stores a reference to it
|
||||
func (h *Histogram) initializeMetric() {
|
||||
h.HistogramOpts.annotateStabilityLevel()
|
||||
// this actually creates the underlying prometheus gauge.
|
||||
h.setPrometheusHistogram(prometheus.NewHistogram(h.HistogramOpts.toPromHistogramOpts()))
|
||||
}
|
||||
|
||||
// initializeDeprecatedMetric invokes the actual prometheus.Histogram object instantiation
|
||||
// but modifies the Help description prior to object instantiation.
|
||||
func (h *Histogram) initializeDeprecatedMetric() {
|
||||
h.HistogramOpts.markDeprecated()
|
||||
h.initializeMetric()
|
||||
}
|
||||
|
||||
// HistogramVec is the internal representation of our wrapping struct around prometheus
|
||||
// histogramVecs.
|
||||
type HistogramVec struct {
|
||||
*prometheus.HistogramVec
|
||||
*HistogramOpts
|
||||
lazyMetric
|
||||
originalLabels []string
|
||||
}
|
||||
|
||||
// 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.
|
||||
func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec {
|
||||
opts.StabilityLevel.setDefaults()
|
||||
|
||||
v := &HistogramVec{
|
||||
HistogramVec: noopHistogramVec,
|
||||
HistogramOpts: opts,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
}
|
||||
v.lazyInit(v)
|
||||
return v
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (v *HistogramVec) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(v.HistogramOpts.DeprecatedVersion)
|
||||
}
|
||||
|
||||
func (v *HistogramVec) initializeMetric() {
|
||||
v.HistogramOpts.annotateStabilityLevel()
|
||||
v.HistogramVec = prometheus.NewHistogramVec(v.HistogramOpts.toPromHistogramOpts(), v.originalLabels)
|
||||
}
|
||||
|
||||
func (v *HistogramVec) initializeDeprecatedMetric() {
|
||||
v.HistogramOpts.markDeprecated()
|
||||
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.
|
||||
// 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
|
||||
|
||||
// WithLabelValues returns the ObserverMetric 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 ObserverMetric is created IFF the HistogramVec
|
||||
// has been registered to a metrics registry.
|
||||
func (v *HistogramVec) WithLabelValues(lvs ...string) ObserverMetric {
|
||||
if !v.IsCreated() {
|
||||
return noop
|
||||
}
|
||||
return v.HistogramVec.WithLabelValues(lvs...)
|
||||
}
|
||||
|
||||
// With returns the ObserverMetric for the given Labels map (the label names
|
||||
// must match those of the VariableLabels in Desc). If that label map is
|
||||
// accessed for the first time, a new ObserverMetric is created IFF the HistogramVec has
|
||||
// been registered to a metrics registry.
|
||||
func (v *HistogramVec) With(labels map[string]string) ObserverMetric {
|
||||
if !v.IsCreated() {
|
||||
return noop
|
||||
}
|
||||
return v.HistogramVec.With(labels)
|
||||
}
|
||||
|
||||
// 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 *HistogramVec) 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.HistogramVec.Delete(labels)
|
||||
}
|
||||
|
||||
// Reset deletes all metrics in this vector.
|
||||
func (v *HistogramVec) Reset() {
|
||||
if !v.IsCreated() {
|
||||
return
|
||||
}
|
||||
|
||||
v.HistogramVec.Reset()
|
||||
}
|
63
vendor/k8s.io/component-base/metrics/http.go
generated
vendored
Normal file
63
vendor/k8s.io/component-base/metrics/http.go
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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 (
|
||||
"net/http"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
// These constants cause handlers serving metrics to behave as described if
|
||||
// errors are encountered.
|
||||
const (
|
||||
// Serve an HTTP status code 500 upon the first error
|
||||
// encountered. Report the error message in the body.
|
||||
HTTPErrorOnError promhttp.HandlerErrorHandling = iota
|
||||
|
||||
// Ignore errors and try to serve as many metrics as possible. However,
|
||||
// if no metrics can be served, serve an HTTP status code 500 and the
|
||||
// last error message in the body. Only use this in deliberate "best
|
||||
// effort" metrics collection scenarios. In this case, it is highly
|
||||
// recommended to provide other means of detecting errors: By setting an
|
||||
// ErrorLog in HandlerOpts, the errors are logged. By providing a
|
||||
// Registry in HandlerOpts, the exposed metrics include an error counter
|
||||
// "promhttp_metric_handler_errors_total", which can be used for
|
||||
// alerts.
|
||||
ContinueOnError
|
||||
|
||||
// Panic upon the first error encountered (useful for "crash only" apps).
|
||||
PanicOnError
|
||||
)
|
||||
|
||||
// HandlerOpts specifies options how to serve metrics via an http.Handler. The
|
||||
// zero value of HandlerOpts is a reasonable default.
|
||||
type HandlerOpts promhttp.HandlerOpts
|
||||
|
||||
func (ho *HandlerOpts) toPromhttpHandlerOpts() promhttp.HandlerOpts {
|
||||
return promhttp.HandlerOpts(*ho)
|
||||
}
|
||||
|
||||
// HandlerFor returns an uninstrumented http.Handler for the provided
|
||||
// Gatherer. The behavior of the Handler is defined by the provided
|
||||
// HandlerOpts. Thus, HandlerFor is useful to create http.Handlers for custom
|
||||
// Gatherers, with non-default HandlerOpts, and/or with custom (or no)
|
||||
// instrumentation. Use the InstrumentMetricHandler function to apply the same
|
||||
// kind of instrumentation as it is used by the Handler function.
|
||||
func HandlerFor(reg Gatherer, opts HandlerOpts) http.Handler {
|
||||
return promhttp.HandlerFor(reg, opts.toPromhttpHandlerOpts())
|
||||
}
|
22
vendor/k8s.io/component-base/metrics/labels.go
generated
vendored
Normal file
22
vendor/k8s.io/component-base/metrics/labels.go
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
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 "github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
// Labels represents a collection of label name -> value mappings.
|
||||
type Labels prometheus.Labels
|
222
vendor/k8s.io/component-base/metrics/metric.go
generated
vendored
Normal file
222
vendor/k8s.io/component-base/metrics/metric.go
generated
vendored
Normal file
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
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 (
|
||||
"sync"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
|
||||
"k8s.io/klog"
|
||||
)
|
||||
|
||||
/*
|
||||
kubeCollector extends the prometheus.Collector interface to allow customization of the metric
|
||||
registration process. Defer metric initialization until Create() is called, which then
|
||||
delegates to the underlying metric's initializeMetric or initializeDeprecatedMetric
|
||||
method call depending on whether the metric is deprecated or not.
|
||||
*/
|
||||
type kubeCollector interface {
|
||||
Collector
|
||||
lazyKubeMetric
|
||||
DeprecatedVersion() *semver.Version
|
||||
// Each collector metric should provide an initialization function
|
||||
// for both deprecated and non-deprecated variants of a metric. This
|
||||
// is necessary since metric instantiation will be deferred
|
||||
// until the metric is actually registered somewhere.
|
||||
initializeMetric()
|
||||
initializeDeprecatedMetric()
|
||||
}
|
||||
|
||||
/*
|
||||
lazyKubeMetric defines our metric registration interface. lazyKubeMetric objects are expected
|
||||
to lazily instantiate metrics (i.e defer metric instantiation until when
|
||||
the Create() function is explicitly called).
|
||||
*/
|
||||
type lazyKubeMetric interface {
|
||||
Create(*semver.Version) bool
|
||||
IsCreated() bool
|
||||
IsHidden() bool
|
||||
IsDeprecated() bool
|
||||
}
|
||||
|
||||
/*
|
||||
lazyMetric implements lazyKubeMetric. A lazy metric is lazy because it waits until metric
|
||||
registration time before instantiation. Add it as an anonymous field to a struct that
|
||||
implements kubeCollector to get deferred registration behavior. You must call lazyInit
|
||||
with the kubeCollector itself as an argument.
|
||||
*/
|
||||
type lazyMetric struct {
|
||||
isDeprecated bool
|
||||
isHidden bool
|
||||
isCreated bool
|
||||
createLock sync.RWMutex
|
||||
markDeprecationOnce sync.Once
|
||||
createOnce sync.Once
|
||||
self kubeCollector
|
||||
}
|
||||
|
||||
func (r *lazyMetric) IsCreated() bool {
|
||||
r.createLock.RLock()
|
||||
defer r.createLock.RUnlock()
|
||||
return r.isCreated
|
||||
}
|
||||
|
||||
// lazyInit provides the lazyMetric with a reference to the kubeCollector it is supposed
|
||||
// to allow lazy initialization for. It should be invoked in the factory function which creates new
|
||||
// kubeCollector type objects.
|
||||
func (r *lazyMetric) lazyInit(self kubeCollector) {
|
||||
r.self = self
|
||||
}
|
||||
|
||||
// determineDeprecationStatus figures out whether the lazy metric should be deprecated or not.
|
||||
// This method takes a Version argument which should be the version of the binary in which
|
||||
// this code is currently being executed.
|
||||
func (r *lazyMetric) determineDeprecationStatus(version semver.Version) {
|
||||
selfVersion := r.self.DeprecatedVersion()
|
||||
if selfVersion == nil {
|
||||
return
|
||||
}
|
||||
r.markDeprecationOnce.Do(func() {
|
||||
if selfVersion.LTE(version) {
|
||||
r.isDeprecated = true
|
||||
}
|
||||
if ShouldShowHidden() {
|
||||
klog.Warningf("Hidden metrics have been manually overridden, showing this very deprecated metric.")
|
||||
return
|
||||
}
|
||||
if shouldHide(&version, selfVersion) {
|
||||
// TODO(RainbowMango): Remove this log temporarily. https://github.com/kubernetes/kubernetes/issues/85369
|
||||
// klog.Warningf("This metric has been deprecated for more than one release, hiding.")
|
||||
r.isHidden = true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (r *lazyMetric) IsHidden() bool {
|
||||
return r.isHidden
|
||||
}
|
||||
|
||||
func (r *lazyMetric) IsDeprecated() bool {
|
||||
return r.isDeprecated
|
||||
}
|
||||
|
||||
// Create forces the initialization of metric which has been deferred until
|
||||
// the point at which this method is invoked. This method will determine whether
|
||||
// the metric is deprecated or hidden, no-opting if the metric should be considered
|
||||
// hidden. Furthermore, this function no-opts and returns true if metric is already
|
||||
// created.
|
||||
func (r *lazyMetric) Create(version *semver.Version) bool {
|
||||
if version != nil {
|
||||
r.determineDeprecationStatus(*version)
|
||||
}
|
||||
// let's not create if this metric is slated to be hidden
|
||||
if r.IsHidden() {
|
||||
return false
|
||||
}
|
||||
r.createOnce.Do(func() {
|
||||
r.createLock.Lock()
|
||||
defer r.createLock.Unlock()
|
||||
r.isCreated = true
|
||||
if r.IsDeprecated() {
|
||||
r.self.initializeDeprecatedMetric()
|
||||
} else {
|
||||
r.self.initializeMetric()
|
||||
}
|
||||
})
|
||||
return r.IsCreated()
|
||||
}
|
||||
|
||||
// ClearState will clear all the states marked by Create.
|
||||
// It intends to be used for re-register a hidden metric.
|
||||
func (r *lazyMetric) ClearState() {
|
||||
r.createLock.Lock()
|
||||
defer r.createLock.Unlock()
|
||||
|
||||
r.isDeprecated = false
|
||||
r.isHidden = false
|
||||
r.isCreated = false
|
||||
r.markDeprecationOnce = *(new(sync.Once))
|
||||
r.createOnce = *(new(sync.Once))
|
||||
}
|
||||
|
||||
/*
|
||||
This code is directly lifted from the prometheus codebase. It's a convenience struct which
|
||||
allows you satisfy the Collector interface automatically if you already satisfy the Metric interface.
|
||||
|
||||
For reference: https://github.com/prometheus/client_golang/blob/v0.9.2/prometheus/collector.go#L98-L120
|
||||
*/
|
||||
type selfCollector struct {
|
||||
metric prometheus.Metric
|
||||
}
|
||||
|
||||
func (c *selfCollector) initSelfCollection(m prometheus.Metric) {
|
||||
c.metric = m
|
||||
}
|
||||
|
||||
func (c *selfCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- c.metric.Desc()
|
||||
}
|
||||
|
||||
func (c *selfCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
ch <- c.metric
|
||||
}
|
||||
|
||||
// no-op vecs for convenience
|
||||
var noopCounterVec = &prometheus.CounterVec{}
|
||||
var noopHistogramVec = &prometheus.HistogramVec{}
|
||||
var noopSummaryVec = &prometheus.SummaryVec{}
|
||||
var noopGaugeVec = &prometheus.GaugeVec{}
|
||||
var noopObserverVec = &noopObserverVector{}
|
||||
|
||||
// just use a convenience struct for all the no-ops
|
||||
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) {}
|
||||
|
||||
type noopObserverVector struct{}
|
||||
|
||||
func (noopObserverVector) GetMetricWith(prometheus.Labels) (prometheus.Observer, error) {
|
||||
return noop, nil
|
||||
}
|
||||
func (noopObserverVector) GetMetricWithLabelValues(...string) (prometheus.Observer, error) {
|
||||
return noop, nil
|
||||
}
|
||||
func (noopObserverVector) With(prometheus.Labels) prometheus.Observer { return noop }
|
||||
func (noopObserverVector) WithLabelValues(...string) prometheus.Observer { return noop }
|
||||
func (noopObserverVector) CurryWith(prometheus.Labels) (prometheus.ObserverVec, error) {
|
||||
return noopObserverVec, nil
|
||||
}
|
||||
func (noopObserverVector) MustCurryWith(prometheus.Labels) prometheus.ObserverVec {
|
||||
return noopObserverVec
|
||||
}
|
||||
func (noopObserverVector) Describe(chan<- *prometheus.Desc) {}
|
||||
func (noopObserverVector) Collect(chan<- prometheus.Metric) {}
|
245
vendor/k8s.io/component-base/metrics/opts.go
generated
vendored
Normal file
245
vendor/k8s.io/component-base/metrics/opts.go
generated
vendored
Normal file
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
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"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// KubeOpts is superset struct for prometheus.Opts. The prometheus Opts structure
|
||||
// is purposefully not embedded here because that would change struct initialization
|
||||
// in the manner which people are currently accustomed.
|
||||
//
|
||||
// Name must be set to a non-empty string. DeprecatedVersion is defined only
|
||||
// if the metric for which this options applies is, in fact, deprecated.
|
||||
type KubeOpts struct {
|
||||
Namespace string
|
||||
Subsystem string
|
||||
Name string
|
||||
Help string
|
||||
ConstLabels map[string]string
|
||||
DeprecatedVersion string
|
||||
deprecateOnce sync.Once
|
||||
annotateOnce sync.Once
|
||||
StabilityLevel StabilityLevel
|
||||
}
|
||||
|
||||
// BuildFQName joins the given three name components by "_". Empty name
|
||||
// components are ignored. If the name parameter itself is empty, an empty
|
||||
// string is returned, no matter what. Metric implementations included in this
|
||||
// library use this function internally to generate the fully-qualified metric
|
||||
// name from the name component in their Opts. Users of the library will only
|
||||
// need this function if they implement their own Metric or instantiate a Desc
|
||||
// (with NewDesc) directly.
|
||||
func BuildFQName(namespace, subsystem, name string) string {
|
||||
return prometheus.BuildFQName(namespace, subsystem, name)
|
||||
}
|
||||
|
||||
// StabilityLevel represents the API guarantees for a given defined metric.
|
||||
type StabilityLevel string
|
||||
|
||||
const (
|
||||
// ALPHA metrics have no stability guarantees, as such, labels may
|
||||
// be arbitrarily added/removed and the metric may be deleted at any time.
|
||||
ALPHA StabilityLevel = "ALPHA"
|
||||
// STABLE metrics are guaranteed not be mutated and removal is governed by
|
||||
// the deprecation policy outlined in by the control plane metrics stability KEP.
|
||||
STABLE StabilityLevel = "STABLE"
|
||||
)
|
||||
|
||||
// setDefaults takes 'ALPHA' in case of empty.
|
||||
func (sl *StabilityLevel) setDefaults() {
|
||||
switch *sl {
|
||||
case "":
|
||||
*sl = ALPHA
|
||||
default:
|
||||
// no-op, since we have a StabilityLevel already
|
||||
}
|
||||
}
|
||||
|
||||
// CounterOpts is an alias for Opts. See there for doc comments.
|
||||
type CounterOpts KubeOpts
|
||||
|
||||
// Modify help description on the metric description.
|
||||
func (o *CounterOpts) 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 *CounterOpts) 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 *CounterOpts) toPromCounterOpts() prometheus.CounterOpts {
|
||||
return prometheus.CounterOpts{
|
||||
Namespace: o.Namespace,
|
||||
Subsystem: o.Subsystem,
|
||||
Name: o.Name,
|
||||
Help: o.Help,
|
||||
ConstLabels: o.ConstLabels,
|
||||
}
|
||||
}
|
||||
|
||||
// GaugeOpts is an alias for Opts. See there for doc comments.
|
||||
type GaugeOpts KubeOpts
|
||||
|
||||
// Modify help description on the metric description.
|
||||
func (o *GaugeOpts) 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 *GaugeOpts) 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 GaugeOpts) toPromGaugeOpts() prometheus.GaugeOpts {
|
||||
return prometheus.GaugeOpts{
|
||||
Namespace: o.Namespace,
|
||||
Subsystem: o.Subsystem,
|
||||
Name: o.Name,
|
||||
Help: o.Help,
|
||||
ConstLabels: o.ConstLabels,
|
||||
}
|
||||
}
|
||||
|
||||
// HistogramOpts bundles the options for creating a Histogram 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 HistogramOpts struct {
|
||||
Namespace string
|
||||
Subsystem string
|
||||
Name string
|
||||
Help string
|
||||
ConstLabels map[string]string
|
||||
Buckets []float64
|
||||
DeprecatedVersion string
|
||||
deprecateOnce sync.Once
|
||||
annotateOnce sync.Once
|
||||
StabilityLevel StabilityLevel
|
||||
}
|
||||
|
||||
// Modify help description on the metric description.
|
||||
func (o *HistogramOpts) 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 *HistogramOpts) 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 HistogramOpts) toPromHistogramOpts() prometheus.HistogramOpts {
|
||||
return prometheus.HistogramOpts{
|
||||
Namespace: o.Namespace,
|
||||
Subsystem: o.Subsystem,
|
||||
Name: o.Name,
|
||||
Help: o.Help,
|
||||
ConstLabels: o.ConstLabels,
|
||||
Buckets: o.Buckets,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// a help string and to explicitly set the Objectives field to the desired value
|
||||
// as the default value will change in the upcoming v0.10 of the library.
|
||||
type SummaryOpts struct {
|
||||
Namespace string
|
||||
Subsystem string
|
||||
Name string
|
||||
Help string
|
||||
ConstLabels map[string]string
|
||||
Objectives map[float64]float64
|
||||
MaxAge time.Duration
|
||||
AgeBuckets uint32
|
||||
BufCap uint32
|
||||
DeprecatedVersion string
|
||||
deprecateOnce sync.Once
|
||||
annotateOnce sync.Once
|
||||
StabilityLevel StabilityLevel
|
||||
}
|
||||
|
||||
// Modify help description on the metric description.
|
||||
func (o *SummaryOpts) 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 *SummaryOpts) annotateStabilityLevel() {
|
||||
o.annotateOnce.Do(func() {
|
||||
o.Help = fmt.Sprintf("[%v] %v", o.StabilityLevel, o.Help)
|
||||
})
|
||||
}
|
||||
|
||||
// Deprecated: DefObjectives will not be used as the default objectives in
|
||||
// v1.0.0 of the library. The default Summary will have no quantiles then.
|
||||
var (
|
||||
defObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
|
||||
)
|
||||
|
||||
// convenience function to allow easy transformation to the prometheus
|
||||
// counterpart. This will do more once we have a proper label abstraction
|
||||
func (o SummaryOpts) toPromSummaryOpts() prometheus.SummaryOpts {
|
||||
// we need to retain existing quantile behavior for backwards compatibility,
|
||||
// so let's do what prometheus used to do prior to v1.
|
||||
objectives := o.Objectives
|
||||
if objectives == nil {
|
||||
objectives = defObjectives
|
||||
}
|
||||
return prometheus.SummaryOpts{
|
||||
Namespace: o.Namespace,
|
||||
Subsystem: o.Subsystem,
|
||||
Name: o.Name,
|
||||
Help: o.Help,
|
||||
ConstLabels: o.ConstLabels,
|
||||
Objectives: objectives,
|
||||
MaxAge: o.MaxAge,
|
||||
AgeBuckets: o.AgeBuckets,
|
||||
BufCap: o.BufCap,
|
||||
}
|
||||
}
|
66
vendor/k8s.io/component-base/metrics/processstarttime.go
generated
vendored
Normal file
66
vendor/k8s.io/component-base/metrics/processstarttime.go
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
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 (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/procfs"
|
||||
|
||||
"k8s.io/klog"
|
||||
)
|
||||
|
||||
var processStartTime = prometheus.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "process_start_time_seconds",
|
||||
Help: "Start time of the process since unix epoch in seconds.",
|
||||
},
|
||||
[]string{},
|
||||
)
|
||||
|
||||
// Registerer is an interface expected by RegisterProcessStartTime in order to register the metric
|
||||
type Registerer interface {
|
||||
Register(prometheus.Collector) error
|
||||
}
|
||||
|
||||
// RegisterProcessStartTime registers the process_start_time_seconds to
|
||||
// a prometheus registry. This metric needs to be included to ensure counter
|
||||
// data fidelity.
|
||||
func RegisterProcessStartTime(registrationFunc func(prometheus.Collector) error) error {
|
||||
start, err := getProcessStart()
|
||||
if err != nil {
|
||||
klog.Errorf("Could not get process start time, %v", err)
|
||||
start = float64(time.Now().Unix())
|
||||
}
|
||||
processStartTime.WithLabelValues().Set(start)
|
||||
return registrationFunc(processStartTime)
|
||||
}
|
||||
|
||||
func getProcessStart() (float64, error) {
|
||||
pid := os.Getpid()
|
||||
p, err := procfs.NewProc(pid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if stat, err := p.NewStat(); err == nil {
|
||||
return stat.StartTime()
|
||||
}
|
||||
return 0, err
|
||||
}
|
265
vendor/k8s.io/component-base/metrics/registry.go
generated
vendored
Normal file
265
vendor/k8s.io/component-base/metrics/registry.go
generated
vendored
Normal file
@@ -0,0 +1,265 @@
|
||||
/*
|
||||
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"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"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 (
|
||||
showHiddenOnce sync.Once
|
||||
showHidden atomic.Value
|
||||
registries []*kubeRegistry // stores all registries created by NewKubeRegistry()
|
||||
registriesLock sync.RWMutex
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func validateShowHiddenMetricsVersion(currentVersion semver.Version, targetVersionStr string) error {
|
||||
if targetVersionStr == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
validVersionStr := fmt.Sprintf("%d.%d", currentVersion.Major, currentVersion.Minor-1)
|
||||
if targetVersionStr != validVersionStr {
|
||||
return fmt.Errorf("--show-hidden-metrics-for-version must be omitted or have the value '%v'. Only the previous minor version is allowed", validVersionStr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return showHidden.Load() != nil && showHidden.Load().(bool)
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// KubeRegistry is an interface which implements a subset of prometheus.Registerer and
|
||||
// prometheus.Gatherer interfaces
|
||||
type KubeRegistry interface {
|
||||
// Deprecated
|
||||
RawRegister(prometheus.Collector) error
|
||||
// Deprecated
|
||||
RawMustRegister(...prometheus.Collector)
|
||||
CustomRegister(c StableCollector) error
|
||||
CustomMustRegister(cs ...StableCollector)
|
||||
Register(Registerable) error
|
||||
MustRegister(...Registerable)
|
||||
Unregister(Registerable) bool
|
||||
Gather() ([]*dto.MetricFamily, error)
|
||||
}
|
||||
|
||||
// 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
|
||||
hiddenCollectors []Registerable // stores all collectors that has been hidden
|
||||
hiddenCollectorsLock sync.RWMutex
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return kr.PromRegistry.Register(c)
|
||||
}
|
||||
|
||||
kr.trackHiddenCollector(c)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
} else {
|
||||
kr.trackHiddenCollector(c)
|
||||
}
|
||||
}
|
||||
kr.PromRegistry.MustRegister(metrics...)
|
||||
}
|
||||
|
||||
// CustomRegister registers a new custom collector.
|
||||
func (kr *kubeRegistry) CustomRegister(c StableCollector) error {
|
||||
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) {
|
||||
collectors := make([]prometheus.Collector, 0, len(cs))
|
||||
for _, c := range cs {
|
||||
if c.Create(&kr.version, c) {
|
||||
collectors = append(collectors, c)
|
||||
}
|
||||
}
|
||||
|
||||
kr.PromRegistry.MustRegister(collectors...)
|
||||
}
|
||||
|
||||
// RawRegister 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) RawRegister(c prometheus.Collector) error {
|
||||
return kr.PromRegistry.Register(c)
|
||||
}
|
||||
|
||||
// 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...)
|
||||
}
|
||||
|
||||
// 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).
|
||||
func (kr *kubeRegistry) Unregister(collector Registerable) bool {
|
||||
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()
|
||||
|
||||
kr.hiddenCollectors = append(kr.hiddenCollectors, c)
|
||||
}
|
||||
|
||||
// enableHiddenCollectors will re-register all of the hidden collectors.
|
||||
func (kr *kubeRegistry) enableHiddenCollectors() {
|
||||
kr.hiddenCollectorsLock.Lock()
|
||||
defer kr.hiddenCollectorsLock.Unlock()
|
||||
|
||||
for _, c := range kr.hiddenCollectors {
|
||||
c.ClearState()
|
||||
kr.MustRegister(c)
|
||||
}
|
||||
kr.hiddenCollectors = nil
|
||||
}
|
||||
|
||||
func newKubeRegistry(v apimachineryversion.Info) *kubeRegistry {
|
||||
r := &kubeRegistry{
|
||||
PromRegistry: prometheus.NewRegistry(),
|
||||
version: parseVersion(v),
|
||||
}
|
||||
|
||||
registriesLock.Lock()
|
||||
defer registriesLock.Unlock()
|
||||
registries = append(registries, r)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// NewKubeRegistry creates a new vanilla Registry without any Collectors
|
||||
// pre-registered.
|
||||
func NewKubeRegistry() KubeRegistry {
|
||||
r := newKubeRegistry(version.Get())
|
||||
|
||||
return r
|
||||
}
|
171
vendor/k8s.io/component-base/metrics/summary.go
generated
vendored
Normal file
171
vendor/k8s.io/component-base/metrics/summary.go
generated
vendored
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
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 (
|
||||
"github.com/blang/semver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// Summary is our internal representation for our wrapping struct around prometheus
|
||||
// summaries. Summary implements both kubeCollector and ObserverMetric
|
||||
//
|
||||
// DEPRECATED: as per the metrics overhaul KEP
|
||||
type Summary struct {
|
||||
ObserverMetric
|
||||
*SummaryOpts
|
||||
lazyMetric
|
||||
selfCollector
|
||||
}
|
||||
|
||||
// NewSummary returns an object which is Summary-like. However, nothing
|
||||
// will be measured until the summary is registered somewhere.
|
||||
//
|
||||
// DEPRECATED: as per the metrics overhaul KEP
|
||||
func NewSummary(opts *SummaryOpts) *Summary {
|
||||
opts.StabilityLevel.setDefaults()
|
||||
|
||||
s := &Summary{
|
||||
SummaryOpts: opts,
|
||||
lazyMetric: lazyMetric{},
|
||||
}
|
||||
s.setPrometheusSummary(noopMetric{})
|
||||
s.lazyInit(s)
|
||||
return s
|
||||
}
|
||||
|
||||
// setPrometheusSummary sets the underlying KubeGauge object, i.e. the thing that does the measurement.
|
||||
func (s *Summary) setPrometheusSummary(summary prometheus.Summary) {
|
||||
s.ObserverMetric = summary
|
||||
s.initSelfCollection(summary)
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (s *Summary) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(s.SummaryOpts.DeprecatedVersion)
|
||||
}
|
||||
|
||||
// initializeMetric invokes the actual prometheus.Summary object instantiation
|
||||
// and stores a reference to it
|
||||
func (s *Summary) initializeMetric() {
|
||||
s.SummaryOpts.annotateStabilityLevel()
|
||||
// this actually creates the underlying prometheus gauge.
|
||||
s.setPrometheusSummary(prometheus.NewSummary(s.SummaryOpts.toPromSummaryOpts()))
|
||||
}
|
||||
|
||||
// initializeDeprecatedMetric invokes the actual prometheus.Summary object instantiation
|
||||
// but modifies the Help description prior to object instantiation.
|
||||
func (s *Summary) initializeDeprecatedMetric() {
|
||||
s.SummaryOpts.markDeprecated()
|
||||
s.initializeMetric()
|
||||
}
|
||||
|
||||
// SummaryVec is the internal representation of our wrapping struct around prometheus
|
||||
// summaryVecs.
|
||||
//
|
||||
// DEPRECATED: as per the metrics overhaul KEP
|
||||
type SummaryVec struct {
|
||||
*prometheus.SummaryVec
|
||||
*SummaryOpts
|
||||
lazyMetric
|
||||
originalLabels []string
|
||||
}
|
||||
|
||||
// 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.
|
||||
//
|
||||
// DEPRECATED: as per the metrics overhaul KEP
|
||||
func NewSummaryVec(opts *SummaryOpts, labels []string) *SummaryVec {
|
||||
opts.StabilityLevel.setDefaults()
|
||||
|
||||
v := &SummaryVec{
|
||||
SummaryOpts: opts,
|
||||
originalLabels: labels,
|
||||
lazyMetric: lazyMetric{},
|
||||
}
|
||||
v.lazyInit(v)
|
||||
return v
|
||||
}
|
||||
|
||||
// DeprecatedVersion returns a pointer to the Version or nil
|
||||
func (v *SummaryVec) DeprecatedVersion() *semver.Version {
|
||||
return parseSemver(v.SummaryOpts.DeprecatedVersion)
|
||||
}
|
||||
|
||||
func (v *SummaryVec) initializeMetric() {
|
||||
v.SummaryOpts.annotateStabilityLevel()
|
||||
v.SummaryVec = prometheus.NewSummaryVec(v.SummaryOpts.toPromSummaryOpts(), v.originalLabels)
|
||||
}
|
||||
|
||||
func (v *SummaryVec) initializeDeprecatedMetric() {
|
||||
v.SummaryOpts.markDeprecated()
|
||||
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.
|
||||
// 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
|
||||
|
||||
// WithLabelValues returns the ObserverMetric 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 ObserverMetric is created IFF the summaryVec
|
||||
// has been registered to a metrics registry.
|
||||
func (v *SummaryVec) WithLabelValues(lvs ...string) ObserverMetric {
|
||||
if !v.IsCreated() {
|
||||
return noop
|
||||
}
|
||||
return v.SummaryVec.WithLabelValues(lvs...)
|
||||
}
|
||||
|
||||
// With returns the ObserverMetric for the given Labels map (the label names
|
||||
// must match those of the VariableLabels in Desc). If that label map is
|
||||
// accessed for the first time, a new ObserverMetric is created IFF the summaryVec has
|
||||
// been registered to a metrics registry.
|
||||
func (v *SummaryVec) With(labels map[string]string) ObserverMetric {
|
||||
if !v.IsCreated() {
|
||||
return noop
|
||||
}
|
||||
return v.SummaryVec.With(labels)
|
||||
}
|
||||
|
||||
// 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 *SummaryVec) 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.SummaryVec.Delete(labels)
|
||||
}
|
||||
|
||||
// Reset deletes all metrics in this vector.
|
||||
func (v *SummaryVec) Reset() {
|
||||
if !v.IsCreated() {
|
||||
return
|
||||
}
|
||||
|
||||
v.SummaryVec.Reset()
|
||||
}
|
60
vendor/k8s.io/component-base/metrics/value.go
generated
vendored
Normal file
60
vendor/k8s.io/component-base/metrics/value.go
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
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 (
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// ValueType is an enumeration of metric types that represent a simple value.
|
||||
type ValueType int
|
||||
|
||||
// Possible values for the ValueType enum.
|
||||
const (
|
||||
_ ValueType = iota
|
||||
CounterValue
|
||||
GaugeValue
|
||||
UntypedValue
|
||||
)
|
||||
|
||||
func (vt *ValueType) toPromValueType() prometheus.ValueType {
|
||||
return prometheus.ValueType(*vt)
|
||||
}
|
||||
|
||||
// NewLazyConstMetric is a helper of MustNewConstMetric.
|
||||
//
|
||||
// Note: If the metrics described by the desc is hidden, the metrics will not be created.
|
||||
func NewLazyConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) Metric {
|
||||
if desc.IsHidden() {
|
||||
return nil
|
||||
}
|
||||
return prometheus.MustNewConstMetric(desc.toPrometheusDesc(), valueType.toPromValueType(), value, labelValues...)
|
||||
}
|
||||
|
||||
// NewLazyMetricWithTimestamp is a helper of NewMetricWithTimestamp.
|
||||
//
|
||||
// Warning: the Metric 'm' must be the one created by NewLazyConstMetric(),
|
||||
// otherwise, no stability guarantees would be offered.
|
||||
func NewLazyMetricWithTimestamp(t time.Time, m Metric) Metric {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return prometheus.NewMetricWithTimestamp(t, m)
|
||||
}
|
37
vendor/k8s.io/component-base/metrics/version.go
generated
vendored
Normal file
37
vendor/k8s.io/component-base/metrics/version.go
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
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 "k8s.io/component-base/version"
|
||||
|
||||
var (
|
||||
buildInfo = NewGaugeVec(
|
||||
&GaugeOpts{
|
||||
Name: "kubernetes_build_info",
|
||||
Help: "A metric with a constant '1' value labeled by major, minor, git version, git commit, git tree state, build date, Go version, and compiler from which Kubernetes was built, and platform on which it is running.",
|
||||
StabilityLevel: ALPHA,
|
||||
},
|
||||
[]string{"major", "minor", "gitVersion", "gitCommit", "gitTreeState", "buildDate", "goVersion", "compiler", "platform"},
|
||||
)
|
||||
)
|
||||
|
||||
// RegisterBuildInfo registers the build and version info in a metadata metric in prometheus
|
||||
func RegisterBuildInfo(r KubeRegistry) {
|
||||
info := version.Get()
|
||||
r.MustRegister(buildInfo)
|
||||
buildInfo.WithLabelValues(info.Major, info.Minor, info.GitVersion, info.GitCommit, info.GitTreeState, info.BuildDate, info.GoVersion, info.Compiler, info.Platform).Set(1)
|
||||
}
|
50
vendor/k8s.io/component-base/metrics/version_parser.go
generated
vendored
Normal file
50
vendor/k8s.io/component-base/metrics/version_parser.go
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
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"
|
||||
"regexp"
|
||||
|
||||
"github.com/blang/semver"
|
||||
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
|
||||
const (
|
||||
versionRegexpString = `^v(\d+\.\d+\.\d+)`
|
||||
)
|
||||
|
||||
var (
|
||||
versionRe = regexp.MustCompile(versionRegexpString)
|
||||
)
|
||||
|
||||
func parseSemver(s string) *semver.Version {
|
||||
if s != "" {
|
||||
sv := semver.MustParse(s)
|
||||
return &sv
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func parseVersion(ver apimachineryversion.Info) semver.Version {
|
||||
matches := versionRe.FindAllStringSubmatch(ver.String(), -1)
|
||||
|
||||
if len(matches) != 1 {
|
||||
panic(fmt.Sprintf("version string \"%v\" doesn't match expected regular expression: \"%v\"", ver.String(), versionRe.String()))
|
||||
}
|
||||
return semver.MustParse(matches[0][1])
|
||||
}
|
95
vendor/k8s.io/component-base/metrics/wrappers.go
generated
vendored
Normal file
95
vendor/k8s.io/component-base/metrics/wrappers.go
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
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 (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
)
|
||||
|
||||
// This file contains a series of interfaces which we explicitly define for
|
||||
// integrating with prometheus. We redefine the interfaces explicitly here
|
||||
// so that we can prevent breakage if methods are ever added to prometheus
|
||||
// variants of them.
|
||||
|
||||
// Collector defines a subset of prometheus.Collector interface methods
|
||||
type Collector interface {
|
||||
Describe(chan<- *prometheus.Desc)
|
||||
Collect(chan<- prometheus.Metric)
|
||||
}
|
||||
|
||||
// Metric defines a subset of prometheus.Metric interface methods
|
||||
type Metric interface {
|
||||
Desc() *prometheus.Desc
|
||||
Write(*dto.Metric) error
|
||||
}
|
||||
|
||||
// CounterMetric is a Metric that represents a single numerical value that only ever
|
||||
// goes up. That implies that it cannot be used to count items whose number can
|
||||
// also go down, e.g. the number of currently running goroutines. Those
|
||||
// "counters" are represented by Gauges.
|
||||
|
||||
// CounterMetric is an interface which defines a subset of the interface provided by prometheus.Counter
|
||||
type CounterMetric interface {
|
||||
Inc()
|
||||
Add(float64)
|
||||
}
|
||||
|
||||
// CounterVecMetric is an interface which prometheus.CounterVec satisfies.
|
||||
type CounterVecMetric interface {
|
||||
WithLabelValues(...string) CounterMetric
|
||||
With(prometheus.Labels) CounterMetric
|
||||
}
|
||||
|
||||
// GaugeMetric is an interface which defines a subset of the interface provided by prometheus.Gauge
|
||||
type GaugeMetric interface {
|
||||
Set(float64)
|
||||
Inc()
|
||||
Dec()
|
||||
Add(float64)
|
||||
Write(out *dto.Metric) error
|
||||
SetToCurrentTime()
|
||||
}
|
||||
|
||||
// ObserverMetric captures individual observations.
|
||||
type ObserverMetric interface {
|
||||
Observe(float64)
|
||||
}
|
||||
|
||||
// PromRegistry is an interface which implements a subset of prometheus.Registerer and
|
||||
// prometheus.Gatherer interfaces
|
||||
type PromRegistry interface {
|
||||
Register(prometheus.Collector) error
|
||||
MustRegister(...prometheus.Collector)
|
||||
Unregister(prometheus.Collector) bool
|
||||
Gather() ([]*dto.MetricFamily, error)
|
||||
}
|
||||
|
||||
// Gatherer is the interface for the part of a registry in charge of gathering
|
||||
// the collected metrics into a number of MetricFamilies.
|
||||
type Gatherer interface {
|
||||
prometheus.Gatherer
|
||||
}
|
||||
|
||||
// GaugeFunc is a Gauge whose value is determined at collect time by calling a
|
||||
// provided function.
|
||||
//
|
||||
// To create GaugeFunc instances, use NewGaugeFunc.
|
||||
type GaugeFunc interface {
|
||||
Metric
|
||||
Collector
|
||||
}
|
1
vendor/k8s.io/component-base/version/.gitattributes
generated
vendored
Normal file
1
vendor/k8s.io/component-base/version/.gitattributes
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
base.go export-subst
|
63
vendor/k8s.io/component-base/version/base.go
generated
vendored
Normal file
63
vendor/k8s.io/component-base/version/base.go
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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 version
|
||||
|
||||
// Base version information.
|
||||
//
|
||||
// This is the fallback data used when version information from git is not
|
||||
// provided via go ldflags. It provides an approximation of the Kubernetes
|
||||
// version for ad-hoc builds (e.g. `go build`) that cannot get the version
|
||||
// information from git.
|
||||
//
|
||||
// If you are looking at these fields in the git tree, they look
|
||||
// strange. They are modified on the fly by the build process. The
|
||||
// in-tree values are dummy values used for "git archive", which also
|
||||
// works for GitHub tar downloads.
|
||||
//
|
||||
// When releasing a new Kubernetes version, this file is updated by
|
||||
// build/mark_new_version.sh to reflect the new version, and then a
|
||||
// git annotated tag (using format vX.Y where X == Major version and Y
|
||||
// == Minor version) is created to point to the commit that updates
|
||||
// component-base/version/base.go
|
||||
var (
|
||||
// TODO: Deprecate gitMajor and gitMinor, use only gitVersion
|
||||
// instead. First step in deprecation, keep the fields but make
|
||||
// them irrelevant. (Next we'll take it out, which may muck with
|
||||
// scripts consuming the kubectl version output - but most of
|
||||
// these should be looking at gitVersion already anyways.)
|
||||
gitMajor string // major version, always numeric
|
||||
gitMinor string // minor version, numeric possibly followed by "+"
|
||||
|
||||
// semantic version, derived by build scripts (see
|
||||
// https://github.com/kubernetes/community/blob/master/contributors/design-proposals/release/versioning.md
|
||||
// for a detailed discussion of this field)
|
||||
//
|
||||
// TODO: This field is still called "gitVersion" for legacy
|
||||
// reasons. For prerelease versions, the build metadata on the
|
||||
// semantic version is a git hash, but the version itself is no
|
||||
// longer the direct output of "git describe", but a slight
|
||||
// translation to be semver compliant.
|
||||
|
||||
// NOTE: The $Format strings are replaced during 'git archive' thanks to the
|
||||
// companion .gitattributes file containing 'export-subst' in this same
|
||||
// directory. See also https://git-scm.com/docs/gitattributes
|
||||
gitVersion = "v0.0.0-master+$Format:%h$"
|
||||
gitCommit = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
|
||||
gitTreeState = "" // state of git tree, either "clean" or "dirty"
|
||||
|
||||
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
||||
)
|
39
vendor/k8s.io/component-base/version/def.bzl
generated
vendored
Normal file
39
vendor/k8s.io/component-base/version/def.bzl
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
# Copyright 2017 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.
|
||||
|
||||
# Implements hack/lib/version.sh's kube::version::ldflags() for Bazel.
|
||||
def version_x_defs():
|
||||
# This should match the list of packages in kube::version::ldflag
|
||||
stamp_pkgs = [
|
||||
"k8s.io/kubernetes/vendor/k8s.io/component-base/version",
|
||||
"k8s.io/kubernetes/vendor/k8s.io/client-go/pkg/version",
|
||||
]
|
||||
|
||||
# This should match the list of vars in kube::version::ldflags
|
||||
# It should also match the list of vars set in hack/print-workspace-status.sh.
|
||||
stamp_vars = [
|
||||
"buildDate",
|
||||
"gitCommit",
|
||||
"gitMajor",
|
||||
"gitMinor",
|
||||
"gitTreeState",
|
||||
"gitVersion",
|
||||
]
|
||||
|
||||
# Generate the cross-product.
|
||||
x_defs = {}
|
||||
for pkg in stamp_pkgs:
|
||||
for var in stamp_vars:
|
||||
x_defs["%s.%s" % (pkg, var)] = "{%s}" % var
|
||||
return x_defs
|
42
vendor/k8s.io/component-base/version/version.go
generated
vendored
Normal file
42
vendor/k8s.io/component-base/version/version.go
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
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 version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
|
||||
// Get returns the overall codebase version. It's for detecting
|
||||
// what code a binary was built from.
|
||||
func Get() apimachineryversion.Info {
|
||||
// These variables typically come from -ldflags settings and in
|
||||
// their absence fallback to the settings in ./base.go
|
||||
return apimachineryversion.Info{
|
||||
Major: gitMajor,
|
||||
Minor: gitMinor,
|
||||
GitVersion: gitVersion,
|
||||
GitCommit: gitCommit,
|
||||
GitTreeState: gitTreeState,
|
||||
BuildDate: buildDate,
|
||||
GoVersion: runtime.Version(),
|
||||
Compiler: runtime.Compiler,
|
||||
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user