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.



23
24
25
26
27
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 23

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

Instance Attribute Details

#outputsObject (readonly)

for test



30
31
32
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 30

def outputs
  @outputs
end

Instance Method Details

#configure(conf) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 36

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)
    @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_slice_proc =
    if @remove_tag_slice
      lindex, rindex = @remove_tag_slice.split('..', 2)
      if lindex.nil? or rindex.nil? or lindex !~ /^-?\d+$/ or rindex !~ /^-?\d+$/
        raise Fluent::ConfigError, "out_elapsed_time: remove_tag_slice must be formatted like [num]..[num]"
      end
      l, r = lindex.to_i, rindex.to_i
      Proc.new {|tag| (tags = tag.split('.')[l..r]).nil? ? "" : tags.join('.') }
    else
      Proc.new {|tag| tag }
    end

  @tag_prefix = "#{@add_tag_prefix}." if @add_tag_prefix
  @tag_prefix_match = "#{@remove_tag_prefix}." if @remove_tag_prefix
  @tag_proc =
    if @tag_prefix and @tag_prefix_match
      Proc.new {|tag| "#{@tag_prefix}#{lstrip(@tag_slice_proc.call(tag), @tag_prefix_match)}" }
    elsif @tag_prefix_match
      Proc.new {|tag| lstrip(@tag_slice_proc.call(tag), @tag_prefix_match) }
    elsif @tag_prefix
      Proc.new {|tag| "#{@tag_prefix}#{@tag_slice_proc.call(tag)}" }
    elsif @tag
      Proc.new {|tag| @tag }
    else
      Proc.new {|tag| @tag_slice_proc.call(tag) }
    end

  @emit_proc =
    if @each == :message
      chain = NullOutputChain.instance
      Proc.new {|tag, es|
        start = Time.now
        es.each do |time, record|
          @outputs.each {|output| output.emit(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
      }
    else
      chain = NullOutputChain.instance
      Proc.new {|tag, es|
        t = Time.now
        @outputs.each {|output| output.emit(tag, es, chain) }
        emit_tag = @tag_proc.call(tag)
        elapsed(emit_tag) << (Time.now - t).to_f
      }
    end
end

#elapsed(tag = "elapsed") ⇒ Object

default: @tag



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

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

#emit(tag, es, chain) ⇒ Object



160
161
162
163
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 160

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

#flush_emitObject



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 148

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

#initial_elapsed(prev_elapsed = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 112

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

#lstrip(string, substring) ⇒ Object



165
166
167
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 165

def lstrip(string, substring)
  string.index(substring) == 0 ? string[substring.size..-1] : string
end

#runObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 137

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



129
130
131
132
133
134
135
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 129

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

#startObject



122
123
124
125
126
127
# File 'lib/fluent/plugin/out_elapsed_time.rb', line 122

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