From 1c461bf8da6c948d2daa47bb6559a24e0c2d9c22 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Sat, 27 Jan 2018 00:48:16 +0100 Subject: [PATCH] Specify exact behavior for @version and @timestamp fields for Logstash encoder --- spec/rackstash/encoder/logstash_spec.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/spec/rackstash/encoder/logstash_spec.rb b/spec/rackstash/encoder/logstash_spec.rb index b5819f0..4c0bd2e 100644 --- a/spec/rackstash/encoder/logstash_spec.rb +++ b/spec/rackstash/encoder/logstash_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true # -# Copyright 2017 Holger Just +# Copyright 2017 - 2018 Holger Just # # This software may be modified and distributed under the terms # of the MIT license. See the LICENSE.txt file for details. @@ -13,10 +13,24 @@ describe Rackstash::Encoder::Logstash do let(:encoder) { described_class.new } describe '#encode' do - it 'formats the passed event hash as a JSON string and includes @version' do + it 'formats the passed event hash as JSON and adds @version and @timstamp' do event = { 'hello' => 'world', 'message' => ["hello\n", 'world'] } 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 + + it 'keeps an existing @version field' do + event = { 'foo' => 'bar', '@version' => '2.5' } + expect(encoder.encode(event)) + .to match(/\A{"foo":"bar","@version":"2.5","@timestamp":"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{6}Z"\}\z/) + end + + it 'formats an existing @timestamp field' do + time = Time.parse('2016-10-17 13:37:00 +03:00') + event = { 'message' => 'msg', '@timestamp' => time } + + expect(encoder.encode(event)) + .to eql '{"message":"msg","@timestamp":"2016-10-17T10:37:00.000000Z","@version":"1"}' + end end end