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.



5
6
7
8
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 5

def initialize
  super
  require 'pathname'
end

Instance Attribute Details

#countsObject

Returns the value of attribute counts.



23
24
25
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 23

def counts
  @counts
end

#last_checkedObject

Returns the value of attribute last_checked.



27
28
29
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 27

def last_checked
  @last_checked
end

#matchesObject

Returns the value of attribute matches.



24
25
26
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 24

def matches
  @matches
end

#saved_atObject

Returns the value of attribute saved_at.



26
27
28
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 26

def saved_at
  @saved_at
end

#saved_durationObject

Returns the value of attribute saved_duration.



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

def saved_duration
  @saved_duration
end

Instance Method Details

#configure(conf) ⇒ Object



29
30
31
32
33
34
35
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
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 29

def configure(conf)
  super

  @interval = @interval.to_i

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

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

  case @aggregate
  when 'all'
    raise Fluent::ConfigError, "anomalydetect: `tag` must be specified with aggregate all" if @tag.nil?
  when 'tag'
    raise Fluent::ConfigError, "anomalydetect: `add_tag_prefix` must be specified with aggregate tag" if @add_tag_prefix.nil?
  else
    raise Fluent::ConfigError, "anomalydetect: aggregate allows tag/all"
  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, @tag_prefix_match)}" }
    elsif @tag_prefix_match
      Proc.new {|tag| lstrip(tag, @tag_prefix_match) }
    elsif @tag_prefix
      Proc.new {|tag| "#{@tag_prefix}#{tag}" }
    elsif @tag
      Proc.new {|tag| @tag }
    else
      Proc.new {|tag| tag }
    end

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

#emit(tag, es, chain) ⇒ Object

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 97

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

  # stats
  count = 0; matches = {}
  es.each do |time,record|
    if record[key]
      # @todo: make an option for calcuation in the same tag. now only sum is supported
      matches[key] = (matches[key] ? matches[key] + record[key] : record[key])
    end
    count += 1
  end

  # thread safe merge
  @counts[tag] ||= 0
  @matches[tag] ||= {}
  @mutex.synchronize do
    if matches[key]
      # @todo: make an option for calcuation in the same tag. now only sum is supported
      @matches[tag][key] = (@matches[tag][key] ? @matches[tag][key] + matches[key] : matches[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



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

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

  if @aggregate == 'all'
    values = matches.values.map {|match| match[@target_key] }.compact
    output = generate_output(values)
    Fluent::Engine.emit(@tag, time, output) if output
  else # aggregate tag
    matches.each do |tag, match|
      values = [match[@target_key]]
      output = generate_output(values)
      emit_tag = @tag_proc.call(tag)
      Fluent::Engine.emit(emit_tag, time, output) if output
    end
  end
end

#generate_output(values) ⇒ Object



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

def generate_output(values)
  case @compare_with
  when :sum
    target_value = values.inject(:+)
  when :max
    target_value = values.max
  when :min
    target_value = values.min
  when :avg
    target_value = values.inject(:+) / values.count unless values.empty?
  end

  return nil if target_value.nil?
  return nil if target_value == 0 # ignore 0 because standby nodes receive 0 message usually
  return nil if @less_than     and @less_than   <= target_value
  return nil if @less_equal    and @less_equal  <  target_value
  return nil if @greater_than  and target_value <= @greater_than
  return nil if @greater_equal and target_value <  @greater_equal

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

#load_status(file_path, interval) ⇒ Object

Load internal status from a file

Parameters:

  • file_path (String)
  • interval (Interger)


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

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 Fluent::Engine.now <= stored[:saved_at] + interval
          @counts = stored[:counts]
          @matches = stored[:matches]
          @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_notiifer: 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)


192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 192

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,
        :matches          => @matches,
        :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



89
90
91
92
93
94
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 89

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

#startObject



83
84
85
86
87
# File 'lib/fluent/plugin/out_stats_notifier.rb', line 83

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

#watcherObject

thread callback



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 127

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