forked from alexcesaro/statsd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
statsd.go
179 lines (159 loc) · 4.01 KB
/
statsd.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package statsd
import "time"
// A Client represents a StatsD client.
type Client struct {
conn *conn
muted bool
rate float32
prefix string
tags string
}
// New returns a new Client, which will always be non-nil, but will be muted
// (permanently inert / stubbed) if returned with an error, or if the Mute
// option was set.
func New(opts ...Option) (*Client, error) {
// The default configuration.
conf := &config{
Client: clientConfig{
Rate: 1,
},
Conn: connConfig{
Addr: ":8125",
FlushPeriod: 100 * time.Millisecond,
// Worst-case scenario:
// Ethernet MTU - IPv6 Header - TCP Header = 1500 - 40 - 20 = 1440
MaxPacketSize: 1440,
Network: "udp",
},
}
for _, o := range opts {
o(conf)
}
conn, err := newConn(conf.Conn, conf.Client.Muted)
c := &Client{
conn: conn,
muted: conf.Client.Muted,
}
if err != nil {
c.muted = true
return c, err
}
c.rate = conf.Client.Rate
c.prefix = conf.Client.Prefix
c.tags = joinTags(conf.Conn.TagFormat, conf.Client.Tags)
return c, nil
}
// Clone returns a clone of the Client. The cloned Client inherits its
// configuration from its parent.
//
// All cloned Clients share the same connection, so cloning a Client is a cheap
// operation.
func (c *Client) Clone(opts ...Option) *Client {
tf := c.conn.tagFormat
conf := &config{
Client: clientConfig{
Rate: c.rate,
Prefix: c.prefix,
Tags: splitTags(tf, c.tags),
},
}
for _, o := range opts {
o(conf)
}
clone := &Client{
conn: c.conn,
muted: c.muted || conf.Client.Muted,
rate: conf.Client.Rate,
prefix: conf.Client.Prefix,
tags: joinTags(tf, conf.Client.Tags),
}
clone.conn = c.conn
return clone
}
// Count adds n to bucket.
func (c *Client) Count(bucket string, n interface{}) {
if c.skip() {
return
}
c.conn.metric(c.prefix, bucket, n, "c", c.rate, c.tags)
}
func (c *Client) skip() bool {
return c.muted || (c.rate != 1 && randFloat() > c.rate)
}
// Increment increments the given bucket. It is equivalent to Count(bucket, 1).
func (c *Client) Increment(bucket string) {
c.Count(bucket, 1)
}
// Gauge records an absolute value for the given bucket.
func (c *Client) Gauge(bucket string, value interface{}) {
if c.skip() {
return
}
c.conn.gauge(c.prefix, bucket, value, c.tags)
}
// GaugeRelative records a relative value for the given bucket.
func (c *Client) GaugeRelative(bucket string, value interface{}) {
if c.skip() {
return
}
c.conn.gaugeRelative(c.prefix, bucket, value, c.tags)
}
// Timing sends a timing value to a bucket.
func (c *Client) Timing(bucket string, value interface{}) {
if c.skip() {
return
}
c.conn.metric(c.prefix, bucket, value, "ms", c.rate, c.tags)
}
// Histogram sends a histogram value to a bucket.
func (c *Client) Histogram(bucket string, value interface{}) {
if c.skip() {
return
}
c.conn.metric(c.prefix, bucket, value, "h", c.rate, c.tags)
}
// Timing is a helper object that eases sending timing values.
type Timing struct {
start time.Time
c *Client
}
// NewTiming creates a new Timing.
func (c *Client) NewTiming() Timing {
return Timing{start: now(), c: c}
}
// Send sends the time elapsed since the creation of the Timing.
func (t Timing) Send(bucket string) {
t.c.Timing(bucket, int(t.Duration()/time.Millisecond))
}
// Duration returns the time elapsed since the creation of the Timing.
func (t Timing) Duration() time.Duration {
return now().Sub(t.start)
}
// Unique sends the given value to a set bucket.
func (c *Client) Unique(bucket string, value string) {
if c.skip() {
return
}
c.conn.unique(c.prefix, bucket, value, c.tags)
}
// Flush flushes the Client's buffer.
func (c *Client) Flush() {
if c.muted {
return
}
c.conn.mu.Lock()
c.conn.flush(0)
c.conn.mu.Unlock()
}
// Close flushes the Client's buffer and releases the associated ressources. The
// Client and all the cloned Clients must not be used afterward.
func (c *Client) Close() {
if c.muted {
return
}
c.conn.mu.Lock()
c.conn.flush(0)
c.conn.handleError(c.conn.w.Close())
c.conn.closed = true
c.conn.mu.Unlock()
}