add interfaces to record metric with context.

This commit is contained in:
yoyinzyc 2021-01-05 15:51:35 -08:00
parent 8b24d1f2f9
commit b561127e50
4 changed files with 120 additions and 0 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package metrics
import (
"context"
"github.com/blang/semver"
"github.com/prometheus/client_golang/prometheus"
)
@ -79,6 +80,11 @@ func (c *Counter) initializeDeprecatedMetric() {
c.initializeMetric()
}
// WithContext allows the normal Counter metric to pass in context. The context is no-op now.
func (c *Counter) WithContext(ctx context.Context) CounterMetric {
return c.CounterMetric
}
// CounterVec is the internal representation of our wrapping struct around prometheus
// counterVecs. CounterVec implements both kubeCollector and CounterVecMetric.
type CounterVec struct {
@ -176,3 +182,27 @@ func (v *CounterVec) Reset() {
v.CounterVec.Reset()
}
// WithContext returns wrapped CounterVec with context
func (v *CounterVec) WithContext(ctx context.Context) *CounterVecWithContext {
return &CounterVecWithContext{
ctx: ctx,
CounterVec: *v,
}
}
// CounterVecWithContext is the wrapper of CounterVec with context.
type CounterVecWithContext struct {
CounterVec
ctx context.Context
}
// WithLabelValues is the wrapper of CounterVec.WithLabelValues.
func (vc *CounterVecWithContext) WithLabelValues(lvs ...string) CounterMetric {
return vc.CounterVec.WithLabelValues(lvs...)
}
// With is the wrapper of CounterVec.With.
func (vc *CounterVecWithContext) With(labels map[string]string) CounterMetric {
return vc.CounterVec.With(labels)
}

View File

@ -17,6 +17,7 @@ limitations under the License.
package metrics
import (
"context"
"github.com/blang/semver"
"github.com/prometheus/client_golang/prometheus"
@ -73,6 +74,11 @@ func (g *Gauge) initializeDeprecatedMetric() {
g.initializeMetric()
}
// WithContext allows the normal Gauge metric to pass in context. The context is no-op now.
func (g *Gauge) WithContext(ctx context.Context) GaugeMetric {
return g.GaugeMetric
}
// GaugeVec is the internal representation of our wrapping struct around prometheus
// gaugeVecs. kubeGaugeVec implements both kubeCollector and KubeGaugeVec.
type GaugeVec struct {
@ -191,3 +197,27 @@ func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc {
return newGaugeFunc(opts, function, v)
}
// WithContext returns wrapped GaugeVec with context
func (v *GaugeVec) WithContext(ctx context.Context) *GaugeVecWithContext {
return &GaugeVecWithContext{
ctx: ctx,
GaugeVec: *v,
}
}
// GaugeVecWithContext is the wrapper of GaugeVec with context.
type GaugeVecWithContext struct {
GaugeVec
ctx context.Context
}
// WithLabelValues is the wrapper of GaugeVec.WithLabelValues.
func (vc *GaugeVecWithContext) WithLabelValues(lvs ...string) GaugeMetric {
return vc.GaugeVec.WithLabelValues(lvs...)
}
// With is the wrapper of GaugeVec.With.
func (vc *GaugeVecWithContext) With(labels map[string]string) GaugeMetric {
return vc.GaugeVec.With(labels)
}

View File

@ -17,6 +17,7 @@ limitations under the License.
package metrics
import (
"context"
"github.com/blang/semver"
"github.com/prometheus/client_golang/prometheus"
)
@ -83,6 +84,11 @@ func (h *Histogram) initializeDeprecatedMetric() {
h.initializeMetric()
}
// WithContext allows the normal Histogram metric to pass in context. The context is no-op now.
func (h *Histogram) WithContext(ctx context.Context) ObserverMetric {
return h.ObserverMetric
}
// HistogramVec is the internal representation of our wrapping struct around prometheus
// histogramVecs.
type HistogramVec struct {
@ -175,3 +181,27 @@ func (v *HistogramVec) Reset() {
v.HistogramVec.Reset()
}
// WithContext returns wrapped HistogramVec with context
func (v *HistogramVec) WithContext(ctx context.Context) *HistogramVecWithContext {
return &HistogramVecWithContext{
ctx: ctx,
HistogramVec: *v,
}
}
// HistogramVecWithContext is the wrapper of HistogramVec with context.
type HistogramVecWithContext struct {
HistogramVec
ctx context.Context
}
// WithLabelValues is the wrapper of HistogramVec.WithLabelValues.
func (vc *HistogramVecWithContext) WithLabelValues(lvs ...string) ObserverMetric {
return vc.HistogramVec.WithLabelValues(lvs...)
}
// With is the wrapper of HistogramVec.With.
func (vc *HistogramVecWithContext) With(labels map[string]string) ObserverMetric {
return vc.HistogramVec.With(labels)
}

View File

@ -17,6 +17,7 @@ limitations under the License.
package metrics
import (
"context"
"github.com/blang/semver"
"github.com/prometheus/client_golang/prometheus"
)
@ -74,6 +75,11 @@ func (s *Summary) initializeDeprecatedMetric() {
s.initializeMetric()
}
// WithContext allows the normal Summary metric to pass in context. The context is no-op now.
func (s *Summary) WithContext(ctx context.Context) ObserverMetric {
return s.ObserverMetric
}
// SummaryVec is the internal representation of our wrapping struct around prometheus
// summaryVecs.
//
@ -169,3 +175,27 @@ func (v *SummaryVec) Reset() {
v.SummaryVec.Reset()
}
// WithContext returns wrapped SummaryVec with context
func (v *SummaryVec) WithContext(ctx context.Context) *SummaryVecWithContext {
return &SummaryVecWithContext{
ctx: ctx,
SummaryVec: *v,
}
}
// SummaryVecWithContext is the wrapper of SummaryVec with context.
type SummaryVecWithContext struct {
SummaryVec
ctx context.Context
}
// WithLabelValues is the wrapper of SummaryVec.WithLabelValues.
func (vc *SummaryVecWithContext) WithLabelValues(lvs ...string) ObserverMetric {
return vc.SummaryVec.WithLabelValues(lvs...)
}
// With is the wrapper of SummaryVec.With.
func (vc *SummaryVecWithContext) With(labels map[string]string) ObserverMetric {
return vc.SummaryVec.With(labels)
}