Module: Fluent::FlowcounterSimple

Included in:
Plugin::FlowCounterSimpleFilter, Plugin::FlowCounterSimpleOutput
Defined in:
lib/fluent/plugin/flowcounter_simple.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_checkedObject

Returns the value of attribute last_checked.



5
6
7
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 5

def last_checked
  @last_checked
end

Class Method Details

.included(klass) ⇒ Object



7
8
9
10
11
12
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 7

def self.included(klass)
  klass.helpers :thread
  klass.config_param :indicator, :string, :default => 'num'
  klass.config_param :unit, :string, :default => 'second'
  klass.config_param :comment, :string, :default => nil
end

Instance Method Details

#configure(conf) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 14

def configure(conf)
  super

  @indicator_proc =
    case @indicator
    when 'num'  then Proc.new { |es| es.size }
    when 'byte' then Proc.new { |es|
                       count = 0
                       es.each { |time, record|
                         count += record.to_msgpack.size
                       }
                       count
                     }
    else
      raise Fluent::ConfigError, "flowcounter-simple count allows num/byte"
    end
  @unit =
    case @unit
    when 'second' then :second
    when 'minute' then :minute
    when 'hour' then :hour
    when 'day' then :day
    else
      raise Fluent::ConfigError, "flowcounter-simple 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, "@unit must be one of second/minute/hour/day"
    end

  @type_str = self.is_a?(Fluent::Plugin::Filter) ? 'filter' : 'out'
  @output_proc =
    if @comment
      Proc.new { |count| "plugin:#{@type_str}_flowcounter_simple\tcount:#{count}\tindicator:#{@indicator}\tunit:#{@unit}\tcomment:#{@comment}" }
    else
      Proc.new { |count| "plugin:#{@type_str}_flowcounter_simple\tcount:#{count}\tindicator:#{@indicator}\tunit:#{@unit}" }
    end

  @count = 0
  @mutex = Mutex.new
end

#countup(count) ⇒ Object



70
71
72
73
74
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 70

def countup(count)
  @mutex.synchronize {
    @count = (@count || 0) + count
  }
end

#flush_emit(step) ⇒ Object



76
77
78
79
80
81
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 76

def flush_emit(step)
  count, @count = @count, 0
  if count > 0
    log.info @output_proc.call(count)
  end
end

#process_count(tag, es) ⇒ Object



96
97
98
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 96

def process_count(tag, es)
  countup(@indicator_proc.call(es))
end

#shutdownObject



66
67
68
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 66

def shutdown
  super
end

#startObject



61
62
63
64
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 61

def start
  super
  thread_create(:flowcounter_simple_watch, &method(:watch))
end

#watchObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fluent/plugin/flowcounter_simple.rb', line 83

def watch
  # instance variable, and public accessable, for test
  @last_checked = Fluent::EventTime.now
  while thread_current_running?
    sleep 0.1
    if Fluent::EventTime.now - @last_checked >= @tick
      now = Fluent::EventTime.now
      flush_emit(now - @last_checked)
      @last_checked = now
    end
  end
end