Skip to content

Commit 634ab0b

Browse files
author
Han Kang
committed
add additional documentation around exposed functionality
1 parent e6fbb59 commit 634ab0b

File tree

6 files changed

+93
-54
lines changed

6 files changed

+93
-54
lines changed

pkg/util/metrics/framework/counter.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ import (
2121
"github.com/prometheus/client_golang/prometheus"
2222
)
2323

24-
// kubeCounter is our internal representation for our wrapping struct around prometheus
25-
// counters. kubeCounter implements both KubeCollector and KubeCounter.
26-
type kubeCounter struct {
27-
KubeCounter
24+
// Counter is our internal representation for our wrapping struct around prometheus
25+
// counters. Counter implements both KubeCollector and CounterMetric.
26+
type Counter struct {
27+
CounterMetric
2828
*CounterOpts
2929
lazyMetric
3030
selfCollector
3131
}
3232

33-
// NewCounter returns an object which satisfies the KubeCollector and KubeCounter interfaces.
33+
// NewCounter returns an object which satisfies the KubeCollector and CounterMetric interfaces.
3434
// However, the object returned will not measure anything unless the collector is first
3535
// registered, since the metric is lazily instantiated.
36-
func NewCounter(opts *CounterOpts) *kubeCounter {
36+
func NewCounter(opts *CounterOpts) *Counter {
3737
// todo: handle defaulting better
3838
if opts.StabilityLevel == "" {
3939
opts.StabilityLevel = ALPHA
4040
}
41-
kc := &kubeCounter{
41+
kc := &Counter{
4242
CounterOpts: opts,
4343
lazyMetric: lazyMetric{},
4444
}
@@ -47,46 +47,46 @@ func NewCounter(opts *CounterOpts) *kubeCounter {
4747
return kc
4848
}
4949

50-
// setPrometheusCounter sets the underlying KubeCounter object, i.e. the thing that does the measurement.
51-
func (c *kubeCounter) setPrometheusCounter(counter prometheus.Counter) {
52-
c.KubeCounter = counter
50+
// setPrometheusCounter sets the underlying CounterMetric object, i.e. the thing that does the measurement.
51+
func (c *Counter) setPrometheusCounter(counter prometheus.Counter) {
52+
c.CounterMetric = counter
5353
c.initSelfCollection(counter)
5454
}
5555

5656
// DeprecatedVersion returns a pointer to the Version or nil
57-
func (c *kubeCounter) DeprecatedVersion() *semver.Version {
57+
func (c *Counter) DeprecatedVersion() *semver.Version {
5858
return c.CounterOpts.DeprecatedVersion
5959
}
6060

6161
// initializeMetric invocation creates the actual underlying Counter. Until this method is called
6262
// the underlying counter is a no-op.
63-
func (c *kubeCounter) initializeMetric() {
63+
func (c *Counter) initializeMetric() {
6464
c.CounterOpts.annotateStabilityLevel()
6565
// this actually creates the underlying prometheus counter.
6666
c.setPrometheusCounter(prometheus.NewCounter(c.CounterOpts.toPromCounterOpts()))
6767
}
6868

6969
// initializeDeprecatedMetric invocation creates the actual (but deprecated) Counter. Until this method
7070
// is called the underlying counter is a no-op.
71-
func (c *kubeCounter) initializeDeprecatedMetric() {
71+
func (c *Counter) initializeDeprecatedMetric() {
7272
c.CounterOpts.markDeprecated()
7373
c.initializeMetric()
7474
}
7575

76-
// kubeCounterVec is the internal representation of our wrapping struct around prometheus
77-
// counterVecs. kubeCounterVec implements both KubeCollector and KubeCounterVec.
78-
type kubeCounterVec struct {
76+
// CounterVec is the internal representation of our wrapping struct around prometheus
77+
// counterVecs. CounterVec implements both KubeCollector and CounterVecMetric.
78+
type CounterVec struct {
7979
*prometheus.CounterVec
8080
*CounterOpts
8181
lazyMetric
8282
originalLabels []string
8383
}
8484

85-
// NewCounterVec returns an object which satisfies the KubeCollector and KubeCounterVec interfaces.
85+
// NewCounterVec returns an object which satisfies the KubeCollector and CounterVecMetric interfaces.
8686
// However, the object returned will not measure anything unless the collector is first
8787
// registered, since the metric is lazily instantiated.
88-
func NewCounterVec(opts *CounterOpts, labels []string) *kubeCounterVec {
89-
cv := &kubeCounterVec{
88+
func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
89+
cv := &CounterVec{
9090
CounterVec: noopCounterVec,
9191
CounterOpts: opts,
9292
originalLabels: labels,
@@ -97,19 +97,19 @@ func NewCounterVec(opts *CounterOpts, labels []string) *kubeCounterVec {
9797
}
9898

9999
// DeprecatedVersion returns a pointer to the Version or nil
100-
func (v *kubeCounterVec) DeprecatedVersion() *semver.Version {
100+
func (v *CounterVec) DeprecatedVersion() *semver.Version {
101101
return v.CounterOpts.DeprecatedVersion
102102
}
103103

104104
// initializeMetric invocation creates the actual underlying CounterVec. Until this method is called
105105
// the underlying counterVec is a no-op.
106-
func (v *kubeCounterVec) initializeMetric() {
106+
func (v *CounterVec) initializeMetric() {
107107
v.CounterVec = prometheus.NewCounterVec(v.CounterOpts.toPromCounterOpts(), v.originalLabels)
108108
}
109109

110110
// initializeDeprecatedMetric invocation creates the actual (but deprecated) CounterVec. Until this method is called
111111
// the underlying counterVec is a no-op.
112-
func (v *kubeCounterVec) initializeDeprecatedMetric() {
112+
func (v *CounterVec) initializeDeprecatedMetric() {
113113
v.CounterOpts.markDeprecated()
114114
v.initializeMetric()
115115
}
@@ -121,17 +121,23 @@ func (v *kubeCounterVec) initializeDeprecatedMetric() {
121121
// for perpetuity (i.e. throughout application lifecycle).
122122
//
123123
// For reference: https://github.com/prometheus/client_golang/blob/v0.9.2/prometheus/counter.go#L179-L197
124-
//
125-
// This method returns a no-op metric if the metric is not actually created/registered, avoiding that
126-
// memory leak.
127-
func (v *kubeCounterVec) WithLabelValues(lvs ...string) KubeCounter {
124+
125+
// WithLabelValues returns the Counter for the given slice of label
126+
// values (same order as the VariableLabels in Desc). If that combination of
127+
// label values is accessed for the first time, a new Counter is created IFF the counterVec
128+
// has been registered to a metrics registry.
129+
func (v *CounterVec) WithLabelValues(lvs ...string) CounterMetric {
128130
if !v.IsCreated() {
129131
return noop // return no-op counter
130132
}
131133
return v.CounterVec.WithLabelValues(lvs...)
132134
}
133135

134-
func (v *kubeCounterVec) With(labels prometheus.Labels) KubeCounter {
136+
// With returns the Counter for the given Labels map (the label names
137+
// must match those of the VariableLabels in Desc). If that label map is
138+
// accessed for the first time, a new Counter is created IFF the counterVec has
139+
// been registered to a metrics registry.
140+
func (v *CounterVec) With(labels prometheus.Labels) CounterMetric {
135141
if !v.IsCreated() {
136142
return noop // return no-op counter
137143
}

pkg/util/metrics/framework/metric.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
/*
28-
This extends the prometheus.Collector interface to allow customization of the metric
28+
KubeCollector extends the prometheus.Collector interface to allow customization of the metric
2929
registration process. Defer metric initialization until Create() is called, which then
3030
delegates to the underlying metric's initializeMetric or initializeDeprecatedMetric
3131
method call depending on whether the metric is deprecated or not.

pkg/util/metrics/framework/opts.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ type KubeOpts struct {
4141
StabilityLevel StabilityLevel
4242
}
4343

44+
// StabilityLevel represents the API guarantees for a given defined metric.
4445
type StabilityLevel string
4546

4647
const (
47-
ALPHA StabilityLevel = "ALPHA"
48+
// ALPHA metrics have no stability guarantees, as such, labels may
49+
// be arbitrarily added/removed and the metric may be deleted at any time.
50+
ALPHA StabilityLevel = "ALPHA"
51+
// STABLE metrics are guaranteed not be mutated and removal is governed by
52+
// the deprecation policy outlined in by the control plane metrics stability KEP.
4853
STABLE StabilityLevel = "STABLE"
4954
)
5055

56+
// CounterOpts is an alias for Opts. See there for doc comments.
5157
type CounterOpts KubeOpts
5258

5359
// Modify help description on the metric description.

pkg/util/metrics/framework/registry.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ import (
2424
"k8s.io/kubernetes/pkg/version"
2525
)
2626

27+
// DefaultGlobalRegistry is a stub for the global registry which prometheus client
28+
// currently uses.
2729
var DefaultGlobalRegistry = NewKubeRegistry()
2830

31+
// KubeRegistry is a wrapper around a prometheus registry-type object. Upon initialization
32+
// the kubernetes binary version information is loaded into the registry object, so that
33+
// automatic behavior can be configured for metric versioning.
2934
type KubeRegistry struct {
3035
PromRegistry
3136
version semver.Version
@@ -43,13 +48,21 @@ func MustRegister(cs ...KubeCollector) {
4348
DefaultGlobalRegistry.MustRegister(cs...)
4449
}
4550

51+
// Register registers a new Collector to be included in metrics
52+
// collection. It returns an error if the descriptors provided by the
53+
// Collector are invalid or if they — in combination with descriptors of
54+
// already registered Collectors — do not fulfill the consistency and
55+
// uniqueness criteria described in the documentation of metric.Desc.
4656
func (kr *KubeRegistry) Register(c KubeCollector) error {
4757
if c.Create(&kr.version) {
4858
return kr.PromRegistry.Register(c)
4959
}
5060
return nil
5161
}
5262

63+
// MustRegister works like Register but registers any number of
64+
// Collectors and panics upon the first registration that causes an
65+
// error.
5366
func (kr *KubeRegistry) MustRegister(cs ...KubeCollector) {
5467
metrics := make([]prometheus.Collector, 0, len(cs))
5568
for _, c := range cs {
@@ -60,14 +73,29 @@ func (kr *KubeRegistry) MustRegister(cs ...KubeCollector) {
6073
kr.PromRegistry.MustRegister(metrics...)
6174
}
6275

76+
// Unregister unregisters the Collector that equals the Collector passed
77+
// in as an argument. (Two Collectors are considered equal if their
78+
// Describe method yields the same set of descriptors.) The function
79+
// returns whether a Collector was unregistered. Note that an unchecked
80+
// Collector cannot be unregistered (as its Describe method does not
81+
// yield any descriptor).
6382
func (kr *KubeRegistry) Unregister(collector KubeCollector) bool {
6483
return kr.PromRegistry.Unregister(collector)
6584
}
6685

86+
// Gather calls the Collect method of the registered Collectors and then
87+
// gathers the collected metrics into a lexicographically sorted slice
88+
// of uniquely named MetricFamily protobufs. Gather ensures that the
89+
// returned slice is valid and self-consistent so that it can be used
90+
// for valid exposition. As an exception to the strict consistency
91+
// requirements described for metric.Desc, Gather will tolerate
92+
// different sets of label names for metrics of the same metric family.
6793
func (kr *KubeRegistry) Gather() ([]*dto.MetricFamily, error) {
6894
return kr.PromRegistry.Gather()
6995
}
7096

97+
// NewKubeRegistry creates a new kubernetes metric registry, loading in the kubernetes
98+
// version information available to the binary.
7199
func NewKubeRegistry() *KubeRegistry {
72100
v, err := parseVersion(version.Get())
73101
if err != nil {

pkg/util/metrics/framework/registry_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var (
6969
func TestRegister(t *testing.T) {
7070
var tests = []struct {
7171
desc string
72-
metrics []*kubeCounter
72+
metrics []*Counter
7373
registryVersion *semver.Version
7474
expectedErrors []error
7575
expectedIsCreatedValues []bool
@@ -78,7 +78,7 @@ func TestRegister(t *testing.T) {
7878
}{
7979
{
8080
desc: "test alpha metric",
81-
metrics: []*kubeCounter{alphaCounter},
81+
metrics: []*Counter{alphaCounter},
8282
registryVersion: &v115,
8383
expectedErrors: []error{nil},
8484
expectedIsCreatedValues: []bool{true},
@@ -87,7 +87,7 @@ func TestRegister(t *testing.T) {
8787
},
8888
{
8989
desc: "test registering same metric multiple times",
90-
metrics: []*kubeCounter{alphaCounter, alphaCounter},
90+
metrics: []*Counter{alphaCounter, alphaCounter},
9191
registryVersion: &v115,
9292
expectedErrors: []error{nil, prometheus.AlreadyRegisteredError{}},
9393
expectedIsCreatedValues: []bool{true, true},
@@ -96,7 +96,7 @@ func TestRegister(t *testing.T) {
9696
},
9797
{
9898
desc: "test alpha deprecated metric",
99-
metrics: []*kubeCounter{alphaDeprecatedCounter},
99+
metrics: []*Counter{alphaDeprecatedCounter},
100100
registryVersion: &v115,
101101
expectedErrors: []error{nil, prometheus.AlreadyRegisteredError{}},
102102
expectedIsCreatedValues: []bool{true},
@@ -105,7 +105,7 @@ func TestRegister(t *testing.T) {
105105
},
106106
{
107107
desc: "test alpha hidden metric",
108-
metrics: []*kubeCounter{alphaHiddenCounter},
108+
metrics: []*Counter{alphaHiddenCounter},
109109
registryVersion: &v115,
110110
expectedErrors: []error{nil, prometheus.AlreadyRegisteredError{}},
111111
expectedIsCreatedValues: []bool{false},
@@ -139,43 +139,43 @@ func TestRegister(t *testing.T) {
139139
func TestMustRegister(t *testing.T) {
140140
var tests = []struct {
141141
desc string
142-
metrics []*kubeCounter
142+
metrics []*Counter
143143
registryVersion *semver.Version
144144
expectedPanics []bool
145145
}{
146146
{
147147
desc: "test alpha metric",
148-
metrics: []*kubeCounter{alphaCounter},
148+
metrics: []*Counter{alphaCounter},
149149
registryVersion: &v115,
150150
expectedPanics: []bool{false},
151151
},
152152
{
153153
desc: "test registering same metric multiple times",
154-
metrics: []*kubeCounter{alphaCounter, alphaCounter},
154+
metrics: []*Counter{alphaCounter, alphaCounter},
155155
registryVersion: &v115,
156156
expectedPanics: []bool{false, true},
157157
},
158158
{
159159
desc: "test alpha deprecated metric",
160-
metrics: []*kubeCounter{alphaDeprecatedCounter},
160+
metrics: []*Counter{alphaDeprecatedCounter},
161161
registryVersion: &v115,
162162
expectedPanics: []bool{false},
163163
},
164164
{
165165
desc: "test must registering same deprecated metric",
166-
metrics: []*kubeCounter{alphaDeprecatedCounter, alphaDeprecatedCounter},
166+
metrics: []*Counter{alphaDeprecatedCounter, alphaDeprecatedCounter},
167167
registryVersion: &v115,
168168
expectedPanics: []bool{false, true},
169169
},
170170
{
171171
desc: "test alpha hidden metric",
172-
metrics: []*kubeCounter{alphaHiddenCounter},
172+
metrics: []*Counter{alphaHiddenCounter},
173173
registryVersion: &v115,
174174
expectedPanics: []bool{false},
175175
},
176176
{
177177
desc: "test must registering same hidden metric",
178-
metrics: []*kubeCounter{alphaHiddenCounter, alphaHiddenCounter},
178+
metrics: []*Counter{alphaHiddenCounter, alphaHiddenCounter},
179179
registryVersion: &v115,
180180
expectedPanics: []bool{false, false}, // hidden metrics no-opt
181181
},

pkg/util/metrics/framework/wrappers.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,37 @@ import (
2727
// so that we can prevent breakage if methods are ever added to prometheus
2828
// variants of them.
2929

30-
/**
31-
* Collector defines a subset of prometheus.Collector interface methods
32-
*/
30+
// Collector defines a subset of prometheus.Collector interface methods
3331
type Collector interface {
3432
Describe(chan<- *prometheus.Desc)
3533
Collect(chan<- prometheus.Metric)
3634
}
3735

38-
/**
39-
* Metric defines a subset of prometheus.Metric interface methods
40-
*/
36+
// Metric defines a subset of prometheus.Metric interface methods
4137
type Metric interface {
4238
Desc() *prometheus.Desc
4339
Write(*dto.Metric) error
4440
}
4541

46-
// Counter is a Metric that represents a single numerical value that only ever
42+
// CounterMetric is a Metric that represents a single numerical value that only ever
4743
// goes up. That implies that it cannot be used to count items whose number can
4844
// also go down, e.g. the number of currently running goroutines. Those
4945
// "counters" are represented by Gauges.
50-
//
51-
// This interface defines a subset of the interface provided by prometheus.Counter
52-
type KubeCounter interface {
46+
47+
// CounterMetric is an interface which defines a subset of the interface provided by prometheus.Counter
48+
type CounterMetric interface {
5349
Inc()
5450
Add(float64)
5551
}
5652

57-
type KubeCounterVec interface {
58-
WithLabelValues(...string) KubeCounter
59-
With(prometheus.Labels) KubeCounter
53+
// CounterVecMetric is an interface which prometheus.CounterVec satisfies.
54+
type CounterVecMetric interface {
55+
WithLabelValues(...string) CounterMetric
56+
With(prometheus.Labels) CounterMetric
6057
}
6158

59+
// PromRegistry is an interface which implements a subset of prometheus.Registerer and
60+
// prometheus.Gatherer interfaces
6261
type PromRegistry interface {
6362
Register(prometheus.Collector) error
6463
MustRegister(...prometheus.Collector)

0 commit comments

Comments
 (0)