-
Notifications
You must be signed in to change notification settings - Fork 70
/
RabbitMQSender.java
325 lines (283 loc) · 9.96 KB
/
RabbitMQSender.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright The OpenZipkin Authors
* SPDX-License-Identifier: Apache-2.0
*/
package zipkin2.reporter.amqp;
import com.rabbitmq.client.Address;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.BytesMessageSender;
import zipkin2.reporter.Call;
import zipkin2.reporter.Callback;
import zipkin2.reporter.CheckResult;
import zipkin2.reporter.ClosedSenderException;
import zipkin2.reporter.Encoding;
import zipkin2.reporter.Sender;
import static zipkin2.reporter.Call.propagateIfFatal;
/**
* This sends (usually json v2) encoded spans to a RabbitMQ queue.
*
* <h3>Usage</h3>
* <p>
* This type is designed for {@link AsyncReporter.Builder#builder(BytesMessageSender) the async
* reporter}.
*
* <p>Here's a simple configuration, configured for json:
*
* <pre>{@code
* sender = RabbitMQSender.create("localhost:5672");
* }</pre>
*
* <p>Here's an example with an explicit SSL connection factory and protocol buffers encoding:
*
* <pre>{@code
* connectionFactory = new ConnectionFactory();
* connectionFactory.setHost("localhost");
* connectionFactory.setPort(5671);
* connectionFactory.useSslProtocol();
* sender = RabbitMQSender.newBuilder()
* .connectionFactory(connectionFactory)
* .encoding(Encoding.PROTO3)
* .build();
* }</pre>
*
* <h3>Compatibility with Zipkin Server</h3>
*
* <a href="https://github.com/openzipkin/zipkin">Zipkin server</a> should be v2.1 or higher.
*
* <h3>Implementation Notes</h3>
*
* <p>The sender does not use <a href="https://www.rabbitmq.com/confirms.html">RabbitMQ Publisher
* Confirms</a>, so messages considered sent may not necessarily be received by consumers in case of
* RabbitMQ failure.
*
* <p>This sender is thread-safe: a channel is created for each thread that calls
* {@link #send(List)}.
*/
public final class RabbitMQSender extends Sender {
/** Creates a sender that sends {@link Encoding#JSON} messages. */
public static RabbitMQSender create(String addresses) {
return newBuilder().addresses(addresses).build();
}
public static Builder newBuilder() {
return new Builder();
}
/** Configuration including defaults needed to send spans to a RabbitMQ queue. */
public static final class Builder {
ConnectionFactory connectionFactory = new ConnectionFactory();
List<Address> addresses;
String queue = "zipkin";
Encoding encoding = Encoding.JSON;
int messageMaxBytes = 500000;
Builder(RabbitMQSender sender) {
connectionFactory = sender.connectionFactory.clone();
addresses = sender.addresses;
queue = sender.queue;
encoding = sender.encoding;
messageMaxBytes = sender.messageMaxBytes;
}
public Builder connectionFactory(ConnectionFactory connectionFactory) {
if (connectionFactory == null) throw new NullPointerException("connectionFactory == null");
this.connectionFactory = connectionFactory;
return this;
}
public Builder addresses(List<Address> addresses) {
if (addresses == null) throw new NullPointerException("addresses == null");
this.addresses = addresses;
return this;
}
/** Comma-separated list of host:port pairs. ex "192.168.99.100:5672" No Default. */
public Builder addresses(String addresses) {
if (addresses == null) throw new NullPointerException("addresses == null");
this.addresses = convertAddresses(addresses);
return this;
}
/** Queue zipkin spans will be send to. Defaults to "zipkin" */
public Builder queue(String queue) {
if (queue == null) throw new NullPointerException("queue == null");
this.queue = queue;
return this;
}
/**
* Use this to change the encoding used in messages. Default is {@linkplain Encoding#JSON}
*
* <p>Note: If ultimately sending to Zipkin, version 2.8+ is required to process protobuf.
*/
public Builder encoding(Encoding encoding) {
if (encoding == null) throw new NullPointerException("encoding == null");
this.encoding = encoding;
return this;
}
/** Connection TCP establishment timeout in milliseconds. Defaults to 60 seconds */
public Builder connectionTimeout(int connectionTimeout) {
connectionFactory.setConnectionTimeout(connectionTimeout);
return this;
}
/** The virtual host to use when connecting to the broker. Defaults to "/" */
public Builder virtualHost(String virtualHost) {
connectionFactory.setVirtualHost(virtualHost);
return this;
}
/** The AMQP user name to use when connecting to the broker. Defaults to "guest" */
public Builder username(String username) {
connectionFactory.setUsername(username);
return this;
}
/** The password to use when connecting to the broker. Defaults to "guest" */
public Builder password(String password) {
connectionFactory.setPassword(password);
return this;
}
/** Maximum size of a message. Default 500KB. */
public Builder messageMaxBytes(int messageMaxBytes) {
this.messageMaxBytes = messageMaxBytes;
return this;
}
public final RabbitMQSender build() {
return new RabbitMQSender(this);
}
Builder() {
}
}
final Encoding encoding;
final int messageMaxBytes;
final List<Address> addresses;
final String queue;
final ConnectionFactory connectionFactory;
RabbitMQSender(Builder builder) {
if (builder.addresses == null) throw new NullPointerException("addresses == null");
encoding = builder.encoding;
messageMaxBytes = builder.messageMaxBytes;
addresses = builder.addresses;
queue = builder.queue;
connectionFactory = builder.connectionFactory.clone();
}
public Builder toBuilder() {
return new Builder(this);
}
/** get and close are typically called from different threads */
volatile Connection connection;
volatile boolean closeCalled;
@Override public Encoding encoding() {
return encoding;
}
@Override public int messageMaxBytes() {
return messageMaxBytes;
}
@Override public int messageSizeInBytes(List<byte[]> encodedSpans) {
return encoding.listSizeInBytes(encodedSpans);
}
@Override public int messageSizeInBytes(int encodedSizeInBytes) {
return encoding.listSizeInBytes(encodedSizeInBytes);
}
/** {@inheritDoc} */
@Override @Deprecated public Call<Void> sendSpans(List<byte[]> encodedSpans) {
if (closeCalled) throw new ClosedSenderException();
byte[] message = encoding.encode(encodedSpans);
return new RabbitMQCall(message);
}
/** {@inheritDoc} */
@Override public void send(List<byte[]> encodedSpans) throws IOException {
if (closeCalled) throw new ClosedSenderException();
publish(encoding.encode(encodedSpans));
}
void publish(byte[] message) throws IOException {
localChannel().basicPublish("", queue, null, message);
}
/** {@inheritDoc} */
@Override @Deprecated public CheckResult check() {
try {
if (localChannel().isOpen()) return CheckResult.OK;
throw new IllegalStateException("Not Open");
} catch (Throwable e) {
propagateIfFatal(e);
return CheckResult.failed(e);
}
}
@Override public String toString() {
return "RabbitMQSender{addresses=" + addresses + ", queue=" + queue + "}";
}
Connection get() {
if (connection == null) {
synchronized (this) {
if (connection == null) {
connection = newConnection();
}
}
}
return connection;
}
Connection newConnection() {
try {
return connectionFactory.newConnection(addresses);
} catch (IOException e) {
throw new RuntimeException("Unable to establish connection to RabbitMQ server", e);
} catch (TimeoutException e) {
throw new RuntimeException("Unable to establish connection to RabbitMQ server", e);
}
}
@Override public synchronized void close() throws IOException {
if (closeCalled) return;
Connection connection = this.connection;
if (connection != null) connection.close();
closeCalled = true;
}
final ThreadLocal<Channel> CHANNEL = new ThreadLocal<Channel>();
/**
* In most circumstances there will only be one thread calling {@link #send(List)}, the
* {@link AsyncReporter}. Just in case someone is flushing manually, we use a thread-local. All of
* this is to avoid recreating a channel for each publish, as that costs two additional network
* roundtrips.
*/
Channel localChannel() throws IOException {
Channel channel = CHANNEL.get();
if (channel == null) {
channel = get().createChannel();
CHANNEL.set(channel);
}
return channel;
}
class RabbitMQCall extends Call.Base<Void> { // RabbitMQFuture is not cancelable
private final byte[] message;
RabbitMQCall(byte[] message) {
this.message = message;
}
@Override protected Void doExecute() throws IOException {
publish(message);
return null;
}
@Override protected void doEnqueue(Callback<Void> callback) {
try {
publish(message);
callback.onSuccess(null);
} catch (Throwable t) {
Call.propagateIfFatal(t);
callback.onError(t);
}
}
@Override public Call<Void> clone() {
return new RabbitMQCall(message);
}
}
static List<Address> convertAddresses(String addresses) {
String[] addressStrings = addresses.split(",");
Address[] addressArray = new Address[addressStrings.length];
for (int i = 0; i < addressStrings.length; i++) {
String[] splitAddress = addressStrings[i].split(":");
String host = splitAddress[0];
Integer port = null;
try {
if (splitAddress.length == 2) port = Integer.parseInt(splitAddress[1]);
} catch (NumberFormatException ignore) {
}
addressArray[i] = (port != null) ? new Address(host, port) : new Address(host);
}
return Arrays.asList(addressArray);
}
}