Class: Fluent::ElapsedTimeOutput

Inherits:
MultiOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_elapsed_time.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeElapsedTimeOutput

Returns a new instance of ElapsedTimeOutput.



33
34
35
36
37
38
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 33

def initialize
  super
  @outputs = []
  @elapsed = {}
  @emit_procs = []
end

Instance Attribute Details

#outputsObject (readonly)

for test



41
42
43
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 41

def outputs
  @outputs
end

Instance Method Details

#configure(conf) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 47

def configure(conf)
  super
  conf.elements.select {|e|
    e.name == 'store'
  }.each {|e|
    type = e['type']
    unless type
      raise ConfigError, "Missing 'type' parameter on <store> directive"
    end
    log.debug "adding store type=#{type.dump}"

    output = Plugin.new_output(type)
    output.configure(e)
    emit_proc = if output.respond_to?(:emit_events)
                  Proc.new {|output, tag, es, _chain| output.emit_events(tag, es)}
                else
                  Proc.new {|output, tag, es, _chain| output.emit(tag, es, NullOutputChain.instance)}
                end
    @emit_procs << emit_proc
    @outputs << output
  }

  case @aggregate
  when 'all'
    raise ConfigError, "out_elapsed_time: `tag` must be specified with aggregate all" if @tag.nil?
  when 'tag'
    raise ConfigError, "out_elapsed_time: `add_tag_prefix` or `remove_tag_prefix` must be specified with aggregate tag" if @add_tag_prefix.nil? and @remove_tag_prefix.nil?
  else
    raise ConfigError, "out_elapsed_time: aggregate allows `tag` or `all`"
  end

  @tag_proc = tag_proc

  @emit_proc =
    if @each == :message
      self.method(:emit_message)
    else
      self.method(:emit_es)
    end
end

#elapsed(tag = "elapsed") ⇒ Object

default: @tag



43
44
45
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 43

def elapsed(tag = "elapsed") # default: @tag
  @elapsed[tag] ||= []
end

#emit(tag, es, chain) ⇒ Object



88
89
90
91
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 88

def emit(tag, es, chain)
  @emit_proc.call(tag, es)
  chain.next
end

#emit_es(tag, es) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 107

def emit_es(tag, es)
  chain = NullOutputChain.instance
  t = Time.now
  @outputs.each_with_index {|output, idx|
    @emit_procs[idx].call(output, tag, es,chain)
  }
  emit_tag = @tag_proc.call(tag)
  elapsed(emit_tag) << (Time.now - t).to_f
end

#emit_message(tag, es) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 93

def emit_message(tag, es)
  chain = NullOutputChain.instance
  start = Time.now
  es.each do |time, record|
    @outputs.each_with_index {|output, idx|
      @emit_procs[idx].call(output, tag, OneEventStream.new(time, record), chain)
    }
    finish = Time.now
    emit_tag = @tag_proc.call(tag)
    elapsed(emit_tag) << (finish - start).to_f
    start = finish
  end
end

#flush_emitObject



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 153

def flush_emit
  flushed_elapsed, @elapsed = @elapsed, initial_elapsed(@elapsed)
  messages = {}
  flushed_elapsed.each do |tag, elapsed|
    num = elapsed.size
    max = num == 0 ? 0 : elapsed.max
    avg = num == 0 ? 0 : elapsed.map(&:to_f).inject(:+) / num.to_f
    messages[tag] = {"max" => max, "avg" => avg, "num" => num}
  end
  messages.each {|tag, message| router.emit(tag, Engine.now, message) }
end

#initial_elapsed(prev_elapsed = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 117

def initial_elapsed(prev_elapsed = nil)
  return {} if !@zero_emit or prev_elapsed.nil?
  elapsed = {}
  prev_elapsed.keys.each do |tag|
    next if prev_elapsed[tag].empty? # Prohibit to emit anymore
    elapsed[tag] = []
  end
  elapsed
end

#runObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 142

def run
  @last_checked ||= Engine.now
  while (sleep 0.1)
    now = Engine.now
    if now - @last_checked >= @interval
      flush_emit
      @last_checked = now
    end
  end
end

#shutdownObject



134
135
136
137
138
139
140
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 134

def shutdown
  @outputs.each {|o|
    o.shutdown
  }
  @thread.terminate
  @thread.join
end

#startObject



127
128
129
130
131
132
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 127

def start
  @outputs.each {|o|
    o.start
  }
  @thread = Thread.new(&method(:run))
end