onyx_newrelic_rpm 2.12.5

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