diff --git a/lib/logstash/inputs/jms.rb b/lib/logstash/inputs/jms.rb index 113a370..6af5a1e 100644 --- a/lib/logstash/inputs/jms.rb +++ b/lib/logstash/inputs/jms.rb @@ -281,13 +281,13 @@ def queue_event(msg, output_queue) if @include_header msg.attributes && msg.attributes.each do |field, value| - event.set(field.to_s, value) unless @skip_headers.include?(field.to_s) + set_field(event, field.to_s, value) unless @skip_headers.include?(field.to_s) end end if @include_properties msg.properties && msg.properties.each do |field, value| - event.set(field.to_s, value) unless @skip_properties.include?(field.to_s) + set_field(event, field.to_s, value) unless @skip_properties.include?(field.to_s) end end @@ -300,6 +300,17 @@ def queue_event(msg, output_queue) end end + def set_field(event, field, value) + begin + event.set(field, value) + rescue Java::JavaLang::RuntimeException => e # Using RuntimeException as a common ancestor to MissingConverterException + # And IllegalArgumentException, which are used across different + # Logstash versions for Valuefier errors + logger.warn("Unable to convert value of type #{value.class} for field #{field}, falling back to using string representation", + :exception => e) + event.set(field, value.to_s) + end + end def subscriber(session, params) destination_key = @pub_sub ? :topic_name : :queue_name diff --git a/spec/inputs/unit/jms_spec.rb b/spec/inputs/unit/jms_spec.rb index f0f00d8..b6a835e 100644 --- a/spec/inputs/unit/jms_spec.rb +++ b/spec/inputs/unit/jms_spec.rb @@ -199,6 +199,19 @@ end end + describe '#set_field' do + let(:event) { LogStash::Event.new } + it 'should set the field correctly' do + plugin.set_field(event, "hello", "fff") + expect(event.get("hello")).to eql("fff") + end + + it 'should set handle field values that are not convertible' do + plugin.set_field(event, "hello", Date.new(1999,1,1)) + expect(event.get("hello")).to eql("1999-01-01") + end + end + describe '#error_hash' do context 'should handle Java exceptions with a chain of causes' do let (:raised) { java.lang.Exception.new("Outer", java.lang.RuntimeException.new("middle", java.io.IOException.new("Inner")))}