Class: Cybertrain::ErrorReporter

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/error_reporter.rb,
cybertrain/error_reporter.rb,
cybertrain/error_reporter/sentry.rb

Overview

The process’s error reporter, error: see the file’s header comment.

Defined Under Namespace

Classes: Filter, Sender, SentrySender

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ErrorReporter

Returns a new instance of ErrorReporter.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'cybertrain/error_reporter.rb', line 328

def initialize
  @lock = Mutex.new
  # Seeded with the element type, then emptied (rule 9).
  @filters = Array.new(0) { Filter.new }
  # How many filters there were when the first test began ({reset!}).
  @boot_filters = 0
  @subscribers = Array.new(0) { ErrorSubscriber.new }
  # How many subscribers were there when the first test began (-1:
  # none has): {reset!} keeps those, the ones config/app.rb added.
  @boot_subscribers = -1
  @sender = Sender.new
  # The sender in place when the first test began: {reset!} puts it
  # back.
  @boot_sender = @sender
  @queue = Queue.new
  # Events queued and not yet handed to every subscriber and the sender.
  @pending = 0
  @started = false
  # The dedupe table: a key's last queueing, as Clock seconds.
  @recent = { "" => 0.0 }
  @recent.delete("")
  @dropped = 0
  # When the last drop line was logged, as Clock seconds; -1.0: never
  # (a Float, not a nullable Time: rule 7).
  @last_drop_log = -1.0
end

Instance Method Details

#add_filter(filter) ⇒ nil

Adds a filter after those already added; it sees every event delivered from now on (Filter). A filter alone does not make an event go anywhere: with no subscriber and a sender that does not send, nothing is queued and no filter runs.

Parameters:

Returns:

  • (nil)


426
427
428
429
# File 'cybertrain/error_reporter.rb', line 426

def add_filter(filter)
  @lock.synchronize { @filters << filter }
  nil
end

#drain_reports(seconds = 2.0) ⇒ Boolean

Waits up to seconds for every queued event to be delivered; true once the queue is empty and nothing is in flight, false at the deadline (what is left stays queued). Each role’s stop calls it.

Parameters:

  • seconds (Float) (defaults to: 2.0)

Returns:

  • (Boolean)


485
486
487
488
489
490
491
492
493
494
# File 'cybertrain/error_reporter.rb', line 485

def drain_reports(seconds = 2.0)
  deadline = Clock.monotonic + seconds.to_f
  while true
    return true if idle?
    return false if Clock.monotonic >= deadline

    sleep 0.002
  end
  false
end

#handle(context: NO_CONTEXT) { ... } ⇒ Object?

Runs the block and answers its value; an exception it raises (StandardError, JSON::ParserError, JSON::NestingError) is reported with handled: true and the block answers nil.

Examples:

price = Cybertrain.error.handle(context: { "feed" => "prices" }) do
  parse_price(text)
end

Parameters:

  • context (Hash) (defaults to: NO_CONTEXT) —

    as #report’s

Yields:

  • the work

Returns:

  • (Object, nil) —

    the block’s value, nil after an exception



399
400
401
402
403
404
405
406
407
408
# File 'cybertrain/error_reporter.rb', line 399

def handle(context: NO_CONTEXT)
  value = nil
  begin
    value = yield
  rescue JSON::NestingError, JSON::ParserError, StandardError => e
    report(e, handled: true, context: context)
    value = nil
  end
  value
end

#report(error, handled: true, context: NO_CONTEXT) ⇒ nil

Records an exception the application rescued (or, with handled: false, one it did not). context: keys and values become Strings, merged over the thread’s Scope context. Answers nil at once: the event is delivered on the reporter’s thread.

Never raises into its caller: a failure while building or queueing the event (a context value whose to_s raises) is logged at error, error report failed: <Class>: <message>, and it answers nil.

Examples:

begin
  charge(order)
rescue PaymentError => e
  Cybertrain.error.report(e, context: { "order_id" => order.id })
end

Parameters:

  • error (Exception)
  • handled (Boolean) (defaults to: true)
  • context (Hash) (defaults to: NO_CONTEXT) —

    keys and values of any type, read with to_s

Returns:

  • (nil)


374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'cybertrain/error_reporter.rb', line 374

def report(error, handled: true, context: NO_CONTEXT)
  return nil unless listened?

  given = ErrorReporter.empty_context
  context.each { |k, v| given[k.to_s] = v.to_s }
  scope = Logger.current_scope
  where = scope.scope_where
  enqueue_error(error, where, handled, given)
rescue JSON::NestingError, JSON::ParserError, StandardError => e
  report_failed(e)
rescue NotImplementedError, SystemStackError, ScriptError => e
  report_failed(e)
end

#sender ⇒ Sender

Where events go after the subscribers.

Returns:



499
500
501
# File 'cybertrain/error_reporter.rb', line 499

def sender
  @sender
end

#sender=(sender) ⇒ nil

Installs the sender (the Sentry sender, at boot).

Parameters:

Returns:

  • (nil)


507
508
509
510
# File 'cybertrain/error_reporter.rb', line 507

def sender=(sender)
  @lock.synchronize { @sender = sender }
  nil
end

#subscribe(subscriber) ⇒ nil

Adds a subscriber; it receives every event from now on.

Parameters:

Returns:

  • (nil)


414
415
416
417
# File 'cybertrain/error_reporter.rb', line 414

def subscribe(subscriber)
  @lock.synchronize { @subscribers << subscriber }
  nil
end