lib/fluent/plugin/out_flowcounter.rb in fluent-plugin-flowcounter-0.1.9 vs lib/fluent/plugin/out_flowcounter.rb in fluent-plugin-flowcounter-0.2.0

- old
+ new

@@ -1,10 +1,15 @@ require 'fluent/mixin/config_placeholders' class Fluent::FlowCounterOutput < Fluent::Output Fluent::Plugin.register_output('flowcounter', self) + # Define `log` method for v0.10.42 or earlier + unless method_defined?(:log) + define_method("log") { $log } + end + config_param :unit, :string, :default => 'minute' config_param :aggregate, :string, :default => 'tag' config_param :output_style, :string, :default => 'joined' config_param :tag, :string, :default => 'flowcount' config_param :input_tag_remove_prefix, :string, :default => nil @@ -13,21 +18,31 @@ include Fluent::Mixin::ConfigPlaceholders attr_accessor :counts attr_accessor :last_checked attr_accessor :count_all + attr_reader :tick def configure(conf) super @unit = case @unit + when 'second' then :second when 'minute' then :minute when 'hour' then :hour when 'day' then :day else - raise Fluent::ConfigError, "flowcounter unit allows minute/hour/day" + raise Fluent::ConfigError, "flowcounter unit allows second/minute/hour/day" end + @tick = case @unit + when :second then 1 + when :minute then 60 + when :hour then 3600 + when :day then 86400 + else + raise Fluent::ConfigError, "flowcounter unit allows second/minute/hour/day" + end @aggregate = case @aggregate when 'tag' then :tag when 'all' then :all else raise Fluent::ConfigError, "flowcounter aggregate allows tag/all" @@ -130,19 +145,12 @@ end def watch # instance variable, and public accessable, for test @last_checked = Fluent::Engine.now - tick = case @unit - when :minute then 60 - when :hour then 3600 - when :day then 86400 - else - raise RuntimeError, "@unit must be one of minute/hour/day" - end while true sleep 0.5 - if Fluent::Engine.now - @last_checked >= tick + if Fluent::Engine.now - @last_checked >= @tick now = Fluent::Engine.now flush_emit(now - @last_checked) @last_checked = now end end