Class: Radar::Application

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/radar/application.rb

Overview

Represents an instance of Radar for a given application. Every application which uses Radar must instantiate an Application.

Constant Summary collapse

@@registered =
{}
@@mutex =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, register = true) ⇒ Application

Creates a new application with the given name and registers it for lookup later. If a block is given, it will be yielded with the new instantiated Radar::Application so you can also #config it all in one go.

Parameters:

  • name (String)

    Application name. This must be unique for any given application or an exception will be raised.



47
48
49
50
51
52
53
54
55
56
# File 'lib/radar/application.rb', line 47

def initialize(name, register=true)
  @@mutex.synchronize do
    raise ApplicationAlreadyExists.new("'#{name}' already defined at '#{self.class.find(name).creation_location}'") if self.class.find(name)

    @name = name
    @creation_location = caller.first
    yield self if block_given?
    @@registered[name] = self if register
  end
end

Instance Attribute Details

#creation_locationObject (readonly)

Returns the value of attribute creation_location.



20
21
22
# File 'lib/radar/application.rb', line 20

def creation_location
  @creation_location
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/radar/application.rb', line 19

def name
  @name
end

Class Method Details

.clear!Object

Removes all registered applications. This is only exposed for testing purposes.



35
36
37
# File 'lib/radar/application.rb', line 35

def self.clear!
  @@registered.clear
end

.find(name) ⇒ Application

Looks up an application which was registered with the given name.

Parameters:

  • name (String)

    Application name.

Returns:



29
30
31
# File 'lib/radar/application.rb', line 29

def self.find(name)
  @@registered[name]
end

Instance Method Details

#config {|Config| ... } ⇒ Config

Configures the application by returning the configuration object. If a block is given, the configuration object will be passed into it, allowing for a cleaner way of configuring your applications.

$app = Radar::Application.new
$app.config do |config|
  config.reporters.use Radar::Reporter::FileReporter
end

You can also just use it without a block:

$app.config.reporters.use Radar::Reporter::FileReporter

Yields:

  • (Config)

    Configuration object, only if block is given.

Returns:



74
75
76
77
78
# File 'lib/radar/application.rb', line 74

def config
  @_config ||= Config.new
  yield @_config if block_given?
  @_config
end

#integrate(integrator, *args, &block) ⇒ Object

Integrate this application with some external system, such as Rack, Rails, Sinatra, etc. For more information on Radar integrations, please read the user guide.



134
135
136
137
# File 'lib/radar/application.rb', line 134

def integrate(integrator, *args, &block)
  integrator = Support::Inflector.constantize("Radar::Integration::#{Support::Inflector.camelize(integrator)}") if !integrator.is_a?(Class)
  integrator.integrate!(self, *args, &block)
end

#loggerLogger

Returns the logger for the application. Each application gets their own logger which is used for lightweight (single line) logging so users can sanity check that Radar is working as expected.

Returns:



86
87
88
# File 'lib/radar/application.rb', line 86

def logger
  @_logger ||= Logger.new(self)
end

#report(exception, extra = nil) ⇒ Object

Reports an exception. This will send the exception on to the various reporters configured for this application. If any matchers are defined, using Config#match, then at least one must match for the report to go forward to reporters.

Parameters:

  • exception (Exception)


96
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
# File 'lib/radar/application.rb', line 96

def report(exception, extra=nil)
  data = ExceptionEvent.new(self, exception, extra)

  # If there are rejecters, then verify that they all fail
  if !config.rejecters.empty?
    config.rejecters.values.each do |r|
      if r.call(data)
        logger.info("Ignoring exception. Matches rejecter: #{r}")
        return
      end
    end
  end

  # If there are matchers, then verify that at least one matches
  # before continuing
  if !config.matchers.empty?
    return if !config.matchers.values.find do |m|
      m.call(data) && logger.info("Reporting exception. Matches: #{m}")
    end
  end

  # Report the exception to each of the reporters
  logger.info "Invoking reporters for exception: #{exception.class}"
  config.reporters.values.each do |reporter|
    reporter.call(data)
  end
end

#rescue_at_exit!Object

Hooks this application into the at_exit handler so that application crashing exceptions are properly reported.



126
127
128
129
# File 'lib/radar/application.rb', line 126

def rescue_at_exit!
  logger.info "Attached to application exit."
  at_exit { report($!) if $! }
end

#to_hashHash

Converts application to a serialization-friendly hash.

Returns:

  • (Hash)


142
143
144
# File 'lib/radar/application.rb', line 142

def to_hash
  { :name => name }
end