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.



7
8
9
# File 'lib/new_relic/rack/error_collector.rb', line 7

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

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/new_relic/rack/error_collector.rb', line 11

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.notice_error(exception,
                                  :uri => request.path,
                                  :referer => request.referer,
                                  :request_params => params)
  end
  raise exception
end

#ignored_in_controller?(exception, request) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/new_relic/rack/error_collector.rb', line 39

def ignored_in_controller?(exception, request)
  return true if request.env['newrelic.ignored']

  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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/new_relic/rack/error_collector.rb', line 60

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)


34
35
36
37
# File 'lib/new_relic/rack/error_collector.rb', line 34

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