Skip to content

Commit

Permalink
metrics/generic: With should track Name
Browse files Browse the repository at this point in the history
Closes #455
  • Loading branch information
peterbourgon committed Feb 10, 2017
1 parent 704043a commit 839f598
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 5 additions & 2 deletions metrics/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewCounter(name string) *Counter {
// With implements Counter.
func (c *Counter) With(labelValues ...string) metrics.Counter {
return &Counter{
Name: c.Name,
bits: atomic.LoadUint64(&c.bits),
lvs: c.lvs.With(labelValues...),
}
Expand Down Expand Up @@ -95,6 +96,7 @@ func NewGauge(name string) *Gauge {
// With implements Gauge.
func (g *Gauge) With(labelValues ...string) metrics.Gauge {
return &Gauge{
Name: g.Name,
bits: atomic.LoadUint64(&g.bits),
lvs: g.lvs.With(labelValues...),
}
Expand Down Expand Up @@ -150,8 +152,9 @@ func NewHistogram(name string, buckets int) *Histogram {
// With implements Histogram.
func (h *Histogram) With(labelValues ...string) metrics.Histogram {
return &Histogram{
lvs: h.lvs.With(labelValues...),
h: h.h,
Name: h.Name,
lvs: h.lvs.With(labelValues...),
h: h.h,
}
}

Expand Down
18 changes: 15 additions & 3 deletions metrics/generic/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import (
)

func TestCounter(t *testing.T) {
counter := generic.NewCounter("my_counter").With("label", "counter").(*generic.Counter)
name := "my_counter"
counter := generic.NewCounter(name).With("label", "counter").(*generic.Counter)
if want, have := name, counter.Name; want != have {
t.Errorf("Name: want %q, have %q", want, have)
}
value := func() float64 { return counter.Value() }
if err := teststat.TestCounter(counter, value); err != nil {
t.Fatal(err)
Expand All @@ -36,15 +40,23 @@ func TestValueReset(t *testing.T) {
}

func TestGauge(t *testing.T) {
gauge := generic.NewGauge("my_gauge").With("label", "gauge").(*generic.Gauge)
name := "my_gauge"
gauge := generic.NewGauge(name).With("label", "gauge").(*generic.Gauge)
if want, have := name, gauge.Name; want != have {
t.Errorf("Name: want %q, have %q", want, have)
}
value := func() float64 { return gauge.Value() }
if err := teststat.TestGauge(gauge, value); err != nil {
t.Fatal(err)
}
}

func TestHistogram(t *testing.T) {
histogram := generic.NewHistogram("my_histogram", 50).With("label", "histogram").(*generic.Histogram)
name := "my_histogram"
histogram := generic.NewHistogram(name, 50).With("label", "histogram").(*generic.Histogram)
if want, have := name, histogram.Name; want != have {
t.Errorf("Name: want %q, have %q", want, have)
}
quantiles := func() (float64, float64, float64, float64) {
return histogram.Quantile(0.50), histogram.Quantile(0.90), histogram.Quantile(0.95), histogram.Quantile(0.99)
}
Expand Down

0 comments on commit 839f598

Please sign in to comment.