zrok/controller/metrics/amqpSource.go

144 lines
2.9 KiB
Go
Raw Normal View History

2023-03-15 21:14:06 +01:00
package metrics
2023-03-15 19:52:14 +01:00
import (
"github.com/michaelquigley/cf"
2023-03-15 20:08:06 +01:00
"github.com/openziti/zrok/controller/env"
2023-03-15 19:52:14 +01:00
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/sirupsen/logrus"
"time"
2023-03-15 19:52:14 +01:00
)
2023-03-15 20:08:06 +01:00
func init() {
env.GetCfOptions().AddFlexibleSetter("amqpSource", loadAmqpSourceConfig)
}
2023-03-15 19:52:14 +01:00
type AmqpSourceConfig struct {
Url string `cf:"+secret"`
QueueName string
}
func loadAmqpSourceConfig(v interface{}, _ *cf.Options) (interface{}, error) {
if submap, ok := v.(map[string]interface{}); ok {
cfg := &AmqpSourceConfig{}
if err := cf.Bind(cfg, submap, cf.DefaultOptions()); err != nil {
return nil, err
}
return newAmqpSource(cfg)
}
return nil, errors.New("invalid config structure for 'amqpSource'")
}
type amqpSource struct {
cfg *AmqpSourceConfig
conn *amqp.Connection
ch *amqp.Channel
queue amqp.Queue
msgs <-chan amqp.Delivery
errs chan *amqp.Error
events chan ZitiEventMsg
close chan struct{}
join chan struct{}
2023-03-15 19:52:14 +01:00
}
func newAmqpSource(cfg *AmqpSourceConfig) (*amqpSource, error) {
as := &amqpSource{
cfg: cfg,
close: make(chan struct{}),
join: make(chan struct{}),
}
return as, nil
2023-03-15 19:52:14 +01:00
}
2023-05-01 20:45:45 +02:00
func (s *amqpSource) Start(events chan ZitiEventMsg) (join chan struct{}, err error) {
s.events = events
go s.run()
return s.join, nil
}
func (s *amqpSource) Stop() {
close(s.close)
<-s.join
}
func (s *amqpSource) run() {
logrus.Info("started")
defer logrus.Info("stopped")
defer close(s.join)
mainLoop:
for {
logrus.Infof("connecting to '%v'", s.cfg.Url)
if err := s.connect(); err != nil {
logrus.Errorf("error connecting to '%v': %v", s.cfg.Url, err)
select {
case <-time.After(10 * time.Second):
continue mainLoop
case <-s.close:
break mainLoop
}
}
logrus.Infof("connected to '%v'", s.cfg.Url)
msgLoop:
for {
select {
2023-06-14 21:31:11 +02:00
case err, ok := <-s.errs:
if err != nil || !ok {
logrus.Error(err)
break msgLoop
}
2023-03-15 19:52:14 +01:00
case <-s.close:
break mainLoop
2023-06-16 19:18:46 +02:00
case event, ok := <-s.msgs:
if !ok {
2023-06-16 19:28:41 +02:00
logrus.Debug("selecting on msg !ok")
2023-06-16 19:18:46 +02:00
break msgLoop
}
if event.Body != nil {
s.events <- &ZitiEventAMQP{
data: ZitiEventJson(event.Body),
msg: event,
}
} else {
2023-06-16 19:28:41 +02:00
logrus.Debug("event body was nil!")
2023-06-16 19:18:46 +02:00
break msgLoop
}
}
}
2023-03-15 19:52:14 +01:00
}
}
func (s *amqpSource) connect() error {
conn, err := amqp.Dial(s.cfg.Url)
if err != nil {
return errors.Wrap(err, "error dialing amqp broker")
}
ch, err := conn.Channel()
if err != nil {
return errors.Wrap(err, "error getting amqp channel")
}
queue, err := ch.QueueDeclare(s.cfg.QueueName, true, false, false, false, nil)
if err != nil {
return errors.Wrap(err, "error declaring queue")
}
msgs, err := ch.Consume(s.cfg.QueueName, "zrok", false, false, false, false, nil)
if err != nil {
return errors.Wrap(err, "error consuming")
}
s.errs = make(chan *amqp.Error)
conn.NotifyClose(s.errs)
s.conn = conn
s.ch = ch
s.queue = queue
s.msgs = msgs
return nil
}