-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamqp.go
173 lines (158 loc) · 4.09 KB
/
amqp.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
package metrics_amqp
import (
"encoding/json"
"github.com/rcrowley/go-metrics"
"github.com/streadway/amqp"
"log"
uurl "net/url"
"time"
)
type reporter struct {
reg metrics.Registry
interval time.Duration
url string
connection *amqp.Connection
qname string
}
type datum struct {
Name string
Fields map[string]interface{}
}
func Amqp(r metrics.Registry, d time.Duration, url string, qn string) {
_, err := uurl.Parse(url)
if err != nil {
log.Printf("Unable to parse AMQP URL %s: %s", url, err)
return
}
rep := &reporter{
reg: r,
interval: d,
url: url,
qname: qn,
}
if err := rep.makeClient(); err != nil {
log.Printf("Unable to connect to AMQP URL %s: %v", url, err)
return
}
log.Printf("AMQP connection to %s established", url)
rep.run()
}
func (r *reporter) makeClient() (err error) {
r.connection, err = amqp.Dial(r.url)
if err != nil {
return err
}
channel, err := r.connection.Channel()
defer channel.Close()
if err != nil {
return err
}
log.Printf("Creating AMQP queue %s", r.qname)
durable, exclusive := false, false
autoDelete, noWait := true, true
q, _ := channel.QueueDeclare(r.qname, durable, autoDelete, exclusive, noWait, nil)
channel.QueueBind(q.Name, "#", "amq.topic", false, nil)
return
}
func (r *reporter) run() {
for t := range time.Tick(r.interval * time.Second) {
if err := r.send(); err != nil {
log.Printf("Unable to send metrics to AMQP %s at %s: %v", r.url, t, err)
}
log.Printf("Sent metrics")
}
}
func (r *reporter) send() (err error) {
var m datum
r.reg.Each(func(name string, i interface{}) {
now := time.Now()
switch metric := i.(type) {
case metrics.Counter:
m = datum{
Name: name,
Fields: map[string]interface{}{"count": metric.Count()},
}
case metrics.Gauge: //, metrics.GaugeFloat64:
m = datum{
Name: name,
Fields: map[string]interface{}{"value": metric.Value()},
}
case metrics.GaugeFloat64:
m = datum{
Name: name,
Fields: map[string]interface{}{"value": metric.Value()},
}
case metrics.Histogram:
ps := metric.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999})
// Remember to take a snapshot of complex metrics for consistent data
metric = metric.Snapshot()
m = datum{
Name: name,
Fields: map[string]interface{}{"count": metric.Count(),
"max": metric.Max(),
"mean": metric.Mean(),
"min": metric.Min(),
"stddev": metric.StdDev(),
"variance": metric.Variance(),
"p50": ps[0],
"p75": ps[1],
"p95": ps[2],
"p99": ps[3],
"p999": ps[4],
"p9999": ps[5]},
}
case metrics.Meter:
metric = metric.Snapshot()
m = datum{
Name: name,
Fields: map[string]interface{}{"count": metric.Count(),
"m1": metric.Rate1(),
"m5": metric.Rate5(),
"m15": metric.Rate15(),
"mean": metric.RateMean()},
}
case metrics.Timer:
metric = metric.Snapshot()
ps := metric.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999})
m = datum{
Name: name,
Fields: map[string]interface{}{"count": metric.Count(),
"max": metric.Max(),
"mean": metric.Mean(),
"min": metric.Min(),
"stddev": metric.StdDev(),
"variance": metric.Variance(),
"p50": ps[0],
"p75": ps[1],
"p95": ps[2],
"p99": ps[3],
"p999": ps[4],
"p9999": ps[5],
"m1": metric.Rate1(),
"m5": metric.Rate5(),
"m15": metric.Rate15(),
"meanrate": metric.RateMean()},
}
}
message, err := json.Marshal(m)
if err != nil {
log.Printf("Unable to marshal %s: %v", m, err)
return
}
// log.Printf("Message: %v\tJSON: %v", m, message)
channel, err := r.connection.Channel()
if err != nil {
log.Printf("Unable to establish AMQP connection %s: %v", r.url, err)
return
}
msg := amqp.Publishing{
DeliveryMode: 1,
Timestamp: now,
ContentType: "text/json",
Body: message,
}
mandatory, immediate := false, false
channel.Publish("amq.topic", "ping", mandatory, immediate, msg)
})
return
}