forked from dynport/metrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp_output.go
47 lines (43 loc) · 1023 Bytes
/
amqp_output.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
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/streadway/amqp"
)
const AMQP_EXCHANGE = "metrix"
func PublishMetricsWithAMQP(address string, metrics []*Metric, hostname string) (e error) {
con, e := amqp.Dial("amqp://" + address)
if e != nil {
return e
}
defer con.Close()
channel, e := con.Channel()
if e != nil {
return e
}
defer channel.Close()
e = channel.ExchangeDeclare(AMQP_EXCHANGE, "fanout", false, false, false, false, nil)
if e != nil {
return e
}
started := time.Now()
for _, m := range metrics {
amqpKey := hostname + "." + m.Key
b, e := json.Marshal(m)
if e != nil {
logger.Printf(e.Error())
continue
}
e = channel.Publish(AMQP_EXCHANGE, amqpKey, false, false, amqp.Publishing{
Body: b,
ContentType: "application/json",
Timestamp: time.Now(),
})
if e != nil {
logger.Printf("ERROR: error publishing " + e.Error())
}
}
fmt.Printf("sent %d metrics in %.06f\n", len(metrics), time.Now().Sub(started).Seconds())
return nil
}