Class: Fluent::LatencyOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLatencyOutput

Returns a new instance of LatencyOutput.



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

def initialize
  super
  @latency = []
end

Instance Attribute Details

#latencyObject (readonly)

for test



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

def latency
  @latency
end

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
# File 'lib/fluent/plugin/out_latency.rb', line 20

def configure(conf)
  super
end

#emit(tag, es, chain) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/fluent/plugin/out_latency.rb', line 24

def emit(tag, es, chain)
  current = Time.now.to_f
  es.each do |time, record|
    @latency << (current - time)
  end
  chain.next
end

#flush_emitObject



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

def flush_emit
  latency, @latency = @latency, []
  num = latency.size
  max = num == 0 ? 0 : latency.max
  avg = num == 0 ? 0 : latency.map(&:to_f).inject(:+) / num.to_f
  message = {"max" => max, "avg" => avg, "num" => num}
  Engine.emit(@tag, Engine.now, message)
end

#runObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/fluent/plugin/out_latency.rb', line 43

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

#shutdownObject



37
38
39
40
41
# File 'lib/fluent/plugin/out_latency.rb', line 37

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

#startObject



32
33
34
35
# File 'lib/fluent/plugin/out_latency.rb', line 32

def start
  super
  @thread = Thread.new(&method(:run))
end