newrelic_rpm 3.1.0 → 3.1.1.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of newrelic_rpm might be problematic. Click here for more details.

Files changed (94) hide show
  1. data/CHANGELOG +3 -0
  2. data/lib/new_relic/agent.rb +29 -12
  3. data/lib/new_relic/agent/agent.rb +355 -78
  4. data/lib/new_relic/agent/beacon_configuration.rb +49 -7
  5. data/lib/new_relic/agent/browser_monitoring.rb +20 -1
  6. data/lib/new_relic/agent/busy_calculator.rb +11 -3
  7. data/lib/new_relic/agent/chained_call.rb +2 -2
  8. data/lib/new_relic/agent/error_collector.rb +229 -183
  9. data/lib/new_relic/agent/instrumentation.rb +2 -2
  10. data/lib/new_relic/agent/instrumentation/active_merchant.rb +5 -1
  11. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +5 -1
  12. data/lib/new_relic/agent/instrumentation/authlogic.rb +4 -0
  13. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +16 -5
  14. data/lib/new_relic/agent/instrumentation/data_mapper.rb +5 -1
  15. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +5 -1
  16. data/lib/new_relic/agent/instrumentation/memcache.rb +5 -1
  17. data/lib/new_relic/agent/instrumentation/merb/controller.rb +5 -1
  18. data/lib/new_relic/agent/instrumentation/merb/errors.rb +5 -1
  19. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +0 -5
  20. data/lib/new_relic/agent/instrumentation/net.rb +5 -1
  21. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +7 -3
  22. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +5 -1
  23. data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +5 -1
  24. data/lib/new_relic/agent/instrumentation/rails/errors.rb +4 -0
  25. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +4 -0
  26. data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +13 -6
  27. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +4 -0
  28. data/lib/new_relic/agent/instrumentation/sinatra.rb +4 -0
  29. data/lib/new_relic/agent/instrumentation/sunspot.rb +4 -0
  30. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +5 -1
  31. data/lib/new_relic/agent/method_tracer.rb +205 -99
  32. data/lib/new_relic/agent/shim_agent.rb +0 -1
  33. data/lib/new_relic/agent/stats_engine.rb +1 -0
  34. data/lib/new_relic/agent/stats_engine/metric_stats.rb +23 -7
  35. data/lib/new_relic/agent/stats_engine/samplers.rb +8 -2
  36. data/lib/new_relic/agent/stats_engine/transactions.rb +26 -12
  37. data/lib/new_relic/agent/transaction_sampler.rb +3 -1
  38. data/lib/new_relic/agent/worker_loop.rb +13 -5
  39. data/lib/new_relic/collection_helper.rb +6 -3
  40. data/lib/new_relic/control.rb +1 -3
  41. data/lib/new_relic/control/class_methods.rb +8 -3
  42. data/lib/new_relic/control/configuration.rb +24 -5
  43. data/lib/new_relic/control/frameworks.rb +10 -0
  44. data/lib/new_relic/control/frameworks/external.rb +4 -4
  45. data/lib/new_relic/control/frameworks/merb.rb +1 -0
  46. data/lib/new_relic/control/frameworks/rails.rb +5 -5
  47. data/lib/new_relic/control/frameworks/rails3.rb +5 -3
  48. data/lib/new_relic/control/frameworks/ruby.rb +5 -5
  49. data/lib/new_relic/control/frameworks/sinatra.rb +1 -4
  50. data/lib/new_relic/control/instance_methods.rb +23 -7
  51. data/lib/new_relic/control/instrumentation.rb +22 -3
  52. data/lib/new_relic/control/logging_methods.rb +25 -7
  53. data/lib/new_relic/control/server_methods.rb +16 -6
  54. data/lib/new_relic/data_serialization.rb +83 -14
  55. data/lib/new_relic/delayed_job_injection.rb +7 -1
  56. data/lib/new_relic/local_environment.rb +55 -25
  57. data/lib/new_relic/metric_data.rb +7 -2
  58. data/lib/new_relic/metric_spec.rb +5 -3
  59. data/lib/new_relic/stats.rb +16 -7
  60. data/lib/new_relic/transaction_analysis.rb +2 -1
  61. data/lib/new_relic/transaction_analysis/segment_summary.rb +4 -2
  62. data/lib/new_relic/transaction_sample.rb +33 -7
  63. data/lib/new_relic/transaction_sample/segment.rb +21 -3
  64. data/lib/new_relic/version.rb +2 -2
  65. data/newrelic_rpm.gemspec +7 -11
  66. data/test/config/newrelic.yml +1 -1
  67. data/test/new_relic/agent/agent/start_worker_thread_test.rb +1 -4
  68. data/test/new_relic/agent/agent_test.rb +16 -0
  69. data/test/new_relic/agent/agent_test_controller.rb +1 -1
  70. data/test/new_relic/agent/agent_test_controller_test.rb +14 -19
  71. data/test/new_relic/agent/beacon_configuration_test.rb +2 -2
  72. data/test/new_relic/agent/browser_monitoring_test.rb +7 -3
  73. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +13 -4
  74. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +0 -10
  75. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +1 -1
  76. data/test/new_relic/agent_test.rb +168 -0
  77. data/test/new_relic/collection_helper_test.rb +21 -3
  78. data/test/new_relic/control/configuration_test.rb +25 -0
  79. data/test/new_relic/data_serialization_test.rb +58 -3
  80. data/test/new_relic/delayed_job_injection_test.rb +17 -0
  81. data/test/new_relic/transaction_analysis/segment_summary_test.rb +14 -0
  82. data/test/new_relic/transaction_analysis_test.rb +3 -3
  83. data/test/new_relic/transaction_sample/segment_test.rb +11 -0
  84. data/test/test_helper.rb +1 -1
  85. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +4 -18
  86. metadata +13 -13
  87. data/lib/new_relic/histogram.rb +0 -91
  88. data/lib/new_relic/rack/metric_app.rb +0 -65
  89. data/lib/new_relic/rack/mongrel_rpm.ru +0 -28
  90. data/lib/new_relic/rack/newrelic.yml +0 -27
  91. data/lib/new_relic/rack_app.rb +0 -6
  92. data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
  93. data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
  94. data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
@@ -1,16 +1,39 @@
1
1
  module NewRelic
2
2
  module Agent
3
+ # This class contains the configuration data for setting up RUM
4
+ # headers and footers - acts as a cache of this data so we don't
5
+ # need to look it up or reconfigure it every request
3
6
  class BeaconConfiguration
7
+
8
+ # the statically generated header - generated when the beacon
9
+ # configuration is created - does not vary per page
4
10
  attr_reader :browser_timing_header
11
+
12
+ # the static portion of the RUM footer - this part does not vary
13
+ # by which request is in progress
5
14
  attr_reader :browser_timing_static_footer
15
+
16
+ # the application id we include in the javascript -
17
+ # crossreferences with the application id on the collectors
6
18
  attr_reader :application_id
19
+
20
+ # the key used for browser monitoring. This is different from
21
+ # the account key
7
22
  attr_reader :browser_monitoring_key
23
+
24
+ # which beacon we should report to - set by startup of the agent
8
25
  attr_reader :beacon
9
- attr_reader :rum_enabled
10
- attr_reader :license_bytes
11
26
 
27
+ # whether RUM is enabled or not - determined based on server and
28
+ # local config
29
+ attr_reader :rum_enabled
30
+
31
+ # A static javascript header that is identical for every account
32
+ # and application
12
33
  JS_HEADER = "<script type=\"text/javascript\">var NREUMQ=[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"
13
-
34
+
35
+ # Creates a new browser configuration data. Argument is a hash
36
+ # of configuration values from the server
14
37
  def initialize(connect_data)
15
38
  @browser_monitoring_key = connect_data['browser_key']
16
39
  @application_id = connect_data['application_id']
@@ -23,7 +46,9 @@ module NewRelic
23
46
  @browser_timing_static_footer = build_load_file_js(connect_data)
24
47
  NewRelic::Control.instance.log.debug("Browser timing static footer: #{@browser_timing_static_footer.inspect}")
25
48
  end
26
-
49
+
50
+ # returns a memoized version of the bytes in the license key for
51
+ # obscuring transaction names in the javascript
27
52
  def license_bytes
28
53
  if @license_bytes.nil?
29
54
  @license_bytes = []
@@ -31,18 +56,35 @@ module NewRelic
31
56
  end
32
57
  @license_bytes
33
58
  end
34
-
59
+
60
+ # returns a snippet of text that does not change
61
+ # per-transaction. Is empty when rum is disabled, or we are not
62
+ # including the episodes file dynamically (i.e. the user
63
+ # includes it themselves)
35
64
  def build_load_file_js(connect_data)
36
65
  return "" unless connect_data.fetch('rum.load_episodes_file', true)
37
66
 
38
67
  episodes_url = connect_data.fetch('episodes_url', '')
39
- "(function(){var d=document;var e=d.createElement(\"script\");e.async=true;e.src=\"#{episodes_url}\";e.type=\"text/javascript\";var s=d.getElementsByTagName(\"script\")[0];s.parentNode.insertBefore(e,s);})();"
68
+
69
+ <<-eos
70
+ if (!NREUMQ.f) NREUMQ.f=function() {
71
+ NREUMQ.push(["load",new Date().getTime()]);
72
+ var e=document.createElement(\"script\");
73
+ e.type=\"text/javascript\";e.async=true;e.src=\"#{episodes_url}\";
74
+ document.body.appendChild(e);
75
+ if(NREUMQ.a)NREUMQ.a();
76
+ };
77
+ if(window.onload!==NREUMQ.f){NREUMQ.a=window.onload;window.onload=NREUMQ.f;};
78
+ eos
40
79
  end
41
80
 
81
+ # returns a copy of the static javascript header, in case people
82
+ # are munging strings somewhere down the line
42
83
  def javascript_header
43
84
  JS_HEADER.dup
44
85
  end
45
-
86
+
87
+ # Returns the header string, properly html-safed if needed
46
88
  def build_browser_timing_header
47
89
  return "" if !@rum_enabled
48
90
  return "" if @browser_monitoring_key.nil?
@@ -2,8 +2,19 @@ require 'base64'
2
2
  require 'new_relic/agent/beacon_configuration'
3
3
  module NewRelic
4
4
  module Agent
5
+ # This module contains support for Real User Monitoring - the
6
+ # javascript generation and configuration
5
7
  module BrowserMonitoring
6
-
8
+
9
+ # This method returns a string suitable for inclusion in a page
10
+ # - known as 'manual instrumentation' for Real User
11
+ # Monitoring. Can return either a script tag with associated
12
+ # javascript, or in the case of disabled Real User Monitoring,
13
+ # an empty string
14
+ #
15
+ # This is the header string - it should be placed as high in the
16
+ # page as is reasonably possible - that is, before any style or
17
+ # javascript inclusions, but after any header-related meta tags
7
18
  def browser_timing_header
8
19
  return "" if NewRelic::Agent.instance.beacon_configuration.nil?
9
20
  return "" if !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced?
@@ -11,6 +22,14 @@ module NewRelic
11
22
  NewRelic::Agent.instance.beacon_configuration.browser_timing_header
12
23
  end
13
24
 
25
+ # This method returns a string suitable for inclusion in a page
26
+ # - known as 'manual instrumentation' for Real User
27
+ # Monitoring. Can return either a script tag with associated
28
+ # javascript, or in the case of disabled Real User Monitoring,
29
+ # an empty string
30
+ #
31
+ # This is the footer string - it should be placed as low in the
32
+ # page as is reasonably possible.
14
33
  def browser_timing_footer
15
34
  config = NewRelic::Agent.instance.beacon_configuration
16
35
  return "" if config.nil? || !config.rum_enabled || config.browser_monitoring_key.nil?
@@ -14,7 +14,10 @@ module NewRelic
14
14
 
15
15
  # For testability, add accessors:
16
16
  attr_reader :harvest_start, :accumulator
17
-
17
+
18
+ # sets up busy calculations based on the start and end of
19
+ # transactions - used for a rough estimate of what percentage of
20
+ # wall clock time is spent processing requests
18
21
  def dispatcher_start(time)
19
22
  Thread.current[:busy_entries] ||= 0
20
23
  callers = Thread.current[:busy_entries] += 1
@@ -23,7 +26,10 @@ module NewRelic
23
26
  @entrypoint_stack.push time
24
27
  end
25
28
  end
26
-
29
+
30
+ # called when a transaction finishes, to add time to the
31
+ # instance variable accumulator. this is harvested when we send
32
+ # data to the server
27
33
  def dispatcher_finish(end_time = Time.now)
28
34
  callers = Thread.current[:busy_entries] -= 1
29
35
  # Ignore nested calls
@@ -36,7 +42,9 @@ module NewRelic
36
42
  end
37
43
  end
38
44
  end
39
-
45
+
46
+ # this returns the size of the entry point stack, which
47
+ # determines how many transactions are running
40
48
  def busy_count
41
49
  @entrypoint_stack.size
42
50
  end
@@ -1,5 +1,5 @@
1
- # This is used to allow obfuscators to be chained.
2
-
1
+ # This class is used by NewRelic::Agent.set_sql_obfuscator to chain multiple
2
+ # obfuscation blocks when not using the default :replace action
3
3
  class NewRelic::ChainedCall
4
4
  def initialize(block1, block2)
5
5
  @block1 = block1
@@ -1,203 +1,249 @@
1
-
2
1
  module NewRelic
3
2
  module Agent
4
- class ErrorCollector
5
- include NewRelic::CollectionHelper
6
-
7
- # Defined the methods that need to be stubbed out when the
8
- # agent is disabled
9
- module Shim #:nodoc:
10
- def notice_error(*args); end
11
- end
12
-
13
- MAX_ERROR_QUEUE_LENGTH = 20 unless defined? MAX_ERROR_QUEUE_LENGTH
14
-
15
- attr_accessor :enabled
16
- attr_reader :config_enabled
17
-
18
- def initialize
19
- @errors = []
20
- # lookup of exception class names to ignore. Hash for fast access
21
- @ignore = {}
22
- @ignore_filter = nil
23
-
24
- config = NewRelic::Control.instance.fetch('error_collector', {})
25
-
26
- @enabled = @config_enabled = config.fetch('enabled', true)
27
- @capture_source = config.fetch('capture_source', true)
28
-
29
- ignore_errors = config.fetch('ignore_errors', "")
30
- ignore_errors = ignore_errors.split(",") if ignore_errors.is_a? String
31
- ignore_errors.each { |error| error.strip! }
32
- ignore(ignore_errors)
33
- @lock = Mutex.new
34
- end
35
-
36
- def control
37
- NewRelic::Control.instance
38
- end
39
-
40
- def ignore_error_filter(&block)
41
- if block
42
- @ignore_filter = block
43
- else
44
- @ignore_filter
45
- end
46
- end
47
-
48
- # errors is an array of Exception Class Names
49
- #
50
- def ignore(errors)
51
- errors.each { |error| @ignore[error] = true; log.debug("Ignoring errors of type '#{error}'") }
52
- end
53
-
54
- module NoticeError
55
- def disabled?
56
- !@enabled
57
- end
58
-
59
- def filtered_by_error_filter?(error)
60
- return unless @ignore_filter
61
- !@ignore_filter.call(error)
62
- end
63
-
64
- def filtered_error?(error)
65
- @ignore[error.class.name] || filtered_by_error_filter?(error)
66
- end
67
-
68
- def error_is_ignored?(error)
69
- error && filtered_error?(error)
3
+ # This class collects errors from the parent application, storing
4
+ # them until they are harvested and transmitted to the server
5
+ class ErrorCollector
6
+ include NewRelic::CollectionHelper
7
+
8
+ # Defined the methods that need to be stubbed out when the
9
+ # agent is disabled
10
+ module Shim #:nodoc:
11
+ def notice_error(*args); end
12
+ end
13
+
14
+ # Maximum possible length of the queue - defaults to 20, may be
15
+ # made configurable in the future. This is a tradeoff between
16
+ # memory and data retention
17
+ MAX_ERROR_QUEUE_LENGTH = 20 unless defined? MAX_ERROR_QUEUE_LENGTH
18
+
19
+ attr_accessor :enabled
20
+ attr_reader :config_enabled
21
+
22
+ # Returns a new error collector
23
+ def initialize
24
+ @errors = []
25
+ # lookup of exception class names to ignore. Hash for fast access
26
+ @ignore = {}
27
+ @ignore_filter = nil
28
+
29
+ config = NewRelic::Control.instance.fetch('error_collector', {})
30
+
31
+ @enabled = @config_enabled = config.fetch('enabled', true)
32
+ @capture_source = config.fetch('capture_source', true)
33
+
34
+ ignore_errors = config.fetch('ignore_errors', "")
35
+ ignore_errors = ignore_errors.split(",") if ignore_errors.is_a? String
36
+ ignore_errors.each { |error| error.strip! }
37
+ ignore(ignore_errors)
38
+ @lock = Mutex.new
39
+ end
40
+
41
+ # Helper method to get the NewRelic::Control.instance
42
+ def control
43
+ NewRelic::Control.instance
44
+ end
45
+
46
+ # Returns the error filter proc that is used to check if an
47
+ # error should be reported. When given a block, resets the
48
+ # filter to the provided block
49
+ def ignore_error_filter(&block)
50
+ if block
51
+ @ignore_filter = block
52
+ else
53
+ @ignore_filter
54
+ end
70
55
  end
71
56
 
72
- def increment_error_count!
73
- NewRelic::Agent.get_stats("Errors/all").increment_count
57
+ # errors is an array of Exception Class Names
58
+ #
59
+ def ignore(errors)
60
+ errors.each { |error| @ignore[error] = true; log.debug("Ignoring errors of type '#{error}'") }
74
61
  end
75
-
76
- def should_exit_notice_error?(exception)
77
- if @enabled
78
- if !error_is_ignored?(exception)
79
- increment_error_count!
80
- return exception.nil?
62
+
63
+ # This module was extracted from the notice_error method - it is
64
+ # internally tested and can be refactored without major issues.
65
+ module NoticeError
66
+ # Whether the error collector is disabled or not
67
+ def disabled?
68
+ !@enabled
69
+ end
70
+
71
+ # Checks the provided error against the error filter, if there
72
+ # is an error filter
73
+ def filtered_by_error_filter?(error)
74
+ return unless @ignore_filter
75
+ !@ignore_filter.call(error)
76
+ end
77
+
78
+ # Checks the array of error names and the error filter against
79
+ # the provided error
80
+ def filtered_error?(error)
81
+ @ignore[error.class.name] || filtered_by_error_filter?(error)
82
+ end
83
+
84
+ # an error is ignored if it is nil or if it is filtered
85
+ def error_is_ignored?(error)
86
+ error && filtered_error?(error)
87
+ end
88
+
89
+ # Increments a statistic that tracks total error rate
90
+ def increment_error_count!
91
+ NewRelic::Agent.get_stats("Errors/all").increment_count
92
+ end
93
+
94
+ # whether we should return early from the notice_error process
95
+ # - based on whether the error is ignored or the error
96
+ # collector is disabled
97
+ def should_exit_notice_error?(exception)
98
+ if @enabled
99
+ if !error_is_ignored?(exception)
100
+ increment_error_count!
101
+ return exception.nil? # exit early if the exception is nil
102
+ end
81
103
  end
104
+ # disabled or an ignored error, per above
105
+ true
82
106
  end
83
- # disabled or an ignored error, per above
84
- true
85
- end
86
-
87
- def fetch_from_options(options, key, default=nil)
88
- options.delete(key) || default
89
- end
90
-
91
- def uri_ref_and_root(options)
92
- {
93
- :request_uri => fetch_from_options(options, :uri, ''),
94
- :request_referer => fetch_from_options(options, :referer, ''),
95
- :rails_root => control.root
96
- }
97
- end
98
-
99
- def custom_params_from_opts(options)
100
- # If anything else is left over, treat it like a custom param:
101
- fetch_from_options(options, :custom_params, {}).merge(options)
102
- end
103
-
104
- def request_params_from_opts(options)
105
- value = options.delete(:request_params)
106
- if control.capture_params
107
- value
108
- else
109
- nil
107
+
108
+ # acts just like Hash#fetch, but deletes the key from the hash
109
+ def fetch_from_options(options, key, default=nil)
110
+ options.delete(key) || default
111
+ end
112
+
113
+ # returns some basic option defaults pulled from the provided
114
+ # options hash
115
+ def uri_ref_and_root(options)
116
+ {
117
+ :request_uri => fetch_from_options(options, :uri, ''),
118
+ :request_referer => fetch_from_options(options, :referer, ''),
119
+ :rails_root => control.root
120
+ }
121
+ end
122
+
123
+ # If anything else is left over, we treat it like a custom param
124
+ def custom_params_from_opts(options)
125
+ # If anything else is left over, treat it like a custom param:
126
+ fetch_from_options(options, :custom_params, {}).merge(options)
127
+ end
128
+
129
+ # takes the request parameters out of the options hash, and
130
+ # returns them if we are capturing parameters, otherwise
131
+ # returns nil
132
+ def request_params_from_opts(options)
133
+ value = options.delete(:request_params)
134
+ if control.capture_params
135
+ value
136
+ else
137
+ nil
138
+ end
139
+ end
140
+
141
+ # normalizes the request and custom parameters before attaching
142
+ # them to the error. See NewRelic::CollectionHelper#normalize_params
143
+ def normalized_request_and_custom_params(options)
144
+ {
145
+ :request_params => normalize_params(request_params_from_opts(options)),
146
+ :custom_params => normalize_params(custom_params_from_opts(options))
147
+ }
148
+ end
149
+
150
+ # Merges together many of the options into something that can
151
+ # actually be attached to the error
152
+ def error_params_from_options(options)
153
+ uri_ref_and_root(options).merge(normalized_request_and_custom_params(options))
154
+ end
155
+
156
+ # calls a method on an object, if it responds to it - used for
157
+ # detection and soft fail-safe. Returns nil if the method does
158
+ # not exist
159
+ def sense_method(object, method)
160
+ object.send(method) if object.respond_to?(method)
161
+ end
162
+
163
+ # extracts source from the exception, if the exception supports
164
+ # that method
165
+ def extract_source(exception)
166
+ sense_method(exception, 'source_extract') if @capture_source
167
+ end
168
+
169
+ # extracts a stack trace from the exception for debugging purposes
170
+ def extract_stack_trace(exception)
171
+ actual_exception = sense_method(exception, 'original_exception') || exception
172
+ sense_method(actual_exception, 'backtrace') || '<no stack trace>'
173
+ end
174
+
175
+ # extracts a bunch of information from the exception to include
176
+ # in the noticed error - some may or may not be available, but
177
+ # we try to include all of it
178
+ def exception_info(exception)
179
+ {
180
+ :file_name => sense_method(exception, 'file_name'),
181
+ :line_number => sense_method(exception, 'line_number'),
182
+ :source => extract_source(exception),
183
+ :stack_trace => extract_stack_trace(exception)
184
+ }
185
+ end
186
+
187
+ # checks the size of the error queue to make sure we are under
188
+ # the maximum limit, and logs a warning if we are over the limit.
189
+ def over_queue_limit?(message)
190
+ over_limit = (@errors.length >= MAX_ERROR_QUEUE_LENGTH)
191
+ log.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
192
+ over_limit
110
193
  end
111
- end
112
-
113
- def normalized_request_and_custom_params(options)
114
- {
115
- :request_params => normalize_params(request_params_from_opts(options)),
116
- :custom_params => normalize_params(custom_params_from_opts(options))
117
- }
118
- end
119
-
120
- def error_params_from_options(options)
121
- uri_ref_and_root(options).merge(normalized_request_and_custom_params(options))
122
- end
123
-
124
- def sense_method(object, method)
125
- object.send(method) if object.respond_to?(method)
126
- end
127
-
128
- def extract_source(exception)
129
- sense_method(exception, 'source_extract') if @capture_source
130
- end
131
-
132
- def extract_stack_trace(exception)
133
- actual_exception = sense_method(exception, 'original_exception') || exception
134
- sense_method(actual_exception, 'backtrace') || '<no stack trace>'
135
- end
136
-
137
- def exception_info(exception)
138
- {
139
- :file_name => sense_method(exception, 'file_name'),
140
- :line_number => sense_method(exception, 'line_number'),
141
- :source => extract_source(exception),
142
- :stack_trace => extract_stack_trace(exception)
143
- }
144
- end
145
-
146
- def over_queue_limit?(message)
147
- over_limit = (@errors.length >= MAX_ERROR_QUEUE_LENGTH)
148
- log.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
149
- over_limit
150
- end
151
-
152
194
 
153
- def add_to_error_queue(noticed_error)
154
- @lock.synchronize do
155
- @errors << noticed_error unless over_queue_limit?(noticed_error.message)
195
+
196
+ # Synchronizes adding an error to the error queue, and checks if
197
+ # the error queue is too long - if so, we drop the error on the
198
+ # floor after logging a warning.
199
+ def add_to_error_queue(noticed_error)
200
+ @lock.synchronize do
201
+ @errors << noticed_error unless over_queue_limit?(noticed_error.message)
202
+ end
156
203
  end
157
204
  end
158
- end
159
-
160
- include NoticeError
161
205
 
162
- # Notice the error with the given available options:
163
- #
164
- # * <tt>:uri</tt> => The request path, minus any request params or query string.
165
- # * <tt>:referer</tt> => The URI of the referer
166
- # * <tt>:metric</tt> => The metric name associated with the transaction
167
- # * <tt>:request_params</tt> => Request parameters, already filtered if necessary
168
- # * <tt>:custom_params</tt> => Custom parameters
169
- #
170
- # If anything is left over, it's added to custom params
171
- # If exception is nil, the error count is bumped and no traced error is recorded
172
- def notice_error(exception, options={})
173
- return if should_exit_notice_error?(exception)
174
- action_path = fetch_from_options(options, :metric, (NewRelic::Agent.instance.stats_engine.scope_name || ''))
175
- exception_options = error_params_from_options(options).merge(exception_info(exception))
176
- add_to_error_queue(NewRelic::NoticedError.new(action_path, exception_options, exception))
177
- exception
178
- rescue Exception => e
179
- log.error("Error capturing an error, yodawg. #{e}")
180
- end
206
+ include NoticeError
207
+
208
+ # Notice the error with the given available options:
209
+ #
210
+ # * <tt>:uri</tt> => The request path, minus any request params or query string.
211
+ # * <tt>:referer</tt> => The URI of the referer
212
+ # * <tt>:metric</tt> => The metric name associated with the transaction
213
+ # * <tt>:request_params</tt> => Request parameters, already filtered if necessary
214
+ # * <tt>:custom_params</tt> => Custom parameters
215
+ #
216
+ # If anything is left over, it's added to custom params
217
+ # If exception is nil, the error count is bumped and no traced error is recorded
218
+ def notice_error(exception, options={})
219
+ return if should_exit_notice_error?(exception)
220
+ action_path = fetch_from_options(options, :metric, (NewRelic::Agent.instance.stats_engine.scope_name || ''))
221
+ exception_options = error_params_from_options(options).merge(exception_info(exception))
222
+ add_to_error_queue(NewRelic::NoticedError.new(action_path, exception_options, exception))
223
+ exception
224
+ rescue Exception => e
225
+ log.error("Error capturing an error, yodawg. #{e}")
226
+ end
227
+
228
+ # Get the errors currently queued up. Unsent errors are left
229
+ # over from a previous unsuccessful attempt to send them to the server.
230
+ def harvest_errors(unsent_errors)
231
+ @lock.synchronize do
232
+ errors = @errors
233
+ @errors = []
181
234
 
182
- # Get the errors currently queued up. Unsent errors are left
183
- # over from a previous unsuccessful attempt to send them to the server.
184
- def harvest_errors(unsent_errors)
185
- @lock.synchronize do
186
- errors = @errors
187
- @errors = []
235
+ if unsent_errors && !unsent_errors.empty?
236
+ errors = unsent_errors + errors
237
+ end
188
238
 
189
- if unsent_errors && !unsent_errors.empty?
190
- errors = unsent_errors + errors
239
+ errors
191
240
  end
192
-
193
- errors
194
241
  end
195
- end
196
242
 
197
- private
198
- def log
199
- NewRelic::Agent.logger
243
+ private
244
+ def log
245
+ NewRelic::Agent.logger
246
+ end
200
247
  end
201
248
  end
202
249
  end
203
- end