Skip to content

Commit 300635e

Browse files
committed
add '--ratio' option to change threshold of collection ratio
In addition, change the default value to 0.5. Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
1 parent 2f78623 commit 300635e

5 files changed

Lines changed: 40 additions & 18 deletions

File tree

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ If you use [follow_inodes](https://docs.fluentd.org/input/tail#follow_inodes), t
161161
$ tailcheck --follow_inodes /var/log/td-agent/pos/secure
162162
```
163163

164+
You can change the minimum ratio of collection of each target log file by specify `--ratio DECIMAL`.
165+
By default (`0.5`), the command detects log files that have not been collected up to 50% of the filesize.
166+
167+
```
168+
$ tailcheck --ratio 0.7 /var/log/td-agent/pos/secure
169+
```
170+
164171
### Result example
165172

166173
#### No anomalies found
@@ -211,9 +218,9 @@ Check /path/to/pos.
211218
Done duplication check for 2 PosEntries.
212219
Done collection ratio check for 2 files.
213220
Collection ratio of some files are too low. Collection of those files may not be keeping up. Or it may have stopped with some anomalies. This can be a known log missing issue of the follow_inodes feature that was fixed in Fluentd v1.16.2 (fluent-package v5.0.0, td-agent v4.5.1). If you are using any version older than these, updating Fluentd will resolve the issue.
214-
Filepaths with too low collection ratio (threshold: 0.8):
215-
/test/bar.log (ratio: 0.7)
216-
/test/foo.log (ratio: 0.7)
221+
Filepaths with too low collection ratio (threshold: 0.5):
222+
/test/bar.log (ratio: 0.2)
223+
/test/foo.log (ratio: 0.1)
217224
218225
All check completed.
219226
Some anomalies are found. Please check whether there is any log loss.
@@ -222,9 +229,9 @@ Some anomalies are found. Please check whether there is any log loss.
222229
In this case, collection ratio of some target files are too low.
223230
Collection of those files may have stopped or may not be keeping up.
224231

225-
> /test/bar.log (ratio: 0.7)
232+
> /test/bar.log (ratio: 0.2)
226233
227-
This means that only 70% of the data of the file is collected for the filesize.
234+
This means that only 20% of the data of the file is collected for the filesize.
228235
If it is not keeping up temporarily, then it is no problem.
229236
If this is always the case, or if collection has stopped completely, then log missing may occur.
230237

lib/fluent/tail_checker/collection_ratio_checker.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
module Fluent
3232
module TailChecker
3333
class CollectionRatioChecker
34-
def initialize(posfile, follow_inodes)
34+
def initialize(posfile, follow_inodes, collection_ratio_threshold)
3535
@posfile = posfile
3636
@follow_inodes = follow_inodes
37-
@collection_ratio_threshold = 0.8
37+
@collection_ratio_threshold = collection_ratio_threshold
3838
end
3939

4040
def check

lib/fluent/tail_checker/tail_check.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TailCheck
2626
def initialize
2727
@pos_filepaths = []
2828
@follow_inodes = false
29+
@collection_ratio_threshold = 0.5
2930
end
3031

3132
def run(argv=ARGV)
@@ -51,8 +52,20 @@ def parse_command_line(argv)
5152
parser.on("--follow_inodes", "Check the specified pos files with the condition that the follow_inodes feature is enabled.", "Default: Disabled") do
5253
@follow_inodes = true
5354
end
55+
parser.on("--ratio NUM", Float, "Minimum ratio of collection of each target log file to accept.", "Default: #{@collection_ratio_threshold}") do |v|
56+
@collection_ratio_threshold = v
57+
end
58+
59+
begin
60+
@pos_filepaths = parser.parse(argv)
5461

55-
@pos_filepaths = parser.parse(argv)
62+
if @collection_ratio_threshold < 0 or @collection_ratio_threshold > 1
63+
raise OptionParser::InvalidArgument, "--ratio #{@collection_ratio_threshold} must be an decimal from 0 to 1."
64+
end
65+
rescue OptionParser::ParseError => e
66+
$stderr.puts e, "", parser.help, ""
67+
raise
68+
end
5669
end
5770

5871
def validate_paths(paths)
@@ -82,7 +95,7 @@ def check
8295
next if pos_file.nil?
8396

8497
succeeded = DuplicatedPosChecker.new(pos_file, @follow_inodes).check && succeeded
85-
succeeded = CollectionRatioChecker.new(pos_file, @follow_inodes).check && succeeded
98+
succeeded = CollectionRatioChecker.new(pos_file, @follow_inodes, @collection_ratio_threshold).check && succeeded
8699
end
87100

88101
puts "\nAll check completed."

test/fluent/tail_checker/collection_ratio_checker_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
class Fluent::TailChecker::CollectionRatioCheckerTest < Test::Unit::TestCase
66
data("Normal ratio", [["test/data/log/foo.log", "test/data/log/bar.log"], 0.9, false, true])
7-
data("Too low ratio", [["test/data/log/foo.log", "test/data/log/bar.log"], 0.7, false, false])
7+
data("Too low ratio", [["test/data/log/foo.log", "test/data/log/bar.log"], 0.3, false, false])
88
data("Normal ratio with follow_inodes", [["test/data/log/foo.log", "test/data/log/bar.log"], 0.9, true, true])
9-
data("Too low ratio with follow_inodes", [["test/data/log/foo.log", "test/data/log/foo.log.1"], 0.7, true, false])
9+
data("Too low ratio with follow_inodes", [["test/data/log/foo.log", "test/data/log/foo.log.1"], 0.3, true, false])
1010
test "Check should return false when too low collection ratio is detected" do |(paths, ratio, follow_inodes, expected)|
1111
pos_entries = paths.map do |path|
1212
stat = Fluent::FileWrapper.stat(path)
1313
Fluent::TailChecker::PosEntry.new(path, stat.size * ratio, stat.ino)
1414
end
1515
pos_file = Fluent::TailChecker::PosFile.new(pos_entries)
16-
checker = Fluent::TailChecker::CollectionRatioChecker.new(pos_file, follow_inodes)
16+
checker = Fluent::TailChecker::CollectionRatioChecker.new(pos_file, follow_inodes, 0.5)
1717

1818
result = checker.check
1919

test/fluent/tail_checker_test.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ class Fluent::TailCheckerTest < Test::Unit::TestCase
1414
"Minimum",
1515
[
1616
["/path/to/pos"],
17-
{ pos_filepaths: ["/path/to/pos"], follow_inodes: false },
17+
{ pos_filepaths: ["/path/to/pos"], follow_inodes: false, collection_ratio_threshold: 0.5 },
1818
]
1919
)
2020
data(
2121
"Full",
2222
[
23-
["--follow_inodes", "/path/to/pos", "/path/to/pos2"],
24-
{ pos_filepaths: ["/path/to/pos", "/path/to/pos2"], follow_inodes: true },
23+
["--follow_inodes", "--ratio", "0.7", "/path/to/pos", "/path/to/pos2"],
24+
{ pos_filepaths: ["/path/to/pos", "/path/to/pos2"], follow_inodes: true, collection_ratio_threshold: 0.7 },
2525
]
2626
)
2727
test "Correct args" do |(args, expected)|
@@ -32,15 +32,17 @@ class Fluent::TailCheckerTest < Test::Unit::TestCase
3232
result = {
3333
pos_filepaths: tail_check.instance_variable_get(:@pos_filepaths),
3434
follow_inodes: tail_check.instance_variable_get(:@follow_inodes),
35+
collection_ratio_threshold: tail_check.instance_variable_get(:@collection_ratio_threshold),
3536
}
3637
assert_equal(expected, result)
3738
end
3839

3940
data("Invalid options", ["--foo", "/path/to/pos"])
41+
data("--ratio: invalid value: ", ["--ratio", "70", "/path/to/pos"])
4042
test "Raise error for invalid options" do |args|
4143
tail_check = Fluent::TailChecker::TailCheck.new
4244

43-
assert_raise(OptionParser::InvalidOption) do
45+
assert_raise do
4446
tail_check.parse_command_line(args)
4547
end
4648
end
@@ -84,7 +86,7 @@ class Fluent::TailCheckerTest < Test::Unit::TestCase
8486
end
8587

8688
data("Acceptable ratio", [0.9, true])
87-
data("Too low ratio", [0.7, false])
89+
data("Too low ratio", [0.3, false])
8890
test "Return false when too low collection ratio is detected" do |(stub_ratio, expected)|
8991
any_instance_of(Fluent::TailChecker::CollectionRatioChecker) do |checker|
9092
mock(checker).get_file_size_from_path(anything).at_least(1) do |pos_entry|
@@ -104,7 +106,7 @@ class Fluent::TailCheckerTest < Test::Unit::TestCase
104106
end
105107

106108
data("Acceptable ratio", [0.9, true])
107-
data("Too low ratio", [0.7, false])
109+
data("Too low ratio", [0.3, false])
108110
test "Return false when too low collection ratio is detected (follow_inodes)" do |(stub_ratio, expected)|
109111
any_instance_of(Fluent::TailChecker::CollectionRatioChecker) do |checker|
110112
mock(checker).get_file_size_from_path.never

0 commit comments

Comments
 (0)