Class: NewRelic::Rack::ErrorCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/rack/error_collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ErrorCollector

Returns a new instance of ErrorCollector.



3
4
5
# File 'lib/new_relic/rack/error_collector.rb', line 3

def initialize(app, options={})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/new_relic/rack/error_collector.rb', line 7

def call(env)
  @app.call(env)
rescue Exception => exception
  NewRelic::Agent.logger.debug "collecting %p: %s" % [ exception.class, exception.message ]
  request = Rack::Request.new(env)

  if !should_ignore_error?(exception, request)
    params = begin
      request.params
    rescue => err
      warning = "failed to capture request parameters: %p: %s" % [ err.class, err.message ]
      NewRelic::Agent.logger.warn(warning)
      {'error' => warning}
    end

    NewRelic::Agent.instance.error_collector.notice_error(exception,
                                                          :uri => request.path,
                                                      :referer => request.referer,
                                               :request_params => params)
  end
  raise exception
end

#ignored_in_controller?(exception, request) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/new_relic/rack/error_collector.rb', line 35

def ignored_in_controller?(exception, request)
  if request.env['action_dispatch.request.parameters']
    ignore_actions = newrelic_ignore_for_controller(request.env['action_dispatch.request.parameters']['controller'])
    action_name = request.env['action_dispatch.request.parameters']['action']

    case ignore_actions
    when nil; false
    when Hash
      only_actions = Array(ignore_actions[:only])
      except_actions = Array(ignore_actions[:except])
      only_actions.include?(action_name.to_sym) ||
        (except_actions.any? &&
         !except_actions.include?(action_name.to_sym))
    else
      true
    end
  end
end

#newrelic_ignore_for_controller(controller_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/new_relic/rack/error_collector.rb', line 54

def newrelic_ignore_for_controller(controller_name)
  if controller_name
    controller_constant_name = (controller_name + "_controller").camelize
    if Object.const_defined?(controller_constant_name)
      controller = controller_constant_name.constantize
      controller.instance_variable_get(:@do_not_trace)
    end
  end
rescue NameError
  nil
end

#should_ignore_error?(error, request) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/new_relic/rack/error_collector.rb', line 30

def should_ignore_error?(error, request)
  NewRelic::Agent.instance.error_collector.error_is_ignored?(error) ||
    ignored_in_controller?(error, request)
end