-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
115 lines (96 loc) · 3 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Package metrics is a library for collecting [statsd metrics].
//
// The library's only strong opinion is that your application should catalog the
// names and types of the metrics that it generates. Other than that, it is just
// a convenient, generic wrapper around [go-metrics] for statsd, specifically.
//
// [statsd metrics]: https://github.com/statsd/statsd/blob/master/docs/metric_types.md
// [go-metrics]: https://pkg.go.dev/github.com/armon/go-metrics
package metrics
import (
"time"
"github.com/armon/go-metrics"
)
var global, _ = NewClient()
// Client is configured to emit metrics to a statsd sink.
type Client struct {
addr string
tags []string
statsd goMetrics
}
func (c Client) count(name string, val int64, opts ...metricOption) error {
mo := c.applyOptions(opts...)
c.statsd.IncrCounterWithLabels([]string{name}, float32(val), mo.Labels())
return nil
}
func (c Client) gauge(name string, val float32, opts ...metricOption) error {
mo := c.applyOptions(opts...)
c.statsd.SetGaugeWithLabels([]string{name}, val, mo.Labels())
return nil
}
func (c Client) distribution(name string, val float32, opts ...metricOption) error {
mo := c.applyOptions(opts...)
c.statsd.AddSampleWithLabels([]string{name}, val, mo.Labels())
return nil
}
func (c Client) timing(name string, val time.Duration, opts ...metricOption) error {
return c.distribution(name, float32(val), opts...)
}
// Close should be called when the application shuts down. It will flush metrics
// to the underlying sink.
func (c Client) Close() error {
c.statsd.Shutdown()
return nil
}
type clientOption func(*Client)
// WithPersistentTags configures a set of tags that should be applied to all
// metrics emitted by the [Client]. Tags should be strings of the form key:value.
func WithPersistentTags(tags ...string) clientOption {
return func(c *Client) {
c.tags = tags
}
}
// WithSinkAddress sets the address of the statsd sink to which the [Client] will
// emit metrics. The default address is "127.0.0.1:8125".
func WithSinkAddress(addr string) clientOption {
return func(c *Client) {
c.addr = addr
}
}
// NewClient creates a new [Client].
//
// Options include:
// - [WithPersistentTags]
// - [WithSinkAddress]
func NewClient(opts ...clientOption) (Client, error) {
var none Client
c := Client{tags: []string{}, addr: "127.0.0.1:8125"}
for _, opt := range opts {
opt(&c)
}
sink, err := metrics.NewStatsdSink(c.addr)
if err != nil {
return none, err
}
statsd, err := metrics.New(&metrics.Config{}, sink)
if err != nil {
return none, err
}
c.statsd = statsd
return c, nil
}
// GlobalConfig configures the package's global [Client], allowing for the use
// of the [GlobalEmit] function.
//
// Options include:
// - [WithPersistentTags]
// - [WithSinkAddress]
func GlobalConfig(opts ...clientOption) (err error) {
global, err = NewClient(opts...)
return
}
// GlobalClose can be used to flush any metrics in the global [Client], prior to
// an application shutting down.
func GlobalClose() error {
return global.Close()
}