-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatsd.go
More file actions
57 lines (47 loc) · 1.09 KB
/
statsd.go
File metadata and controls
57 lines (47 loc) · 1.09 KB
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
package main
import (
"log"
"time"
"github.com/alexcesaro/statsd"
)
type StatsdClient struct {
*statsd.Client
options []statsd.Option
muted bool
}
func (sc *StatsdClient) Muted() bool {
return sc.muted
}
func NewStatsdClient(opts ...statsd.Option) (*StatsdClient, error) {
statsd_client, err := statsd.New(opts...)
return &StatsdClient{
Client: statsd_client,
options: opts,
muted: err != nil,
}, err
}
type StatsdTracker struct {
*StatsdClient
}
func NewStatsdTracker(client *StatsdClient) *StatsdTracker {
return &StatsdTracker{client}
}
func (st *StatsdTracker) Track(event *Event) error {
if st.Muted() {
st.StatsdClient, err = NewStatsdClient(st.StatsdClient.options...)
if nil != err {
log.Println(err)
}
}
switch event.Properties["Type"] {
case Timer:
st.Timing(event.Event, int(event.Properties["Value"].(time.Duration)/time.Millisecond))
case Counter:
st.Count(event.Event, event.Properties["Value"])
case Gauge:
st.Gauge(event.Event, event.Properties["Value"])
case Set:
st.Unique(event.Event, event.Properties["Value"].(string))
}
return nil
}