|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright 2025 Daijiro Fukuda |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +require "fluent/env" |
| 18 | + |
| 19 | +# For compatibility with Fluentd v1.15.2 and earlier. |
| 20 | +# See https://github.com/fluent/fluentd/pull/3883 |
| 21 | +if Fluent.windows? |
| 22 | + require "fluent/file_wrapper" |
| 23 | +else |
| 24 | + Fluent::FileWrapper = File |
| 25 | +end |
| 26 | + |
| 27 | +module Fluent |
| 28 | + module TailChecker |
| 29 | + class CollectionRatioChecker |
| 30 | + def initialize(posfile, follow_inodes) |
| 31 | + @posfile = posfile |
| 32 | + @follow_inodes = follow_inodes |
| 33 | + @collection_ratio_threshold = 0.8 |
| 34 | + end |
| 35 | + |
| 36 | + def check |
| 37 | + unacceptable_collection_ratio_found = false |
| 38 | + unacceptable_collection_ratio_path_and_ratio_list = [] |
| 39 | + checked_file_counts = 0 |
| 40 | + |
| 41 | + @posfile.watching_entries.each do |entry| |
| 42 | + size = get_file_size(entry) |
| 43 | + next if size.nil? |
| 44 | + |
| 45 | + checked_file_counts += 1 |
| 46 | + ratio = collection_ratio(entry.pos, size) |
| 47 | + if ratio < @collection_ratio_threshold |
| 48 | + unacceptable_collection_ratio_found = true |
| 49 | + unacceptable_collection_ratio_path_and_ratio_list.append([entry.path, ratio]) |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + puts "Checked collection ratio of #{checked_file_counts} files." |
| 54 | + |
| 55 | + if unacceptable_collection_ratio_found |
| 56 | + log_issue(unacceptable_collection_ratio_path_and_ratio_list) |
| 57 | + return false |
| 58 | + end |
| 59 | + |
| 60 | + true |
| 61 | + end |
| 62 | + |
| 63 | + def get_file_size(pos_entry) |
| 64 | + if @follow_inodes |
| 65 | + get_file_size_from_inode(pos_entry) |
| 66 | + else |
| 67 | + get_file_size_from_path(pos_entry) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def get_file_size_from_path(pos_entry) |
| 72 | + FileTest.size?(pos_entry.path) |
| 73 | + end |
| 74 | + |
| 75 | + def get_file_size_from_inode(pos_entry) |
| 76 | + return nil unless FileTest.exist?(pos_entry.path) |
| 77 | + |
| 78 | + stat = Fluent::FileWrapper.stat(pos_entry.path) |
| 79 | + # If follow_inodes is enabled, the inode of the current logfile should match the inode in the pos_file. |
| 80 | + # It may not match for the rotated logfiles because the path info in the pos_file is not updated. |
| 81 | + # So, at least, check the current logfile. |
| 82 | + # For rotated logfiles, check them if inode matches. |
| 83 | + return nil unless stat.ino == pos_entry.ino |
| 84 | + |
| 85 | + stat.size |
| 86 | + end |
| 87 | + |
| 88 | + def collection_ratio(pos, file_size) |
| 89 | + return 1.0 if file_size == 0 |
| 90 | + |
| 91 | + pos.to_f / file_size |
| 92 | + end |
| 93 | + |
| 94 | + def log_issue(path_and_ratio_list) |
| 95 | + puts "Collection ratio of some files are too low. Collection of those files may have stopped or may not be keeping up." |
| 96 | + if @follow_inodes |
| 97 | + puts "This can be a known log loss issue of the follow_inodes feature that was fixed in Fluentd v1.16.2." |
| 98 | + end |
| 99 | + |
| 100 | + puts "Filepaths with too low collection ratio (threshold: #{@collection_ratio_threshold}):" |
| 101 | + path_and_ratio_list.each do |path, ratio| |
| 102 | + puts " #{path} (ratio: #{ratio})" |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments