Skip to content

Commit

Permalink
Merge pull request #65 from getamis/counter-vec
Browse files Browse the repository at this point in the history
metrics: support counter vec
  • Loading branch information
markya0616 authored Jan 24, 2019
2 parents fb5cc6a + d67a2f7 commit 90abfb0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
19 changes: 13 additions & 6 deletions metrics/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (d *DummyRegistry) NewHistogramVec(key string, labels []string, opts ...Opt
return &dummyHistogramVec{}
}

func (d *DummyRegistry) NewCounterVec(key string, labels []string, opts ...Option) CounterVec {
return &dummyCounterVec{}
}

func (d *DummyRegistry) NewTimer(key string, opts ...Option) Timer {
return &dummyTimer{}
}
Expand Down Expand Up @@ -95,17 +99,20 @@ func (d *dummyHistogram) Observe(float64) {}

type dummyHistogramVec struct{}

func (d *dummyHistogramVec) GetMetricWith(HistogramLabels) (HistogramObserver, error) {
func (d *dummyHistogramVec) GetMetricWith(MetricsLabels) (Histogram, error) {
return &dummyHistogram{}, nil
}
func (d *dummyHistogramVec) GetMetricWithLabelValues(lvs ...string) (HistogramObserver, error) {
func (d *dummyHistogramVec) GetMetricWithLabelValues(lvs ...string) (Histogram, error) {
return &dummyHistogram{}, nil
}
func (d *dummyHistogramVec) With(HistogramLabels) HistogramObserver {
return &dummyHistogram{}

type dummyCounterVec struct{}

func (d *dummyCounterVec) GetMetricWith(MetricsLabels) (Counter, error) {
return &dummyCounter{}, nil
}
func (d *dummyHistogramVec) WithLabelValues(...string) HistogramObserver {
return &dummyHistogram{}
func (d *dummyCounterVec) GetMetricWithLabelValues(lvs ...string) (Counter, error) {
return &dummyCounter{}, nil
}

type dummyTimer struct{}
Expand Down
15 changes: 10 additions & 5 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ type Counter interface {
Add(float64)
}

type MetricsLabels = prom.Labels

// CounterVec is returned by NewCounterVec.
type CounterVec interface {
GetMetricWith(MetricsLabels) (Counter, error)
GetMetricWithLabelValues(lvs ...string) (Counter, error)
}

// Gauge is a Metric that represents a single numerical value that can
// arbitrarily go up and down.
// A Gauge is typically used for measured values like temperatures or current
Expand All @@ -47,13 +55,10 @@ type Histogram interface {
Observe(float64)
}

type HistogramLabels = prom.Labels
type HistogramObserver = prom.Observer

// HistogramVec is returned by NewHistogramVec.
type HistogramVec interface {
GetMetricWith(HistogramLabels) (HistogramObserver, error)
GetMetricWithLabelValues(lvs ...string) (HistogramObserver, error)
GetMetricWith(MetricsLabels) (Histogram, error)
GetMetricWithLabelValues(lvs ...string) (Histogram, error)
}

// Timer represents a Histogram Metrics to observe the time duration according to given begin time.
Expand Down
40 changes: 33 additions & 7 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ func (p *PrometheusRegistry) NewHistogramVec(key string, labels []string, opts .
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
return reg.ExistingCollector.(*prom.HistogramVec)
return &histogramVec{reg.ExistingCollector.(*prom.HistogramVec)}
}
log.Warn("Failed to register a histogram vector", "key", key, "err", err)
}
return hv
return &histogramVec{hv}
}

func (p *PrometheusRegistry) NewTimer(key string, opts ...Option) Timer {
Expand All @@ -198,7 +198,7 @@ func (p *PrometheusRegistry) NewTimer(key string, opts ...Option) Timer {
}

func (p *PrometheusRegistry) NewWorker(key string, opts ...Option) Worker {
counterVec := p.newCounterVec(key, []string{"result"}, opts...)
counterVec := p.NewCounterVec(key, []string{"result"}, opts...)
// These are just references (no increments),
// as just referencing will create the labels but not set values.
success, _ := counterVec.GetMetricWithLabelValues("success")
Expand All @@ -210,7 +210,7 @@ func (p *PrometheusRegistry) NewWorker(key string, opts ...Option) Worker {
}
}

func (p *PrometheusRegistry) newCounterVec(key string, labelNames []string, opts ...Option) *prom.CounterVec {
func (p *PrometheusRegistry) NewCounterVec(key string, labelNames []string, opts ...Option) CounterVec {
options := NewOptions(p.namespace, "", p.labels)
for _, fn := range opts {
fn(options)
Expand All @@ -226,12 +226,12 @@ func (p *PrometheusRegistry) newCounterVec(key string, labelNames []string, opts
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
return reg.ExistingCollector.(*prom.CounterVec)
existingCV := reg.ExistingCollector.(*prom.CounterVec)
return &counterVec{existingCV}
}
log.Warn("Failed to register a counter vec", "key", key, "err", err)
}
return cnt

return &counterVec{cnt}
}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -260,3 +260,29 @@ func (w *worker) Observe(begin time.Time, err error) {
}
w.success.Inc()
}

// -----------------------------------------------------------------------------
// counterVec
type counterVec struct {
*prom.CounterVec
}

func (c *counterVec) GetMetricWith(labels MetricsLabels) (Counter, error) {
return c.CounterVec.GetMetricWith(labels)
}
func (c *counterVec) GetMetricWithLabelValues(lvs ...string) (Counter, error) {
return c.CounterVec.GetMetricWithLabelValues(lvs...)
}

// -----------------------------------------------------------------------------
// histogramVec
type histogramVec struct {
*prom.HistogramVec
}

func (h *histogramVec) GetMetricWith(labels MetricsLabels) (Histogram, error) {
return h.HistogramVec.GetMetricWith(labels)
}
func (h *histogramVec) GetMetricWithLabelValues(lvs ...string) (Histogram, error) {
return h.HistogramVec.GetMetricWithLabelValues(lvs...)
}
5 changes: 5 additions & 0 deletions metrics/registery.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Registry interface {
NewGauge(key string, opts ...Option) Gauge
NewHistogram(key string, opts ...Option) Histogram
NewHistogramVec(key string, labels []string, opts ...Option) HistogramVec
NewCounterVec(key string, labels []string, opts ...Option) CounterVec
NewTimer(key string, opts ...Option) Timer
NewWorker(key string, opts ...Option) Worker

Expand Down Expand Up @@ -77,6 +78,10 @@ func NewHistogramVec(key string, labels []string, opts ...Option) HistogramVec {
return DefaultRegistry.NewHistogramVec(key, labels, opts...)
}

func NewCounterVec(key string, labels []string, opts ...Option) CounterVec {
return DefaultRegistry.NewCounterVec(key, labels, opts...)
}

func NewTimer(key string, opts ...Option) Timer {
return DefaultRegistry.NewTimer(key, opts...)
}
Expand Down

0 comments on commit 90abfb0

Please sign in to comment.