Module: NewRelic::Agent::ErrorCollector::NoticeError

Included in:
NewRelic::Agent::ErrorCollector
Defined in:
lib/new_relic/agent/error_collector.rb

Overview

This module was extracted from the notice_error method - it is internally tested and can be refactored without major issues.

Instance Method Summary collapse

Instance Method Details

#add_to_error_queue(noticed_error) ⇒ Object

Synchronizes adding an error to the error queue, and checks if the error queue is too long - if so, we drop the error on the floor after logging a warning.



238
239
240
241
242
243
244
# File 'lib/new_relic/agent/error_collector.rb', line 238

def add_to_error_queue(noticed_error)
  @lock.synchronize do
    if !over_queue_limit?(noticed_error.message) && !@errors.include?(noticed_error)
      @errors << noticed_error
    end
  end
end

#blamed_metric_name(options) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/new_relic/agent/error_collector.rb', line 109

def blamed_metric_name(options)
  if options[:metric] && options[:metric] != ::NewRelic::Agent::UNKNOWN_METRIC
    "Errors/#{options[:metric]}"
  else
    if txn = TransactionState.get.transaction
      "Errors/#{txn.name}"
    end
  end
end

#custom_params_from_opts(options) ⇒ Object

If anything else is left over, we treat it like a custom param



164
165
166
167
# File 'lib/new_relic/agent/error_collector.rb', line 164

def custom_params_from_opts(options)
  # If anything else is left over, treat it like a custom param:
  fetch_from_options(options, :custom_params, {}).merge(options)
end

#error_is_ignored?(error) ⇒ Boolean

an error is ignored if it is nil or if it is filtered

Returns:

  • (Boolean)


95
96
97
# File 'lib/new_relic/agent/error_collector.rb', line 95

def error_is_ignored?(error)
  error && filtered_error?(error)
end

#error_params_from_options(options) ⇒ Object

Merges together many of the options into something that can actually be attached to the error



192
193
194
# File 'lib/new_relic/agent/error_collector.rb', line 192

def error_params_from_options(options)
  uri_ref_and_root(options).merge(normalized_request_and_custom_params(options))
end

#exception_info(exception) ⇒ Object

extracts a bunch of information from the exception to include in the noticed error - some may or may not be available, but we try to include all of it



218
219
220
221
222
223
224
225
# File 'lib/new_relic/agent/error_collector.rb', line 218

def exception_info(exception)
  {
    :file_name => sense_method(exception, 'file_name'),
    :line_number => sense_method(exception, 'line_number'),
    :source => extract_source(exception),
    :stack_trace => extract_stack_trace(exception)
  }
end

#extract_source(exception) ⇒ Object

extracts source from the exception, if the exception supports that method



205
206
207
# File 'lib/new_relic/agent/error_collector.rb', line 205

def extract_source(exception)
  sense_method(exception, 'source_extract') if @capture_source
end

#extract_stack_trace(exception) ⇒ Object

extracts a stack trace from the exception for debugging purposes



210
211
212
213
# File 'lib/new_relic/agent/error_collector.rb', line 210

def extract_stack_trace(exception)
  actual_exception = sense_method(exception, 'original_exception') || exception
  sense_method(actual_exception, 'backtrace') || '<no stack trace>'
end

#fetch_from_options(options, key, default = nil) ⇒ Object

acts just like Hash#fetch, but deletes the key from the hash



149
150
151
# File 'lib/new_relic/agent/error_collector.rb', line 149

def fetch_from_options(options, key, default=nil)
  options.delete(key) || default
end

#filtered_by_error_filter?(error) ⇒ Boolean

Checks the provided error against the error filter, if there is an error filter

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/new_relic/agent/error_collector.rb', line 83

def filtered_by_error_filter?(error)
  return unless @ignore_filter
  !@ignore_filter.call(error)
end

#filtered_error?(error) ⇒ Boolean

Checks the array of error names and the error filter against the provided error

Returns:

  • (Boolean)


90
91
92
# File 'lib/new_relic/agent/error_collector.rb', line 90

def filtered_error?(error)
  @ignore[error.class.name] || filtered_by_error_filter?(error)
end

#increment_error_count!(exception, options = {}) ⇒ Object

Increments a statistic that tracks total error rate Be sure not to double-count same exception. This clears per harvest.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/new_relic/agent/error_collector.rb', line 121

def increment_error_count!(exception, options={})
  return if seen?(exception)
  tag_as_seen(exception)

  metric_names = ["Errors/all"]
  blamed_metric = blamed_metric_name(options)
  metric_names << blamed_metric if blamed_metric

  stats_engine = NewRelic::Agent.agent.stats_engine
  stats_engine.record_metrics(metric_names) do |stats|
    stats.increment_count
  end
end

#normalized_request_and_custom_params(options) ⇒ Object

normalizes the request and custom parameters before attaching them to the error. See NewRelic::CollectionHelper#normalize_params



183
184
185
186
187
188
# File 'lib/new_relic/agent/error_collector.rb', line 183

def normalized_request_and_custom_params(options)
  {
    :request_params => normalize_params(request_params_from_opts(options)),
    :custom_params  => normalize_params(custom_params_from_opts(options))
  }
end

#over_queue_limit?(message) ⇒ Boolean

checks the size of the error queue to make sure we are under the maximum limit, and logs a warning if we are over the limit.

Returns:

  • (Boolean)


229
230
231
232
233
# File 'lib/new_relic/agent/error_collector.rb', line 229

def over_queue_limit?(message)
  over_limit = (@errors.reject{|err| err.exception_class_constant < NewRelic::Agent::InternalAgentError}.length >= MAX_ERROR_QUEUE_LENGTH)
  ::NewRelic::Agent.logger.warn("The error reporting queue has reached #{MAX_ERROR_QUEUE_LENGTH}. The error detail for this and subsequent errors will not be transmitted to New Relic until the queued errors have been sent: #{message}") if over_limit
  over_limit
end

#request_params_from_opts(options) ⇒ Object

takes the request parameters out of the options hash, and returns them if we are capturing parameters, otherwise returns nil



172
173
174
175
176
177
178
179
# File 'lib/new_relic/agent/error_collector.rb', line 172

def request_params_from_opts(options)
  value = options.delete(:request_params)
  if Agent.config[:capture_params]
    value
  else
    nil
  end
end

#seen?(exception) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/new_relic/agent/error_collector.rb', line 99

def seen?(exception)
  error_ids = TransactionState.get.transaction_noticed_error_ids
  error_ids.include?(exception.object_id)
end

#sense_method(object, method) ⇒ Object

calls a method on an object, if it responds to it - used for detection and soft fail-safe. Returns nil if the method does not exist



199
200
201
# File 'lib/new_relic/agent/error_collector.rb', line 199

def sense_method(object, method)
  object.send(method) if object.respond_to?(method)
end

#should_exit_notice_error?(exception) ⇒ Boolean

whether we should return early from the notice_error process

  • based on whether the error is ignored or the error

collector is disabled

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
# File 'lib/new_relic/agent/error_collector.rb', line 138

def should_exit_notice_error?(exception)
  if enabled?
    if !error_is_ignored?(exception)
      return exception.nil? # exit early if the exception is nil
    end
  end
  # disabled or an ignored error, per above
  true
end

#tag_as_seen(exception) ⇒ Object



104
105
106
107
# File 'lib/new_relic/agent/error_collector.rb', line 104

def tag_as_seen(exception)
  txn = Transaction.current
  txn.noticed_error_ids << exception.object_id if txn
end

#uri_ref_and_root(options) ⇒ Object

returns some basic option defaults pulled from the provided options hash



155
156
157
158
159
160
161
# File 'lib/new_relic/agent/error_collector.rb', line 155

def uri_ref_and_root(options)
  {
    :request_uri => fetch_from_options(options, :uri, ''),
    :request_referer => fetch_from_options(options, :referer, ''),
    :rails_root => NewRelic::Control.instance.root
  }
end