Skip to content

Commit f4d3017

Browse files
ignore blank/empty json lines
1 parent 84c41b0 commit f4d3017

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/logstash/codecs/json_lines.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ def from_json_parse(json, &block)
5959

6060
# legacy_parse uses the LogStash::Json class to deserialize json
6161
def legacy_parse(json, &block)
62-
yield LogStash::Event.new(LogStash::Json.load(json))
62+
# ignore empty/blank lines which LogStash::Json#load returns as nil
63+
o = LogStash::Json.load(json)
64+
yield(LogStash::Event.new(o)) if o
6365
rescue LogStash::Json::ParserError
6466
yield LogStash::Event.new("message" => json, "tags" => ["_jsonparsefailure"])
6567
end
6668

69+
# keep compatibility with all v2.x distributions. only in 2.3 will the Event#from_json method be introduced
70+
# and we need to keep compatibility for all v2 releases.
6771
alias_method :parse, LogStash::Event.respond_to?(:from_json) ? :from_json_parse : :legacy_parse
68-
69-
end # class LogStash::Codecs::JSONLines
72+
end

spec/codecs/json_lines_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
end
7979

8080
context "when json could not be parsed" do
81-
8281
let(:message) { "random_message\n" }
8382

8483
it "add the failure tag" do
@@ -98,8 +97,26 @@
9897
expect(event['tags']).to include "_jsonparsefailure"
9998
end
10099
end
100+
end
101+
102+
context "blank lines" do
103+
let(:collector) { Array.new }
101104

105+
it "should ignore bare blanks" do
106+
subject.decode("\n\n") do |event|
107+
collector.push(event)
108+
end
109+
expect(collector.size).to eq(0)
110+
end
111+
112+
it "should ignore in between blank lines" do
113+
subject.decode("\n{\"a\":1}\n\n{\"b\":2}\n\n") do |event|
114+
collector.push(event)
115+
end
116+
expect(collector.size).to eq(2)
117+
end
102118
end
119+
103120
end
104121

105122
context "#encode" do

0 commit comments

Comments
 (0)