Skip to content

Commit

Permalink
Merge pull request #67 from getamis/add-gauge-vec
Browse files Browse the repository at this point in the history
metrics: add gauge vec
  • Loading branch information
bailantaotao authored Mar 11, 2019
2 parents 7741ea7 + 91c746f commit 49919f4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions metrics/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (d *DummyRegistry) NewGauge(key string, opts ...Option) Gauge {
return &dummyGauge{}
}

func (d *DummyRegistry) NewGaugeVec(key string, labels []string, opts ...Option) GaugeVec {
return &dummyGaugeVec{}
}

func (d *DummyRegistry) NewHistogram(key string, opts ...Option) Histogram {
return &dummyHistogram{}
}
Expand Down Expand Up @@ -93,6 +97,15 @@ type dummyGauge struct{}

func (d *dummyGauge) Set(float64) {}

type dummyGaugeVec struct{}

func (d *dummyGaugeVec) GetMetricWith(MetricsLabels) (Gauge, error) {
return &dummyGauge{}, nil
}
func (d *dummyGaugeVec) GetMetricWithLabelValues(lvs ...string) (Gauge, error) {
return &dummyGauge{}, nil
}

type dummyHistogram struct{}

func (d *dummyHistogram) Observe(float64) {}
Expand Down
6 changes: 6 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type Gauge interface {
Set(float64)
}

// GaugeVec is returned by NewGaugeVec.
type GaugeVec interface {
GetMetricWith(MetricsLabels) (Gauge, error)
GetMetricWithLabelValues(lvs ...string) (Gauge, error)
}

// A Histogram counts individual observations from an event or sample stream in
// configurable buckets. Similar to a summary, it also provides a sum of
// observations and an observation count.
Expand Down
37 changes: 37 additions & 0 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,30 @@ func (p *PrometheusRegistry) NewGauge(key string, opts ...Option) Gauge {
return g
}

func (p *PrometheusRegistry) NewGaugeVec(key string, labelNames []string, opts ...Option) GaugeVec {
options := NewOptions(p.namespace, "", p.labels)
for _, fn := range opts {
fn(options)
}
cnt := prom.NewGaugeVec(prom.GaugeOpts{
Namespace: options.Namespace,
Subsystem: options.Subsystem,
Name: key,
Help: key,
ConstLabels: prom.Labels(options.Labels),
}, labelNames)
err := p.registry.Register(cnt)
if err != nil {
reg, ok := err.(prom.AlreadyRegisteredError)
if ok {
existingCV := reg.ExistingCollector.(*prom.GaugeVec)
return &gaugeVec{existingCV}
}
log.Warn("Failed to register a gauge vec", "key", key, "err", err)
}
return &gaugeVec{cnt}
}

func (p *PrometheusRegistry) NewHistogram(key string, opts ...Option) Histogram {
options := NewOptions(p.namespace, "", p.labels)
for _, fn := range opts {
Expand Down Expand Up @@ -274,6 +298,19 @@ func (c *counterVec) GetMetricWithLabelValues(lvs ...string) (Counter, error) {
return c.CounterVec.GetMetricWithLabelValues(lvs...)
}

// -----------------------------------------------------------------------------
// gaugeVec
type gaugeVec struct {
*prom.GaugeVec
}

func (c *gaugeVec) GetMetricWith(labels MetricsLabels) (Gauge, error) {
return c.GaugeVec.GetMetricWith(labels)
}
func (c *gaugeVec) GetMetricWithLabelValues(lvs ...string) (Gauge, error) {
return c.GaugeVec.GetMetricWithLabelValues(lvs...)
}

// -----------------------------------------------------------------------------
// histogramVec
type histogramVec struct {
Expand Down
5 changes: 5 additions & 0 deletions metrics/registery.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Registry interface {
NewServerMetrics(opts ...Option) ServerMetrics
NewCounter(key string, opts ...Option) Counter
NewGauge(key string, opts ...Option) Gauge
NewGaugeVec(key string, labels []string, opts ...Option) GaugeVec
NewHistogram(key string, opts ...Option) Histogram
NewHistogramVec(key string, labels []string, opts ...Option) HistogramVec
NewCounterVec(key string, labels []string, opts ...Option) CounterVec
Expand All @@ -70,6 +71,10 @@ func NewGauge(key string, opts ...Option) Gauge {
return DefaultRegistry.NewGauge(key, opts...)
}

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

func NewHistogram(key string, opts ...Option) Histogram {
return DefaultRegistry.NewHistogram(key, opts...)
}
Expand Down

0 comments on commit 49919f4

Please sign in to comment.