Class: NewRelic::Agent::ErrorCollector

Inherits:
Object
  • Object
show all
Includes:
NoticeError, CollectionHelper
Defined in:
lib/new_relic/agent/error_collector.rb

Overview

This class collects errors from the parent application, storing them until they are harvested and transmitted to the server

Defined Under Namespace

Modules: NoticeError, Shim

Constant Summary collapse

MAX_ERROR_QUEUE_LENGTH =

Maximum possible length of the queue - defaults to 20, may be made configurable in the future. This is a tradeoff between memory and data retention

20

Constants included from CollectionHelper

CollectionHelper::DEFAULT_ARRAY_TRUNCATION_SIZE, CollectionHelper::DEFAULT_TRUNCATION_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NoticeError

#add_to_error_queue, #custom_params_from_opts, #error_is_ignored?, #error_params_from_options, #exception_info, #extract_source, #extract_stack_trace, #fetch_from_options, #filtered_by_error_filter?, #filtered_error?, #increment_error_count!, #normalized_request_and_custom_params, #over_queue_limit?, #request_params_from_opts, #sense_method, #should_exit_notice_error?, #uri_ref_and_root

Methods included from CollectionHelper

#normalize_params, #strip_nr_from_backtrace

Constructor Details

#initializeErrorCollector

Returns a new error collector



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/new_relic/agent/error_collector.rb', line 22

def initialize
  @errors = []
  # lookup of exception class names to ignore.  Hash for fast access
  @ignore = {}
  @capture_source = Agent.config[:'error_collector.capture_source']

  initialize_ignored_errors(Agent.config[:'error_collector.ignore_errors'])
  @lock = Mutex.new

  Agent.config.register_callback(:'error_collector.enabled') do |config_enabled|
    log.debug "Errors will #{config_enabled ? '' : 'not '}be sent to the New Relic service."
  end
  Agent.config.register_callback(:'error_collector.ignore_errors') do |ignore_errors|
    initialize_ignored_errors(ignore_errors)
  end
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



19
20
21
# File 'lib/new_relic/agent/error_collector.rb', line 19

def errors
  @errors
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/new_relic/agent/error_collector.rb', line 46

def enabled?
  Agent.config[:'error_collector.enabled']
end

#harvest_errors(unsent_errors) ⇒ Object

Get the errors currently queued up. Unsent errors are left over from a previous unsuccessful attempt to send them to the server.



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/new_relic/agent/error_collector.rb', line 236

def harvest_errors(unsent_errors)
  @lock.synchronize do
    errors = @errors
    @errors = []

    if unsent_errors && !unsent_errors.empty?
      errors = unsent_errors + errors
    end

    errors
  end
end

#ignore(errors) ⇒ Object

errors is an array of Exception Class Names



66
67
68
69
70
71
# File 'lib/new_relic/agent/error_collector.rb', line 66

def ignore(errors)
  errors.each do |error|
    @ignore[error] = true
    log.debug("Ignoring errors of type '#{error}'")
  end
end

#ignore_error_filter(&block) ⇒ Object

Returns the error filter proc that is used to check if an error should be reported. When given a block, resets the filter to the provided block. The define_method() is used to wrap the block in a lambda so return statements don’t result in a LocalJump exception.



55
56
57
58
59
60
61
62
# File 'lib/new_relic/agent/error_collector.rb', line 55

def ignore_error_filter(&block)
  if block
    self.class.class_eval { define_method(:ignore_filter_proc, &block) }
    @ignore_filter = method(:ignore_filter_proc)
  else
    @ignore_filter
  end
end

#initialize_ignored_errors(ignore_errors) ⇒ Object



39
40
41
42
43
44
# File 'lib/new_relic/agent/error_collector.rb', line 39

def initialize_ignored_errors(ignore_errors)
  @ignore.clear
  ignore_errors = ignore_errors.split(",") if ignore_errors.is_a? String
  ignore_errors.each { |error| error.strip! }
  ignore(ignore_errors)
end

#notice_error(exception, options = {}) ⇒ Object

Notice the error with the given available options:

  • :uri => The request path, minus any request params or query string.

  • :referer => The URI of the referer

  • :metric => The metric name associated with the transaction

  • :request_params => Request parameters, already filtered if necessary

  • :custom_params => Custom parameters

If anything is left over, it’s added to custom params If exception is nil, the error count is bumped and no traced error is recorded



224
225
226
227
228
229
230
231
232
# File 'lib/new_relic/agent/error_collector.rb', line 224

def notice_error(exception, options={})
  return if should_exit_notice_error?(exception)
  action_path     = fetch_from_options(options, :metric, (NewRelic::Agent.instance.stats_engine.scope_name || ''))
  exception_options = error_params_from_options(options).merge(exception_info(exception))
  add_to_error_queue(NewRelic::NoticedError.new(action_path, exception_options, exception))
  exception
rescue => e
  log.error("Error capturing an error #{e}")
end