Class: Fluent::TraceAccumulator

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

Overview

Buffers and groups log records if they contain exception stack traces.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_field, languages, max_lines: 0, max_bytes: 0, &emit_callback) ⇒ TraceAccumulator

If message_field is nil, the instance is set up to accumulate records that are plain strings (i.e. the whole record is concatenated). Otherwise, the instance accepts records that are dictionaries (usually originating from structured JSON logs) and accumulates just the content of the given message field. message_field may contain the empty string. In this case, the TraceAccumulator ‘learns’ the field name from the first record by checking for some pre-defined common field names of text logs. The named parameters max_lines and max_bytes limit the maximum amount of data to be buffered. The default value 0 indicates ‘no limit’.



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/fluent/plugin/exception_detector.rb', line 261

def initialize(message_field, languages, max_lines: 0, max_bytes: 0,
               &emit_callback)
  @exception_detector = Fluent::ExceptionDetector.new(*languages)
  @max_lines = max_lines
  @max_bytes = max_bytes
  @message_field = message_field
  @messages = []
  @buffer_start_time = Time.now
  @buffer_size = 0
  @first_record = nil
  @first_timestamp = nil
  @emit = emit_callback
end

Instance Attribute Details

#buffer_start_timeObject (readonly)

Returns the value of attribute buffer_start_time.



249
250
251
# File 'lib/fluent/plugin/exception_detector.rb', line 249

def buffer_start_time
  @buffer_start_time
end

Instance Method Details

#flushObject



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/fluent/plugin/exception_detector.rb', line 291

def flush
  case @messages.length
  when 0
    return
  when 1
    @emit.call(@first_timestamp, @first_record)
  else
    combined_message = @messages.join
    if @message_field.nil?
      output_record = combined_message
    else
      output_record = @first_record
      output_record[@message_field] = combined_message
    end
    @emit.call(@first_timestamp, output_record)
  end
  @messages = []
  @first_record = nil
  @first_timestamp = nil
  @buffer_size = 0
end

#force_flushObject



313
314
315
316
# File 'lib/fluent/plugin/exception_detector.rb', line 313

def force_flush
  flush
  @exception_detector.reset
end

#push(time_sec, record) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/fluent/plugin/exception_detector.rb', line 275

def push(time_sec, record)
  message = extract_message(record)
  if message.nil?
    @exception_detector.reset
    detection_status = :no_trace
  else
    force_flush if @max_bytes > 0 &&
                   @buffer_size + message.length > @max_bytes
    detection_status = @exception_detector.update(message)
  end

  update_buffer(detection_status, time_sec, record, message)

  force_flush if @max_lines > 0 && @messages.length == @max_lines
end