Skip to content

Commit

Permalink
Merge pull request #1060 from okkez/migrate-v0.14-api-in_gc_stat
Browse files Browse the repository at this point in the history
Migrate in_gc_stat to v0.14 API
  • Loading branch information
tagomoris authored Jun 24, 2016
2 parents e6f1eb1 + 6908ce6 commit 0afbbc4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 43 deletions.
44 changes: 7 additions & 37 deletions lib/fluent/plugin/in_gc_stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
# limitations under the License.
#

require 'cool.io'
require 'fluent/plugin/input'

require 'fluent/input'
module Fluent::Plugin
class GCStatInput < Fluent::Plugin::Input
Fluent::Plugin.register_input('gc_stat', self)

module Fluent
class GCStatInput < Input
Plugin.register_input('gc_stat', self)
helpers :timer

def initialize
super
Expand All @@ -29,52 +29,22 @@ def initialize
config_param :emit_interval, :time, default: 60
config_param :tag, :string

class TimerWatcher < Coolio::TimerWatcher
def initialize(interval, repeat, log, &callback)
@callback = callback
@log = log
super(interval, repeat)
end

def on_timer
@callback.call
rescue
# TODO log?
@log.error $!.to_s
@log.error_backtrace
end
end

def configure(conf)
super
end

def start
super

@loop = Coolio::Loop.new
@timer = TimerWatcher.new(@emit_interval, true, log, &method(:on_timer))
@loop.attach(@timer)
@thread = Thread.new(&method(:run))
timer_execute(:in_gc_stat, @emit_interval, &method(:on_timer))
end

def shutdown
@loop.watchers.each {|w| w.detach }
@loop.stop
@thread.join

super
end

def run
@loop.run
rescue
log.error "unexpected error", error: $!.to_s
log.error_backtrace
end

def on_timer
now = Engine.now
now = Fluent::EventTime.now
record = GC.stat
router.emit(@tag, now, record)
end
Expand Down
12 changes: 6 additions & 6 deletions test/plugin/test_in_gc_stat.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative '../helper'
require 'fluent/test'
require 'fluent/test/driver/input'
require 'fluent/plugin/in_gc_stat'

class GCStatInputTest < Test::Unit::TestCase
Expand All @@ -13,7 +13,7 @@ def setup
]

def create_driver(conf=CONFIG)
Fluent::Test::InputTestDriver.new(Fluent::GCStatInput).configure(conf)
Fluent::Test::Driver::Input.new(Fluent::Plugin::GCStatInput).configure(conf)
end

def test_configure
Expand All @@ -31,9 +31,9 @@ def test_emit
sleep 2
end

emits = d.emits
assert(emits.length > 0)
assert_equal(stat, emits[0][2])
assert(emits[0][1].is_a?(Fluent::EventTime))
events = d.events
assert(events.length > 0)
assert_equal(stat, events[0][2])
assert(events[0][1].is_a?(Fluent::EventTime))
end
end

0 comments on commit 0afbbc4

Please sign in to comment.