From 4eff660e606e3f8635007d4563bfdf5541107aa1 Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Wed, 10 Jul 2019 20:28:38 +0900 Subject: [PATCH 1/2] Use same object in Buffer#statistics Signed-off-by: Masahiro Nakagawa --- lib/fluent/plugin/buffer.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/fluent/plugin/buffer.rb b/lib/fluent/plugin/buffer.rb index 12b5db6e46..262a67c4dd 100644 --- a/lib/fluent/plugin/buffer.rb +++ b/lib/fluent/plugin/buffer.rb @@ -742,14 +742,15 @@ def write_step_by_step(metadata, data, format, splits_count, &block) end def statistics - buffer_space = 1.0 - ((@stage_size + @queue_size * 1.0) / @total_limit_size).round + stage_size, queue_size = @stage_size, @queue_size + buffer_space = 1.0 - ((stage_size + queue_size * 1.0) / @total_limit_size).round stats = { 'stage_length' => @stage.size, - 'stage_byte_size' => @stage_size, + 'stage_byte_size' => stage_size, 'queue_length' => @queue.size, - 'queue_byte_size' => @queue_size, + 'queue_byte_size' => queue_size, 'available_buffer_space_ratios' => buffer_space * 100, - 'total_queued_size' => @stage_size + @queue_size, + 'total_queued_size' => stage_size + queue_size, } if (m = timekeys.min) From e252ba45179450f7faaf04ea1e07711321801c6f Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Wed, 10 Jul 2019 20:40:38 +0900 Subject: [PATCH 2/2] Avoid memory allocation for static value in Output#statistics Signed-off-by: Masahiro Nakagawa --- lib/fluent/plugin/buffer.rb | 11 +++++++++++ lib/fluent/plugin/output.rb | 8 +++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/fluent/plugin/buffer.rb b/lib/fluent/plugin/buffer.rb index 262a67c4dd..fbf3559436 100644 --- a/lib/fluent/plugin/buffer.rb +++ b/lib/fluent/plugin/buffer.rb @@ -741,6 +741,17 @@ def write_step_by_step(metadata, data, format, splits_count, &block) retry end + STATS_KEYS = [ + 'stage_length', + 'stage_byte_size', + 'queue_length', + 'queue_byte_size', + 'available_buffer_space_ratios', + 'total_queued_size', + 'oldest_timekey', + 'newest_timekey' + ] + def statistics stage_size, queue_size = @stage_size, @queue_size buffer_space = 1.0 - ((stage_size + queue_size * 1.0) / @total_limit_size).round diff --git a/lib/fluent/plugin/output.rb b/lib/fluent/plugin/output.rb index a919cf8cb8..be44e3990b 100644 --- a/lib/fluent/plugin/output.rb +++ b/lib/fluent/plugin/output.rb @@ -16,6 +16,7 @@ require 'fluent/error' require 'fluent/plugin/base' +require 'fluent/plugin/buffer' require 'fluent/plugin_helper/record_accessor' require 'fluent/log' require 'fluent/plugin_id' @@ -1466,6 +1467,11 @@ def flush_thread_run(state) end end + BUFFER_STATS_KEYS = {} + Fluent::Plugin::Buffer::STATS_KEYS.each { |key| + BUFFER_STATS_KEYS[key] = "buffer_#{key}" + } + def statistics stats = { 'emit_records' => @emit_records, @@ -1481,7 +1487,7 @@ def statistics if @buffer && @buffer.respond_to?(:statistics) (@buffer.statistics['buffer'] || {}).each do |k, v| - stats["buffer_#{k}"] = v + stats[BUFFER_STATS_KEYS[k]] = v end end