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

format in_tail path with the specified timezone #2719

Merged
merged 1 commit into from
Dec 19, 2019
Merged
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
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