Skip to content

Commit

Permalink
Merge pull request #3008 from fluent/gc_stat-string-keys-record
Browse files Browse the repository at this point in the history
in_gc_stat: Add use_symbol_keys parameter
  • Loading branch information
repeatedly committed May 26, 2020
2 parents 5a2afdc + 05cf811 commit 190d06f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/fluent/plugin/in_gc_stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ class GCStatInput < Fluent::Plugin::Input

def initialize
super
@key_map = nil
end

config_param :emit_interval, :time, default: 60
config_param :use_symbol_keys, :bool, default: true
config_param :tag, :string

def configure(conf)
super

unless @use_symbol_keys
@key_map = {}
GC.stat.each_key { |key|
@key_map[key] = key.to_s
}
end
end

def multi_workers_ready?
Expand All @@ -50,6 +59,13 @@ def shutdown
def on_timer
now = Fluent::EventTime.now
record = GC.stat
unless @use_symbol_keys
new_record = {}
record.each_pair { |k, v|
new_record[@key_map[k]] = v
}
record = new_record
end
router.emit(@tag, now, record)
end
end
Expand Down
25 changes: 24 additions & 1 deletion test/plugin/test_in_gc_stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ def test_configure
assert_equal("t1", d.instance.tag)
end

def test_emit
def setup_gc_stat
stat = GC.stat
stub(GC).stat { stat }
stat
end

def test_emit
stat = setup_gc_stat

d = create_driver
d.run(expect_emits: 2)
Expand All @@ -36,4 +41,22 @@ def test_emit
assert(events[i][1].is_a?(Fluent::EventTime))
}
end

def test_emit_with_use_symbol_keys_false
stat = setup_gc_stat
result = {}
stat.each_pair { |k, v|
result[k.to_s] = v
}

d = create_driver(CONFIG + "use_symbol_keys false")
d.run(expect_emits: 2)

events = d.events
assert(events.length > 0)
events.each_index {|i|
assert_equal(result, events[i][2])
assert(events[i][1].is_a?(Fluent::EventTime))
}
end
end

0 comments on commit 190d06f

Please sign in to comment.