Package list golang-github-go-kit-kit / 8114e4a
metrics: cleanup Peter Bourgon 5 years ago
31 changed file(s) with 52 addition(s) and 251 deletion(s). Raw diff Collapse all Expand all
33 import (
44 "github.com/circonus-labs/circonus-gometrics"
55
6 "github.com/go-kit/kit/metrics3"
6 "github.com/go-kit/kit/metrics"
77 )
88
99 // Circonus wraps a CirconusMetrics object and provides constructors for each of
1010 "github.com/circonus-labs/circonus-gometrics"
1111 "github.com/circonus-labs/circonus-gometrics/checkmgr"
1212
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"
1515 )
1616
1717 func TestCounter(t *testing.T) {
00 // Package discard provides a no-op metrics backend.
11 package discard
22
3 import "github.com/go-kit/kit/metrics3"
3 import "github.com/go-kit/kit/metrics"
44
55 type counter struct{}
66
1616 "time"
1717
1818 "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"
2222 "github.com/go-kit/kit/util/conn"
2323 )
2424
33 "testing"
44
55 "github.com/go-kit/kit/log"
6 "github.com/go-kit/kit/metrics3/teststat"
6 "github.com/go-kit/kit/metrics/teststat"
77 )
88
99 func TestCounter(t *testing.T) {
55 "expvar"
66 "sync"
77
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"
1010 )
1111
1212 // Counter implements the counter metric with an expvar float.
33 "strconv"
44 "testing"
55
6 "github.com/go-kit/kit/metrics3/teststat"
6 "github.com/go-kit/kit/metrics/teststat"
77 )
88
99 func TestCounter(t *testing.T) {
1111
1212 "github.com/VividCortex/gohistogram"
1313
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"
1616 )
1717
1818 // Counter is an in-memory implementation of a Counter.
88 "math/rand"
99 "testing"
1010
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"
1313 )
1414
1515 func TestCounter(t *testing.T) {
1313 "time"
1414
1515 "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"
1818 "github.com/go-kit/kit/util/conn"
1919 )
2020
66 "testing"
77
88 "github.com/go-kit/kit/log"
9 "github.com/go-kit/kit/metrics3/teststat"
9 "github.com/go-kit/kit/metrics/teststat"
1010 )
1111
1212 func TestCounter(t *testing.T) {
88 influxdb "github.com/influxdata/influxdb/client/v2"
99
1010 "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"
1313 )
1414
1515 // Influx is a store for metrics that will be emitted to an Influx database.
77 "strings"
88 "testing"
99
10 influxdb "github.com/influxdata/influxdb/client/v2"
11
1012 "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"
1415 )
1516
1617 func TestCounter(t *testing.T) {
+0
-94
metrics/internal/emitting/buffer.go less more
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
-107
metrics/internal/emitting/metrics.go less more
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 }
66 import (
77 "github.com/prometheus/client_golang/prometheus"
88
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"
1111 )
1212
1313 // Counter implements Counter, via a Prometheus CounterVec.
1010 "strings"
1111 "testing"
1212
13 "github.com/go-kit/kit/metrics3/teststat"
1413 stdprometheus "github.com/prometheus/client_golang/prometheus"
14
15 "github.com/go-kit/kit/metrics/teststat"
1516 )
1617
1718 func TestCounter(t *testing.T) {
00 package provider
11
22 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"
55 )
66
77 type circonusProvider struct {
00 package provider
11
22 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"
55 )
66
77 type discardProvider struct{}
00 package provider
11
22 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"
55 )
66
77 type dogstatsdProvider struct {
00 package provider
11
22 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"
55 )
66
77 type expvarProvider struct{}
00 package provider
11
22 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"
55 )
66
77 type graphiteProvider struct {
00 package provider
11
22 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"
55 )
66
77 type influxProvider struct {
22 import (
33 stdprometheus "github.com/prometheus/client_golang/prometheus"
44
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"
77 )
88
99 type prometheusProvider struct {
2323 package provider
2424
2525 import (
26 "github.com/go-kit/kit/metrics3"
26 "github.com/go-kit/kit/metrics"
2727 )
2828
2929 // Provider abstracts over constructors and lifecycle management functions for
00 package provider
11
22 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"
55 )
66
77 type statsdProvider struct {
1313 "time"
1414
1515 "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"
1919 "github.com/go-kit/kit/util/conn"
2020 )
2121
33 "testing"
44
55 "github.com/go-kit/kit/log"
6 "github.com/go-kit/kit/metrics3/teststat"
6 "github.com/go-kit/kit/metrics/teststat"
77 )
88
99 func TestCounter(t *testing.T) {
66 "regexp"
77 "strconv"
88
9 "github.com/go-kit/kit/metrics3/generic"
9 "github.com/go-kit/kit/metrics/generic"
1010 )
1111
1212 // SumLines expects a regex whose first capture group can be parsed as a
33 "math"
44 "math/rand"
55
6 "github.com/go-kit/kit/metrics3"
6 "github.com/go-kit/kit/metrics"
77 )
88
99 // PopulateNormalHistogram makes a series of normal random observations into the
77 "math/rand"
88 "strings"
99
10 "github.com/go-kit/kit/metrics3"
10 "github.com/go-kit/kit/metrics"
1111 )
1212
1313 // TestCounter puts some deltas through the counter, and then calls the value