diff --git a/lib/rackstash/encoders/logstash.rb b/lib/rackstash/encoders/logstash.rb index fa25dfc..dab3b39 100644 --- a/lib/rackstash/encoders/logstash.rb +++ b/lib/rackstash/encoders/logstash.rb @@ -20,6 +20,8 @@ module Rackstash # @return [String] the event as a single-line JSON string def encode(event) event[FIELD_VERSION] = '1'.freeze if event[FIELD_VERSION].nil? + event[FIELD_TIMESTAMP] ||= Time.now.utc.freeze + super(event) end end diff --git a/spec/rackstash/encoders/logstash_spec.rb b/spec/rackstash/encoders/logstash_spec.rb index cf09eb5..28a4395 100644 --- a/spec/rackstash/encoders/logstash_spec.rb +++ b/spec/rackstash/encoders/logstash_spec.rb @@ -14,7 +14,8 @@ describe Rackstash::Encoders::Logstash do describe '#encode' do it 'formats the passed event hash as a JSON string and includes @version' do event = { 'hello' => 'world', 'message' => ["hello\n", "world"] } - expect(encoder.encode(event)).to eql '{"hello":"world","message":"hello\nworld","@version":"1"}' + expect(encoder.encode(event)) + .to match /\A\{"hello":"world","message":"hello\\nworld","@version":"1","@timestamp":"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{6}Z"\}\z/ end end end