raise error when formatting time without @timestamp field

Closes #2730

Fixes #2736
This commit is contained in:
Tal Levy 2015-03-02 12:57:12 -08:00 committed by Jordan Sissel
parent c3230fea1f
commit 203d1e5fc7
2 changed files with 18 additions and 0 deletions

View file

@ -224,6 +224,10 @@ class LogStash::Event
# Take the inside of the %{ ... }
key = tok[2 ... -1]
if key[0] == "+" && !@data.has_key?(TIMESTAMP)
raise LogStash::Error, "Unable to format \"#{key}\" in string \"#{format}\", #{TIMESTAMP} field not found"
end
if key == "+%s"
# Got %{+%s}, support for unix epoch time
next @data[TIMESTAMP].to_i

View file

@ -67,12 +67,26 @@ describe LogStash::Event do
expect(subject.sprintf("%{+%s}")).to eq("1356998400")
end
it "should raise error when formatting %{+%s} when @timestamp field is missing" do
str = "hello-%{+%s}"
subj = subject.clone
subj.remove("[@timestamp]")
expect{ subj.sprintf(str) }.to raise_error(LogStash::Error)
end
it "should report a time with %{+format} syntax", :if => RUBY_ENGINE == "jruby" do
expect(subject.sprintf("%{+YYYY}")).to eq("2013")
expect(subject.sprintf("%{+MM}")).to eq("01")
expect(subject.sprintf("%{+HH}")).to eq("00")
end
it "should raise error with %{+format} syntax when @timestamp field is missing", :if => RUBY_ENGINE == "jruby" do
str = "logstash-%{+YYYY}"
subj = subject.clone
subj.remove("[@timestamp]")
expect{ subj.sprintf(str) }.to raise_error(LogStash::Error)
end
it "should report fields with %{field} syntax" do
expect(subject.sprintf("%{type}")).to eq("sprintf")
expect(subject.sprintf("%{message}")).to eq(subject["message"])