Codebase list golang-github-go-kit-kit / 3c6be53
metrics: unit tests for callback Gauges Peter Bourgon 9 years ago
4 changed file(s) with 59 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
00 package expvar_test
11
22 import (
3 stdexpvar "expvar"
4 "fmt"
35 "testing"
46
57 "github.com/peterbourgon/gokit/metrics/expvar"
1517 teststat.PopulateNormalHistogram(t, h, seed, mean, stdev)
1618 teststat.AssertExpvarNormalHistogram(t, metricName, mean, stdev, quantiles)
1719 }
20
21 func TestCallbackGauge(t *testing.T) {
22 value := 42.43
23 metricName := "foo"
24 expvar.PublishCallbackGauge(metricName, func() float64 { return value })
25 if want, have := fmt.Sprint(value), stdexpvar.Get(metricName).String(); want != have {
26 t.Errorf("want %q, have %q", want, have)
27 }
28 }
6565 }
6666 }
6767
68 func TestPrometheusCallbackGauge(t *testing.T) {
69 value := 123.456
70 cb := func() float64 { return value }
71 prometheus.RegisterCallbackGauge("test", "prometheus_gauge", "bazbaz", "Help string.", cb)
72 if want, have := strings.Join([]string{
73 `# HELP test_prometheus_gauge_bazbaz Help string.`,
74 `# TYPE test_prometheus_gauge_bazbaz gauge`,
75 `test_prometheus_gauge_bazbaz 123.456`,
76 }, "\n"), teststat.ScrapePrometheus(t); !strings.Contains(have, want) {
77 t.Errorf("metric stanza not found or incorrect\n%s", have)
78 }
79 }
80
6881 func TestPrometheusHistogram(t *testing.T) {
6982 h := prometheus.NewHistogram("test", "prometheus_histogram", "foobar", "Qwerty asdf.", []string{})
7083
7777 // It collects values every scrape interval from the callback. Values are
7878 // buffered for the report interval or until the buffer exceeds a max packet
7979 // size, whichever comes first. The report and scrape intervals may be the
80 // same. Fields are ignored.
80 // same. The callback determines the value, and fields are ignored, so
81 // NewCallbackGauge returns nothing.
8182 func NewCallbackGauge(w io.Writer, key string, reportInterval, scrapeInterval time.Duration, callback func() float64) {
8283 go fwd(w, key, reportInterval, emitEvery(scrapeInterval, callback))
8384 }
8485
85 func emitEvery(d time.Duration, f func() float64) <-chan string {
86 func emitEvery(d time.Duration, callback func() float64) <-chan string {
8687 c := make(chan string)
8788 go func() {
88 for range time.Tick(d) {
89 c <- fmt.Sprintf("%f|g", f())
89 for range tick(d) {
90 c <- fmt.Sprintf("%f|g", callback())
9091 }
9192 }()
9293 return c
33
44 import (
55 "bytes"
6 "fmt"
67 "runtime"
78 "testing"
89 "time"
3738 buf := &bytes.Buffer{}
3839 g := NewGauge(buf, "test_statsd_gauge", time.Second)
3940
40 g.Add(1) // send command
41 delta := 1.0
42 g.Add(delta) // send command
4143 runtime.Gosched() // yield to buffer write
4244 ch <- time.Now() // signal flush
4345 runtime.Gosched() // yield to flush
44 if want, have := "test_statsd_gauge:+1.000000|g\n", buf.String(); want != have {
46 if want, have := fmt.Sprintf("test_statsd_gauge:+%f|g\n", delta), buf.String(); want != have {
4547 t.Errorf("want %q, have %q", want, have)
4648 }
4749
4850 buf.Reset()
4951
50 g.Add(-2)
52 delta = -2.0
53 g.Add(delta)
5154 runtime.Gosched()
5255 ch <- time.Now()
5356 runtime.Gosched()
54 if want, have := "test_statsd_gauge:-2.000000|g\n", buf.String(); want != have {
57 if want, have := fmt.Sprintf("test_statsd_gauge:%f|g\n", delta), buf.String(); want != have {
5558 t.Errorf("want %q, have %q", want, have)
5659 }
5760
5861 buf.Reset()
5962
60 g.Set(3)
63 value := 3.0
64 g.Set(value)
6165 runtime.Gosched()
6266 ch <- time.Now()
6367 runtime.Gosched()
64 if want, have := "test_statsd_gauge:3.000000|g\n", buf.String(); want != have {
68 if want, have := fmt.Sprintf("test_statsd_gauge:%f|g\n", value), buf.String(); want != have {
69 t.Errorf("want %q, have %q", want, have)
70 }
71 }
72
73 func TestCallbackGauge(t *testing.T) {
74 ch := make(chan time.Time)
75 tick = func(time.Duration) <-chan time.Time { return ch }
76 defer func() { tick = time.Tick }()
77
78 buf := &bytes.Buffer{}
79 value := 55.55
80 cb := func() float64 { return value }
81 NewCallbackGauge(buf, "test_statsd_callback_gauge", time.Second, time.Nanosecond, cb)
82
83 ch <- time.Now() // signal emitter
84 runtime.Gosched() // yield to emitter
85 ch <- time.Now() // signal flush
86 runtime.Gosched() // yield to flush
87
88 if want, have := fmt.Sprintf("test_statsd_callback_gauge:%f|g\n", value), buf.String(); want != have {
6589 t.Errorf("want %q, have %q", want, have)
6690 }
6791 }