Skip to content

Commit

Permalink
k8s.io/component-base: support GetCounterMetricValue for Counter
Browse files Browse the repository at this point in the history
GetCounterMetricValue has an unchecked conversion to metrics.Metric,
something that didn't work for a *Counter because it didn't implement
that interface.
  • Loading branch information
pohly committed Mar 2, 2021
1 parent 6cb28fd commit ef6f65b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions staging/src/k8s.io/component-base/metrics/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"github.com/blang/semver"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)

// Counter is our internal representation for our wrapping struct around prometheus
Expand All @@ -31,6 +32,9 @@ type Counter struct {
selfCollector
}

// The implementation of the Metric interface is expected by testutil.GetCounterMetricValue.
var _ Metric = &Counter{}

// 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.
Expand All @@ -46,6 +50,14 @@ func NewCounter(opts *CounterOpts) *Counter {
return kc
}

func (c *Counter) Desc() *prometheus.Desc {
return c.metric.Desc()
}

func (c *Counter) Write(to *dto.Metric) error {
return c.metric.Write(to)
}

// Reset resets the underlying prometheus Counter to start counting from 0 again
func (c *Counter) Reset() {
if !c.IsCreated() {
Expand Down

0 comments on commit ef6f65b

Please sign in to comment.