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.



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

def initialize
  super
  @latency = []
end

Instance Attribute Details

#latencyObject (readonly)

for test



18
19
20
# File 'lib/fluent/plugin/out_latency.rb', line 18

def latency
  @latency
end

Instance Method Details

#configure(conf) ⇒ Object



25
26
27
# File 'lib/fluent/plugin/out_latency.rb', line 25

def configure(conf)
  super
end

#emit(tag, es, chain) ⇒ Object



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

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

#flush_emitObject



59
60
61
62
63
64
65
66
# File 'lib/fluent/plugin/out_latency.rb', line 59

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}
  router.emit(@tag, Engine.now, message)
end

#runObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/plugin/out_latency.rb', line 48

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



42
43
44
45
46
# File 'lib/fluent/plugin/out_latency.rb', line 42

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

#startObject



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

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