Class: Fluent::StatsNotifierOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatsNotifierOutput

Returns a new instance of StatsNotifierOutput.



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

def initialize
  super
  require 'pathname'
end

Instance Attribute Details

#countsObject

Returns the value of attribute counts.



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

def counts
  @counts
end

#last_checkedObject

Returns the value of attribute last_checked.



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

def last_checked
  @last_checked
end

#queuesObject

Returns the value of attribute queues.



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

def queues
  @queues
end

#saved_atObject

Returns the value of attribute saved_at.



40
41
42
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 40

def saved_at
  @saved_at
end

#saved_durationObject

Returns the value of attribute saved_duration.



39
40
41
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 39

def saved_duration
  @saved_duration
end

Instance Method Details

#configure(conf) ⇒ Object



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
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 43

def configure(conf)
  super

  @interval = @interval.to_i

  if @less_than and @less_equal
    raise Fluent::ConfigError, "out_stats_notifier: Only either of `less_than` or `less_equal` can be specified."
  end
  if @greater_than and @greater_equal
    raise Fluent::ConfigError, "out_stats_notifier: Only either of `greater_than` or `greater_equal` can be specified."
  end

  @aggregate_stats = @compare_with if @compare_with # Support old version compatibility
  case @aggregate_stats
  when "sum"
    @aggregate_stats = :sum
  when "max"
    @aggregate_stats = :max
  when "min"
    @aggregate_stats = :min
  when "avg"
    @aggregate_stats = :avg
  else
    raise Fluent::ConfigError, "out_stats_notifier: `aggregate_stats` must be one of `sum`, `max`, `min`, `avg`"
  end

  case @stats
  when "sum"
    @stats = :sum
  when "max"
    @stats = :max
  when "min"
    @stats = :min
  when "avg"
    @stats = :avg
  else
    raise Fluent::ConfigError, "out_stats_notifier: `stats` must be one of `sum`, `max`, `min`, `avg`"
  end

  if @tag.nil? and @add_tag_prefix.nil? and @remove_tag_prefix.nil? and @add_tag_suffix.nil? and @remove_tag_suffix.nil?
    raise Fluent::ConfigError, "out_stats_notifier: No tag option is specified"
  end
  @tag_proc = tag_proc

  case @aggregate
  when 'all'
    raise Fluent::ConfigError, "out_stats_notifier: `tag` must be specified with aggregate all" if @tag.nil?
    @aggregate = :all
  when 'tag'
    # raise Fluent::ConfigError, "out_stats_notifier: `add_tag_prefix` must be specified with aggregate tag" if @add_tag_prefix.nil?
    @aggregate = :tag
  else
    raise Fluent::ConfigError, "out_stats_notifier: aggregate allows tag/all"
  end

  @counts = {}
  @queues = {}
  @mutex = Mutex.new
end

#emit(tag, es, chain) ⇒ Object

Called when new line comes. This method actually does not emit



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 117

def emit(tag, es, chain)
  key = @target_key

  # enqueus
  count = 0; queues = {}
  es.each do |time,record|
    if record[key]
      queues[key] ||= []
      queues[key] << record[key]
    end
    count += 1
  end

  # thread safe merge
  @counts[tag] ||= 0
  @queues[tag] ||= {}
  @mutex.synchronize do
    if queues[key]
      @queues[tag][key] ||= []
      @queues[tag][key].concat(queues[key])
    end
    @counts[tag] += count
  end

  chain.next
rescue => e
  log.warn "#{e.class} #{e.message} #{e.backtrace.first}"
end

#flush_emit(step) ⇒ Object

This method is the real one to emit



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 167

def flush_emit(step)
  time = Fluent::Engine.now
  counts, queues, @counts, @queues = @counts, @queues, {}, {}

  # Get statistical value among events
  evented_queues = {}
  queues.each do |tag, queue|
    evented_queues[tag] ||= {}
    evented_queues[tag][@target_key] = get_stats(queue[@target_key], @stats) if queue[@target_key]
  end

  if @aggregate == :all
    values = evented_queues.values.map {|queue| queue[@target_key] }.compact
    value = get_stats(values, @aggregate_stats)
    output = generate_output(value) if value
    router.emit(@tag, time, output) if output
  else # aggregate tag
    evented_queues.each do |tag, queue|
      value = queue[@target_key]
      output = generate_output(value) if value
      emit_tag = @tag_proc.call(tag)
      router.emit(emit_tag, time, output) if output
    end
  end
end

#generate_output(value) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 206

def generate_output(value)
  return nil if value == 0 # ignore 0 because standby nodes receive 0 message usually
  return nil if @less_than     and @less_than   <= value
  return nil if @less_equal    and @less_equal  <  value
  return nil if @greater_than  and value <= @greater_than
  return nil if @greater_equal and value <  @greater_equal

  output = {}
  output[@target_key] = value
  output
end

#get_stats(values, method = :max) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 193

def get_stats(values, method = :max)
  case method
  when :sum
    stats = values.inject(:+)
  when :max
    stats = values.max
  when :min
    stats = values.min
  when :avg
    stats = values.inject(:+) / values.count unless values.empty?
  end
end

#load_status(file_path, interval) ⇒ Object

Load internal status from a file

Parameters:

  • file_path (String)
  • interval (Interger)


266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 266

def load_status(file_path, interval)
  return unless (f = Pathname.new(file_path)).exist?

  begin
    f.open('rb') do |f|
      stored = Marshal.load(f)
      if stored[:target_key] == @target_key
        if stored[:queues]
          if Fluent::Engine.now <= stored[:saved_at] + interval
            @counts = stored[:counts]
            @queues = stored[:queues]
            @saved_at = stored[:saved_at]
            @saved_duration = stored[:saved_duration]

            # skip the saved duration to continue counting
            @last_checked = Fluent::Engine.now - @saved_duration
          else
            log.warn "out_stats_notifier: stored data is outdated. ignore stored data"
          end
        else
          log.warn "out_stats_notifier: stored data is incompatible. ignore stored data"
        end
      else
        log.warn "out_stats_notifier: configuration param was changed. ignore stored data"
      end
    end
  rescue => e
    log.warn "out_stats_notifier: Can't load store_file #{e.class} #{e.message}"
  end
end

#save_status(file_path) ⇒ Object

Store internal status into a file

Parameters:

  • file_path (String)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 242

def save_status(file_path)
  return unless file_path

  begin
    Pathname.new(file_path).open('wb') do |f|
      @saved_at = Fluent::Engine.now
      @saved_duration = @saved_at - @last_checked
      Marshal.dump({
        :counts           => @counts,
        :queues          => @queues,
        :saved_at         => @saved_at,
        :saved_duration   => @saved_duration,
        :target_key       => @target_key,
      }, f)
    end
  rescue => e
    log.warn "out_stats_notifier: Can't write store_file #{e.class} #{e.message}"
  end
end

#shutdownObject



109
110
111
112
113
114
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 109

def shutdown
  super
  @watcher.terminate
  @watcher.join
  save_status(@store_file) if @store_file
end

#startObject



103
104
105
106
107
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 103

def start
  super
  load_status(@store_file, @interval) if @store_file
  @watcher = Thread.new(&method(:watcher))
end

#tag_procObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 218

def tag_proc
  rstrip = Proc.new {|str, substr| str.chomp(substr) }
  lstrip = Proc.new {|str, substr| str.start_with?(substr) ? str[substr.size..-1] : str }
  tag_prefix = "#{rstrip.call(@add_tag_prefix, '.')}." if @add_tag_prefix
  tag_suffix = ".#{lstrip.call(@add_tag_suffix, '.')}" if @add_tag_suffix
  tag_prefix_match = "#{rstrip.call(@remove_tag_prefix, '.')}." if @remove_tag_prefix
  tag_suffix_match = ".#{lstrip.call(@remove_tag_suffix, '.')}" if @remove_tag_suffix
  tag_fixed = @tag if @tag
  if tag_fixed
    Proc.new {|tag| tag_fixed }
  elsif tag_prefix_match and tag_suffix_match
    Proc.new {|tag| "#{tag_prefix}#{rstrip.call(lstrip.call(tag, tag_prefix_match), tag_suffix_match)}#{tag_suffix}" }
  elsif tag_prefix_match
    Proc.new {|tag| "#{tag_prefix}#{lstrip.call(tag, tag_prefix_match)}#{tag_suffix}" }
  elsif tag_suffix_match
    Proc.new {|tag| "#{tag_prefix}#{rstrip.call(tag, tag_suffix_match)}#{tag_suffix}" }
  else
    Proc.new {|tag| "#{tag_prefix}#{tag}#{tag_suffix}" }
  end
end

#watcherObject

thread callback



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 147

def watcher
  # instance variable, and public accessable, for test
  @last_checked = Fluent::Engine.now
  # skip the passed time when loading @counts form file
  @last_checked -= @passed_time if @passed_time
  while true
    sleep 0.5
    begin
      if Fluent::Engine.now - @last_checked >= @interval
        now = Fluent::Engine.now
        flush_emit(now - @last_checked)
        @last_checked = now
      end
    rescue => e
      log.warn "#{e.class} #{e.message} #{e.backtrace.first}"
    end
  end
end