More revisions after review

This commit is contained in:
Mike Spreitzer 2022-05-12 10:27:58 -04:00
parent c87cd36d3e
commit d23254b7ea
4 changed files with 37 additions and 31 deletions

View File

@ -141,10 +141,10 @@ func (v *GaugeVec) initializeDeprecatedMetric() {
} }
func (v *GaugeVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, error) { func (v *GaugeVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, error) {
if !v.IsCreated() {
if v.IsHidden() { if v.IsHidden() {
return noop, nil return noop, nil
} }
if !v.IsCreated() {
return noop, errNotRegistered // return no-op gauge return noop, errNotRegistered // return no-op gauge
} }
if v.LabelValueAllowLists != nil { if v.LabelValueAllowLists != nil {
@ -178,10 +178,10 @@ func (v *GaugeVec) WithLabelValues(lvs ...string) GaugeMetric {
} }
func (v *GaugeVec) WithChecked(labels map[string]string) (GaugeMetric, error) { func (v *GaugeVec) WithChecked(labels map[string]string) (GaugeMetric, error) {
if !v.IsCreated() {
if v.IsHidden() { if v.IsHidden() {
return noop, nil return noop, nil
} }
if !v.IsCreated() {
return noop, errNotRegistered // return no-op gauge return noop, errNotRegistered // return no-op gauge
} }
if v.LabelValueAllowLists != nil { if v.LabelValueAllowLists != nil {

View File

@ -24,10 +24,16 @@ import (
promext "k8s.io/component-base/metrics/prometheusextension" promext "k8s.io/component-base/metrics/prometheusextension"
) )
// TimingHistogram is our internal representation for our wrapping struct around timing // PrometheusTimingHistogram is the abstraction of the underlying histogram
// histograms. It implements both kubeCollector and GaugeMetric // that we want to promote from the wrapper.
type TimingHistogram struct { type PrometheusTimingHistogram interface {
GaugeMetric GaugeMetric
}
// TimingHistogram is our internal representation for our wrapping struct around
// timing histograms. It implements both kubeCollector and GaugeMetric
type TimingHistogram struct {
PrometheusTimingHistogram
*TimingHistogramOpts *TimingHistogramOpts
nowFunc func() time.Time nowFunc func() time.Time
lazyMetric lazyMetric
@ -60,7 +66,7 @@ func NewTestableTimingHistogram(nowFunc func() time.Time, opts *TimingHistogramO
// setPrometheusHistogram sets the underlying KubeGauge object, i.e. the thing that does the measurement. // setPrometheusHistogram sets the underlying KubeGauge object, i.e. the thing that does the measurement.
func (h *TimingHistogram) setPrometheusHistogram(histogram promext.TimingHistogram) { func (h *TimingHistogram) setPrometheusHistogram(histogram promext.TimingHistogram) {
h.GaugeMetric = histogram h.PrometheusTimingHistogram = histogram
h.initSelfCollection(histogram) h.initSelfCollection(histogram)
} }
@ -90,7 +96,7 @@ func (h *TimingHistogram) initializeDeprecatedMetric() {
// WithContext allows the normal TimingHistogram metric to pass in context. The context is no-op now. // WithContext allows the normal TimingHistogram metric to pass in context. The context is no-op now.
func (h *TimingHistogram) WithContext(ctx context.Context) GaugeMetric { func (h *TimingHistogram) WithContext(ctx context.Context) GaugeMetric {
return h.GaugeMetric return h.PrometheusTimingHistogram
} }
// TimingHistogramVec is the internal representation of our wrapping struct around prometheus // TimingHistogramVec is the internal representation of our wrapping struct around prometheus
@ -151,20 +157,20 @@ func (v *TimingHistogramVec) initializeDeprecatedMetric() {
v.initializeMetric() v.initializeMetric()
} }
// WithLabelValuesChecked, if called on a hidden vector, // WithLabelValuesChecked, if called before this vector has been registered in
// will return a noop gauge and a nil error.
// If called before this vector has been registered in
// at least one registry, will return a noop gauge and // at least one registry, will return a noop gauge and
// an error that passes ErrIsNotRegistered. // an error that passes ErrIsNotRegistered.
// If called on a hidden vector,
// will return a noop gauge and a nil error.
// If called with a syntactic problem in the labels, will // If called with a syntactic problem in the labels, will
// return a noop gauge and an error about the labels. // return a noop gauge and an error about the labels.
// If none of the above apply, this method will return // If none of the above apply, this method will return
// the appropriate vector member and a nil error. // the appropriate vector member and a nil error.
func (v *TimingHistogramVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, error) { func (v *TimingHistogramVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, error) {
if !v.IsCreated() {
if v.IsHidden() { if v.IsHidden() {
return noop, nil return noop, nil
} }
if !v.IsCreated() {
return noop, errNotRegistered return noop, errNotRegistered
} }
if v.LabelValueAllowLists != nil { if v.LabelValueAllowLists != nil {
@ -187,20 +193,20 @@ func (v *TimingHistogramVec) WithLabelValues(lvs ...string) GaugeMetric {
panic(err) panic(err)
} }
// WithChecked, if called on a hidden vector, // WithChecked, if called before this vector has been registered in
// will return a noop gauge and a nil error.
// If called before this vector has been registered in
// at least one registry, will return a noop gauge and // at least one registry, will return a noop gauge and
// an error that passes ErrIsNotRegistered. // an error that passes ErrIsNotRegistered.
// If called on a hidden vector,
// will return a noop gauge and a nil error.
// If called with a syntactic problem in the labels, will // If called with a syntactic problem in the labels, will
// return a noop gauge and an error about the labels. // return a noop gauge and an error about the labels.
// If none of the above apply, this method will return // If none of the above apply, this method will return
// the appropriate vector member and a nil error. // the appropriate vector member and a nil error.
func (v *TimingHistogramVec) WithChecked(labels map[string]string) (GaugeMetric, error) { func (v *TimingHistogramVec) WithChecked(labels map[string]string) (GaugeMetric, error) {
if !v.IsCreated() {
if v.IsHidden() { if v.IsHidden() {
return noop, nil return noop, nil
} }
if !v.IsCreated() {
return noop, errNotRegistered return noop, errNotRegistered
} }
if v.LabelValueAllowLists != nil { if v.LabelValueAllowLists != nil {

View File

@ -102,8 +102,8 @@ func TestTimingHistogram(t *testing.T) {
}() }()
m1 := <-metricChan m1 := <-metricChan
gm1, ok := m1.(GaugeMetric) gm1, ok := m1.(GaugeMetric)
if !ok || gm1 != c.GaugeMetric { if !ok || gm1 != c.PrometheusTimingHistogram {
t.Error("Unexpected metric", m1, c.GaugeMetric) t.Error("Unexpected metric", m1, c.PrometheusTimingHistogram)
} }
m2, ok := <-metricChan m2, ok := <-metricChan
if ok { if ok {

View File

@ -80,11 +80,11 @@ type GaugeVecMetric interface {
// In contrast, the Vec behavior in this package is that member extraction before registration // In contrast, the Vec behavior in this package is that member extraction before registration
// returns a permanent noop object. // returns a permanent noop object.
// WithLabelValuesChecked, if called on a hidden vector, // WithLabelValuesChecked, if called before this vector has been registered in
// will return a noop gauge and a nil error.
// If called before this vector has been registered in
// at least one registry, will return a noop gauge and // at least one registry, will return a noop gauge and
// an error that passes ErrIsNotRegistered. // an error that passes ErrIsNotRegistered.
// If called on a hidden vector,
// will return a noop gauge and a nil error.
// If called with a syntactic problem in the labels, will // If called with a syntactic problem in the labels, will
// return a noop gauge and an error about the labels. // return a noop gauge and an error about the labels.
// If none of the above apply, this method will return // If none of the above apply, this method will return
@ -98,11 +98,11 @@ type GaugeVecMetric interface {
// all other errors cause a panic. // all other errors cause a panic.
WithLabelValues(labelValues ...string) GaugeMetric WithLabelValues(labelValues ...string) GaugeMetric
// WithChecked, if called on a hidden vector, // WithChecked, if called before this vector has been registered in
// will return a noop gauge and a nil error.
// If called before this vector has been registered in
// at least one registry, will return a noop gauge and // at least one registry, will return a noop gauge and
// an error that passes ErrIsNotRegistered. // an error that passes ErrIsNotRegistered.
// If called on a hidden vector,
// will return a noop gauge and a nil error.
// If called with a syntactic problem in the labels, will // If called with a syntactic problem in the labels, will
// return a noop gauge and an error about the labels. // return a noop gauge and an error about the labels.
// If none of the above apply, this method will return // If none of the above apply, this method will return