Skip to content

Commit

Permalink
Merge pull request #3220 from abicky/support-unixtime-micros-and-nanos
Browse files Browse the repository at this point in the history
Support unixtime_micros and unixtime_nanos to inject time
  • Loading branch information
repeatedly authored Jan 4, 2021
2 parents 36d017d + e2da752 commit 78c93e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/fluent/plugin_helper/inject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module InjectParams
config_param :time_key, :string, default: nil

# To avoid defining :time_type twice
config_param :time_type, :enum, list: [:float, :unixtime, :unixtime_millis, :string], default: :float
config_param :time_type, :enum, list: [:float, :unixtime, :unixtime_millis, :unixtime_micros, :unixtime_nanos, :string], default: :float

Fluent::TimeMixin::TIME_PARAMETERS.each do |name, type, opts|
config_param(name, type, **opts)
Expand Down Expand Up @@ -132,7 +132,9 @@ def configure(conf)
if @_inject_time_key
@_inject_time_formatter = case @inject_config.time_type
when :float then ->(time){ time.to_r.truncate(+6).to_f } # microsecond floating point value
when :unixtime_millis then ->(time) { time.to_f.floor(3) * 1000 }
when :unixtime_millis then ->(time) { time.respond_to?(:nsec) ? time.to_i * 1_000 + time.nsec / 1_000_000 : (time * 1_000).floor }
when :unixtime_micros then ->(time) { time.respond_to?(:nsec) ? time.to_i * 1_000_000 + time.nsec / 1_000 : (time * 1_000_000).floor }
when :unixtime_nanos then ->(time) { time.respond_to?(:nsec) ? time.to_i * 1_000_000_000 + time.nsec : (time * 1_000_000_000).floor }
when :unixtime then ->(time){ time.to_i }
else
localtime = @inject_config.localtime && !@inject_config.utc
Expand Down
1 change: 1 addition & 0 deletions lib/fluent/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def nsec
def to_int
@sec
end
alias :to_i :to_int

def to_f
@sec + @nsec / 1_000_000_000.0
Expand Down
29 changes: 29 additions & 0 deletions test/plugin_helper/test_inject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,35 @@ def config_inject_section(hash = {})

record = {"key1" => "value1", "key2" => 2}
assert_equal record.merge({"timedata" => unixtime_millis}), @d.inject_values_to_record('tag', time, record)
assert_equal record.merge({"timedata" => time_in_unix * 1_000}), @d.inject_values_to_record('tag', time_in_unix, record)
end

test 'injects time as unix time micros into specified key' do
time_in_unix = Time.parse("2016-06-21 08:10:11 +0900").to_i
time_subsecond = 320_101_224
time = Fluent::EventTime.new(time_in_unix, time_subsecond)
unixtime_micros = 1466464211320101

@d.configure(config_inject_section("time_key" => "timedata", "time_type" => "unixtime_micros"))
@d.start

record = {"key1" => "value1", "key2" => 2}
assert_equal record.merge({"timedata" => unixtime_micros}), @d.inject_values_to_record('tag', time, record)
assert_equal record.merge({"timedata" => time_in_unix * 1_000_000}), @d.inject_values_to_record('tag', time_in_unix, record)
end

test 'injects time as unix time nanos into specified key' do
time_in_unix = Time.parse("2016-06-21 08:10:11 +0900").to_i
time_subsecond = 320_101_224
time = Fluent::EventTime.new(time_in_unix, time_subsecond)
unixtime_nanos = 1466464211320101224

@d.configure(config_inject_section("time_key" => "timedata", "time_type" => "unixtime_nanos"))
@d.start

record = {"key1" => "value1", "key2" => 2}
assert_equal record.merge({"timedata" => unixtime_nanos}), @d.inject_values_to_record('tag', time, record)
assert_equal record.merge({"timedata" => time_in_unix * 1_000_000_000}), @d.inject_values_to_record('tag', time_in_unix, record)
end

test 'injects time as unix time into specified key' do
Expand Down

0 comments on commit 78c93e8

Please sign in to comment.