Skip to content

Commit

Permalink
Support unixtime_millis
Browse files Browse the repository at this point in the history
It is useful to add timestamp as milliseconds since Unix epoch.
This is the short hand to add the filed using filter
record_transformer like followings:

```
<filter>
  @type record_transformer
  enable_ruby
  <record>
    time ${time.to_f.floor(3) * 1000}
  </record>
</filter>
```
  • Loading branch information
okkez committed Oct 8, 2020
1 parent 9170665 commit 317112b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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, :string], default: :float
config_param :time_type, :enum, list: [:float, :unixtime, :unixtime_millis, :string], default: :float

Fluent::TimeMixin::TIME_PARAMETERS.each do |name, type, opts|
config_param(name, type, **opts)
Expand Down Expand Up @@ -132,6 +132,7 @@ 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 then ->(time){ time.to_i }
else
localtime = @inject_config.localtime && !@inject_config.utc
Expand Down
13 changes: 13 additions & 0 deletions test/plugin_helper/test_inject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ def config_inject_section(hash = {})
assert_equal record.merge({"timedata" => float_time}), @d.inject_values_to_record('tag', time, record)
end

test 'injects time as unix time millis 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_millis = 1466464211320

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

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

test 'injects time as unix time into specified key' do
time_in_unix = Time.parse("2016-06-21 08:10:11 +0900").to_i
time_subsecond = 320_101_224
Expand Down

0 comments on commit 317112b

Please sign in to comment.