Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_gc_stat: Add use_symbol_keys parameter #3008

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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