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

in_tail: Expand glob capability for square brackets and one character matcher #4401

Merged
merged 13 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 13 additions & 1 deletion lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def initialize
config_param :path, :string
desc 'path delimiter used for spliting path config'
config_param :path_delimiter, :string, default: ','
desc 'Use extended glob patterns. Adding a capability to handle [] and ?.'
config_param :use_extended_glob, :bool, default: false
desc 'The tag of the event.'
config_param :tag, :string
desc 'The paths to exclude the files from watcher list.'
Expand Down Expand Up @@ -283,6 +285,16 @@ def have_read_capability?
@capability.have_capability?(:effective, :dac_override)
end

# Curly braces is not supported for now because the default delimiter of path is ",".
# This should be collided for wildcard pattern for curly braces.
def use_glob?(path)
if @use_extended_glob
path.include?('*') || path.include?('?') || /\[.*\]/.match(path)
else
path.include?('*')
end
end

def expand_paths
date = Fluent::EventTime.now
paths = []
Expand All @@ -292,7 +304,7 @@ def expand_paths
else
date.to_time.strftime(path)
end
if path.include?('*')
if use_glob?(path)
paths += Dir.glob(path).select { |p|
begin
is_file = !File.directory?(p)
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
74 changes: 74 additions & 0 deletions test/plugin/test_in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,80 @@ def test_expand_paths
assert_equal(ex_paths - [ex_paths.last], plugin.expand_paths.values.sort_by { |path_ino| path_ino.path })
end

sub_test_case "expand_paths with glob" do |data|
sub_test_case "use_extended_glob" do
data("square brackets" => [true, "test/plugin/data/log_numeric/[0-1][2-4].log"],
"asterisk" => [true, "test/plugin/data/log/*.log"],
"one character matcher" => [true, "test/plugin/data/log/tes?.log"],
)
def test_expand_paths_with_use_glob_p
result, path = data
config = config_element("", "", {
"tag" => "tail",
"path" => path,
"format" => "none",
"pos_file" => "#{@tmp_dir}/tail.pos",
"read_from_head" => true,
"refresh_interval" => 30,
"use_extended_glob" => true,
"rotate_wait" => "#{EX_ROTATE_WAIT}s",
"follow_inodes" => "#{EX_FOLLOW_INODES}",
})
plugin = create_driver(config, false).instance
assert_equal(result, !!plugin.use_glob?(path))
end
end

sub_test_case "only_use_normal_glob" do
data("square brackets" => [false, "test/plugin/data/log_numeric/[0-1][2-4].log"],
"asterisk" => [true, "test/plugin/data/log/*.log"],
"one character matcher" => [false, "test/plugin/data/log/tes?.log"],
)
def test_expand_paths_with_use_glob_p
result, path = data
config = config_element("", "", {
"tag" => "tail",
"path" => path,
"format" => "none",
"pos_file" => "#{@tmp_dir}/tail.pos",
"read_from_head" => true,
"refresh_interval" => 30,
"use_extended_glob" => false,
"rotate_wait" => "#{EX_ROTATE_WAIT}s",
"follow_inodes" => "#{EX_FOLLOW_INODES}",
})
plugin = create_driver(config, false).instance
assert_equal(result, !!plugin.use_glob?(path))
end
end
end

def ex_config_with_brackets
config_element("", "", {
"tag" => "tail",
"path" => "test/plugin/data/log_numeric/[0-1][2-4].log",
"format" => "none",
"pos_file" => "#{@tmp_dir}/tail.pos",
"read_from_head" => true,
"refresh_interval" => 30,
"use_extended_glob" => true,
"rotate_wait" => "#{EX_ROTATE_WAIT}s",
"follow_inodes" => "#{EX_FOLLOW_INODES}",
})
end

def test_expand_paths_with_brackets
expanded_paths = [
create_target_info('test/plugin/data/log_numeric/01.log'),
create_target_info('test/plugin/data/log_numeric/02.log'),
create_target_info('test/plugin/data/log_numeric/12.log'),
create_target_info('test/plugin/data/log_numeric/14.log'),
]

plugin = create_driver(ex_config_with_brackets, false).instance
assert_equal(expanded_paths - [expanded_paths.first], plugin.expand_paths.values.sort_by { |path_ino| path_ino.path })
end

def test_expand_paths_with_duplicate_configuration
expanded_paths = [
create_target_info('test/plugin/data/log/foo/bar.log'),
Expand Down
Loading