Make sure that sprintf return a new clone

In some cases `Event#sprintf` was returning a reference to the value and
not a new instance. This would lead in some weird behavior when using
`add_fields` with the sprintf syntax.

Fixes #4592

Fixes #4605
This commit is contained in:
Pier-Hugues Pellerin 2016-02-01 16:04:23 -05:00
parent ad109f0a5b
commit 49d22f9771
2 changed files with 20 additions and 1 deletions

View file

@ -125,7 +125,9 @@ module LogStash
when Hash
LogStash::Json.dump(value)
else
value
# Make sure we dont work on the refence of the value
# The Java Event implementation was always returning a string.
"#{value}"
end
end
end

View file

@ -1,5 +1,6 @@
# encoding: utf-8
require "spec_helper"
require "logstash/util/decorators"
require "json"
describe LogStash::Event do
@ -73,6 +74,22 @@ describe LogStash::Event do
end
context "#sprintf" do
it "should not return a String reference" do
data = "NOT-A-REFERENCE"
event = LogStash::Event.new({ "reference" => data })
LogStash::Util::Decorators.add_fields({"reference_test" => "%{reference}"}, event, "dummy-plugin")
data.downcase!
expect(event["reference_test"]).not_to eq(data)
end
it "should not return a Fixnum reference" do
data = 1
event = LogStash::Event.new({ "reference" => data })
LogStash::Util::Decorators.add_fields({"reference_test" => "%{reference}"}, event, "dummy-plugin")
data += 41
expect(event["reference_test"]).to eq("1")
end
it "should report a unix timestamp for %{+%s}" do
expect(subject.sprintf("%{+%s}")).to eq("1356998400")
end