Skip to content

Commit efed7be

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

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/fluent/plugin/in_tail.rb

Lines changed: 8 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
@@ -474,6 +478,7 @@ def refresh_watchers
474478

475479
stop_watchers(removed_hash, unwatched: need_unwatch_in_stop_watchers) unless removed_hash.empty?
476480
start_watchers(added_hash) unless added_hash.empty?
481+
@metrics.tracked.set(@tails.size)
477482
@startup = false if @startup
478483
end
479484

@@ -814,6 +819,7 @@ def statistics
814819
'closed_file_count' => @metrics.closed.get,
815820
'rotated_file_count' => @metrics.rotated.get,
816821
'throttled_log_count' => @metrics.throttled.get,
822+
'tracked_file_count' => @metrics.tracked.get,
817823
})
818824
}
819825
stats

test/plugin/test_in_tail.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,4 +3443,32 @@ 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+
end
34463474
end

0 commit comments

Comments
 (0)