Consistency updates
- metrics/prometheus: pass thru XxxOpts structs consistently
- addsvc: stdlib log redirects to gokit's PrefixLogger (reverse to prev)
Peter Bourgon
8 years ago
13 | 13 | "time" |
14 | 14 | |
15 | 15 | "github.com/apache/thrift/lib/go/thrift" |
16 | stdprometheus "github.com/prometheus/client_golang/prometheus" | |
16 | 17 | "github.com/streadway/handy/cors" |
17 | 18 | "github.com/streadway/handy/encoding" |
18 | 19 | "golang.org/x/net/context" |
23 | 24 | kitlog "github.com/go-kit/kit/log" |
24 | 25 | "github.com/go-kit/kit/metrics" |
25 | 26 | "github.com/go-kit/kit/metrics/expvar" |
27 | "github.com/go-kit/kit/metrics/prometheus" | |
26 | 28 | "github.com/go-kit/kit/metrics/statsd" |
27 | 29 | "github.com/go-kit/kit/server" |
28 | 30 | "github.com/go-kit/kit/tracing/zipkin" |
44 | 46 | // `package metrics` domain |
45 | 47 | requests := metrics.NewMultiCounter( |
46 | 48 | expvar.NewCounter("requests"), |
47 | statsd.NewCounter(ioutil.Discard, "requests", time.Second), | |
49 | statsd.NewCounter(ioutil.Discard, "requests_total", time.Second), | |
50 | prometheus.NewCounter(stdprometheus.CounterOpts{ | |
51 | Namespace: "addsvc", | |
52 | Subsystem: "add", | |
53 | Name: "requests_total", | |
54 | Help: "Total number of received requests.", | |
55 | }, []string{}), | |
48 | 56 | ) |
49 | 57 | duration := metrics.NewMultiHistogram( |
50 | expvar.NewHistogram("duration_ns", 0, 100000000, 3), | |
51 | statsd.NewHistogram(ioutil.Discard, "duration_ns", time.Second), | |
58 | expvar.NewHistogram("duration_nanoseconds_total", 0, 100000000, 3), | |
59 | statsd.NewHistogram(ioutil.Discard, "duration_nanoseconds_total", time.Second), | |
60 | prometheus.NewHistogram(stdprometheus.HistogramOpts{ | |
61 | Namespace: "addsvc", | |
62 | Subsystem: "add", | |
63 | Name: "duration_nanoseconds_total", | |
64 | Help: "Total nanoseconds spend serving requests.", | |
65 | }, []string{}), | |
52 | 66 | ) |
53 | 67 | |
54 | 68 | // `package tracing` domain |
59 | 73 | |
60 | 74 | // `package log` domain |
61 | 75 | var logger kitlog.Logger |
62 | logger = kitlog.NewPrefixLogger(kitlog.StdlibWriter{}) | |
76 | logger = kitlog.NewPrefixLogger(os.Stderr) | |
63 | 77 | logger = kitlog.With(logger, "ts", kitlog.DefaultTimestampUTC) |
64 | kitlog.DefaultLogger = logger // for other gokit components | |
65 | stdlog.SetOutput(os.Stderr) // | |
66 | stdlog.SetFlags(0) // flags are handled in our logger | |
78 | kitlog.DefaultLogger = logger // for other gokit components | |
79 | stdlog.SetOutput(kitlog.NewStdlibAdapter(logger)) // redirect stdlib logging to us | |
80 | stdlog.SetFlags(0) // flags are handled in our logger | |
67 | 81 | |
68 | 82 | // Our business and operational domain |
69 | 83 | var a Add |
22 | 22 | func TestMultiWith(t *testing.T) { |
23 | 23 | c := metrics.NewMultiCounter( |
24 | 24 | expvar.NewCounter("foo"), |
25 | prometheus.NewCounter("test", "multi_with", "bar", "Bar counter.", []string{"a"}), | |
25 | prometheus.NewCounter(stdprometheus.CounterOpts{ | |
26 | Namespace: "test", | |
27 | Subsystem: "multi_with", | |
28 | Name: "bar", | |
29 | Help: "Bar counter.", | |
30 | }, []string{"a"}), | |
26 | 31 | ) |
27 | 32 | |
28 | 33 | c.Add(1) |
42 | 47 | func TestMultiCounter(t *testing.T) { |
43 | 48 | metrics.NewMultiCounter( |
44 | 49 | expvar.NewCounter("alpha"), |
45 | prometheus.NewCounter("test", "multi_counter", "beta", "Beta counter.", []string{}), | |
50 | prometheus.NewCounter(stdprometheus.CounterOpts{ | |
51 | Namespace: "test", | |
52 | Subsystem: "multi_counter", | |
53 | Name: "beta", | |
54 | Help: "Beta counter.", | |
55 | }, []string{}), | |
46 | 56 | ).Add(123) |
47 | 57 | |
48 | 58 | if want, have := "123", stdexpvar.Get("alpha").String(); want != have { |
61 | 71 | func TestMultiGauge(t *testing.T) { |
62 | 72 | g := metrics.NewMultiGauge( |
63 | 73 | expvar.NewGauge("delta"), |
64 | prometheus.NewGauge("test", "multi_gauge", "kappa", "Kappa gauge.", []string{}), | |
74 | prometheus.NewGauge(stdprometheus.GaugeOpts{ | |
75 | Namespace: "test", | |
76 | Subsystem: "multi_gauge", | |
77 | Name: "kappa", | |
78 | Help: "Kappa gauge.", | |
79 | }, []string{}), | |
65 | 80 | ) |
66 | 81 | |
67 | 82 | g.Set(34) |
20 | 20 | |
21 | 21 | // NewCounter returns a new Counter backed by a Prometheus metric. The counter |
22 | 22 | // is automatically registered via prometheus.Register. |
23 | func NewCounter(namespace, subsystem, name, help string, fieldKeys []string) metrics.Counter { | |
24 | return NewCounterWithLabels(namespace, subsystem, name, help, fieldKeys, prometheus.Labels{}) | |
25 | } | |
26 | ||
27 | // NewCounterWithLabels is the same as NewCounter, but attaches a set of const | |
28 | // label pairs to the metric. | |
29 | func NewCounterWithLabels(namespace, subsystem, name, help string, fieldKeys []string, constLabels prometheus.Labels) metrics.Counter { | |
30 | m := prometheus.NewCounterVec( | |
31 | prometheus.CounterOpts{ | |
32 | Namespace: namespace, | |
33 | Subsystem: subsystem, | |
34 | Name: name, | |
35 | Help: help, | |
36 | ConstLabels: constLabels, | |
37 | }, | |
38 | fieldKeys, | |
39 | ) | |
23 | func NewCounter(opts prometheus.CounterOpts, fieldKeys []string) metrics.Counter { | |
24 | m := prometheus.NewCounterVec(opts, fieldKeys) | |
40 | 25 | prometheus.MustRegister(m) |
41 | ||
42 | 26 | p := map[string]string{} |
43 | 27 | for _, fieldName := range fieldKeys { |
44 | 28 | p[fieldName] = PrometheusLabelValueUnknown |
45 | 29 | } |
46 | ||
47 | 30 | return prometheusCounter{ |
48 | 31 | CounterVec: m, |
49 | 32 | Pairs: p, |
68 | 51 | |
69 | 52 | // NewGauge returns a new Gauge backed by a Prometheus metric. The gauge is |
70 | 53 | // automatically registered via prometheus.Register. |
71 | func NewGauge(namespace, subsystem, name, help string, fieldKeys []string) metrics.Gauge { | |
72 | return NewGaugeWithLabels(namespace, subsystem, name, help, fieldKeys, prometheus.Labels{}) | |
73 | } | |
74 | ||
75 | // NewGaugeWithLabels is the same as NewGauge, but attaches a set of const | |
76 | // label pairs to the metric. | |
77 | func NewGaugeWithLabels(namespace, subsystem, name, help string, fieldKeys []string, constLabels prometheus.Labels) metrics.Gauge { | |
78 | m := prometheus.NewGaugeVec( | |
79 | prometheus.GaugeOpts{ | |
80 | Namespace: namespace, | |
81 | Subsystem: subsystem, | |
82 | Name: name, | |
83 | Help: help, | |
84 | ConstLabels: constLabels, | |
85 | }, | |
86 | fieldKeys, | |
87 | ) | |
54 | func NewGauge(opts prometheus.GaugeOpts, fieldKeys []string) metrics.Gauge { | |
55 | m := prometheus.NewGaugeVec(opts, fieldKeys) | |
88 | 56 | prometheus.MustRegister(m) |
89 | ||
90 | 57 | return prometheusGauge{ |
91 | 58 | GaugeVec: m, |
92 | 59 | Pairs: pairsFrom(fieldKeys), |
112 | 79 | // determined at collect time by the passed callback function. The callback |
113 | 80 | // determines the value, and fields are ignored, so RegisterCallbackGauge |
114 | 81 | // returns nothing. |
115 | func RegisterCallbackGauge(namespace, subsystem, name, help string, callback func() float64) { | |
116 | RegisterCallbackGaugeWithLabels(namespace, subsystem, name, help, prometheus.Labels{}, callback) | |
117 | } | |
118 | ||
119 | // RegisterCallbackGaugeWithLabels is the same as RegisterCallbackGauge, but | |
120 | // attaches a set of const label pairs to the metric. | |
121 | func RegisterCallbackGaugeWithLabels(namespace, subsystem, name, help string, constLabels prometheus.Labels, callback func() float64) { | |
122 | prometheus.MustRegister(prometheus.NewGaugeFunc( | |
123 | prometheus.GaugeOpts{ | |
124 | Namespace: namespace, | |
125 | Subsystem: subsystem, | |
126 | Name: name, | |
127 | Help: help, | |
128 | ConstLabels: constLabels, | |
129 | }, | |
130 | callback, | |
131 | )) | |
82 | func RegisterCallbackGauge(opts prometheus.GaugeOpts, callback func() float64) { | |
83 | prometheus.MustRegister(prometheus.NewGaugeFunc(opts, callback)) | |
132 | 84 | } |
133 | 85 | |
134 | 86 | type prometheusSummary struct { |
11 | 11 | ) |
12 | 12 | |
13 | 13 | func TestPrometheusLabelBehavior(t *testing.T) { |
14 | c := prometheus.NewCounter("test", "prometheus_label_behavior", "foobar", "Abc def.", []string{"used_key", "unused_key"}) | |
14 | c := prometheus.NewCounter(stdprometheus.CounterOpts{ | |
15 | Namespace: "test", | |
16 | Subsystem: "prometheus_label_behavior", | |
17 | Name: "foobar", | |
18 | Help: "Abc def.", | |
19 | }, []string{"used_key", "unused_key"}) | |
15 | 20 | c.With(metrics.Field{Key: "used_key", Value: "declared"}).Add(1) |
16 | 21 | c.Add(1) |
17 | 22 | |
26 | 31 | } |
27 | 32 | |
28 | 33 | func TestPrometheusCounter(t *testing.T) { |
29 | c := prometheus.NewCounter("test", "prometheus_counter", "foobar", "Lorem ipsum.", []string{}) | |
34 | c := prometheus.NewCounter(stdprometheus.CounterOpts{ | |
35 | Namespace: "test", | |
36 | Subsystem: "prometheus_counter", | |
37 | Name: "foobar", | |
38 | Help: "Lorem ipsum.", | |
39 | }, []string{}) | |
30 | 40 | c.Add(1) |
31 | 41 | c.Add(2) |
32 | 42 | if want, have := strings.Join([]string{ |
48 | 58 | } |
49 | 59 | |
50 | 60 | func TestPrometheusGauge(t *testing.T) { |
51 | c := prometheus.NewGauge("test", "prometheus_gauge", "foobar", "Dolor sit.", []string{}) | |
61 | c := prometheus.NewGauge(stdprometheus.GaugeOpts{ | |
62 | Namespace: "test", | |
63 | Subsystem: "prometheus_gauge", | |
64 | Name: "foobar", | |
65 | Help: "Dolor sit.", | |
66 | }, []string{}) | |
52 | 67 | c.Set(42) |
53 | 68 | if want, have := strings.Join([]string{ |
54 | 69 | `# HELP test_prometheus_gauge_foobar Dolor sit.`, |
70 | 85 | func TestPrometheusCallbackGauge(t *testing.T) { |
71 | 86 | value := 123.456 |
72 | 87 | cb := func() float64 { return value } |
73 | prometheus.RegisterCallbackGauge("test", "prometheus_gauge", "bazbaz", "Help string.", cb) | |
88 | prometheus.RegisterCallbackGauge(stdprometheus.GaugeOpts{ | |
89 | Namespace: "test", | |
90 | Subsystem: "prometheus_gauge", | |
91 | Name: "bazbaz", | |
92 | Help: "Help string.", | |
93 | }, cb) | |
74 | 94 | if want, have := strings.Join([]string{ |
75 | 95 | `# HELP test_prometheus_gauge_bazbaz Help string.`, |
76 | 96 | `# TYPE test_prometheus_gauge_bazbaz gauge`, |