This repository has been archived by the owner on Jan 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
178 lines (152 loc) · 4.29 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
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
package statsd
import (
"errors"
"fmt"
"log"
"math/rand"
"net"
"strings"
"sync"
"time"
)
const metricTypeCount = "c"
const metricTypeGauge = "g"
const metricTypeTiming = "ms"
const metricTypeSet = "s"
// The Client type
type Client struct {
host string
port int
conn net.Conn // UDP connection to StatsD server
rand *rand.Rand // rand generator to skip messages by sample rate
keyBuffer []string // array of messages to send
keyBufferLock sync.RWMutex // mutex to lock buffer of keys
buffered bool // send metrics on every call
}
// NewClient creates new StatsD client with disabled buffer.
func NewClient(host string, port int) *Client {
client := Client{
host: host,
port: port,
rand: rand.New(rand.NewSource(time.Now().Unix())),
keyBuffer: nil,
}
return &client
}
// NewBufferedClient creates new StatsD client with enabled buffer.
// Manual call of Flush() required to send metrics to StatsD server.
func NewBufferedClient(host string, port int) *Client {
client := Client{
host: host,
port: port,
rand: rand.New(rand.NewSource(time.Now().Unix())),
keyBuffer: make([]string, 0),
}
return &client
}
// Open UDP connection to statsd server
func (client *Client) Open() {
connectionString := fmt.Sprintf("%s:%d", client.host, client.port)
conn, err := net.Dial("udp", connectionString)
if err != nil {
log.Println(err)
}
client.conn = conn
}
// Close UDP connection to statsd server
func (client *Client) Close() {
client.conn.Close()
client.conn = nil
}
// Timing track in milliseconds with sampling
func (client *Client) Timing(key string, time int64, sampleRate float32) {
metricValue := fmt.Sprintf("%d|%s", time, metricTypeTiming)
if sampleRate < 1 {
if client.isSendAcceptedBySampleRate(sampleRate) {
metricValue = fmt.Sprintf("%s|@%g", metricValue, sampleRate)
} else {
return
}
}
client.addToBuffer(key, metricValue)
}
// Count tack
func (client *Client) Count(key string, value int, sampleRate float32) {
metricValue := fmt.Sprintf("%d|%s", value, metricTypeCount)
if sampleRate < 1 {
if client.isSendAcceptedBySampleRate(sampleRate) {
metricValue = fmt.Sprintf("%s|@%g", metricValue, sampleRate)
} else {
return
}
}
client.addToBuffer(key, metricValue)
}
// Gauge track
func (client *Client) Gauge(key string, value int) {
metricValue := fmt.Sprintf("%d|%s", value, metricTypeGauge)
client.addToBuffer(key, metricValue)
}
// GaugeShift decrease previously set value if negative value passed, and increase if positive.
func (client *Client) GaugeShift(key string, value int) {
metricValue := fmt.Sprintf("%+d|%s", value, metricTypeGauge)
client.addToBuffer(key, metricValue)
}
// Set tracking
func (client *Client) Set(key string, value int) {
metricValue := fmt.Sprintf("%d|%s", value, metricTypeSet)
client.addToBuffer(key, metricValue)
}
// add to buffer and flush if auto flush enabled
func (client *Client) addToBuffer(key string, metricValue string) {
// build metric
metric := fmt.Sprintf("%s:%s", key, metricValue)
// flush
if client.keyBuffer == nil {
// send metric now
go client.send(metric)
} else {
// add metric to buffer for next manual flush
client.keyBufferLock.Lock()
client.keyBuffer = append(client.keyBuffer, metric)
client.keyBufferLock.Unlock()
}
}
// Check if acceptable by sample rate
func (client *Client) isSendAcceptedBySampleRate(sampleRate float32) bool {
if sampleRate >= 1 {
return true
}
randomNumber := client.rand.Float32()
return randomNumber <= sampleRate
}
// Flush buffer to statsd daemon by UDP when buffer disabled
func (client *Client) Flush() error {
// check if buffer enabled
if client.keyBuffer == nil {
return errors.New("Invalid call of flush in unbuffered mode")
}
// check if buffer has metrics
if len(client.keyBuffer) == 0 {
return nil
}
// lock
client.keyBufferLock.Lock()
// build packet
metricPacket := strings.Join(client.keyBuffer, "\n")
// clear key buffer
client.keyBuffer = make([]string, 0)
// lock
client.keyBufferLock.Unlock()
// send packet
go client.send(metricPacket)
return nil
}
// Send StatsD packet
func (client *Client) send(metricPacket string) {
// send metric packet
_, err := fmt.Fprintf(client.conn, metricPacket)
if err != nil {
log.Println(err)
}
}