honkster-newrelic_rpm 2.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. data/CHANGELOG +462 -0
  2. data/LICENSE +37 -0
  3. data/README.rdoc +172 -0
  4. data/bin/mongrel_rpm +33 -0
  5. data/bin/newrelic +13 -0
  6. data/bin/newrelic_cmd +5 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +9 -0
  9. data/lib/new_relic/agent.rb +382 -0
  10. data/lib/new_relic/agent/agent.rb +741 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/error_collector.rb +131 -0
  14. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  15. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +92 -0
  16. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +45 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +409 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +58 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +22 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +40 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +9 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +319 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +17 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +22 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  28. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  30. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  31. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +45 -0
  32. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +21 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -0
  34. data/lib/new_relic/agent/instrumentation/sunspot.rb +17 -0
  35. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +10 -0
  36. data/lib/new_relic/agent/method_tracer.rb +350 -0
  37. data/lib/new_relic/agent/sampler.rb +50 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +54 -0
  39. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +37 -0
  40. data/lib/new_relic/agent/samplers/memory_sampler.rb +142 -0
  41. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  42. data/lib/new_relic/agent/shim_agent.rb +25 -0
  43. data/lib/new_relic/agent/stats_engine.rb +24 -0
  44. data/lib/new_relic/agent/stats_engine/metric_stats.rb +118 -0
  45. data/lib/new_relic/agent/stats_engine/samplers.rb +83 -0
  46. data/lib/new_relic/agent/stats_engine/transactions.rb +149 -0
  47. data/lib/new_relic/agent/transaction_sampler.rb +330 -0
  48. data/lib/new_relic/agent/worker_loop.rb +81 -0
  49. data/lib/new_relic/collection_helper.rb +71 -0
  50. data/lib/new_relic/command.rb +85 -0
  51. data/lib/new_relic/commands/deployments.rb +105 -0
  52. data/lib/new_relic/commands/install.rb +81 -0
  53. data/lib/new_relic/control.rb +203 -0
  54. data/lib/new_relic/control/configuration.rb +149 -0
  55. data/lib/new_relic/control/frameworks/external.rb +13 -0
  56. data/lib/new_relic/control/frameworks/merb.rb +24 -0
  57. data/lib/new_relic/control/frameworks/rails.rb +126 -0
  58. data/lib/new_relic/control/frameworks/rails3.rb +60 -0
  59. data/lib/new_relic/control/frameworks/ruby.rb +36 -0
  60. data/lib/new_relic/control/frameworks/sinatra.rb +18 -0
  61. data/lib/new_relic/control/instrumentation.rb +95 -0
  62. data/lib/new_relic/control/logging_methods.rb +74 -0
  63. data/lib/new_relic/control/profiling.rb +24 -0
  64. data/lib/new_relic/control/server_methods.rb +88 -0
  65. data/lib/new_relic/delayed_job_injection.rb +27 -0
  66. data/lib/new_relic/histogram.rb +91 -0
  67. data/lib/new_relic/local_environment.rb +333 -0
  68. data/lib/new_relic/merbtasks.rb +6 -0
  69. data/lib/new_relic/metric_data.rb +42 -0
  70. data/lib/new_relic/metric_parser.rb +136 -0
  71. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  72. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  73. data/lib/new_relic/metric_parser/active_record.rb +28 -0
  74. data/lib/new_relic/metric_parser/apdex.rb +88 -0
  75. data/lib/new_relic/metric_parser/controller.rb +62 -0
  76. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  77. data/lib/new_relic/metric_parser/errors.rb +6 -0
  78. data/lib/new_relic/metric_parser/external.rb +50 -0
  79. data/lib/new_relic/metric_parser/mem_cache.rb +50 -0
  80. data/lib/new_relic/metric_parser/other_transaction.rb +36 -0
  81. data/lib/new_relic/metric_parser/view.rb +61 -0
  82. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  83. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  84. data/lib/new_relic/metric_spec.rb +67 -0
  85. data/lib/new_relic/metrics.rb +9 -0
  86. data/lib/new_relic/noticed_error.rb +24 -0
  87. data/lib/new_relic/rack/developer_mode.rb +257 -0
  88. data/lib/new_relic/rack/metric_app.rb +64 -0
  89. data/lib/new_relic/rack/mongrel_rpm.ru +26 -0
  90. data/lib/new_relic/rack/newrelic.yml +27 -0
  91. data/lib/new_relic/rack_app.rb +6 -0
  92. data/lib/new_relic/recipes.rb +82 -0
  93. data/lib/new_relic/stats.rb +368 -0
  94. data/lib/new_relic/timer_lib.rb +27 -0
  95. data/lib/new_relic/transaction_analysis.rb +119 -0
  96. data/lib/new_relic/transaction_sample.rb +586 -0
  97. data/lib/new_relic/url_rule.rb +14 -0
  98. data/lib/new_relic/version.rb +55 -0
  99. data/lib/new_relic_api.rb +276 -0
  100. data/lib/newrelic_rpm.rb +49 -0
  101. data/lib/tasks/all.rb +4 -0
  102. data/lib/tasks/install.rake +7 -0
  103. data/lib/tasks/tests.rake +15 -0
  104. data/newrelic.yml +246 -0
  105. data/newrelic_rpm.gemspec +254 -0
  106. data/recipes/newrelic.rb +6 -0
  107. data/test/active_record_fixtures.rb +55 -0
  108. data/test/config/newrelic.yml +48 -0
  109. data/test/config/test_control.rb +36 -0
  110. data/test/new_relic/agent/active_record_instrumentation_test.rb +286 -0
  111. data/test/new_relic/agent/agent_controller_test.rb +294 -0
  112. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  113. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  114. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  115. data/test/new_relic/agent/error_collector_test.rb +163 -0
  116. data/test/new_relic/agent/memcache_instrumentation_test.rb +103 -0
  117. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  118. data/test/new_relic/agent/metric_data_test.rb +53 -0
  119. data/test/new_relic/agent/metric_frame_test.rb +51 -0
  120. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  121. data/test/new_relic/agent/net_instrumentation_test.rb +77 -0
  122. data/test/new_relic/agent/rpm_agent_test.rb +142 -0
  123. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  124. data/test/new_relic/agent/stats_engine/samplers_test.rb +72 -0
  125. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  126. data/test/new_relic/agent/task_instrumentation_test.rb +188 -0
  127. data/test/new_relic/agent/testable_agent.rb +13 -0
  128. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  129. data/test/new_relic/agent/transaction_sample_test.rb +192 -0
  130. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  131. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  132. data/test/new_relic/control_test.rb +127 -0
  133. data/test/new_relic/deployments_api_test.rb +69 -0
  134. data/test/new_relic/environment_test.rb +75 -0
  135. data/test/new_relic/metric_parser_test.rb +226 -0
  136. data/test/new_relic/metric_spec_test.rb +177 -0
  137. data/test/new_relic/rack/episodes_test.rb +318 -0
  138. data/test/new_relic/shim_agent_test.rb +9 -0
  139. data/test/new_relic/stats_test.rb +312 -0
  140. data/test/new_relic/version_number_test.rb +89 -0
  141. data/test/test_contexts.rb +28 -0
  142. data/test/test_helper.rb +72 -0
  143. data/ui/helpers/developer_mode_helper.rb +359 -0
  144. data/ui/helpers/google_pie_chart.rb +49 -0
  145. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  146. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  147. data/ui/views/newrelic/_sample.rhtml +20 -0
  148. data/ui/views/newrelic/_segment.rhtml +29 -0
  149. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  150. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  151. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  152. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  153. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  154. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  155. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  156. data/ui/views/newrelic/_table.rhtml +12 -0
  157. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  158. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  159. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  160. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  161. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  162. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  163. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  164. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  165. data/ui/views/newrelic/file/images/textmate.png +0 -0
  166. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  167. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  168. data/ui/views/newrelic/file/stylesheets/style.css +484 -0
  169. data/ui/views/newrelic/index.rhtml +59 -0
  170. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  171. data/ui/views/newrelic/show_sample.rhtml +79 -0
  172. data/ui/views/newrelic/show_source.rhtml +3 -0
  173. data/ui/views/newrelic/threads.rhtml +52 -0
  174. metadata +307 -0
@@ -0,0 +1,58 @@
1
+
2
+ # NewRelic instrumentation for DataMapper
3
+ # For now, we have to refer to all db metrics as "ActiveRecord"
4
+ if defined? DataMapper
5
+
6
+ DataMapper::Model.class_eval do
7
+ add_method_tracer :get, 'ActiveRecord/#{self.name}/find'
8
+ add_method_tracer :first, 'ActiveRecord/#{self.name}/find'
9
+ add_method_tracer :first_or_create, 'ActiveRecord/#{self.name}/find'
10
+ add_method_tracer :all, 'ActiveRecord/#{self.name}/find_all'
11
+ end
12
+ DataMapper::Resource.class_eval do
13
+
14
+ @@my_sql_defined = defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
15
+ @@postgres_defined = defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
16
+
17
+ for method in [:query] do
18
+ add_method_tracer method, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/execute'
19
+ add_method_tracer method, 'ActiveRecord/all', :push_scope => false
20
+ end
21
+ for method in [:update, :save] do
22
+ add_method_tracer method, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/save'
23
+ add_method_tracer method, 'ActiveRecord/save', :push_scope => false
24
+ end
25
+ add_method_tracer :destroy, 'ActiveRecord/#{self.class.name[/[^:]*$/]}/destroy'
26
+ add_method_tracer :destroy, 'ActiveRecord/destroy', :push_scope => false
27
+
28
+ def log_with_newrelic_instrumentation(sql, name, &block)
29
+ # if we aren't in a blamed context, then add one so that we can
30
+ # see that controllers are calling SQL directly we check
31
+ # scope_depth vs 2 since the controller is 1
32
+ if NewRelic::Agent.instance.transaction_sampler.scope_depth < 2
33
+ self.class.trace_method_execution "Database/DirectSQL", true, true do
34
+ log_with_capture_sql(sql, name, &block)
35
+ end
36
+ else
37
+ log_with_capture_sql(sql, name, &block)
38
+ end
39
+ end
40
+
41
+ def log_with_capture_sql(sql, name, &block)
42
+ if @@my_sql_defined && self.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
43
+ config = @config
44
+ elsif @@postgres_defined && self.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
45
+ config = @config
46
+ else
47
+ config = nil
48
+ end
49
+
50
+ t0 = Time.now
51
+ result = log_without_newrelic_instrumentation(sql, name, &block)
52
+
53
+ NewRelic::Agent.instance.transaction_sampler.notice_sql(sql, config, (Time.now - t0).to_f)
54
+
55
+ result
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ require 'new_relic/agent/instrumentation/controller_instrumentation'
2
+
3
+ if defined?(Delayed::Job) and not NewRelic::Control.instance['disable_dj']
4
+ module NewRelic
5
+ module Agent
6
+ module Instrumentation
7
+ module DelayedJobInstrumentation
8
+
9
+ Delayed::Job.class_eval do
10
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
11
+ if self.instance_methods.include?('name')
12
+ add_transaction_tracer "invoke_job", :category => 'OtherTransaction/DelayedJob', :path => '#{self.name}'
13
+ else
14
+ add_transaction_tracer "invoke_job", :category => 'OtherTransaction/DelayedJob'
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ # NOTE there are multiple implementations of the MemCache client in Ruby,
2
+ # each with slightly different API's and semantics.
3
+ # See:
4
+ # http://www.deveiate.org/code/Ruby-MemCache/ (Gem: Ruby-MemCache)
5
+ # http://seattlerb.rubyforge.org/memcache-client/ (Gem: memcache-client)
6
+ unless NewRelic::Control.instance['disable_memcache_instrumentation']
7
+
8
+ def self.instrument_method(the_class, method_name)
9
+ return unless the_class.method_defined? method_name.to_sym
10
+ the_class.class_eval <<-EOD
11
+ def #{method_name}_with_newrelic_trace(*args)
12
+ metrics = ["MemCache/#{method_name}",
13
+ (NewRelic::Agent::Instrumentation::MetricFrame.recording_web_transaction? ? 'MemCache/allWeb' : 'MemCache/allOther')]
14
+ self.class.trace_execution_scoped(metrics) do
15
+ t0 = Time.now
16
+ begin
17
+ #{method_name}_without_newrelic_trace(*args)
18
+ ensure
19
+ #{memcache_key_snippet(method_name)}
20
+ end
21
+ end
22
+ end
23
+ alias #{method_name}_without_newrelic_trace #{method_name}
24
+ alias #{method_name} #{method_name}_with_newrelic_trace
25
+ EOD
26
+ end
27
+
28
+ def self.memcache_key_snippet(method_name)
29
+ return "" unless NewRelic::Control.instance['capture_memcache_keys']
30
+ "NewRelic::Agent.instance.transaction_sampler.notice_nosql(args.first.inspect, (Time.now - t0).to_f) rescue nil"
31
+ end
32
+
33
+
34
+
35
+ %w[get get_multi set add incr decr delete replace append prepand cas].each do | method_name |
36
+ instrument_method(::MemCache, method_name) if defined? ::MemCache
37
+ instrument_method(::Memcached, method_name) if defined? ::Memcached
38
+ end
39
+
40
+ end
@@ -0,0 +1,26 @@
1
+ require 'set'
2
+ require 'merb-core/controller/merb_controller'
3
+
4
+ Merb::Controller.class_eval do
5
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
6
+
7
+ class_inheritable_accessor :do_not_trace
8
+ class_inheritable_accessor :ignore_apdex
9
+
10
+ def self.newrelic_write_attr(attr_name, value) # :nodoc:
11
+ self.send "#{attr_name}=", attr_name, value
12
+ end
13
+
14
+ def self.newrelic_read_attr(attr_name) # :nodoc:
15
+ self.send attr_name
16
+ end
17
+
18
+ protected
19
+ # determine the path that is used in the metric name for
20
+ # the called controller action
21
+ def newrelic_metric_path
22
+ "#{controller_name}/#{action_name}"
23
+ end
24
+ alias_method :perform_action_without_newrelic_trace, :_dispatch
25
+ alias_method :_dispatch, :perform_action_with_newrelic_trace
26
+ end
@@ -0,0 +1,9 @@
1
+ # Hook in the notification to merb
2
+ error_notifier = Proc.new {
3
+ if request.exceptions #check that there's actually an exception
4
+ # Note, this assumes we have already captured the other information such as uri and params in the MetricFrame.
5
+ NewRelic::Agent::Instrumentation::MetricFrame.notice_error(request.exceptions.first)
6
+ end
7
+ }
8
+ Merb::Dispatcher::DefaultException.before error_notifier
9
+ Exceptions.before error_notifier
@@ -0,0 +1,319 @@
1
+ # A struct holding the information required to measure a controller
2
+ # action. This is put on the thread local. Handles the issue of
3
+ # re-entrancy, or nested action calls.
4
+ #
5
+ # This class is not part of the public API. Avoid making calls on it directly.
6
+ #
7
+ module NewRelic
8
+ module Agent
9
+ module Instrumentation
10
+ class MetricFrame
11
+ attr_accessor :start # A Time instance for the start time, never nil
12
+ attr_accessor :apdex_start # A Time instance used for calculating the apdex score, which
13
+ # might end up being @start, or it might be further upstream if
14
+ # we can find a request header for the queue entry time
15
+ attr_accessor :exception,
16
+ :filtered_params, :force_flag,
17
+ :jruby_cpu_start, :process_cpu_start, :database_metric_name
18
+
19
+ # Give the current metric frame a request context. Use this to
20
+ # get the URI and referer. The request is interpreted loosely
21
+ # as a Rack::Request or an ActionController::AbstractRequest.
22
+ attr_accessor :request
23
+
24
+
25
+ @@check_server_connection = false
26
+ def self.check_server_connection=(value)
27
+ @@check_server_connection = value
28
+ end
29
+ # Return the currently active metric frame, or nil. Call with +true+
30
+ # to create a new metric frame if one is not already on the thread.
31
+ def self.current(create_if_empty=nil)
32
+ f = Thread.current[:newrelic_metric_frame]
33
+ return f if f || !create_if_empty
34
+
35
+ # Reconnect to the server if necessary. This is only done
36
+ # for old versions of passenger that don't implement an explicit after_fork
37
+ # event.
38
+ NewRelic::Agent.instance.after_fork(:keep_retrying => false) if @@check_server_connection
39
+
40
+ Thread.current[:newrelic_metric_frame] = new
41
+ end
42
+
43
+ # This is the name of the model currently assigned to database
44
+ # measurements, overriding the default.
45
+ def self.database_metric_name
46
+ current && current.database_metric_name
47
+ end
48
+
49
+ def self.referer
50
+ current && current.referer
51
+ end
52
+
53
+ @@java_classes_loaded = false
54
+
55
+ if defined? JRuby
56
+ begin
57
+ require 'java'
58
+ include_class 'java.lang.management.ManagementFactory'
59
+ include_class 'com.sun.management.OperatingSystemMXBean'
60
+ @@java_classes_loaded = true
61
+ rescue Exception => e
62
+ end
63
+ end
64
+
65
+ attr_reader :depth
66
+
67
+ def initialize
68
+ @start = Time.now
69
+ @path_stack = [] # stack of [controller, path] elements
70
+ @jruby_cpu_start = jruby_cpu_time
71
+ @process_cpu_start = process_cpu
72
+ end
73
+
74
+ # Indicate that we are entering a measured controller action or task.
75
+ # Make sure you unwind every push with a pop call.
76
+ def push(m)
77
+ NewRelic::Agent.instance.transaction_sampler.notice_first_scope_push(start)
78
+ @path_stack.push NewRelic::MetricParser.for_metric_named(m)
79
+ end
80
+
81
+ # Indicate that you don't want to keep the currently saved transaction
82
+ # information
83
+ def self.abort_transaction!
84
+ current.abort_transaction! if current
85
+ end
86
+
87
+ # For the current web transaction, return the path of the URI minus the host part and query string, or nil.
88
+ def uri
89
+ @uri ||= self.class.uri_from_request(@request) unless @request.nil?
90
+ end
91
+
92
+ # For the current web transaction, return the full referer, minus the host string, or nil.
93
+ def referer
94
+ @referer ||= self.class.referer_from_request(@request)
95
+ end
96
+
97
+ # Call this to ensure that the current transaction is not saved
98
+ def abort_transaction!
99
+ NewRelic::Agent.instance.transaction_sampler.ignore_transaction
100
+ end
101
+ # This needs to be called after entering the call to trace the controller action, otherwise
102
+ # the controller action blames itself. It gets reset in the normal #pop call.
103
+ def start_transaction
104
+ NewRelic::Agent.instance.stats_engine.start_transaction metric_name
105
+ # Only push the transaction context info once, on entry:
106
+ if @path_stack.size == 1
107
+ NewRelic::Agent.instance.transaction_sampler.notice_transaction(metric_name, uri, filtered_params)
108
+ end
109
+ end
110
+
111
+ def current_metric
112
+ @path_stack.last
113
+ end
114
+
115
+ # Return the path, the part of the metric after the category
116
+ def path
117
+ @path_stack.last.last
118
+ end
119
+
120
+ # Unwind one stack level. It knows if it's back at the outermost caller and
121
+ # does the appropriate wrapup of the context.
122
+ def pop
123
+ metric = @path_stack.pop
124
+ if metric.nil?
125
+ NewRelic::Agent.logger.error "Underflow in metric frames: #{caller.join("\n ")}"
126
+ end
127
+ if @path_stack.empty?
128
+ if NewRelic::Agent.is_execution_traced?
129
+ cpu_burn = nil
130
+ if @process_cpu_start
131
+ cpu_burn = process_cpu - @process_cpu_start
132
+ elsif @jruby_cpu_start
133
+ cpu_burn = jruby_cpu_time - @jruby_cpu_start
134
+ NewRelic::Agent.get_stats_no_scope(NewRelic::Metrics::USER_TIME).record_data_point(cpu_burn)
135
+ end
136
+ NewRelic::Agent.instance.transaction_sampler.notice_transaction_cpu_time(cpu_burn) if cpu_burn
137
+ NewRelic::Agent.instance.histogram.process((Time.now - start).to_f) if metric.is_web_transaction?
138
+ NewRelic::Agent.instance.transaction_sampler.notice_scope_empty
139
+ end
140
+ NewRelic::Agent.instance.stats_engine.end_transaction
141
+ Thread.current[:newrelic_metric_frame] = nil
142
+ else # path stack not empty
143
+ # change the transaction name back to whatever was on the stack.
144
+ NewRelic::Agent.instance.stats_engine.scope_name = metric_name
145
+ end
146
+ end
147
+
148
+ # If we have an active metric frame, notice the error and increment the error metric.
149
+ # Options:
150
+ # * <tt>:request</tt> => Request object to get the uri and referer
151
+ # * <tt>:uri</tt> => The request path, minus any request params or query string.
152
+ # * <tt>:referer</tt> => The URI of the referer
153
+ # * <tt>:metric</tt> => The metric name associated with the transaction
154
+ # * <tt>:request_params</tt> => Request parameters, already filtered if necessary
155
+ # * <tt>:custom_params</tt> => Custom parameters
156
+ # Anything left over is treated as custom params
157
+
158
+ def self.notice_error(e, options={})
159
+ if request = options.delete(:request)
160
+ options[:referer] = referer_from_request(request)
161
+ options[:uri] = uri_from_request(request)
162
+ end
163
+ if current
164
+ current.notice_error(e, options)
165
+ else
166
+ NewRelic::Agent.instance.error_collector.notice_error(e, options)
167
+ end
168
+ end
169
+
170
+ # Do not call this. Invoke the class method instead.
171
+ def notice_error(e, options={}) # :nodoc:
172
+ params = custom_parameters
173
+ options[:referer] = referer if referer
174
+ options[:request_params] = filtered_params if filtered_params
175
+ options[:uri] = uri if uri
176
+ options[:metric] = metric_name
177
+ options.merge!(custom_parameters)
178
+ if exception != e
179
+ result = NewRelic::Agent.instance.error_collector.notice_error(e, options)
180
+ self.exception = result if result
181
+ end
182
+ end
183
+
184
+ # Add context parameters to the metric frame. This information will be passed in to errors
185
+ # and transaction traces. Keys and Values should be strings, numbers or date/times.
186
+ def self.add_custom_parameters(p)
187
+ current.add_custom_parameters(p) if current
188
+ end
189
+
190
+ def self.custom_parameters
191
+ (current && current.custom_parameters) ? current.custom_parameters : {}
192
+ end
193
+
194
+ def record_apdex()
195
+ return unless recording_web_transaction? && NewRelic::Agent.is_execution_traced?
196
+ t = Time.now
197
+ self.class.record_apdex(current_metric, t - start, t - apdex_start, !exception.nil?)
198
+ end
199
+
200
+ def metric_name
201
+ return nil if @path_stack.empty?
202
+ current_metric.name
203
+ end
204
+
205
+ # Return the array of metrics to record for the current metric frame.
206
+ def recorded_metrics
207
+ metrics = [ metric_name ]
208
+ metrics += current_metric.summary_metrics if @path_stack.size == 1
209
+ metrics
210
+ end
211
+
212
+ # Yield to a block that is run with a database metric name context. This means
213
+ # the Database instrumentation will use this for the metric name if it does not
214
+ # otherwise know about a model. This is re-entrant.
215
+ #
216
+ # * <tt>model</tt> is the DB model class
217
+ # * <tt>method</tt> is the name of the finder method or other method to identify the operation with.
218
+ #
219
+ def with_database_metric_name(model, method)
220
+ previous = @database_metric_name
221
+ model_name = case model
222
+ when Class
223
+ model.name
224
+ when String
225
+ model
226
+ else
227
+ model.to_s
228
+ end
229
+ @database_metric_name = "ActiveRecord/#{model_name}/#{method}"
230
+ yield
231
+ ensure
232
+ @database_metric_name=previous
233
+ end
234
+
235
+ def custom_parameters
236
+ @custom_parameters ||= {}
237
+ end
238
+
239
+ def add_custom_parameters(p)
240
+ custom_parameters.merge!(p)
241
+ end
242
+
243
+ def self.recording_web_transaction?
244
+ if c = Thread.current[:newrelic_metric_frame]
245
+ c.recording_web_transaction?
246
+ end
247
+ end
248
+
249
+ def recording_web_transaction?
250
+ current_metric && current_metric.is_web_transaction?
251
+ end
252
+
253
+ def is_web_transaction?(metric)
254
+ 0 == metric.index("Controller")
255
+ end
256
+
257
+ # Make a safe attempt to get the referer from a request object, generally successful when
258
+ # it's a Rack request.
259
+ def self.referer_from_request(request)
260
+ if request && request.respond_to?(:referer)
261
+ request.referer.to_s.split('?').first
262
+ end
263
+ end
264
+
265
+ # Make a safe attempt to get the URI, without the host and query string.
266
+ def self.uri_from_request(request)
267
+ approximate_uri = case
268
+ when request.respond_to?(:fullpath) then request.fullpath
269
+ when request.respond_to?(:path) then request.path
270
+ when request.respond_to?(:request_uri) then request.request_uri
271
+ when request.respond_to?(:uri) then request.uri
272
+ when request.respond_to?(:url) then request.url
273
+ end
274
+ return approximate_uri[%r{^(https?://.*?)?(/[^?]*)}, 2] || '/' if approximate_uri # '
275
+ end
276
+
277
+ def self.record_apdex(current_metric, action_duration, total_duration, is_error)
278
+ summary_stat = NewRelic::Agent.instance.stats_engine.get_custom_stats("Apdex", NewRelic::ApdexStats)
279
+ controller_stat = NewRelic::Agent.instance.stats_engine.get_custom_stats(current_metric.apdex_metric_path, NewRelic::ApdexStats)
280
+ update_apdex(summary_stat, total_duration, is_error)
281
+ update_apdex(controller_stat, action_duration, is_error)
282
+ end
283
+
284
+ # Record an apdex value for the given stat. when `failed`
285
+ # the apdex should be recorded as a failure regardless of duration.
286
+ def self.update_apdex(stat, duration, failed)
287
+ duration = duration.to_f
288
+ apdex_t = NewRelic::Control.instance.apdex_t
289
+ case
290
+ when failed
291
+ stat.record_apdex_f
292
+ when duration <= apdex_t
293
+ stat.record_apdex_s
294
+ when duration <= 4 * apdex_t
295
+ stat.record_apdex_t
296
+ else
297
+ stat.record_apdex_f
298
+ end
299
+ end
300
+
301
+ private
302
+
303
+ def process_cpu
304
+ return nil if defined? JRuby
305
+ p = Process.times
306
+ p.stime + p.utime
307
+ end
308
+
309
+ def jruby_cpu_time # :nodoc:
310
+ return nil unless @@java_classes_loaded
311
+ threadMBean = ManagementFactory.getThreadMXBean()
312
+ java_utime = threadMBean.getCurrentThreadUserTime() # ns
313
+ -1 == java_utime ? 0.0 : java_utime/1e9
314
+ end
315
+
316
+ end
317
+ end
318
+ end
319
+ end