-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstats.go
129 lines (105 loc) · 3.22 KB
/
stats.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
package templar
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/amir/raidman"
)
type DebugStats struct{}
func (d *DebugStats) StartRequest(req *http.Request) {
fmt.Printf("[%s] S %s %s\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL)
}
func (d *DebugStats) Emit(req *http.Request, dur time.Duration) {
fmt.Printf("[%s] E %s %s (%s)\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL, dur)
}
func (d *DebugStats) RequestTimeout(req *http.Request, timeout time.Duration) {
fmt.Printf("[%s] T %s %s (%s)\n", time.Now().Format(time.RFC3339Nano), req.Method, req.URL, timeout)
}
var _ = Stats(&DebugStats{})
type StatsdOutput struct {
client StatsdClient
}
var _ = Stats(&StatsdOutput{})
func NewStatsdOutput(client StatsdClient) *StatsdOutput {
return &StatsdOutput{client}
}
func (s *StatsdOutput) url(req *http.Request) string {
return req.Host + strings.Replace(req.URL.Path, "/", "-", -1)
}
func (s *StatsdOutput) StartRequest(req *http.Request) {
s.client.Incr("templar.request.method."+req.Method, 1)
s.client.Incr("templar.request.host."+req.Host, 1)
s.client.Incr("templar.request.url."+s.url(req), 1)
s.client.GaugeDelta("templar.requests.active", 1)
}
func (s *StatsdOutput) Emit(req *http.Request, delta time.Duration) {
s.client.GaugeDelta("templar.requests.active", -1)
s.client.PrecisionTiming("templar.request.url."+s.url(req), delta)
}
func (s *StatsdOutput) RequestTimeout(req *http.Request, timeout time.Duration) {
s.client.Incr("templar.timeout.host."+req.Host, 1)
s.client.Incr("templar.timeout.url."+s.url(req), 1)
}
type RiemannOutput struct {
client RiemannClient
}
func NewRiemannOutput(client RiemannClient) *RiemannOutput {
return &RiemannOutput{client}
}
func (r *RiemannOutput) StartRequest(req *http.Request) {
attributes := make(map[string]string)
attributes["method"] = req.Method
attributes["host"] = req.Host
attributes["path"] = req.URL.Path
var event = &raidman.Event{
State: "ok",
Service: "templar request",
Metric: 1,
Attributes: attributes,
}
r.client.Send(event)
}
func (r *RiemannOutput) Emit(req *http.Request, delta time.Duration) {
attributes := make(map[string]string)
attributes["method"] = req.Method
attributes["host"] = req.Host
attributes["path"] = req.URL.Path
var event = &raidman.Event{
State: "ok",
Service: "templar response",
Metric: 1000.0 * delta.Seconds(),
Attributes: attributes,
}
r.client.Send(event)
}
func (r *RiemannOutput) RequestTimeout(req *http.Request, timeout time.Duration) {
attributes := make(map[string]string)
attributes["method"] = req.Method
attributes["host"] = req.Host
attributes["path"] = req.URL.Path
var event = &raidman.Event{
State: "warning",
Service: "templar timeout",
Metric: timeout.Seconds() * 1000.0,
Attributes: attributes,
}
r.client.Send(event)
}
type MultiStats []Stats
var _ = Stats(MultiStats{})
func (m MultiStats) StartRequest(req *http.Request) {
for _, s := range m {
s.StartRequest(req)
}
}
func (m MultiStats) Emit(req *http.Request, t time.Duration) {
for _, s := range m {
s.Emit(req, t)
}
}
func (m MultiStats) RequestTimeout(req *http.Request, timeout time.Duration) {
for _, s := range m {
s.RequestTimeout(req, timeout)
}
}