Merge pull request #1211 from colinsurprenant/event_overwrite_fix

missing accessors reset on overwrite & specs
This commit is contained in:
Colin Surprenant 2014-03-27 16:20:44 -04:00
commit 6350d68639
2 changed files with 26 additions and 2 deletions

View file

@ -154,7 +154,11 @@ class LogStash::Event
public
def overwrite(event)
@data = event.to_hash
# pickup new event @data and also pickup @accessors
# otherwise it will be pointing on previous data
@data = event.instance_variable_get(:@data)
@accessors = event.instance_variable_get(:@accessors)
#convert timestamp if it is a String
if @data[TIMESTAMP].is_a?(String)
@data[TIMESTAMP] = LogStash::Time.parse_iso8601(@data[TIMESTAMP])
@ -170,6 +174,9 @@ class LogStash::Event
public
def append(event)
# non-destructively merge that event with ourselves.
# no need to reset @accessors here because merging will not disrupt any existing field paths
# and if new ones are created they will be picked up.
LogStash::Util.hash_merge(@data, event.to_hash)
end # append
@ -188,7 +195,7 @@ class LogStash::Event
# any format values, delimited by %{foo} where 'foo' is a field or
# metadata member.
#
# For example, if the event has type == "foo" and source == "bar"
# For example, if the event has type == "foo" and host == "bar"
# then this string:
# "type is %{type} and source is %{host}"
# will return

View file

@ -113,6 +113,23 @@ describe LogStash::Event do
end
end
context "#overwrite" do
it "should swap data with new content" do
new_event = LogStash::Event.new(
"type" => "new",
"message" => "foo bar",
)
subject.overwrite(new_event)
insist { subject["message"] } == "foo bar"
insist { subject["type"] } == "new"
["tags", "source", "a", "c", "f", "j"].each do |field|
insist { subject[field] } == nil
end
end
end
context "#append" do
it "should append strings to an array" do
subject.append(LogStash::Event.new("message" => "another thing"))