Skip to content

Commit 938adcf

Browse files
committed
Add 'metadata' option to include @metadata field in output.
Just like the rubydebug codec, being able to include the @metadata field in the output of the json_lines codec can be useful, for example for debugging or test tools that prefer machine-readable output. Since the Event class's constructor mutates the input hash I had to refactor some existing tests to send in a clone of the hash since it'll otherwise end up without a @metadata field.
1 parent c980fe9 commit 938adcf

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

lib/logstash/codecs/json_lines.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,21 @@ class LogStash::Codecs::JSONLines < LogStash::Codecs::Base
2828
# Change the delimiter that separates lines
2929
config :delimiter, :validate => :string, :default => "\n"
3030

31+
# If true, the event's metadata (the `@metadata` field) will be
32+
# included when used as an output codec.
33+
config :metadata, :validate => :boolean, :default => false
34+
3135
public
3236

3337
def register
3438
@buffer = FileWatch::BufferedTokenizer.new(@delimiter)
3539
@converter = LogStash::Util::Charset.new(@charset)
3640
@converter.logger = @logger
41+
if @metadata
42+
@encoder = method(:encode_with_metadata)
43+
else
44+
@encoder = method(:encode_default)
45+
end
3746
end
3847

3948
def decode(data, &block)
@@ -43,12 +52,22 @@ def decode(data, &block)
4352
end
4453

4554
def encode(event)
55+
@encoder.call(event)
56+
end
57+
58+
private
59+
60+
def encode_default(event)
4661
# Tack on a @delimiter for now because previously most of logstash's JSON
4762
# outputs emitted one per line, and whitespace is OK in json.
4863
@on_event.call(event, "#{event.to_json}#{@delimiter}")
4964
end
5065

51-
private
66+
def encode_with_metadata(event)
67+
# Tack on a @delimiter for now because previously most of logstash's JSON
68+
# outputs emitted one per line, and whitespace is OK in json.
69+
@on_event.call(event, "#{event.to_json_with_metadata}#{@delimiter}")
70+
end
5271

5372
# from_json_parse uses the Event#from_json method to deserialize and directly produce events
5473
def from_json_parse(json, &block)

spec/codecs/json_lines_spec.rb

+16-4
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@
120120
end
121121

122122
context "#encode" do
123-
let(:data) { { LogStash::Event::TIMESTAMP => "2015-12-07T11:37:00.000Z", "foo" => "bar", "baz" => {"bah" => ["a","b","c"]}} }
124-
let(:event) { LogStash::Event.new(data) }
123+
let(:data) { { LogStash::Event::TIMESTAMP => "2015-12-07T11:37:00.000Z", "foo" => "bar", "baz" => {"bah" => ["a","b","c"]}, "@metadata" => {"metafield" => "metavalue"} } }
124+
let(:event) { LogStash::Event.new(data.clone) }
125125

126126
it "should return json data" do
127127
got_event = false
128128
subject.on_event do |e, d|
129-
insist { d } == "#{LogStash::Event.new(data).to_json}\n"
129+
insist { d } == "#{LogStash::Event.new(data.clone).to_json}\n"
130130
insist { LogStash::Json.load(d)["foo"] } == data["foo"]
131131
insist { LogStash::Json.load(d)["baz"] } == data["baz"]
132132
insist { LogStash::Json.load(d)["bah"] } == data["bah"]
@@ -142,7 +142,19 @@
142142

143143
it "should decode multiple lines separated by the delimiter" do
144144
subject.on_event do |e, d|
145-
insist { d } == "#{LogStash::Event.new(data).to_json}#{delimiter}"
145+
insist { d } == "#{LogStash::Event.new(data.clone).to_json}#{delimiter}"
146+
end
147+
subject.encode(event)
148+
end
149+
end
150+
151+
context "when enabling metadata output" do
152+
let(:codec_options) { { "metadata" => true } }
153+
154+
it "should include the @metadata field" do
155+
subject.on_event do |e, d|
156+
insist { d } == "#{LogStash::Event.new(data.clone).to_json_with_metadata}\n"
157+
insist { LogStash::Json.load(d)["@metadata"] } == data["@metadata"]
146158
end
147159
subject.encode(event)
148160
end

0 commit comments

Comments
 (0)