Skip to content

Commit

Permalink
format in_tail path with the specified timezone
Browse files Browse the repository at this point in the history
Signed-off-by: tatsu-yam <[email protected]>
  • Loading branch information
tatsu-yam committed Dec 16, 2019
1 parent bfc39b5 commit 5bc0994
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def initialize
config_param :skip_refresh_on_startup, :bool, default: false
desc 'Ignore repeated permission error logs'
config_param :ignore_repeated_permission_error, :bool, default: false
desc 'Format path with the specified timezone'
config_param :path_timezone, :string, default: nil

config_section :parse, required: false, multi: true, init: true, param_name: :parser_configs do
config_argument :usage, :string, default: 'in_tail_parser'
Expand Down Expand Up @@ -129,6 +131,11 @@ def configure(conf)
if @paths.empty?
raise Fluent::ConfigError, "tail: 'path' parameter is required on tail input"
end
if @path_timezone
Fluent::Timezone.validate!(@path_timezone)
@path_formatters = @paths.map{|path| [path, Fluent::Timezone.formatter(@path_timezone, path)]}.to_h
@exclude_path_formatters = @exclude_path.map{|path| [path, Fluent::Timezone.formatter(@path_timezone, path)]}.to_h
end

# TODO: Use plugin_root_dir and storage plugin to store positions if available
if @pos_file
Expand Down Expand Up @@ -225,17 +232,20 @@ def close
end

def expand_paths
date = Time.now
date = Fluent::EventTime.now
paths = []

@paths.each { |path|
path = date.strftime(path)
path = if @path_timezone
@path_formatters[path].call(date)
else
date.to_time.strftime(path)
end
if path.include?('*')
paths += Dir.glob(path).select { |p|
begin
is_file = !File.directory?(p)
if File.readable?(p) && is_file
if @limit_recently_modified && File.mtime(p) < (date - @limit_recently_modified)
if @limit_recently_modified && File.mtime(p) < (date.to_time - @limit_recently_modified)
false
else
true
Expand All @@ -259,7 +269,14 @@ def expand_paths
paths << path
end
}
excluded = @exclude_path.map { |path| path = date.strftime(path); path.include?('*') ? Dir.glob(path) : path }.flatten.uniq
excluded = @exclude_path.map { |path|
path = if @path_timezone
@exclude_path_formatters[path].call(date)
else
date.to_time.strftime(path)
end
path.include?('*') ? Dir.glob(path) : path
}.flatten.uniq
paths - excluded
end

Expand Down
21 changes: 21 additions & 0 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,27 @@ def test_expand_paths
assert_equal EX_PATHS - [EX_PATHS.last], plugin.expand_paths.sort
end

def test_expand_paths_with_timezone
['Asia/Taipei', '+08'].each do |tz_type|
taipei_config = EX_CONFIG + config_element("", "", {"path_timezone" => tz_type})
plugin = create_driver(taipei_config, false).instance

# Test exclude
exclude_config = taipei_config + config_element("", "", { "exclude_path" => %Q(["test/plugin/**/%Y%m%d-%H%M%S.log"]) })
exclude_plugin = create_driver(exclude_config, false).instance

with_timezone('utc') do
flexstub(Time) do |timeclass|
# env : 2010-01-01 19:04:05 (UTC), tail path : 2010-01-02 03:04:05 (Asia/Taipei)
timeclass.should_receive(:now).with_no_args.and_return(Time.new(2010, 1, 1, 19, 4, 5))

assert_equal EX_PATHS, plugin.expand_paths.sort
assert_equal EX_PATHS - [EX_PATHS.first], exclude_plugin.expand_paths.sort
end
end
end
end

def test_log_file_without_extension
expected_files = [
'test/plugin/data/log/bar',
Expand Down

0 comments on commit 5bc0994

Please sign in to comment.