lib/fluent/plugin/out_grepcounter.rb in fluent-plugin-grepcounter-0.1.2 vs lib/fluent/plugin/out_grepcounter.rb in fluent-plugin-grepcounter-0.1.3
- old
+ new
@@ -5,10 +5,11 @@
config_param :input_key, :string
config_param :regexp, :string, :default => nil
config_param :count_interval, :time, :default => 5
config_param :exclude, :string, :default => nil
config_param :threshold, :integer, :default => 1
+ config_param :comparison, :string, :default => '>='
config_param :output_tag, :string, :default => nil
config_param :add_tag_prefix, :string, :default => 'count'
config_param :output_with_joined_delimiter, :string, :default => nil
config_param :aggregate, :string, :default => 'tag'
config_param :replace_invalid_sequence, :bool, :default => false
@@ -23,19 +24,23 @@
@input_key = @input_key.to_s
@regexp = Regexp.compile(@regexp) if @regexp
@exclude = Regexp.compile(@exclude) if @exclude
@threshold = @threshold.to_i
+ unless ['>=', '<='].include?(@comparison)
+ raise Fluent::ConfigError, "grepcounter: comparison allows >=, <="
+ end
+
unless ['tag', 'all'].include?(@aggregate)
- raise Fluent::ConfigError, "grepcounter aggregate allows tag/all"
+ raise Fluent::ConfigError, "grepcounter: aggregate allows tag/all"
end
case @aggregate
when 'all'
- raise Fluent::ConfigError, "output_tag must be specified with aggregate all" if @output_tag.nil?
+ raise Fluent::ConfigError, "grepcounter: output_tag must be specified with aggregate all" if @output_tag.nil?
when 'tag'
- # raise Fluent::ConfigError, "add_tag_prefix must be specified with aggregate tag" if @add_tag_prefix.nil?
+ # raise Fluent::ConfigError, "grepcounter: add_tag_prefix must be specified with aggregate tag" if @add_tag_prefix.nil?
end
@matches = {}
@counts = {}
@mutex = Mutex.new
@@ -70,12 +75,11 @@
@matches[tag] += matches
end
chain.next
rescue => e
- $log.warn e.message
- $log.warn e.backtrace.join(', ')
+ $log.warn "grepcounter: #{e.class} #{e.message} #{e.backtrace.first}"
end
# thread callback
def watcher
# instance variable, and public accessable, for test
@@ -87,12 +91,11 @@
now = Fluent::Engine.now
flush_emit(now - @last_checked)
@last_checked = now
end
rescue => e
- $log.warn e.message
- $log.warn e.backtrace.join(", ")
+ $log.warn "grepcounter: #{e.class} #{e.message} #{e.backtrace.first}"
end
end
end
# This method is the real one to emit
@@ -119,10 +122,10 @@
end
end
def generate_output(count, matches, tag = nil)
return nil if count.nil?
- return nil if count < @threshold
+ return nil unless eval("#{count} #{@comparison} #{@threshold}")
output = {}
output['count'] = count
output['message'] = @output_with_joined_delimiter.nil? ? matches : matches.join(@output_with_joined_delimiter)
if tag
output['input_tag'] = tag