Class: Fluent::FlowCounterOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::FlowCounterOutput
- Includes:
- Mixin::ConfigPlaceholders
- Defined in:
- lib/fluent/plugin/out_flowcounter.rb
Instance Attribute Summary collapse
-
#count_all ⇒ Object
Returns the value of attribute count_all.
-
#counts ⇒ Object
Returns the value of attribute counts.
-
#last_checked ⇒ Object
Returns the value of attribute last_checked.
-
#tick ⇒ Object
readonly
Returns the value of attribute tick.
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #count_initialized(keys = nil) ⇒ Object
- #countup(name, counts, bytes) ⇒ Object
- #emit(tag, es, chain) ⇒ Object
- #flush(step) ⇒ Object
- #flush_emit(step) ⇒ Object
- #generate_output(counts, step) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #start_watch ⇒ Object
- #tagged_flush(step) ⇒ Object
- #watch ⇒ Object
Instance Attribute Details
#count_all ⇒ Object
Returns the value of attribute count_all.
22 23 24 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 22 def count_all @count_all end |
#counts ⇒ Object
Returns the value of attribute counts.
20 21 22 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 20 def counts @counts end |
#last_checked ⇒ Object
Returns the value of attribute last_checked.
21 22 23 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 21 def last_checked @last_checked end |
#tick ⇒ Object (readonly)
Returns the value of attribute tick.
23 24 25 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 23 def tick @tick end |
Instance Method Details
#configure(conf) ⇒ Object
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 60 61 62 63 64 65 66 67 68 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 25 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 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" end @output_style = case @output_style when 'joined' then :joined when 'tagged' then :tagged else raise Fluent::ConfigError, "flowcounter output_style allows joined/tagged" end if @output_style == :tagged and @aggregate != :tag raise Fluent::ConfigError, "flowcounter aggregate must be 'tag' when output_style is 'tagged'" end if @input_tag_remove_prefix @removed_prefix_string = @input_tag_remove_prefix + '.' @removed_length = @removed_prefix_string.length end @count_keys = @count_keys.split(',') @count_all = (@count_keys == ['*']) @counts = count_initialized @mutex = Mutex.new end |
#count_initialized(keys = nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 81 def count_initialized(keys=nil) if @aggregate == :all {'count' => 0, 'bytes' => 0} elsif keys values = Array.new(keys.length){|i| 0 } Hash[[keys, values].transpose] else {} end end |
#countup(name, counts, bytes) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 92 def countup(name, counts, bytes) c = 'count' b = 'bytes' if @aggregate == :tag c = name + '_count' b = name + '_bytes' end @mutex.synchronize { @counts[c] = (@counts[c] || 0) + counts @counts[b] = (@counts[b] || 0) + bytes } end |
#emit(tag, es, chain) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 160 def emit(tag, es, chain) name = tag if @input_tag_remove_prefix and ( (tag.start_with?(@removed_prefix_string) and tag.length > @removed_length) or tag == @input_tag_remove_prefix) name = tag[@removed_length..-1] end c,b = 0,0 if @count_all es.each {|time,record| c += 1 b += record.to_msgpack.bytesize } else es.each {|time,record| c += 1 b += @count_keys.inject(0){|s,k| s + record[k].bytesize} } end countup(name, c, b) chain.next end |
#flush(step) ⇒ Object
113 114 115 116 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 113 def flush(step) flushed,@counts = @counts,count_initialized(@counts.keys) generate_output(flushed, step) end |
#flush_emit(step) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 132 def flush_emit(step) if @output_style == :tagged tagged_flush(step).each do |data| Fluent::Engine.emit(@tag, Fluent::Engine.now, data) end else Fluent::Engine.emit(@tag, Fluent::Engine.now, flush(step)) end end |
#generate_output(counts, step) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 105 def generate_output(counts, step) rates = {} counts.keys.each {|key| rates[key + '_rate'] = ((counts[key] * 100.0) / (1.00 * step)).floor / 100.0 } counts.update(rates) end |
#shutdown ⇒ Object
75 76 77 78 79 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 75 def shutdown super @watcher.terminate @watcher.join end |
#start ⇒ Object
70 71 72 73 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 70 def start super start_watch end |
#start_watch ⇒ Object
142 143 144 145 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 142 def start_watch # for internal, or tests only @watcher = Thread.new(&method(:watch)) end |
#tagged_flush(step) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 118 def tagged_flush(step) flushed,@counts = @counts,count_initialized(@counts.keys) names = flushed.keys.select {|x| x.end_with?('_count')}.map {|x| x.chomp('_count')} names.map {|name| counts = { 'count' => flushed[name + '_count'], 'bytes' => flushed[name + '_bytes'], } data = generate_output(counts, step) data['tag'] = name data } end |
#watch ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/fluent/plugin/out_flowcounter.rb', line 147 def watch # instance variable, and public accessable, for test @last_checked = Fluent::Engine.now while true sleep 0.5 if Fluent::Engine.now - @last_checked >= @tick now = Fluent::Engine.now flush_emit(now - @last_checked) @last_checked = now end end end |