metrics: cleanup
Peter Bourgon
7 years ago
3 | 3 | import ( |
4 | 4 | "github.com/circonus-labs/circonus-gometrics" |
5 | 5 | |
6 | "github.com/go-kit/kit/metrics3" | |
6 | "github.com/go-kit/kit/metrics" | |
7 | 7 | ) |
8 | 8 | |
9 | 9 | // Circonus wraps a CirconusMetrics object and provides constructors for each of |
10 | 10 | "github.com/circonus-labs/circonus-gometrics" |
11 | 11 | "github.com/circonus-labs/circonus-gometrics/checkmgr" |
12 | 12 | |
13 | "github.com/go-kit/kit/metrics3/generic" | |
14 | "github.com/go-kit/kit/metrics3/teststat" | |
13 | "github.com/go-kit/kit/metrics/generic" | |
14 | "github.com/go-kit/kit/metrics/teststat" | |
15 | 15 | ) |
16 | 16 | |
17 | 17 | func TestCounter(t *testing.T) { |
0 | 0 | // Package discard provides a no-op metrics backend. |
1 | 1 | package discard |
2 | 2 | |
3 | import "github.com/go-kit/kit/metrics3" | |
3 | import "github.com/go-kit/kit/metrics" | |
4 | 4 | |
5 | 5 | type counter struct{} |
6 | 6 |
16 | 16 | "time" |
17 | 17 | |
18 | 18 | "github.com/go-kit/kit/log" |
19 | "github.com/go-kit/kit/metrics3" | |
20 | "github.com/go-kit/kit/metrics3/internal/lv" | |
21 | "github.com/go-kit/kit/metrics3/internal/ratemap" | |
19 | "github.com/go-kit/kit/metrics" | |
20 | "github.com/go-kit/kit/metrics/internal/lv" | |
21 | "github.com/go-kit/kit/metrics/internal/ratemap" | |
22 | 22 | "github.com/go-kit/kit/util/conn" |
23 | 23 | ) |
24 | 24 |
3 | 3 | "testing" |
4 | 4 | |
5 | 5 | "github.com/go-kit/kit/log" |
6 | "github.com/go-kit/kit/metrics3/teststat" | |
6 | "github.com/go-kit/kit/metrics/teststat" | |
7 | 7 | ) |
8 | 8 | |
9 | 9 | func TestCounter(t *testing.T) { |
5 | 5 | "expvar" |
6 | 6 | "sync" |
7 | 7 | |
8 | "github.com/go-kit/kit/metrics3" | |
9 | "github.com/go-kit/kit/metrics3/generic" | |
8 | "github.com/go-kit/kit/metrics" | |
9 | "github.com/go-kit/kit/metrics/generic" | |
10 | 10 | ) |
11 | 11 | |
12 | 12 | // Counter implements the counter metric with an expvar float. |
3 | 3 | "strconv" |
4 | 4 | "testing" |
5 | 5 | |
6 | "github.com/go-kit/kit/metrics3/teststat" | |
6 | "github.com/go-kit/kit/metrics/teststat" | |
7 | 7 | ) |
8 | 8 | |
9 | 9 | func TestCounter(t *testing.T) { |
11 | 11 | |
12 | 12 | "github.com/VividCortex/gohistogram" |
13 | 13 | |
14 | "github.com/go-kit/kit/metrics3" | |
15 | "github.com/go-kit/kit/metrics3/internal/lv" | |
14 | "github.com/go-kit/kit/metrics" | |
15 | "github.com/go-kit/kit/metrics/internal/lv" | |
16 | 16 | ) |
17 | 17 | |
18 | 18 | // Counter is an in-memory implementation of a Counter. |
8 | 8 | "math/rand" |
9 | 9 | "testing" |
10 | 10 | |
11 | "github.com/go-kit/kit/metrics3/generic" | |
12 | "github.com/go-kit/kit/metrics3/teststat" | |
11 | "github.com/go-kit/kit/metrics/generic" | |
12 | "github.com/go-kit/kit/metrics/teststat" | |
13 | 13 | ) |
14 | 14 | |
15 | 15 | func TestCounter(t *testing.T) { |
13 | 13 | "time" |
14 | 14 | |
15 | 15 | "github.com/go-kit/kit/log" |
16 | "github.com/go-kit/kit/metrics3" | |
17 | "github.com/go-kit/kit/metrics3/generic" | |
16 | "github.com/go-kit/kit/metrics" | |
17 | "github.com/go-kit/kit/metrics/generic" | |
18 | 18 | "github.com/go-kit/kit/util/conn" |
19 | 19 | ) |
20 | 20 |
6 | 6 | "testing" |
7 | 7 | |
8 | 8 | "github.com/go-kit/kit/log" |
9 | "github.com/go-kit/kit/metrics3/teststat" | |
9 | "github.com/go-kit/kit/metrics/teststat" | |
10 | 10 | ) |
11 | 11 | |
12 | 12 | func TestCounter(t *testing.T) { |
8 | 8 | influxdb "github.com/influxdata/influxdb/client/v2" |
9 | 9 | |
10 | 10 | "github.com/go-kit/kit/log" |
11 | "github.com/go-kit/kit/metrics3" | |
12 | "github.com/go-kit/kit/metrics3/internal/lv" | |
11 | "github.com/go-kit/kit/metrics" | |
12 | "github.com/go-kit/kit/metrics/internal/lv" | |
13 | 13 | ) |
14 | 14 | |
15 | 15 | // Influx is a store for metrics that will be emitted to an Influx database. |
7 | 7 | "strings" |
8 | 8 | "testing" |
9 | 9 | |
10 | influxdb "github.com/influxdata/influxdb/client/v2" | |
11 | ||
10 | 12 | "github.com/go-kit/kit/log" |
11 | "github.com/go-kit/kit/metrics3/generic" | |
12 | "github.com/go-kit/kit/metrics3/teststat" | |
13 | influxdb "github.com/influxdata/influxdb/client/v2" | |
13 | "github.com/go-kit/kit/metrics/generic" | |
14 | "github.com/go-kit/kit/metrics/teststat" | |
14 | 15 | ) |
15 | 16 | |
16 | 17 | func TestCounter(t *testing.T) { |
0 | package emitting | |
1 | ||
2 | import ( | |
3 | "fmt" | |
4 | "strings" | |
5 | "sync" | |
6 | ||
7 | "sort" | |
8 | ||
9 | "github.com/go-kit/kit/metrics3/generic" | |
10 | ) | |
11 | ||
12 | type Buffer struct { | |
13 | buckets int | |
14 | ||
15 | mtx sync.Mutex | |
16 | counters map[point]*generic.Counter | |
17 | gauges map[point]*generic.Gauge | |
18 | histograms map[point]*generic.Histogram | |
19 | } | |
20 | ||
21 | func (b *Buffer) Add(a Add) { | |
22 | pt := makePoint(a.Name, a.LabelValues) | |
23 | b.mtx.Lock() | |
24 | defer b.mtx.Unlock() | |
25 | c, ok := b.counters[pt] | |
26 | if !ok { | |
27 | c = generic.NewCounter(a.Name).With(a.LabelValues...).(*generic.Counter) | |
28 | } | |
29 | c.Add(a.Delta) | |
30 | b.counters[pt] = c | |
31 | } | |
32 | ||
33 | func (b *Buffer) Set(s Set) { | |
34 | pt := makePoint(s.Name, s.LabelValues) | |
35 | b.mtx.Lock() | |
36 | defer b.mtx.Unlock() | |
37 | g, ok := b.gauges[pt] | |
38 | if !ok { | |
39 | g = generic.NewGauge(s.Name).With(s.LabelValues...).(*generic.Gauge) | |
40 | } | |
41 | g.Set(s.Value) | |
42 | b.gauges[pt] = g | |
43 | } | |
44 | ||
45 | func (b *Buffer) Obv(o Obv) { | |
46 | pt := makePoint(o.Name, o.LabelValues) | |
47 | b.mtx.Lock() | |
48 | defer b.mtx.Unlock() | |
49 | h, ok := b.histograms[pt] | |
50 | if !ok { | |
51 | h = generic.NewHistogram(o.Name, b.buckets).With(o.LabelValues...).(*generic.Histogram) | |
52 | } | |
53 | h.Observe(o.Value) | |
54 | b.histograms[pt] = h | |
55 | } | |
56 | ||
57 | // point as in point in N-dimensional vector space; | |
58 | // a string encoding of name + sorted k/v pairs. | |
59 | type point string | |
60 | ||
61 | const ( | |
62 | recordDelimiter = "•" | |
63 | fieldDelimiter = "·" | |
64 | ) | |
65 | ||
66 | // (foo, [a b c d]) => "foo•a·b•c·d" | |
67 | func makePoint(name string, labelValues []string) point { | |
68 | if len(labelValues)%2 != 0 { | |
69 | panic("odd number of label values; programmer error!") | |
70 | } | |
71 | pairs := make([]string, 0, len(labelValues)/2) | |
72 | for i := 0; i < len(labelValues); i += 2 { | |
73 | pairs = append(pairs, fmt.Sprintf("%s%s%s", labelValues[i], fieldDelimiter, labelValues[i+1])) | |
74 | } | |
75 | sort.Strings(sort.StringSlice(pairs)) | |
76 | pairs = append([]string{name}, pairs...) | |
77 | return point(strings.Join(pairs, recordDelimiter)) | |
78 | } | |
79 | ||
80 | // "foo•a·b•c·d" => (foo, [a b c d]) | |
81 | func (p point) nameLabelValues() (name string, labelValues []string) { | |
82 | records := strings.Split(string(p), recordDelimiter) | |
83 | if len(records)%2 != 1 { // always name + even number of label/values | |
84 | panic("even number of point records; programmer error!") | |
85 | } | |
86 | name, records = records[0], records[1:] | |
87 | labelValues = make([]string, 0, len(records)*2) | |
88 | for _, record := range records { | |
89 | fields := strings.SplitN(record, fieldDelimiter, 2) | |
90 | labelValues = append(labelValues, fields[0], fields[1]) | |
91 | } | |
92 | return name, labelValues | |
93 | } |
0 | package emitting | |
1 | ||
2 | import ( | |
3 | "github.com/go-kit/kit/metrics3" | |
4 | "github.com/go-kit/kit/metrics3/internal/lv" | |
5 | ) | |
6 | ||
7 | type Counter struct { | |
8 | name string | |
9 | lvs lv.LabelValues | |
10 | sampleRate float64 | |
11 | c chan Add | |
12 | } | |
13 | ||
14 | type Add struct { | |
15 | Name string | |
16 | LabelValues []string | |
17 | SampleRate float64 | |
18 | Delta float64 | |
19 | } | |
20 | ||
21 | func NewCounter(name string, sampleRate float64, c chan Add) *Counter { | |
22 | return &Counter{ | |
23 | name: name, | |
24 | sampleRate: sampleRate, | |
25 | c: c, | |
26 | } | |
27 | } | |
28 | ||
29 | func (c *Counter) With(labelValues ...string) metrics.Counter { | |
30 | return &Counter{ | |
31 | name: c.name, | |
32 | lvs: c.lvs.With(labelValues...), | |
33 | sampleRate: c.sampleRate, | |
34 | c: c.c, | |
35 | } | |
36 | } | |
37 | ||
38 | func (c *Counter) Add(delta float64) { | |
39 | c.c <- Add{c.name, c.lvs, c.sampleRate, delta} | |
40 | } | |
41 | ||
42 | type Gauge struct { | |
43 | name string | |
44 | lvs lv.LabelValues | |
45 | c chan Set | |
46 | } | |
47 | ||
48 | type Set struct { | |
49 | Name string | |
50 | LabelValues []string | |
51 | Value float64 | |
52 | } | |
53 | ||
54 | func NewGauge(name string, c chan Set) *Gauge { | |
55 | return &Gauge{ | |
56 | name: name, | |
57 | c: c, | |
58 | } | |
59 | } | |
60 | ||
61 | func (g *Gauge) With(labelValues ...string) metrics.Gauge { | |
62 | return &Gauge{ | |
63 | name: g.name, | |
64 | lvs: g.lvs.With(labelValues...), | |
65 | c: g.c, | |
66 | } | |
67 | } | |
68 | ||
69 | func (g *Gauge) Set(value float64) { | |
70 | g.c <- Set{g.name, g.lvs, value} | |
71 | } | |
72 | ||
73 | type Histogram struct { | |
74 | name string | |
75 | lvs lv.LabelValues | |
76 | sampleRate float64 | |
77 | c chan Obv | |
78 | } | |
79 | ||
80 | type Obv struct { | |
81 | Name string | |
82 | LabelValues []string | |
83 | SampleRate float64 | |
84 | Value float64 | |
85 | } | |
86 | ||
87 | func NewHistogram(name string, sampleRate float64, c chan Obv) *Histogram { | |
88 | return &Histogram{ | |
89 | name: name, | |
90 | sampleRate: sampleRate, | |
91 | c: c, | |
92 | } | |
93 | } | |
94 | ||
95 | func (h *Histogram) With(labelValues ...string) metrics.Histogram { | |
96 | return &Histogram{ | |
97 | name: h.name, | |
98 | lvs: h.lvs.With(labelValues...), | |
99 | sampleRate: h.sampleRate, | |
100 | c: h.c, | |
101 | } | |
102 | } | |
103 | ||
104 | func (h *Histogram) Observe(value float64) { | |
105 | h.c <- Obv{h.name, h.lvs, h.sampleRate, value} | |
106 | } |
6 | 6 | import ( |
7 | 7 | "github.com/prometheus/client_golang/prometheus" |
8 | 8 | |
9 | "github.com/go-kit/kit/metrics3" | |
10 | "github.com/go-kit/kit/metrics3/internal/lv" | |
9 | "github.com/go-kit/kit/metrics" | |
10 | "github.com/go-kit/kit/metrics/internal/lv" | |
11 | 11 | ) |
12 | 12 | |
13 | 13 | // Counter implements Counter, via a Prometheus CounterVec. |
10 | 10 | "strings" |
11 | 11 | "testing" |
12 | 12 | |
13 | "github.com/go-kit/kit/metrics3/teststat" | |
14 | 13 | stdprometheus "github.com/prometheus/client_golang/prometheus" |
14 | ||
15 | "github.com/go-kit/kit/metrics/teststat" | |
15 | 16 | ) |
16 | 17 | |
17 | 18 | func TestCounter(t *testing.T) { |
0 | 0 | package provider |
1 | 1 | |
2 | 2 | import ( |
3 | "github.com/go-kit/kit/metrics3" | |
4 | "github.com/go-kit/kit/metrics3/circonus" | |
3 | "github.com/go-kit/kit/metrics" | |
4 | "github.com/go-kit/kit/metrics/circonus" | |
5 | 5 | ) |
6 | 6 | |
7 | 7 | type circonusProvider struct { |
0 | 0 | package provider |
1 | 1 | |
2 | 2 | import ( |
3 | "github.com/go-kit/kit/metrics3" | |
4 | "github.com/go-kit/kit/metrics3/discard" | |
3 | "github.com/go-kit/kit/metrics" | |
4 | "github.com/go-kit/kit/metrics/discard" | |
5 | 5 | ) |
6 | 6 | |
7 | 7 | type discardProvider struct{} |
0 | 0 | package provider |
1 | 1 | |
2 | 2 | import ( |
3 | "github.com/go-kit/kit/metrics3" | |
4 | "github.com/go-kit/kit/metrics3/dogstatsd" | |
3 | "github.com/go-kit/kit/metrics" | |
4 | "github.com/go-kit/kit/metrics/dogstatsd" | |
5 | 5 | ) |
6 | 6 | |
7 | 7 | type dogstatsdProvider struct { |
0 | 0 | package provider |
1 | 1 | |
2 | 2 | import ( |
3 | "github.com/go-kit/kit/metrics3" | |
4 | "github.com/go-kit/kit/metrics3/expvar" | |
3 | "github.com/go-kit/kit/metrics" | |
4 | "github.com/go-kit/kit/metrics/expvar" | |
5 | 5 | ) |
6 | 6 | |
7 | 7 | type expvarProvider struct{} |
0 | 0 | package provider |
1 | 1 | |
2 | 2 | import ( |
3 | "github.com/go-kit/kit/metrics3" | |
4 | "github.com/go-kit/kit/metrics3/graphite" | |
3 | "github.com/go-kit/kit/metrics" | |
4 | "github.com/go-kit/kit/metrics/graphite" | |
5 | 5 | ) |
6 | 6 | |
7 | 7 | type graphiteProvider struct { |
0 | 0 | package provider |
1 | 1 | |
2 | 2 | import ( |
3 | "github.com/go-kit/kit/metrics3" | |
4 | "github.com/go-kit/kit/metrics3/influx" | |
3 | "github.com/go-kit/kit/metrics" | |
4 | "github.com/go-kit/kit/metrics/influx" | |
5 | 5 | ) |
6 | 6 | |
7 | 7 | type influxProvider struct { |
2 | 2 | import ( |
3 | 3 | stdprometheus "github.com/prometheus/client_golang/prometheus" |
4 | 4 | |
5 | "github.com/go-kit/kit/metrics3" | |
6 | "github.com/go-kit/kit/metrics3/prometheus" | |
5 | "github.com/go-kit/kit/metrics" | |
6 | "github.com/go-kit/kit/metrics/prometheus" | |
7 | 7 | ) |
8 | 8 | |
9 | 9 | type prometheusProvider struct { |
23 | 23 | package provider |
24 | 24 | |
25 | 25 | import ( |
26 | "github.com/go-kit/kit/metrics3" | |
26 | "github.com/go-kit/kit/metrics" | |
27 | 27 | ) |
28 | 28 | |
29 | 29 | // Provider abstracts over constructors and lifecycle management functions for |
0 | 0 | package provider |
1 | 1 | |
2 | 2 | import ( |
3 | "github.com/go-kit/kit/metrics3" | |
4 | "github.com/go-kit/kit/metrics3/statsd" | |
3 | "github.com/go-kit/kit/metrics" | |
4 | "github.com/go-kit/kit/metrics/statsd" | |
5 | 5 | ) |
6 | 6 | |
7 | 7 | type statsdProvider struct { |
13 | 13 | "time" |
14 | 14 | |
15 | 15 | "github.com/go-kit/kit/log" |
16 | "github.com/go-kit/kit/metrics3" | |
17 | "github.com/go-kit/kit/metrics3/internal/lv" | |
18 | "github.com/go-kit/kit/metrics3/internal/ratemap" | |
16 | "github.com/go-kit/kit/metrics" | |
17 | "github.com/go-kit/kit/metrics/internal/lv" | |
18 | "github.com/go-kit/kit/metrics/internal/ratemap" | |
19 | 19 | "github.com/go-kit/kit/util/conn" |
20 | 20 | ) |
21 | 21 |
3 | 3 | "testing" |
4 | 4 | |
5 | 5 | "github.com/go-kit/kit/log" |
6 | "github.com/go-kit/kit/metrics3/teststat" | |
6 | "github.com/go-kit/kit/metrics/teststat" | |
7 | 7 | ) |
8 | 8 | |
9 | 9 | func TestCounter(t *testing.T) { |
6 | 6 | "regexp" |
7 | 7 | "strconv" |
8 | 8 | |
9 | "github.com/go-kit/kit/metrics3/generic" | |
9 | "github.com/go-kit/kit/metrics/generic" | |
10 | 10 | ) |
11 | 11 | |
12 | 12 | // SumLines expects a regex whose first capture group can be parsed as a |