forked from beatlabs/patron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp.go
144 lines (120 loc) · 3.39 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
package amqp
import (
"context"
"github.com/mantzas/patron/encoding/json"
patronerrors "github.com/mantzas/patron/errors"
"github.com/mantzas/patron/trace"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/pkg/errors"
"github.com/streadway/amqp"
)
// Message abstraction for publishing.
type Message struct {
contentType string
body []byte
}
// NewMessage creates a new message.
func NewMessage(ct string, body []byte) *Message {
return &Message{contentType: ct, body: body}
}
// NewJSONMessage creates a new message with a JSON encoded body.
func NewJSONMessage(d interface{}) (*Message, error) {
body, err := json.Encode(d)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal to JSON")
}
return &Message{contentType: json.ContentType, body: body}, nil
}
// Publisher interface of a RabbitMQ publisher.
type Publisher interface {
Publish(ctx context.Context, msg *Message) error
Close(ctx context.Context) error
}
// TracedPublisher defines a RabbitMQ publisher with tracing instrumentation.
type TracedPublisher struct {
cn *amqp.Connection
ch *amqp.Channel
exc string
tag opentracing.Tag
}
// NewPublisher creates a new publisher with the following defaults
// - exchange type: fanout
// - notifications are not handled at this point TBD.
func NewPublisher(url, exc string) (*TracedPublisher, error) {
if url == "" {
return nil, errors.New("url is required")
}
if exc == "" {
return nil, errors.New("exchange is required")
}
p := TracedPublisher{
exc: exc,
tag: opentracing.Tag{Key: "exchange", Value: exc},
}
conn, err := amqp.Dial(url)
if err != nil {
return nil, errors.Wrap(err, "failed to open RabbitMq connection")
}
p.cn = conn
ch, err := conn.Channel()
if err != nil {
return nil, errors.Wrap(err, "failed to open RabbitMq channel")
}
p.ch = ch
err = ch.ExchangeDeclare(
exc, // name
amqp.ExchangeDirect, // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
if err != nil {
return nil, errors.Wrap(err, "failed to declare exchange")
}
return &p, nil
}
// Publish a message to a exchange.
func (tc *TracedPublisher) Publish(ctx context.Context, msg *Message) error {
sp, _ := trace.ChildSpan(
ctx,
trace.ComponentOpName(trace.AMQPPublisherComponent, tc.exc),
trace.AMQPPublisherComponent,
ext.SpanKindProducer,
tc.tag,
)
p := amqp.Publishing{
Headers: amqp.Table{},
ContentType: msg.contentType,
Body: msg.body,
}
c := amqpHeadersCarrier(p.Headers)
err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, c)
if err != nil {
return errors.Wrap(err, "failed to inject tracing headers")
}
err = tc.ch.Publish(tc.exc, "", false, false, p)
if err != nil {
trace.SpanError(sp)
return errors.Wrap(err, "failed to publish message")
}
trace.SpanSuccess(sp)
return nil
}
// Close the connection and channel of the publisher.
func (tc *TracedPublisher) Close(_ context.Context) error {
aggError := patronerrors.New()
aggError.Append(tc.ch.Close())
aggError.Append(tc.cn.Close())
if aggError.Count() > 0 {
return aggError
}
return nil
}
type amqpHeadersCarrier map[string]interface{}
// Set implements Set() of opentracing.TextMapWriter.
func (c amqpHeadersCarrier) Set(key, val string) {
c[key] = val
}