logstash-integration-rabbitmq 7.0.3-java → 7.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6997aa19019326916e120623107352d7ba274e8370312017561cb4fae3a2e064
4
- data.tar.gz: 3fc3f9aff8d9700f233392a7311637f0b78d26c66681d8e83f1b52e991285c72
3
+ metadata.gz: 9c1e05393387b83c218237946d3db74ead4bf025c6511b8e570c5ed0eefa4b93
4
+ data.tar.gz: e6999db3c7f8e2398b21e0744a7a24da9bf2d5219a35c6c86fcbd02c7d3f47da
5
5
  SHA512:
6
- metadata.gz: 5c0265553a26b422764bd45d9f3e0599d39f391f98a2effcc20a171f30b4219869d831cb54519142dc4ee8b4a3aabf47135548e4e69d327fc780f80a0fe766ac
7
- data.tar.gz: 28255006283a7f813b7336732bd99e0c5c32a27e53f3464cc2686ab514895c4cf5cbe772245ace33c564277115ffac64ff4c864c70c8eeabdb95224278f24b6b
6
+ metadata.gz: 90b33bd2d1e1fa18f7ec4a6366cf6ae6dc1d5d705065687df0c3b65bc1fb0b297f04cf9dca152df1af0fd227aeb5d46af674a605ab021e41d65eacfe601ae7bd
7
+ data.tar.gz: 6df53fd963c003e02c6d20bbfdc839c5cfe63738fbe03ae09109b3f2d2b4322a4a9a2df098b5e9331f8ea7239625840b5d463599c0a79d2aa274288d4a115f23
@@ -1,3 +1,10 @@
1
+ ## 7.1.0
2
+ - Added support in Output plugin for `sprintf` templates in values provided to `message_properties` ([#8](https://github.com/logstash-plugins/logstash-integration-rabbitmq/issues/8))
3
+ - Added support for _extended_ metadata including raw payload to events generated by the Input Plugin [#13](https://github.com/logstash-plugins/logstash-integration-rabbitmq/issues/13)
4
+ - Fixes an issue with custom port assignment, in which the custom port was not being applied when more than one host was supplied [#12](https://github.com/logstash-plugins/logstash-integration-rabbitmq/pull/12)
5
+ - Fixes bug where attempting to read from undeclared exchange resulted in infinite retry loop ([#10](https://github.com/logstash-plugins/logstash-integration-rabbitmq/issues/10))
6
+ - Fixes bug where failing to establish initial connection resulted in a pipeline that refused to shut down ([#11](https://github.com/logstash-plugins/logstash-integration-rabbitmq/issues/11))
7
+
1
8
  ## 7.0.3
2
9
  - Refactor: scope (and remove unused) java imports [#29](https://github.com/logstash-plugins/logstash-integration-rabbitmq/pull/29)
3
10
 
data/LICENSE CHANGED
@@ -1,13 +1,202 @@
1
- Copyright (c) 2012-2018 Elasticsearch <http://www.elastic.co>
2
1
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
6
5
 
7
- http://www.apache.org/licenses/LICENSE-2.0
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
7
 
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2020 Elastic and contributors
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -34,7 +34,7 @@ and other parts of the RabbitMQ documentation are useful for deeper
34
34
  understanding.
35
35
 
36
36
  The properties of messages received will be stored in the
37
- `[@metadata][rabbitmq_properties]` field if the `@metadata_enabled` setting is checked.
37
+ `[@metadata][rabbitmq_properties]` field if the `@metadata_enabled` setting is enabled.
38
38
  Note that storing metadata may degrade performance.
39
39
  The following properties may be available (in most cases dependent on whether
40
40
  they were set by the sender):
@@ -187,6 +187,8 @@ The default connection timeout in milliseconds. If not specified the timeout is
187
187
  * Default value is `false`
188
188
 
189
189
  Is this queue durable? (aka; Should it survive a broker restart?)
190
+ If consuming directly from a queue you must set this value to match the existing queue setting,
191
+ otherwise the connection will fail due to an inequivalent arg error.
190
192
 
191
193
  [id="plugins-{type}s-{plugin}-exchange"]
192
194
  ===== `exchange`
@@ -259,8 +261,14 @@ This is only relevant for direct or topic exchanges.
259
261
  [id="plugins-{type}s-{plugin}-metadata_enabled"]
260
262
  ===== `metadata_enabled`
261
263
 
262
- * Value type is <<boolean,boolean>>
263
- * Default value is `false`
264
+ * Value type is <<string,string>>
265
+ * Accepted values are:
266
+ - `none`: no metadata is added
267
+ - `basic`: headers and properties are added
268
+ - `extended`: headers, properties, and raw payload are added
269
+ - `false`: deprecated alias for `none`
270
+ - `true`: deprecated alias for `basic`
271
+ * Default value is `none`
264
272
 
265
273
  Enable the storage of message headers and properties in `@metadata`. This may impact performance
266
274
 
@@ -168,7 +168,8 @@ Key to route to by default. Defaults to 'logstash'
168
168
  * Value type is <<hash,hash>>
169
169
  * Default value is `{}`
170
170
 
171
- Add properties to be set per-message here, such as 'content_type', 'priority'
171
+ Add properties to be set per-message here, such as 'content_type', 'priority'.
172
+ Values can be {logstash-ref}/event-dependent-configuration.html#sprintf[`sprintf` templates], whose value for each message will be populated from the event.
172
173
 
173
174
  Example:
174
175
  [source,ruby]
@@ -166,16 +166,26 @@ module LogStash
166
166
  config :subscription_retry_interval_seconds, :validate => :number, :required => true, :default => 5
167
167
 
168
168
  # Enable the storage of message headers and properties in `@metadata`. This may impact performance
169
- config :metadata_enabled, :validate => :boolean, :default => false
169
+ config :metadata_enabled, :validate => %w(none basic extended false true), :default => "none"
170
170
 
171
171
  def register
172
172
  @internal_queue = java.util.concurrent.ArrayBlockingQueue.new(@prefetch_count*2)
173
+ @metadata_level = extract_metadata_level(@metadata_enabled)
173
174
  end
174
175
 
176
+ attr_reader :metadata_level
177
+
175
178
  def run(output_queue)
176
179
  setup!
177
180
  @output_queue = output_queue
178
181
  consume!
182
+ rescue => e
183
+ raise unless stop?
184
+
185
+ logger.warn("Ignoring exception thrown during plugin shutdown",
186
+ :message => e.message,
187
+ :class => e.class.name,
188
+ :location => e.backtrace.first)
179
189
  end
180
190
 
181
191
  def setup!
@@ -184,14 +194,32 @@ module LogStash
184
194
  bind_exchange!
185
195
  @hare_info.channel.prefetch = @prefetch_count
186
196
  rescue => e
197
+ # when encountering an exception during shut-down,
198
+ # re-raise the exception instead of retrying
199
+ raise if stop?
200
+
201
+ reset!
202
+
187
203
  @logger.warn("Error while setting up connection for rabbitmq input! Will retry.",
188
- :message => e.message,
189
- :class => e.class.name,
204
+ :message => e.message,
205
+ :class => e.class.name,
190
206
  :location => e.backtrace.first)
191
207
  sleep_for_retry
192
208
  retry
193
209
  end
194
210
 
211
+ # reset a partially-established connection, enabling subsequent
212
+ # call to `RabbitMQ#setup!` to succeed.
213
+ #
214
+ # @api private
215
+ def reset!
216
+ @hare_info.connection && @hare_info.connection.close
217
+ rescue => e
218
+ @logger.debug("Exception while resetting connection", :exception => e.message, :backtrace => e.backtrace)
219
+ ensure
220
+ @hare_info = nil
221
+ end
222
+
195
223
  def bind_exchange!
196
224
  if @exchange
197
225
  if @exchange_type # Only declare the exchange if @exchange_type is set!
@@ -250,12 +278,11 @@ module LogStash
250
278
 
251
279
  metadata, data = payload
252
280
  @codec.decode(data) do |event|
253
- decorate(event)
254
- if @metadata_enabled
255
- event.set("[@metadata][rabbitmq_headers]", get_headers(metadata))
256
- event.set("[@metadata][rabbitmq_properties]", get_properties(metadata))
281
+ if event
282
+ decorate(event, metadata, data)
283
+
284
+ @output_queue << event
257
285
  end
258
- @output_queue << event if event
259
286
  end
260
287
 
261
288
  i += 1
@@ -270,6 +297,16 @@ module LogStash
270
297
  end
271
298
  end
272
299
 
300
+ def decorate(event, metadata, data)
301
+ super(event)
302
+
303
+ event.set("[@metadata][rabbitmq_headers]", get_headers(metadata)) if metadata_level.include?(:headers)
304
+ event.set("[@metadata][rabbitmq_properties]", get_properties(metadata)) if metadata_level.include?(:properties)
305
+ event.set("[@metadata][rabbitmq_payload]", data) if metadata_level.include?(:payload) && !data.nil?
306
+
307
+ nil
308
+ end
309
+
273
310
  def stop
274
311
  @internal_queue.put(INTERNAL_QUEUE_POISON)
275
312
  shutdown_consumer
@@ -311,6 +348,28 @@ module LogStash
311
348
  acc
312
349
  end
313
350
  end
351
+
352
+ METADATA_NONE = Set[].freeze
353
+ METADATA_BASIC = Set[:headers,:properties].freeze
354
+ METADATA_EXTENDED = Set[:headers,:properties,:payload].freeze
355
+ METADATA_DEPRECATION_MAP = { 'true' => 'basic', 'false' => 'none' }
356
+
357
+ private
358
+ def extract_metadata_level(metadata_enabled_setting)
359
+ metadata_enabled = metadata_enabled_setting
360
+
361
+ if METADATA_DEPRECATION_MAP.include?(metadata_enabled)
362
+ canonical_value = METADATA_DEPRECATION_MAP[metadata_enabled]
363
+ logger.warn("Deprecated value `#{metadata_enabled_setting}` for `metadata_enabled` option; use `#{canonical_value}` instead.")
364
+ metadata_enabled = canonical_value
365
+ end
366
+
367
+ case metadata_enabled
368
+ when 'none' then METADATA_NONE
369
+ when 'basic' then METADATA_BASIC
370
+ when 'extended' then METADATA_EXTENDED
371
+ end
372
+ end
314
373
  end
315
374
  end
316
375
  end
@@ -48,6 +48,8 @@ module LogStash
48
48
  config :message_properties, :validate => :hash, :default => {}
49
49
 
50
50
  def register
51
+ @message_properties_template = MessagePropertiesTemplate.new(symbolize(@message_properties).merge(:persistent => @persistent))
52
+
51
53
  connect!
52
54
  @hare_info.exchange = declare_exchange!(@hare_info.channel, @exchange, @exchange_type, @durable)
53
55
  # The connection close should close all channels, so it is safe to store thread locals here without closing them
@@ -69,8 +71,10 @@ module LogStash
69
71
 
70
72
  def publish(event, message)
71
73
  raise ArgumentError, "No exchange set in HareInfo!!!" unless @hare_info.exchange
74
+ routing_key = event.sprintf(@key)
75
+ message_properties = @message_properties_template.build(event)
72
76
  @gated_executor.execute do
73
- local_exchange.publish(message, :routing_key => event.sprintf(@key), :properties => symbolize(@message_properties.merge(:persistent => @persistent)))
77
+ local_exchange.publish(message, :routing_key => routing_key, :properties => message_properties)
74
78
  end
75
79
  rescue MarchHare::Exception, IOError, AlreadyClosedException, TimeoutException => e
76
80
  @logger.error("Error while publishing. Will retry.",
@@ -128,6 +132,64 @@ module LogStash
128
132
  end
129
133
  end
130
134
  end
135
+
136
+ ##
137
+ # A `MessagePropertiesTemplate` efficiently produces per-event message properties from the
138
+ # provided template Hash.
139
+ #
140
+ # In order to efficiently reuse constant-value objects, returned values may be frozen.
141
+ class MessagePropertiesTemplate
142
+ ##
143
+ # Creates a new `MessagePropertiesTemplate` from the provided `template`
144
+ # @param template [Hash{Symbol=>Object}]
145
+ def initialize(template)
146
+ constant_properties = template.reject { |_,v| templated?(v) }
147
+ variable_properties = template.select { |_,v| templated?(v) }
148
+
149
+ @constant_properties = normalize(constant_properties).freeze
150
+ @variable_properties = variable_properties
151
+ end
152
+
153
+ ##
154
+ # Builds a property mapping for the given `event`, including templated values.
155
+ #
156
+ # @param event [LogStash::Event]: the event with which to populated templated values, if any.
157
+ # @return [Hash{Symbol=>Object}] a possibly-frozen properties hash for the provided `event`.
158
+ def build(event)
159
+ return @constant_properties if @variable_properties.empty?
160
+
161
+ properties = @variable_properties.each_with_object(@constant_properties.dup) do |(k,v), memo|
162
+ memo.store(k, event.sprintf(v))
163
+ end
164
+
165
+ return normalize(properties)
166
+ end
167
+
168
+ private
169
+
170
+ ##
171
+ # Normalize the provided property mapping with respect to the value types the underlying
172
+ # client expects.
173
+ #
174
+ # @api private
175
+ # @param properties [Hash{Symbol=>Object}]: a possibly-frozen Hash whose values may need type-coercion.
176
+ # @return [Hash{Symbol=>Object}]
177
+ def normalize(properties)
178
+ if properties[:priority] && properties[:priority].kind_of?(String)
179
+ properties = properties.merge(:priority => properties[:priority].to_i)
180
+ end
181
+
182
+ properties
183
+ end
184
+
185
+ ##
186
+ # @api private
187
+ # @param [Object]: an object, which may or may not be a template `String`
188
+ # @return [Boolean]: returns `true` IFF `value` is a template `String`
189
+ def templated?(value)
190
+ value.kind_of?(String) && value.include?('%{')
191
+ end
192
+ end
131
193
  end
132
194
  end
133
195
  end
@@ -104,10 +104,10 @@ module LogStash
104
104
  def rabbitmq_settings
105
105
  return @rabbitmq_settings if @rabbitmq_settings
106
106
 
107
+
107
108
  s = {
108
109
  :vhost => @vhost,
109
- :hosts => @host,
110
- :port => @port,
110
+ :addresses => addresses_from_hosts_and_port(@host, @port),
111
111
  :user => @user,
112
112
  :automatic_recovery => @automatic_recovery,
113
113
  :pass => @password ? @password.value : "guest",
@@ -133,6 +133,11 @@ module LogStash
133
133
  @rabbitmq_settings = s
134
134
  end
135
135
 
136
+ def addresses_from_hosts_and_port(hosts, port)
137
+ hosts.map {|host| host.include?(':') ? host : "#{host}:#{port}"}
138
+ end
139
+
140
+
136
141
  def connect!
137
142
  @hare_info = connect() unless @hare_info # Don't duplicate the conn!
138
143
  rescue MarchHare::Exception, java.io.IOException => e
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-integration-rabbitmq'
3
- s.version = '7.0.3'
3
+ s.version = '7.1.0'
4
4
  s.licenses = ['Apache License (2.0)']
5
5
  s.summary = "Integration with RabbitMQ - input and output plugins"
6
6
  s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline "+
@@ -21,8 +21,13 @@ describe LogStash::Inputs::RabbitMQ do
21
21
  "prefetch_count" => 123
22
22
  }
23
23
  }
24
- let(:instance) { klass.new(rabbitmq_settings) }
24
+ subject(:instance) { klass.new(rabbitmq_settings) }
25
25
  let(:hare_info) { instance.instance_variable_get(:@hare_info) }
26
+ let(:instance_logger) { double("Logger").as_null_object }
27
+
28
+ before do
29
+ allow_any_instance_of(klass).to receive(:logger).and_return(instance_logger)
30
+ end
26
31
 
27
32
  context "when connected" do
28
33
  let(:connection) { double("MarchHare Connection") }
@@ -39,6 +44,7 @@ describe LogStash::Inputs::RabbitMQ do
39
44
  allow(connection).to receive(:on_shutdown)
40
45
  allow(connection).to receive(:on_blocked)
41
46
  allow(connection).to receive(:on_unblocked)
47
+ allow(connection).to receive(:close)
42
48
  allow(channel).to receive(:exchange).and_return(exchange)
43
49
  allow(channel).to receive(:queue).and_return(queue)
44
50
  allow(channel).to receive(:prefetch=)
@@ -127,6 +133,187 @@ describe LogStash::Inputs::RabbitMQ do
127
133
  end
128
134
  end
129
135
  end
136
+
137
+ context '#register' do
138
+ let(:rabbitmq_settings) { super().merge(metadata_enabled_override) }
139
+ let(:metadata_enabled_override) { { "metadata_enabled" => metadata_enabled } }
140
+ before do
141
+ instance.register
142
+ end
143
+
144
+ shared_examples('`metadata_enabled => none`') do
145
+ context 'metadata_level' do
146
+ subject(:metadata_level) { instance.metadata_level }
147
+ it { is_expected.to be_empty }
148
+ it { is_expected.to be_frozen }
149
+ end
150
+ end
151
+
152
+ shared_examples('`metadata_enabled => basic`') do
153
+ context 'metadata_level' do
154
+ subject(:metadata_level) { instance.metadata_level }
155
+ it { is_expected.to include :headers }
156
+ it { is_expected.to include :properties }
157
+ it { is_expected.to_not include :payload }
158
+ it { is_expected.to be_frozen }
159
+ end
160
+ end
161
+
162
+ shared_examples("deprecated `metadata_enabled` setting") do |deprecated_value|
163
+ context 'the logger' do
164
+ subject(:logger) { instance_logger }
165
+ it 'receives a useful deprecation warning' do
166
+ expect(logger).to have_received(:warn).with(/Deprecated value `#{Regexp.escape(deprecated_value)}`/)
167
+ end
168
+ end
169
+ end
170
+
171
+ context 'when `metadata_enabled` is `true`' do
172
+ let(:metadata_enabled) { "true" }
173
+ it_behaves_like '`metadata_enabled => basic`'
174
+ include_examples "deprecated `metadata_enabled` setting", "true"
175
+ end
176
+
177
+ context 'when `metadata_enabled` is `false`' do
178
+ let(:metadata_enabled) { "false" }
179
+ it_behaves_like '`metadata_enabled => none`'
180
+ include_examples "deprecated `metadata_enabled` setting", "false"
181
+ end
182
+
183
+ context 'when `metadata_enabled` is not provided' do
184
+ let(:metadata_enabled_override) { Hash.new }
185
+ it_behaves_like '`metadata_enabled => none`'
186
+ end
187
+
188
+ context 'when `metadata_enabled` is `basic`' do
189
+ let(:metadata_enabled) { "basic" }
190
+ include_examples '`metadata_enabled => basic`'
191
+ end
192
+
193
+ context 'when `metadata_enabled` is `none`' do
194
+ let(:metadata_enabled) { "none" }
195
+ include_examples '`metadata_enabled => none`'
196
+ end
197
+
198
+ context 'when `metadata_enabled` is `extended`' do
199
+ let(:metadata_enabled) { "extended" }
200
+ context 'metadata_level' do
201
+ subject(:metadata_level) { instance.metadata_level }
202
+ it { is_expected.to include :headers }
203
+ it { is_expected.to include :properties }
204
+ it { is_expected.to include :payload }
205
+ it { is_expected.to be_frozen }
206
+ end
207
+ end
208
+ end
209
+
210
+ describe "#decorate(event, metadata, data)" do
211
+ let(:rabbitmq_settings) do
212
+ super().merge("metadata_enabled" => metadata_enabled)
213
+ end
214
+ before(:each) { instance.register }
215
+
216
+ let(:metadata) { double("METADATA") }
217
+ let(:headers) { Hash("header_key"=>"header_value") }
218
+ let(:properties) { Hash("property_key"=>"property_value") }
219
+
220
+ let(:data) { %Q({"message"=>"fubar"}\n) }
221
+
222
+ before do
223
+ allow(instance).to receive(:get_headers).with(metadata).and_return(headers)
224
+ allow(instance).to receive(:get_properties).with(metadata).and_return(properties)
225
+ end
226
+
227
+ describe 'the decorated event' do
228
+ subject(:decorated_event) do
229
+ LogStash::Event.new("message"=>"fubar").tap do |e|
230
+ instance.decorate(e, metadata, data)
231
+ end
232
+ end
233
+
234
+ matcher :include_field do |fieldref|
235
+ match do |event|
236
+ # setting `@actual` makes failure messages clearer
237
+ @actual = event.to_hash_with_metadata
238
+
239
+ break false unless event.include?(fieldref)
240
+ break true unless @specific_value
241
+
242
+ values_match?(@expected_value, event.get(fieldref))
243
+ end
244
+ chain :with_value do |expected_value|
245
+ @specific_value = true
246
+ @expected_value = expected_value
247
+ end
248
+ description do
249
+ desc = "include field `#{fieldref}`"
250
+ desc += " with value matching `#{@expected_value.inspect}`" if @specific_value
251
+ desc
252
+ end
253
+ end
254
+
255
+ shared_examples('core decoration features') do
256
+ let(:rabbitmq_settings) do
257
+ super().merge("type" => "decorated_type",
258
+ "add_field" => {"added_field" => "field_value"})
259
+ end
260
+ it 'has been decorated with core decoration features' do
261
+ expect(decorated_event).to include_field("added_field").with_value("field_value")
262
+ expect(decorated_event).to include_field("type").with_value("decorated_type")
263
+ end
264
+ end
265
+
266
+ let(:headers_fieldref) { "[@metadata][rabbitmq_headers]" }
267
+ let(:properties_fieldref) { "[@metadata][rabbitmq_properties]" }
268
+ let(:payload_fieldref) { "[@metadata][rabbitmq_payload]" }
269
+
270
+ shared_examples('`metadata_enabled => none`') do
271
+ it { is_expected.to_not include_field(headers_fieldref) }
272
+ it { is_expected.to_not include_field(properties_fieldref) }
273
+ it { is_expected.to_not include_field(payload_fieldref) }
274
+
275
+ include_examples 'core decoration features'
276
+ end
277
+
278
+ shared_examples('`metadata_enabled => basic`') do
279
+ it { is_expected.to include_field(headers_fieldref).with_value(headers) }
280
+ it { is_expected.to include_field(properties_fieldref).with_value(properties) }
281
+ it { is_expected.to_not include_field(payload_fieldref) }
282
+
283
+ include_examples 'core decoration features'
284
+ end
285
+
286
+ context "with `metadata_enabled => none`" do
287
+ let(:metadata_enabled) { "none" }
288
+ include_examples '`metadata_enabled => none`'
289
+ end
290
+
291
+ context "with `metadata_enabled => basic`" do
292
+ let(:metadata_enabled) { "basic" }
293
+ include_examples '`metadata_enabled => basic`'
294
+ end
295
+
296
+ context 'with `metadata_enabled => extended`' do
297
+ let(:metadata_enabled) { "extended" }
298
+ it { is_expected.to include_field(headers_fieldref).with_value(headers) }
299
+ it { is_expected.to include_field(properties_fieldref).with_value(properties) }
300
+ it { is_expected.to include_field(payload_fieldref).with_value(data) }
301
+ include_examples 'core decoration features'
302
+ end
303
+
304
+ # Deprecated alias: false -> none
305
+ context "with `metadata_enabled => false`" do
306
+ let(:metadata_enabled) { "false" }
307
+ it_behaves_like '`metadata_enabled => none`'
308
+ end
309
+
310
+ # Deprecated alias: true -> basic
311
+ context "with `metadata_enabled => true`" do
312
+ let(:metadata_enabled) { "true" }
313
+ it_behaves_like '`metadata_enabled => basic`'
314
+ end
315
+ end
316
+ end
130
317
  end
131
318
 
132
319
  describe "LogStash::Inputs::RabbitMQ with a live server", :integration => true do
@@ -186,7 +373,7 @@ describe "LogStash::Inputs::RabbitMQ with a live server", :integration => true d
186
373
  end
187
374
 
188
375
  describe "receiving a message with a queue + exchange specified" do
189
- let(:config) { super.merge("queue" => queue_name, "exchange" => exchange_name, "exchange_type" => "fanout", "metadata_enabled" => true) }
376
+ let(:config) { super.merge("queue" => queue_name, "exchange" => exchange_name, "exchange_type" => "fanout", "metadata_enabled" => "true") }
190
377
  let(:event) { output_queue.pop }
191
378
  let(:exchange) { test_channel.exchange(exchange_name, :type => "fanout") }
192
379
  let(:exchange_name) { "logstash-input-rabbitmq-#{rand(0xFFFFFFFF)}" }
@@ -83,18 +83,67 @@ describe LogStash::Outputs::RabbitMQ do
83
83
  before do
84
84
  allow(exchange).to receive(:publish).with(any_args)
85
85
  allow(event).to receive(:sprintf).with(key).and_return(sprinted_key)
86
- instance.send(:publish, event, encoded_event)
87
86
  end
88
87
 
89
88
  it "should send the correct message" do
89
+ instance.send(:publish, event, encoded_event)
90
90
  expect(exchange).to have_received(:publish).with(encoded_event, anything)
91
91
  end
92
92
 
93
93
  it "should send the correct metadata" do
94
94
  expected_metadata = {:routing_key => sprinted_key, :properties => {:persistent => persistent }}
95
95
 
96
+ instance.send(:publish, event, encoded_event)
97
+
96
98
  expect(exchange).to have_received(:publish).with(anything, expected_metadata)
97
99
  end
100
+
101
+ context 'with message_properties' do
102
+ let(:rabbitmq_settings) { super().merge("message_properties" => message_properties) }
103
+ let(:message_properties) { Hash.new }
104
+ context 'priority' do
105
+ let(:message_properties) { super().merge("priority" => priority) }
106
+ context 'as literal Integer value' do
107
+ let(:priority) { 3 }
108
+ it 'publishes with the constant-value priority' do
109
+ instance.send(:publish, event, encoded_event)
110
+ expect(exchange).to have_received(:publish).with(anything, hash_including(:properties => hash_including(:priority => 3)))
111
+ end
112
+ end
113
+
114
+ context 'as literal String value' do
115
+ let(:priority) { "7" }
116
+ it 'publishes with the constant-value priority' do
117
+ instance.send(:publish, event, encoded_event)
118
+ expect(exchange).to have_received(:publish).with(anything, hash_including(:properties => hash_including(:priority => 7)))
119
+ end
120
+ end
121
+
122
+ context 'as template value' do
123
+ let(:priority) { "%{[@metadata][priority]}" }
124
+ context 'when event expands template value' do
125
+ before do
126
+ expect(event).to receive(:sprintf).with(priority).and_return("31")
127
+ end
128
+
129
+ it 'publishes with the priority extracted from the event' do
130
+ instance.send(:publish, event, encoded_event)
131
+ expect(exchange).to have_received(:publish).with(anything, hash_including(:properties => hash_including(:priority => 31)))
132
+ end
133
+ end
134
+ context 'when event cannot expand template value' do
135
+ before do
136
+ expect(event).to receive(:sprintf).with(priority).and_return(priority)
137
+ end
138
+
139
+ it 'publishes with the priority of zero (`0`)' do
140
+ instance.send(:publish, event, encoded_event)
141
+ expect(exchange).to have_received(:publish).with(anything, hash_including(:properties => hash_including(:priority => 0)))
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
98
147
  end
99
148
 
100
149
  context 'when an exception is encountered' do
@@ -14,12 +14,12 @@ end
14
14
 
15
15
  describe LogStash::PluginMixins::RabbitMQConnection do
16
16
  let(:klass) { TestPlugin }
17
+ let(:default_port) { 5672 }
17
18
  let(:host) { "localhost" }
18
- let(:port) { 5672 }
19
+ let(:port) { default_port }
19
20
  let(:rabbitmq_settings) {
20
21
  {
21
- "host" => host,
22
- "port" => port,
22
+ "host" => host
23
23
  }
24
24
  }
25
25
  let(:instance) {
@@ -27,6 +27,22 @@ describe LogStash::PluginMixins::RabbitMQConnection do
27
27
  }
28
28
  let(:hare_info) { instance.instance_variable_get(:@hare_info) }
29
29
 
30
+ shared_examples_for 'it sets the addresses correctly' do
31
+ let(:file) { Stud::Temporary.file }
32
+ let(:path) { file.path }
33
+ let(:host) {%w(host01 host02 host03)}
34
+
35
+ it "should set addresses to the expected value" do
36
+ host.each_with_index do |each_host, index|
37
+ expect(instance.rabbitmq_settings[:addresses][index]).to eql("#{each_host}:#{port}")
38
+ end
39
+ end
40
+
41
+ it "should insert the correct number of address entries" do
42
+ expect(instance.rabbitmq_settings[:addresses].length).to eql(host.count)
43
+ end
44
+ end
45
+
30
46
  describe "rabbitmq_settings" do
31
47
  let(:file) { Stud::Temporary.file }
32
48
  let(:path) { file.path }
@@ -59,12 +75,13 @@ describe LogStash::PluginMixins::RabbitMQConnection do
59
75
  expect(instance.rabbitmq_settings[:tls_certificate_password]).to eql(rabbitmq_settings["ssl_certificate_password"])
60
76
  end
61
77
 
62
- it "should set hosts to the expected value " do
63
- expect(instance.rabbitmq_settings[:hosts][0]).to eql(host)
64
- end
78
+ it_behaves_like 'it sets the addresses correctly'
79
+
80
+ context 'with a custom port' do
81
+ let(:port) { 123 }
82
+ let(:rabbitmq_settings) { super.merge({"port" => port})}
65
83
 
66
- it "should only insert a single host entry" do
67
- expect(instance.rabbitmq_settings[:hosts].length).to eql(1)
84
+ it_behaves_like 'it sets the addresses correctly'
68
85
  end
69
86
  end
70
87
 
@@ -79,21 +96,25 @@ describe LogStash::PluginMixins::RabbitMQConnection do
79
96
  end
80
97
 
81
98
  end
82
- describe "rabbitmq_settings multiple hosts" do
83
- let(:file) { Stud::Temporary.file }
84
- let(:path) { file.path }
85
- after { File.unlink(path)}
86
99
 
87
- let(:rabbitmq_settings) { super.merge({"host" => ["host01", "host02", "host03"]}) }
100
+ describe "rabbitmq_settings with multiple hosts" do
101
+ it_behaves_like 'it sets the addresses correctly'
102
+
103
+ context 'with a custom port' do
104
+ let(:port) { 999 }
105
+ let(:rabbitmq_settings) { super.merge({"port" => port})}
88
106
 
89
- it "should set hosts to the expected value" do
90
- expect(instance.rabbitmq_settings[:hosts][0]).to eql("host01")
91
- expect(instance.rabbitmq_settings[:hosts][1]).to eql("host02")
92
- expect(instance.rabbitmq_settings[:hosts][2]).to eql("host03")
107
+ it_behaves_like 'it sets the addresses correctly'
93
108
  end
94
109
 
95
- it "should insert 3 host entries" do
96
- expect(instance.rabbitmq_settings[:hosts].length).to eql(3)
110
+ context 'when ports are set in the host definition' do
111
+ let(:host) { %w(host01:4444 host02:4445 host03:4446)}
112
+
113
+ it "should set the address correctly" do
114
+ expect(instance.rabbitmq_settings[:addresses][0]).to eql("host01:4444")
115
+ expect(instance.rabbitmq_settings[:addresses][1]).to eql("host02:4445")
116
+ expect(instance.rabbitmq_settings[:addresses][2]).to eql("host03:4446")
117
+ end
97
118
  end
98
119
  end
99
120
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-integration-rabbitmq
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.3
4
+ version: 7.1.0
5
5
  platform: java
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-16 00:00:00.000000000 Z
11
+ date: 2020-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement