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.



241
242
243
244
245
246
247
# File 'lib/new_relic/agent/error_collector.rb', line 241

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



111
112
113
114
115
116
117
118
119
120
# File 'lib/new_relic/agent/error_collector.rb', line 111

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

#custom_params_from_opts(options) ⇒ Object

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



167
168
169
170
# File 'lib/new_relic/agent/error_collector.rb', line 167

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)


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

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



195
196
197
# File 'lib/new_relic/agent/error_collector.rb', line 195

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



221
222
223
224
225
226
227
228
# File 'lib/new_relic/agent/error_collector.rb', line 221

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



208
209
210
# File 'lib/new_relic/agent/error_collector.rb', line 208

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



213
214
215
216
# File 'lib/new_relic/agent/error_collector.rb', line 213

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



152
153
154
# File 'lib/new_relic/agent/error_collector.rb', line 152

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)


87
88
89
90
# File 'lib/new_relic/agent/error_collector.rb', line 87

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)


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

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.



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/new_relic/agent/error_collector.rb', line 124

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



186
187
188
189
190
191
# File 'lib/new_relic/agent/error_collector.rb', line 186

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)


232
233
234
235
236
# File 'lib/new_relic/agent/error_collector.rb', line 232

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



175
176
177
178
179
180
181
182
# File 'lib/new_relic/agent/error_collector.rb', line 175

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)


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

def seen?(exception)
  exception.instance_variable_get(EXCEPTION_TAG_IVAR)
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



202
203
204
# File 'lib/new_relic/agent/error_collector.rb', line 202

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)


141
142
143
144
145
146
147
148
149
# File 'lib/new_relic/agent/error_collector.rb', line 141

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



107
108
109
# File 'lib/new_relic/agent/error_collector.rb', line 107

def tag_as_seen(exception)
  exception.instance_variable_set(EXCEPTION_TAG_IVAR, true)
end

#uri_ref_and_root(options) ⇒ Object

returns some basic option defaults pulled from the provided options hash



158
159
160
161
162
163
164
# File 'lib/new_relic/agent/error_collector.rb', line 158

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