-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
169 lines (155 loc) · 4.34 KB
/
main.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
package main
import (
"flag"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/streadway/amqp"
)
var (
failed = "failed"
success = "success"
promStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "rabbitmq_test",
Name: "produce_consume",
},
[]string{"kind", "status", "node"},
)
)
var addr = flag.String("listen-address", "127.0.0.1:8080", "The address to listen on for HTTP requests (expose metrics)")
var rmqAddr = flag.String("rabbitmq-address", "amqp://guest:guest@localhost:5672/", "The RabbitMQ address")
var exchangeName = flag.String("exchange-name", "rbmq-testt", "The name of the exchange to publish to / consume messages from")
var isProducer = flag.Bool("is-producer", true, "Default true, if false type == consumer")
var testInterval = flag.Int("test-interval-seconds", 10, "Interval between messages beeing produced/consumed")
var nodeName = flag.String("node-name", "localhost", "The name of the node running the container")
func init() {
prometheus.MustRegister(promStatus)
}
func main() {
flag.Parse()
go func() {
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}()
if *isProducer {
for {
tryAgainOnFailure := producer()
if !tryAgainOnFailure {
break
}
time.Sleep(time.Duration(*testInterval) * time.Second)
}
} else {
for {
tryAgainOnFailure := consumer()
if !tryAgainOnFailure {
break
}
time.Sleep(time.Duration(*testInterval) * time.Second)
}
}
}
func setupConnection() (*amqp.Connection, *amqp.Channel, error) {
conn, err := amqp.Dial(*rmqAddr)
if err != nil {
log.Printf("Failed to connect to RabbitMQ: %s", err)
return nil, nil, err
}
ch, err := conn.Channel()
if err != nil {
log.Printf("Failed to open a channel: %s", err)
return nil, nil, err
}
err = ch.ExchangeDeclare(*exchangeName, "fanout", true, false, false, false, nil)
if err != nil {
log.Printf("Failed to declare an exchange: %s", err)
return nil, nil, err
}
return conn, ch, err
}
func producer() bool {
tryAgainOnFailure := true
kind := "producer"
conn, ch, err := setupConnection()
if err != nil {
updateMetrics(kind, failed, success)
return tryAgainOnFailure
}
defer conn.Close()
defer ch.Close()
ticker := time.NewTicker(time.Duration(float64(*testInterval)) * time.Second)
quit := make(chan bool)
log.Printf("Ready to publish messages")
go func() {
for {
select {
case <-ticker.C:
err = ch.Publish(*exchangeName, "", false, false,
amqp.Publishing{
ContentType: "text/plain",
Body: []byte("hello world!"),
})
if err != nil {
log.Printf("Failed to publish message: %s", err)
updateMetrics(kind, failed, success)
quit <- true
} else {
log.Printf("Published message")
updateMetrics(kind, success, failed)
}
case <-quit:
return
}
}
}()
<-quit
return tryAgainOnFailure
}
func consumer() bool {
tryAgainOnFailure := true
kind := "consumer"
conn, ch, err := setupConnection()
if err != nil {
updateMetrics(kind, failed, success)
return tryAgainOnFailure
}
defer conn.Close()
defer ch.Close()
q, err := ch.QueueDeclare("", false, false, true, false, nil)
if err != nil {
log.Printf("Failed to declare queue: %s", err)
updateMetrics(kind, failed, success)
return tryAgainOnFailure
}
err = ch.QueueBind(q.Name, "", *exchangeName, false, nil)
if err != nil {
log.Printf("Failed to bind queue to exchange: %s", err)
updateMetrics(kind, failed, success)
return tryAgainOnFailure
}
msgs, err := ch.Consume(q.Name, "", false, false, false, false, nil)
if err != nil {
log.Printf("Failed to register a consumer: %s", err)
updateMetrics(kind, failed, success)
return tryAgainOnFailure
}
log.Printf("Ready to recive messages")
updateMetrics(kind, success, failed)
for d := range msgs {
log.Printf("Message recived: %s", d.Body)
err = d.Ack(false)
if err != nil{
log.Printf("Ack error: %s",err)
break
}
}
updateMetrics(kind, failed, success)
return tryAgainOnFailure
}
func updateMetrics(kind string, currStatus string, prevStatus string) {
promStatus.With(prometheus.Labels{"kind": kind, "status": currStatus, "node": *nodeName}).Set(1)
promStatus.With(prometheus.Labels{"kind": kind, "status": prevStatus, "node": *nodeName}).Set(0)
}