Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discuss] accept @timestamp numeric value as epoch in event initialization #10369

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions logstash-core/spec/logstash/legacy_ruby_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@
expect(LogStash::Event.new("@timestamp" => "2014-06-12T00:12:17.114Z").timestamp.to_i).to eq(t.to_i)
end

it "should coerce seconds epoch" do
expect(LogStash::Event.new("@timestamp" => 19999999999).timestamp.to_iso8601).to eq("2603-10-11T11:33:19.000Z")
expect(LogStash::Event.new("@timestamp" => 1549479478).timestamp.to_iso8601).to eq("2019-02-06T18:57:58.000Z")
end

it "should coerce milliseconds epoch" do
expect(LogStash::Event.new("@timestamp" => 20000000000).timestamp.to_iso8601).to eq("1970-08-20T11:33:20.000Z")
expect(LogStash::Event.new("@timestamp" => 1549479400053).timestamp.to_iso8601).to eq("2019-02-06T18:56:40.053Z")
end

it "should assign current time when no timestamp" do
expect(LogStash::Event.new({}).timestamp.to_i).to be_within(2).of (Time.now.to_i)
end
Expand All @@ -407,11 +417,6 @@
expect(event.timestamp.to_i).to be_within(2).of Time.now.to_i
expect(event.get("tags")).to eq([LogStash::Event::TIMESTAMP_FAILURE_TAG])
expect(event.get(LogStash::Event::TIMESTAMP_FAILURE_FIELD)).to eq("foo")

event = LogStash::Event.new("@timestamp" => 666)
expect(event.timestamp.to_i).to be_within(2).of Time.now.to_i
expect(event.get("tags")).to eq([LogStash::Event::TIMESTAMP_FAILURE_TAG])
expect(event.get(LogStash::Event::TIMESTAMP_FAILURE_FIELD)).to eq(666)
end

it "should warn for invalid value" do
Expand Down
9 changes: 9 additions & 0 deletions logstash-core/src/main/java/org/logstash/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTime;
import org.jruby.RubyFixnum;
import org.jruby.RubyNil;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
Expand Down Expand Up @@ -41,6 +42,8 @@ public final class Event implements Cloneable, Queueable {

private static final Logger logger = LogManager.getLogger(Event.class);

private static final long MILLISEC_EPOCH_THRESHOLD = 20000000000L;

public Event()
{
this.metadata = new ConvertedMap(10);
Expand Down Expand Up @@ -317,6 +320,12 @@ private static Timestamp parseTimestamp(final Object o) {
return new Timestamp((Date) o);
} else if (o instanceof RubySymbol) {
return new Timestamp(((RubySymbol) o).asJavaString());
} else if (o instanceof RubyFixnum) {
// per https://github.com/elastic/logstash/pull/10369 is was decided to use 20000000000
// as the threshold to differentiate between a seconds vs milliseconds epoch representations
final long epoch = RubyFixnum.num2long((RubyFixnum)o);
final long units_multiplier = (epoch < MILLISEC_EPOCH_THRESHOLD) ? 1000 : 1;
return new Timestamp(epoch * units_multiplier);
} else {
logger.warn("Unrecognized " + TIMESTAMP + " value type=" + o.getClass().toString());
}
Expand Down