Class: Hoodoo::Services::Middleware::ExceptionReporting

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodoo/services/middleware/exception_reporting/exception_reporting.rb,
lib/hoodoo/services/middleware/exception_reporting/base_reporter.rb,
lib/hoodoo/services/middleware/exception_reporting/reporters/airbrake_reporter.rb

Overview

Exception reporting / monitoring through external services.

Defined Under Namespace

Classes: AirbrakeReporter, BaseReporter, Payload

Constant Summary collapse

@@reporter_pool =

Pool of exception reporters.

Hoodoo::Communicators::Pool.new

Class Method Summary collapse

Class Method Details

.add(klass) ⇒ Object

Add an exception reporter class to the set of reporters. See the Hoodoo::Services::Middleware::ExceptionReporting::BaseReporter class for an overview.

Whenever the middleware’s own exception handler catches an exception, it will run through the set of exception reporters (if any) and call each one to report exception details.

Reporters are maintained in a Set. Only one class will ever be stored and called once per exception; the original order of addition before duplicates is maintained (so if you add class A, then B, then A again, then class A is called first and only once, then B once).

Each reporter is called from its own Ruby Thread so that client API call response is kept fast. If a call fails, a debug log entry is made but processing of other reporters continues uninterrupted. It is up to individual reporter classes to manage thread safety.

klass

Hoodoo::Services::Middleware::ExceptionReporting::BaseReporter subclass (class, not instance) to add.



43
44
45
46
47
48
49
# File 'lib/hoodoo/services/middleware/exception_reporting/exception_reporting.rb', line 43

def self.add( klass )
  unless klass < Hoodoo::Services::Middleware::ExceptionReporting::BaseReporter
    raise "Hoodoo::Services::Middleware.add must be called with a Hoodoo::Services::Middleware::ExceptionReporting::BaseReporter subclass"
  end

  @@reporter_pool.add( klass.instance )
end

.contextual_report(exception, context) ⇒ Object

Call all added exception reporters (see ::add) to report an exception based on the context of an in-flight request/response cycle. Reporters need to support the contextual reporting mechanism. If any do not, the simpler ::report mechanism is used as a fallback.

exception

Exception or Exception subclass instance to report.

context

Hoodoo::Services::Context instance describing the in-flight request/response cycle.



88
89
90
91
# File 'lib/hoodoo/services/middleware/exception_reporting/exception_reporting.rb', line 88

def self.contextual_report( exception, context )
  payload = Payload.new( exception: exception, context: context )
  @@reporter_pool.communicate( payload )
end

.remove(klass) ⇒ Object

Remove an exception reporter class from the set of reporters. See ::add for details.

klass

Hoodoo::Services::Middleware::ExceptionReporting::BaseReporter subclass (class, not instance) to remove.



57
58
59
60
61
62
63
# File 'lib/hoodoo/services/middleware/exception_reporting/exception_reporting.rb', line 57

def self.remove( klass )
  unless klass < Hoodoo::Services::Middleware::ExceptionReporting::BaseReporter
    raise "Hoodoo::Services::Middleware.remove must be called with a Hoodoo::Services::Middleware::ExceptionReporting::BaseReporter subclass"
  end

  @@reporter_pool.remove( klass.instance )
end

.report(exception, rack_env = nil) ⇒ Object

Call all added exception reporters (see ::add) to report an exception.

exception

Exception or Exception subclass instance to report.

rack_env

Optional Rack environment hash for the inbound request, for exception reports made in the context of Rack request handling.



73
74
75
76
# File 'lib/hoodoo/services/middleware/exception_reporting/exception_reporting.rb', line 73

def self.report( exception, rack_env = nil )
  payload = Payload.new( exception: exception, rack_env: rack_env )
  @@reporter_pool.communicate( payload )
end

.wait(timeout = 5) ⇒ Object

Wait for all executing reporter threads to catch up before continuing.

timeout

Optional timeout wait delay for each thread. Default is 5 seconds.



98
99
100
# File 'lib/hoodoo/services/middleware/exception_reporting/exception_reporting.rb', line 98

def self.wait( timeout = 5 )
  @@reporter_pool.wait( per_instance_timeout: timeout )
end