/
datadog.go
292 lines (268 loc) · 9.65 KB
/
datadog.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// +build linux darwin freebsd
package commands
import (
"fmt"
"os"
"github.com/PagerDuty/godspeed"
"gopkg.in/zorkian/go-datadog-api.v1"
)
// StatsdSetup sets up the connection to dogstatsd.
func StatsdSetup() *godspeed.Godspeed {
if DogStatsdAddress != "" {
statsd, err := godspeed.New(DogStatsdHost, DogStatsdPort, false)
if err != nil {
Log(fmt.Sprintf("StatsdSetup(): Problem setting up connection. DogStatsdHost='%s' DogStatsdPort='%b'", DogStatsdHost, DogStatsdPort), "info")
return nil
}
return statsd
}
statsd, err := godspeed.NewDefault()
if err != nil {
Log("StatsdSetup(): Problem setting up connection.", "info")
return nil
}
return statsd
}
// StatsdIn sends metrics to Dogstatsd on a `kvexpress in` operation.
func StatsdIn(key string, dataLength int, data string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' stats='in'", DogStatsd, key), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, "complete")
statsd.Incr("kvexpress.in", tags)
statsd.Gauge("kvexpress.bytes", float64(dataLength), tags)
statsd.Gauge("kvexpress.lines", float64(LineCount(data)), tags)
}
}
}
// StatsdOut sends metrics to Dogstatsd on a `kvexpress out` operation.
func StatsdOut(key string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' stats='out'", DogStatsd, key), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, "complete")
statsd.Incr("kvexpress.out", tags)
}
}
}
// StatsdLocked sends metrics to Dogstatsd on a `kvexpress out` operation
// that is blocked by a locked file.
func StatsdLocked(file string) {
Log(fmt.Sprintf("dogstatsd='%t' file='%s' stats='locked'", DogStatsd, file), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(file, "complete")
statsd.Incr("kvexpress.locked", tags)
}
}
}
// StatsdLength sends metrics to Dogstatsd on a `kvexpress out` operation
// where the file isn't long enough.
func StatsdLength(key string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' stats='not_long_enough'", DogStatsd, key), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, "not_long_enough")
statsd.Incr("kvexpress.not_long_enough", tags)
}
}
}
// StatsdChecksum sends metrics to Dogstatsd on a `kvexpress out` operation
// where the checksum doesn't match.
func StatsdChecksum(key string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' stats='checksum_mismatch'", DogStatsd, key), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, "checksum_mismatch")
statsd.Incr("kvexpress.checksum_mismatch", tags)
}
}
}
// StatsdLock sends metrics to Dogstatsd on a `kvexpress lock` operation.
func StatsdLock(key string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' stats='lock'", DogStatsd, key), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, "complete")
statsd.Incr("kvexpress.lock", tags)
}
}
}
// StatsdUnlock sends metrics to Dogstatsd on a `kvexpress unlock` operation.
func StatsdUnlock(key string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' stats='unlock'", DogStatsd, key), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, "complete")
statsd.Incr("kvexpress.unlock", tags)
}
}
}
// StatsdRaw sends metrics to Dogstatsd on a `kvexpress raw` operation.
func StatsdRaw(key string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' stats='raw'", DogStatsd, key), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, "complete")
statsd.Incr("kvexpress.raw", tags)
}
}
}
// StatsdReconnect sends metrics when we have Consul connection retries.
func StatsdReconnect(times int) {
Log(fmt.Sprintf("dogstatsd='%t' reconnect='%d'", DogStatsd, times), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := make([]string, 2)
hostname := GetHostname()
hostTag := fmt.Sprintf("host:%s", hostname)
directionTag := fmt.Sprintf("direction:%s", Direction)
tags = append(tags, hostTag)
tags = append(tags, directionTag)
statsd.Incr("kvexpress.consul_reconnect", tags)
}
}
}
// StatsdRunTime sends metrics to Dogstatsd on various operations.
func StatsdRunTime(key string, location string, msec int64) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' location='%s' msec='%d'", DogStatsd, key, location, msec), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, location)
locationTag := fmt.Sprintf("location:%s", location)
tags = append(tags, locationTag)
statsd.Gauge("kvexpress.time", float64(msec), tags)
}
}
}
// StatsdPanic sends metrics to Dogstatsd when something really bad happens.
// It also stops the execution of kvexpress.
func StatsdPanic(key, location string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' location='%s' stats='panic'", DogStatsd, key, location), "debug")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, location)
statsd.Incr("kvexpress.panic", tags)
}
}
// If we're going to panic, we might as well stop right here.
// Means we can't connect to Consul, download a URL or
// write and/or chown files.
os.Exit(0)
}
// StatsdConsul sends metrics to DogStatsd when Consul has a KV write or delete error.
func StatsdConsul(key, location string) {
Log(fmt.Sprintf("dogstatsd='%t' key='%s' location='%s' stats='consul_error'", DogStatsd, key, location), "info")
if DogStatsd {
statsd := StatsdSetup()
if statsd != nil {
defer statsd.Conn.Close()
tags := makeTags(key, location)
statsd.Incr("kvexpress.consul_error", tags)
}
}
}
// DDAPIConnect connects to the Datadog API and returns a client object.
func DDAPIConnect(api, app string) *datadog.Client {
client := datadog.NewClient(api, app)
return client
}
// makeTags creates some standard tags for use with Dogstatsd and the Datadog API.
func makeTags(key, location string) []string {
tags := make([]string, 4)
keyTag := fmt.Sprintf("key:%s", key)
hostname := GetHostname()
hostTag := fmt.Sprintf("host:%s", hostname)
directionTag := fmt.Sprintf("direction:%s", Direction)
locationTag := fmt.Sprintf("location:%s", location)
tags = append(tags, keyTag)
tags = append(tags, hostTag)
tags = append(tags, directionTag)
tags = append(tags, locationTag)
return tags
}
// TODO: These three functions are ripe for refactoring to be more Golang like.
// DDStopEvent sends a Datadog event to the API when there's a stop key present.
func DDStopEvent(dd *datadog.Client, key, value string) {
Log(fmt.Sprintf("datadog='true' DDStopEvent='true' key='%s'", key), "debug")
tags := makeTags(key, "stop_key_present")
tags = append(tags, "kvexpress:stop")
title := fmt.Sprintf("Stop key is present: %s. Stopping.", key)
event := datadog.Event{Title: title, Text: value, AlertType: "error", Tags: tags}
post, err := dd.PostEvent(&event)
if (post == nil) || (err != nil) {
Log("DDStopEvent(): Error posting to Datadog.", "info")
}
}
// DDLengthEvent sends a Datadog event to the API when the file/url is too short.
func DDLengthEvent(dd *datadog.Client, key, value string) {
Log(fmt.Sprintf("datadog='true' DDLengthEvent='true' key='%s'", key), "debug")
tags := makeTags(key, "not_long_enough")
tags = append(tags, "kvexpress:length")
title := fmt.Sprintf("Not long enough: %s. Stopping.", key)
event := datadog.Event{Title: title, Text: value, AlertType: "error", Tags: tags}
post, err := dd.PostEvent(&event)
if (post == nil) || (err != nil) {
Log("DDLengthEvent(): Error posting to Datadog.", "info")
}
}
// DDSaveDataEvent sends a Datadog event to the API when we have updated a Consul key.
func DDSaveDataEvent(dd *datadog.Client, key, value string) {
Log(fmt.Sprintf("datadog='true' DDSaveDataEvent='true' key='%s'", key), "debug")
tags := makeTags(key, "complete")
tags = append(tags, "kvexpress:success")
title := fmt.Sprintf("Updated: %s", key)
event := datadog.Event{Title: title, Text: value, AlertType: "info", Tags: tags}
post, err := dd.PostEvent(&event)
if (post == nil) || (err != nil) {
Log("DDSaveDataEvent(): Error posting to Datadog.", "info")
}
}
// DDCopyDataEvent sends a Datadog event to the API when we have used `kvexpress copy`
// to copy a Consul key.
func DDCopyDataEvent(dd *datadog.Client, keyFrom, keyTo string) {
Log(fmt.Sprintf("datadog='true' DDCopyDataEvent='true' keyFrom='%s' keyTo='%s'", keyFrom, keyTo), "debug")
tags := makeTags(keyTo, "complete")
tags = append(tags, "kvexpress:success")
tags = append(tags, fmt.Sprintf("keyFrom:%s", keyFrom))
title := fmt.Sprintf("Copy: %s to %s", keyFrom, keyTo)
event := datadog.Event{Title: title, Text: title, AlertType: "info", Tags: tags}
post, err := dd.PostEvent(&event)
if (post == nil) || (err != nil) {
Log("DDCopyDataEvent(): Error posting to Datadog.", "info")
}
}
// DDSaveStopEvent sends a Datadog event when we have added a stop key to Consul.
func DDSaveStopEvent(dd *datadog.Client, key, value string) {
Log(fmt.Sprintf("datadog='true' DDSaveStopEvent='true' key='%s'", key), "debug")
tags := makeTags(key, "stop_key_save")
tags = append(tags, "kvexpress:stop_set")
title := fmt.Sprintf("Set Stop Key: %s", key)
event := datadog.Event{Title: title, Text: value, AlertType: "warning", Tags: tags}
post, err := dd.PostEvent(&event)
if (post == nil) || (err != nil) {
Log("DDSaveStopEvent(): Error posting to Datadog.", "info")
}
}