Merge pull request #82957 from RainbowMango/pr_handle_stability_level_default_better

Handle stability level default better
This commit is contained in:
Kubernetes Prow Robot 2019-09-25 19:43:07 -07:00 committed by GitHub
commit fb1db2eba7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 88 additions and 32 deletions

View File

@ -42,6 +42,7 @@ go_test(
"counter_test.go", "counter_test.go",
"gauge_test.go", "gauge_test.go",
"histogram_test.go", "histogram_test.go",
"opts_test.go",
"registry_test.go", "registry_test.go",
"summary_test.go", "summary_test.go",
"version_parser_test.go", "version_parser_test.go",

View File

@ -34,10 +34,8 @@ type Counter struct {
// However, the object returned will not measure anything unless the collector is first // However, the object returned will not measure anything unless the collector is first
// registered, since the metric is lazily instantiated. // registered, since the metric is lazily instantiated.
func NewCounter(opts *CounterOpts) *Counter { func NewCounter(opts *CounterOpts) *Counter {
// todo: handle defaulting better opts.StabilityLevel.setDefaults()
if opts.StabilityLevel == "" {
opts.StabilityLevel = ALPHA
}
kc := &Counter{ kc := &Counter{
CounterOpts: opts, CounterOpts: opts,
lazyMetric: lazyMetric{}, lazyMetric: lazyMetric{},
@ -86,10 +84,8 @@ type CounterVec struct {
// However, the object returned will not measure anything unless the collector is first // However, the object returned will not measure anything unless the collector is first
// registered, since the metric is lazily instantiated. // registered, since the metric is lazily instantiated.
func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec { func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
// todo: handle defaulting better opts.StabilityLevel.setDefaults()
if opts.StabilityLevel == "" {
opts.StabilityLevel = ALPHA
}
cv := &CounterVec{ cv := &CounterVec{
CounterVec: noopCounterVec, CounterVec: noopCounterVec,
CounterOpts: opts, CounterOpts: opts,

View File

@ -34,10 +34,8 @@ type Gauge struct {
// However, the object returned will not measure anything unless the collector is first // However, the object returned will not measure anything unless the collector is first
// registered, since the metric is lazily instantiated. // registered, since the metric is lazily instantiated.
func NewGauge(opts *GaugeOpts) *Gauge { func NewGauge(opts *GaugeOpts) *Gauge {
// todo: handle defaulting better opts.StabilityLevel.setDefaults()
if opts.StabilityLevel == "" {
opts.StabilityLevel = ALPHA
}
kc := &Gauge{ kc := &Gauge{
GaugeOpts: opts, GaugeOpts: opts,
lazyMetric: lazyMetric{}, lazyMetric: lazyMetric{},
@ -86,10 +84,8 @@ type GaugeVec struct {
// However, the object returned will not measure anything unless the collector is first // However, the object returned will not measure anything unless the collector is first
// registered, since the metric is lazily instantiated. // registered, since the metric is lazily instantiated.
func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec { func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec {
// todo: handle defaulting better opts.StabilityLevel.setDefaults()
if opts.StabilityLevel == "" {
opts.StabilityLevel = ALPHA
}
cv := &GaugeVec{ cv := &GaugeVec{
GaugeVec: noopGaugeVec, GaugeVec: noopGaugeVec,
GaugeOpts: opts, GaugeOpts: opts,

View File

@ -46,10 +46,8 @@ type Histogram struct {
// NewHistogram returns an object which is Histogram-like. However, nothing // NewHistogram returns an object which is Histogram-like. However, nothing
// will be measured until the histogram is registered somewhere. // will be measured until the histogram is registered somewhere.
func NewHistogram(opts *HistogramOpts) *Histogram { func NewHistogram(opts *HistogramOpts) *Histogram {
// todo: handle defaulting better opts.StabilityLevel.setDefaults()
if opts.StabilityLevel == "" {
opts.StabilityLevel = ALPHA
}
h := &Histogram{ h := &Histogram{
HistogramOpts: opts, HistogramOpts: opts,
lazyMetric: lazyMetric{}, lazyMetric: lazyMetric{},
@ -98,10 +96,8 @@ type HistogramVec struct {
// prometheus.HistogramVec object. However, the object returned will not measure // prometheus.HistogramVec object. However, the object returned will not measure
// anything unless the collector is first registered, since the metric is lazily instantiated. // anything unless the collector is first registered, since the metric is lazily instantiated.
func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec { func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec {
// todo: handle defaulting better opts.StabilityLevel.setDefaults()
if opts.StabilityLevel == "" {
opts.StabilityLevel = ALPHA
}
v := &HistogramVec{ v := &HistogramVec{
HistogramVec: noopHistogramVec, HistogramVec: noopHistogramVec,
HistogramOpts: opts, HistogramOpts: opts,

View File

@ -53,6 +53,16 @@ const (
STABLE StabilityLevel = "STABLE" 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. // CounterOpts is an alias for Opts. See there for doc comments.
type CounterOpts KubeOpts type CounterOpts KubeOpts

View File

@ -0,0 +1,61 @@
/*
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 (
"testing"
)
func TestDefaultStabilityLevel(t *testing.T) {
var tests = []struct {
name string
inputValue StabilityLevel
expectValue StabilityLevel
expectPanic bool
}{
{
name: "empty should take ALPHA by default",
inputValue: "",
expectValue: ALPHA,
expectPanic: false,
},
{
name: "ALPHA remain unchanged",
inputValue: ALPHA,
expectValue: ALPHA,
expectPanic: false,
},
{
name: "STABLE remain unchanged",
inputValue: STABLE,
expectValue: STABLE,
expectPanic: false,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
var stability = tc.inputValue
stability.setDefaults()
if stability != tc.expectValue {
t.Errorf("Got %s, expected: %v ", stability, tc.expectValue)
}
})
}
}

View File

@ -37,10 +37,8 @@ type Summary struct {
// //
// DEPRECATED: as per the metrics overhaul KEP // DEPRECATED: as per the metrics overhaul KEP
func NewSummary(opts *SummaryOpts) *Summary { func NewSummary(opts *SummaryOpts) *Summary {
// todo: handle defaulting better opts.StabilityLevel.setDefaults()
if opts.StabilityLevel == "" {
opts.StabilityLevel = ALPHA
}
s := &Summary{ s := &Summary{
SummaryOpts: opts, SummaryOpts: opts,
lazyMetric: lazyMetric{}, lazyMetric: lazyMetric{},
@ -93,10 +91,8 @@ type SummaryVec struct {
// //
// DEPRECATED: as per the metrics overhaul KEP // DEPRECATED: as per the metrics overhaul KEP
func NewSummaryVec(opts *SummaryOpts, labels []string) *SummaryVec { func NewSummaryVec(opts *SummaryOpts, labels []string) *SummaryVec {
// todo: handle defaulting better opts.StabilityLevel.setDefaults()
if opts.StabilityLevel == "" {
opts.StabilityLevel = ALPHA
}
v := &SummaryVec{ v := &SummaryVec{
SummaryOpts: opts, SummaryOpts: opts,
originalLabels: labels, originalLabels: labels,