forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapm.go
404 lines (342 loc) · 10.1 KB
/
apm.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package glog
import (
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/DataDog/datadog-go/statsd"
)
var (
logInterval int
cMap *counterMap
pcMap *counterMap
ddCli *statsd.Client
ddAgent string
ddNamespace string
ddTags string
printStack bool
mLogOnce sync.Once
hLogOnce sync.Once
ddOnce sync.Once
)
const (
separator = ";"
)
type counterMap struct {
m map[string]*dataCounter
ll sync.RWMutex
}
func newCounterMap() *counterMap {
return &counterMap{
m: make(map[string]*dataCounter),
}
}
func initAPM() {
flag.IntVar(&logInterval, "logint", 60, "histogram log interval in seconds")
flag.StringVar(&ddAgent, "ddAgent", "127.0.0.1:8125", "address of the datadog statsd agent as [HOST]:[PORT]")
flag.StringVar(&ddNamespace, "ddNamespace", "wirelessregistry", "namespace to send metrics with")
flag.StringVar(&ddTags, "ddTags", "", "tags to send metrics with")
flag.BoolVar(&printStack, "printStack", false, "print the stack")
}
// SetLogInterval sets the log interval for histogram and memory consumption logging.
// Interval is provided as seconds. Defaults to 60s
func SetLogInterval(l int) {
logInterval = l
}
// LogTimeTaken logs the elapsed from the time provided to the time the function is called.
func LogTimeTaken(name string, start time.Time) {
elapsed := time.Since(start)
Infof("%s took %s", name, elapsed)
}
// StartHistogramLogging launches the periodic logging of the counters.
// All counters are reset after reading their value.
func StartHistogramLogging() {
hLogOnce.Do(func() {
cMap = newCounterMap()
pcMap = newCounterMap()
initPeriodicLogger(time.Duration(logInterval)*time.Second, writeHistogram)
initPeriodicLogger(time.Duration(logInterval)*time.Second, writePersistentCounter)
Infof("printstack %v", printStack)
if printStack {
Info("Init printstack")
initPeriodicLogger(time.Duration(logInterval)*time.Second, writeStack)
}
})
}
// StartDatadog initializes the connection to the datadog agent and launces the periodic send of metrics to datadog
func StartDatadog(agent string) {
ddOnce.Do(func() {
if agent != "" {
ddAgent = agent
}
Infof("Initializing datadog agent at %s\n", ddAgent)
c, err := statsd.New(ddAgent)
if err != nil {
Error(err)
}
// prefix every metric with the app name
c.Namespace = ddNamespace + "."
// send the EC2 availability zone as a tag with every metric
c.Tags = append(c.Tags, ddTags)
c.Tags = append(c.Tags, fmt.Sprintf("binary:%s", defaultBinaryName()))
hostname, _ := os.Hostname()
c.Tags = append(c.Tags, fmt.Sprintf("hostname:%s", hostname))
Infof("Datadog agent ready with namespace '%s' and tags '%s'\n", c.Namespace, c.Tags)
ddCli = c
})
}
func defaultBinaryName() string {
bn := os.Args[0]
// cut path information from executable
i := strings.LastIndex(bn, "/")
if i > -1 {
bn = bn[i+1:]
}
return bn
}
func initPeriodicLogger(t time.Duration, fn func()) chan struct{} {
closeCh := make(chan struct{}, 1)
go func() {
t := time.NewTicker(t)
for {
select {
case <-closeCh:
t.Stop()
return
case <-t.C:
fn()
}
}
}()
return closeCh
}
type dataCounter struct {
i *int64
}
// IncCounter increments the referenced counter by the given value or creates it if it does not exist yet.
func IncCounter(name string, value int64) {
IncTaggedCounter(name, []string{}, value)
}
// IncCounter increments the referenced tagged counter by the given value or creates it if it does not exist yet.
func IncTaggedCounter(name string, tags []string, value int64) {
if cMap == nil {
Errorf("IncCounter() called before StartHistogramLogging()")
return
}
modifyCounter(cMap, name, tags, value, incCounterValue)
}
// DecCounter decrements the referenced counter by the given value or creates it if it does not exist yet.
func DecCounter(name string, value int64) {
DecTaggedCounter(name, []string{}, value)
}
// DecCounter decrements the referenced tagged counter by the given value or creates it if it does not exist yet.
func DecTaggedCounter(name string, tags []string, value int64) {
if cMap == nil {
Errorf("DecCounter() called before StartHistogramLogging()")
return
}
modifyCounter(cMap, name, tags, value, decCounterValue)
}
/// SetCounter sets the referenced counter to the given value or creates it if it does not exist yet.
func SetCounter(name string, value int64) {
SetTaggedCounter(name, []string{}, value)
}
// SetCounter sets the referenced tagged counter to the given value or creates it if it does not exist yet.
func SetTaggedCounter(name string, tags []string, value int64) {
if cMap == nil {
Errorf("SetCounter() called before StartHistogramLogging()")
return
}
modifyCounter(cMap, name, tags, value, setCounterValue)
}
// IncCounter increments the referenced counter by the given value or creates it if it does not exist yet.
func IncPCounter(name string, value int64) {
IncTaggedPCounter(name, []string{}, value)
}
// IncCounter increments the referenced tagged counter by the given value or creates it if it does not exist yet.
func IncTaggedPCounter(name string, tags []string, value int64) {
if pcMap == nil {
Errorf("IncCounter() called before StartHistogramLogging()")
return
}
modifyCounter(pcMap, name, tags, value, incCounterValue)
}
// DecCounter decrements the referenced counter by the given value or creates it if it does not exist yet.
func DecPCounter(name string, value int64) {
DecTaggedPCounter(name, []string{}, value)
}
// DecCounter decrements the referenced tagged counter by the given value or creates it if it does not exist yet.
func DecTaggedPCounter(name string, tags []string, value int64) {
if pcMap == nil {
Errorf("DecCounter() called before StartHistogramLogging()")
return
}
modifyCounter(pcMap, name, tags, value, decCounterValue)
}
/// SetCounter sets the referenced counter to the given value or creates it if it does not exist yet.
func SetPCounter(name string, value int64) {
SetTaggedPCounter(name, []string{}, value)
}
// SetCounter sets the referenced tagged counter to the given value or creates it if it does not exist yet.
func SetTaggedPCounter(name string, tags []string, value int64) {
if cMap == nil {
Errorf("SetCounter() called before StartHistogramLogging()")
return
}
modifyCounter(pcMap, name, tags, value, setCounterValue)
}
func setCounterValue(c *dataCounter, value int64) {
c.set(value)
}
func incCounterValue(c *dataCounter, value int64) {
c.increment(value)
}
func decCounterValue(c *dataCounter, value int64) {
c.decrement(value)
}
func modifyCounter(cm *counterMap, name string, tags []string, value int64, modify func(*dataCounter, int64)) {
key := encodeCounterKey(name, tags)
cm.ll.RLock()
_, ok := cm.m[key]
if !ok {
cm.ll.RUnlock()
cm.ll.Lock()
_, ok := cm.m[key]
if !ok {
c := newDataCounter()
modify(c, value)
cm.m[key] = c
} else {
modify(cm.m[key], value)
}
cm.ll.Unlock()
} else {
modify(cm.m[key], value)
cm.ll.RUnlock()
}
}
// encodeCounterKey encodes the name and tags as a byte in a string.
// The first byte gives the position of the tags in the array.
// If the first byte is 0, the tags are empty.
func encodeCounterKey(name string, tags []string) string {
var key []byte
if len(tags) == 0 {
key = append(key, 0)
key = append(key, ([]byte(name))...)
} else {
key = append(key, byte(len(name)+1))
key = append(key, ([]byte(name))...)
for i, tag := range tags {
if i != 0 {
key = append(key, ([]byte(separator))...)
}
key = append(key, ([]byte(tag))...)
}
}
return string(key[:])
}
// decodeCounterKey extraces the name and tags from the key.
// if the first byte is 0, the tags are considered being empty
func decodeCounterKey(key string) (string, []string) {
bkey := []byte(key)
tagPos := int(bkey[0])
if tagPos == 0 {
return string(key[1:]), []string{}
}
tags := strings.Split(string(key[tagPos:]), separator)
return string(key[1:tagPos]), tags
}
func writeHistogram() {
for key, c := range cMap.m {
val := c.reset()
name, tags := decodeCounterKey(key)
writeMetric(name, tags, val)
}
writeMemoryConsumption()
}
func writePersistentCounter() {
for key, c := range pcMap.m {
val := c.read()
name, tags := decodeCounterKey(key)
writeMetric(name, tags, val)
}
}
func writeStack() {
Info("write printstack")
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
}
func ResetPersistentCounter() {
for _, c := range pcMap.m {
c.reset()
}
}
func writeMemoryConsumption() {
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
writeMetric("memusage.alloc", []string{}, int64(mem.Alloc))
writeMetric("memusage.stackinuse", []string{}, int64(mem.StackInuse))
writeMetric("memusage.heapalloc", []string{}, int64(mem.HeapAlloc))
writeMetric("memusage.heapinuse", []string{}, int64(mem.HeapInuse))
writeMetric("memusage.numgoroutine", []string{}, int64(runtime.NumGoroutine()))
}
func writeMetric(name string, tags []string, val int64) {
logMetric(name, tags, val)
sendMetric(name, tags, val)
}
func logMetric(name string, tags []string, val int64) {
if len(tags) == 0 {
Infof("%s: %d\n", name, val)
} else {
Infof("%s#%v: %d\n", name, tags, val)
}
}
func sendMetric(name string, tags []string, val int64) {
if ddCli == nil {
return
}
ddCli.Gauge(name, float64(val), tags, 1)
}
func newDataCounter() *dataCounter {
return &dataCounter{
i: new(int64),
}
}
func (d *dataCounter) increment(val int64) {
atomic.AddInt64(d.i, val)
}
func (d *dataCounter) decrement(val int64) {
atomic.AddInt64(d.i, -val)
}
func (d *dataCounter) set(val int64) {
atomic.StoreInt64(d.i, val)
}
func (d *dataCounter) read() int64 {
return atomic.LoadInt64(d.i)
}
func (d *dataCounter) reset() int64 {
current := atomic.LoadInt64(d.i)
atomic.StoreInt64(d.i, 0)
return current
}
type Trace struct {
key string
tags []string
start time.Time
}
func StartTrace(key string, tags []string) *Trace {
return &Trace{
key: key,
tags: tags,
start: time.Now(),
}
}
func (t *Trace) Stop() {
delta := time.Since(t.start).Nanoseconds() / 1000000
IncTaggedCounter(t.key+".processed", t.tags, 1)
IncTaggedCounter(t.key+".processingtime", t.tags, delta)
}