Skip to content

Commit 2e5fac2

Browse files
committed
in_tail: add metrics for file tracked count
Signed-off-by: Shizuo Fujita <[email protected]>
1 parent c43586e commit 2e5fac2

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

lib/fluent/plugin/in_tail.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TailInput < Fluent::Plugin::Input
3636
helpers :timer, :event_loop, :parser, :compat_parameters
3737

3838
RESERVED_CHARS = ['/', '*', '%'].freeze
39-
MetricsInfo = Struct.new(:opened, :closed, :rotated, :throttled)
39+
MetricsInfo = Struct.new(:opened, :closed, :rotated, :throttled, :tracked)
4040

4141
class WatcherSetupError < StandardError
4242
def initialize(msg)
@@ -206,11 +206,15 @@ def configure(conf)
206206
@read_bytes_limit_per_second = min_bytes
207207
end
208208
end
209+
209210
opened_file_metrics = metrics_create(namespace: "fluentd", subsystem: "input", name: "files_opened_total", help_text: "Total number of opened files")
210211
closed_file_metrics = metrics_create(namespace: "fluentd", subsystem: "input", name: "files_closed_total", help_text: "Total number of closed files")
211212
rotated_file_metrics = metrics_create(namespace: "fluentd", subsystem: "input", name: "files_rotated_total", help_text: "Total number of rotated files")
212213
throttling_metrics = metrics_create(namespace: "fluentd", subsystem: "input", name: "files_throttled_total", help_text: "Total number of times throttling occurs per file when throttling enabled")
213-
@metrics = MetricsInfo.new(opened_file_metrics, closed_file_metrics, rotated_file_metrics, throttling_metrics)
214+
# The metrics for currently tracking files. Since the value may decrease, it cannot be represented using the counter type, so 'prefer_gauge: true' is used instead.
215+
tracked_file_metrics = metrics_create(namespace: "fluentd", subsystem: "input", name: "files_tracked_count", help_text: "Number of tracked files", prefer_gauge: true)
216+
217+
@metrics = MetricsInfo.new(opened_file_metrics, closed_file_metrics, rotated_file_metrics, throttling_metrics, tracked_file_metrics)
214218
end
215219

216220
def check_dir_permission
@@ -544,6 +548,7 @@ def start_watchers(targets_info)
544548
construct_watcher(target_info)
545549
break if before_shutdown?
546550
}
551+
@metrics.tracked.set(@tails.size)
547552
end
548553

549554
def stop_watchers(targets_info, immediate: false, unwatched: false, remove_watcher: true)
@@ -564,6 +569,7 @@ def stop_watchers(targets_info, immediate: false, unwatched: false, remove_watch
564569
end
565570
end
566571
}
572+
@metrics.tracked.set(@tails.size)
567573
end
568574

569575
def close_watcher_handles
@@ -576,6 +582,7 @@ def close_watcher_handles
576582
@tails_rotate_wait.keys.each do |tw|
577583
tw.close
578584
end
585+
@metrics.tracked.set(@tails.size)
579586
end
580587

581588
# refresh_watchers calls @tails.keys so we don't use stop_watcher -> start_watcher sequence for safety.
@@ -628,6 +635,8 @@ def update_watcher(tail_watcher, pe, new_inode)
628635
end
629636

630637
detach_watcher_after_rotate_wait(tail_watcher, pe.read_inode)
638+
ensure
639+
@metrics.tracked.set(@tails.size)
631640
end
632641

633642
def detach_watcher(tw, ino, close_io = true)
@@ -814,6 +823,7 @@ def statistics
814823
'closed_file_count' => @metrics.closed.get,
815824
'rotated_file_count' => @metrics.rotated.get,
816825
'throttled_log_count' => @metrics.throttled.get,
826+
'tracked_file_count' => @metrics.tracked.get,
817827
})
818828
}
819829
stats

test/plugin/test_in_tail.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,4 +3443,42 @@ def test_next_rotation_occurs_very_fast_while_old_TW_still_waiting_rotate_wait
34433443
d.logs[-2..]
34443444
])
34453445
end
3446+
3447+
test 'statistics' do
3448+
config = config_element("", "", {
3449+
"tag" => "statistics",
3450+
"path" => "#{@tmp_dir}/statistics*.txt",
3451+
"format" => "none",
3452+
"read_from_head" => true,
3453+
})
3454+
Fluent::FileWrapper.open("#{@tmp_dir}/statistics1.txt", "w+") do |f|
3455+
f.puts "foo"
3456+
end
3457+
3458+
d = create_driver(config, false)
3459+
d.run(expect_records: 1, shutdown: false)
3460+
3461+
assert_equal({
3462+
"emit_records" => 0,
3463+
"emit_size" => 0,
3464+
"opened_file_count" => 1,
3465+
"closed_file_count" => 0,
3466+
"rotated_file_count" => 0,
3467+
"throttled_log_count" =>0,
3468+
"tracked_file_count" => 1,
3469+
},
3470+
d.instance.statistics["input"])
3471+
3472+
d.instance_shutdown
3473+
3474+
assert_equal({
3475+
"emit_records" => 0,
3476+
"emit_size" => 0,
3477+
"opened_file_count" => 1,
3478+
"closed_file_count" => 1,
3479+
"rotated_file_count" => 0,
3480+
"throttled_log_count" =>0,
3481+
"tracked_file_count" => 0,
3482+
},
3483+
d.instance.statistics["input"]) end
34463484
end

0 commit comments

Comments
 (0)