Skip to content

Commit

Permalink
Avoid memory allocation for static value in Output#statistics
Browse files Browse the repository at this point in the history
Signed-off-by: Masahiro Nakagawa <repeatedly@gmail.com>
  • Loading branch information
repeatedly committed Jul 10, 2019
1 parent 4eff660 commit e252ba4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/fluent/plugin/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/fluent/plugin/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down

0 comments on commit e252ba4

Please sign in to comment.