Codebase list golang-github-go-kit-kit / 319c1c7
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
4 changed file(s) with 69 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
1313 "time"
1414
1515 "github.com/apache/thrift/lib/go/thrift"
16 stdprometheus "github.com/prometheus/client_golang/prometheus"
1617 "github.com/streadway/handy/cors"
1718 "github.com/streadway/handy/encoding"
1819 "golang.org/x/net/context"
2324 kitlog "github.com/go-kit/kit/log"
2425 "github.com/go-kit/kit/metrics"
2526 "github.com/go-kit/kit/metrics/expvar"
27 "github.com/go-kit/kit/metrics/prometheus"
2628 "github.com/go-kit/kit/metrics/statsd"
2729 "github.com/go-kit/kit/server"
2830 "github.com/go-kit/kit/tracing/zipkin"
4446 // `package metrics` domain
4547 requests := metrics.NewMultiCounter(
4648 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{}),
4856 )
4957 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{}),
5266 )
5367
5468 // `package tracing` domain
5973
6074 // `package log` domain
6175 var logger kitlog.Logger
62 logger = kitlog.NewPrefixLogger(kitlog.StdlibWriter{})
76 logger = kitlog.NewPrefixLogger(os.Stderr)
6377 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
6781
6882 // Our business and operational domain
6983 var a Add
2222 func TestMultiWith(t *testing.T) {
2323 c := metrics.NewMultiCounter(
2424 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"}),
2631 )
2732
2833 c.Add(1)
4247 func TestMultiCounter(t *testing.T) {
4348 metrics.NewMultiCounter(
4449 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{}),
4656 ).Add(123)
4757
4858 if want, have := "123", stdexpvar.Get("alpha").String(); want != have {
6171 func TestMultiGauge(t *testing.T) {
6272 g := metrics.NewMultiGauge(
6373 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{}),
6580 )
6681
6782 g.Set(34)
2020
2121 // NewCounter returns a new Counter backed by a Prometheus metric. The counter
2222 // 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)
4025 prometheus.MustRegister(m)
41
4226 p := map[string]string{}
4327 for _, fieldName := range fieldKeys {
4428 p[fieldName] = PrometheusLabelValueUnknown
4529 }
46
4730 return prometheusCounter{
4831 CounterVec: m,
4932 Pairs: p,
6851
6952 // NewGauge returns a new Gauge backed by a Prometheus metric. The gauge is
7053 // 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)
8856 prometheus.MustRegister(m)
89
9057 return prometheusGauge{
9158 GaugeVec: m,
9259 Pairs: pairsFrom(fieldKeys),
11279 // determined at collect time by the passed callback function. The callback
11380 // determines the value, and fields are ignored, so RegisterCallbackGauge
11481 // 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))
13284 }
13385
13486 type prometheusSummary struct {
1111 )
1212
1313 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"})
1520 c.With(metrics.Field{Key: "used_key", Value: "declared"}).Add(1)
1621 c.Add(1)
1722
2631 }
2732
2833 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{})
3040 c.Add(1)
3141 c.Add(2)
3242 if want, have := strings.Join([]string{
4858 }
4959
5060 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{})
5267 c.Set(42)
5368 if want, have := strings.Join([]string{
5469 `# HELP test_prometheus_gauge_foobar Dolor sit.`,
7085 func TestPrometheusCallbackGauge(t *testing.T) {
7186 value := 123.456
7287 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)
7494 if want, have := strings.Join([]string{
7595 `# HELP test_prometheus_gauge_bazbaz Help string.`,
7696 `# TYPE test_prometheus_gauge_bazbaz gauge`,