Class: Fluent::CombinerOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_combiner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCombinerOutput

Returns a new instance of CombinerOutput.



15
16
17
18
# File 'lib/fluent/plugin/out_combiner.rb', line 15

def initialize
  super
  require 'pathname'
end

Instance Attribute Details

#histObject

Returns the value of attribute hist.



12
13
14
# File 'lib/fluent/plugin/out_combiner.rb', line 12

def hist
  @hist
end

#last_checkedObject

Returns the value of attribute last_checked.



13
14
15
# File 'lib/fluent/plugin/out_combiner.rb', line 13

def last_checked
  @last_checked
end

#tickObject

Returns the value of attribute tick.



13
14
15
# File 'lib/fluent/plugin/out_combiner.rb', line 13

def tick
  @tick
end

Instance Method Details

#add_prefix(tag = "") ⇒ Object

Utils



112
113
114
115
# File 'lib/fluent/plugin/out_combiner.rb', line 112

def add_prefix(tag="")
  return @tag_prefix if tag.empty?
  return @tag_prefix_string + tag
end

#clearObject



107
108
109
# File 'lib/fluent/plugin/out_combiner.rb', line 107

def clear
  @hist = initialize_hist(@hist.keys.dup)
end

#configure(conf) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fluent/plugin/out_combiner.rb', line 20

def configure(conf)
  super

  @tick = @count_interval.to_i if @count_interval
  @tag_prefix_string = @tag_prefix ? @tag_prefix + '.' : @tag + '.'
  if @input_tag_remove_prefix
    @remove_prefix_string = @input_tag_remove_prefix + '.' 
    @remove_prefix_length = @remove_prefix_string.length
  end

  @hist = initialize_hist
end

#countup(tag, keys) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/fluent/plugin/out_combiner.rb', line 99

def countup(tag, keys)
  if keys.is_a?(Array) 
    keys.each {|k| increment(tag, k)}
  elsif keys.is_a?(String)
    increment(tag, keys)
  end
end

#emit(tag, es, chain) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/fluent/plugin/out_combiner.rb', line 63

def emit(tag, es, chain)

  es.each do |time, record|
    keys = record[@count_key]
    countup(tag, keys)
  end

  chain.next
end

#flushObject



45
46
47
48
# File 'lib/fluent/plugin/out_combiner.rb', line 45

def flush
  flushed, @hist = @hist, initialize_hist(@hist.keys.dup)
  generate_output(flushed)
end

#flush_emitObject



50
51
52
53
# File 'lib/fluent/plugin/out_combiner.rb', line 50

def flush_emit
  flushed = flush
  Fluent::Engine.emit(@tag,  Fluent::Engine.now,  flushed)
end

#generate_output(data) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/fluent/plugin/out_combiner.rb', line 55

def generate_output(data)
  output = {}
  data.each do |tag, hist|
    output[add_prefix(stripped_tag(tag))] = hist
  end
  output
end

#increment(tag, key) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fluent/plugin/out_combiner.rb', line 86

def increment(tag, key)
  @hist[tag] ||= {:hist => {}, :sum => 0, :len => 0}
  if @hist[tag][:hist].key? key
    @hist[tag][:hist][key] += 1
    @hist[tag][:sum] += 1
  else
    @hist[tag][:hist][key] = 1
    @hist[tag][:sum] += 1
    @hist[tag][:len] += 1
  end
  @hist
end

#initialize_hist(tags = nil) ⇒ Object

Combiner’s main methods



75
76
77
78
79
80
81
82
83
# File 'lib/fluent/plugin/out_combiner.rb', line 75

def initialize_hist(tags=nil)
  hist = {}
  if tags
    tags.each do |tag|
      hist[tag] = {:hist => {}, :sum => 0, :len => 0}
    end
  end
  hist
end

#shutdownObject



39
40
41
42
43
# File 'lib/fluent/plugin/out_combiner.rb', line 39

def shutdown
  super
  @watcher.terminate
  @watcher.join
end

#startObject

Fluent::Output main methods



34
35
36
37
# File 'lib/fluent/plugin/out_combiner.rb', line 34

def start
  super
  start_watch
end

#stripped_tag(tag) ⇒ Object



117
118
119
120
121
122
# File 'lib/fluent/plugin/out_combiner.rb', line 117

def stripped_tag(tag)
  return tag unless @input_tag_remove_prefix
  return tag[@remove_prefix_length..-1] if tag.start_with?(@remove_prefix_string) && tag.length > @remove_prefix_length
  return "" if tag == @input_tag_remove_prefix
  return tag
end