factorylabs-newrelic_rpm 2.10.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/CHANGELOG +354 -0
  2. data/LICENSE +37 -0
  3. data/README-2.10.2.2 +10 -0
  4. data/README.md +138 -0
  5. data/bin/mongrel_rpm +33 -0
  6. data/bin/newrelic_cmd +4 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +45 -0
  9. data/lib/new_relic/agent.rb +315 -0
  10. data/lib/new_relic/agent/agent.rb +647 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +86 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/collection_helper.rb +66 -0
  14. data/lib/new_relic/agent/error_collector.rb +117 -0
  15. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  16. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +91 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +389 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +12 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +199 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +21 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +109 -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/sinatra.rb +39 -0
  32. data/lib/new_relic/agent/method_tracer.rb +348 -0
  33. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  34. data/lib/new_relic/agent/sampler.rb +46 -0
  35. data/lib/new_relic/agent/samplers/cpu_sampler.rb +50 -0
  36. data/lib/new_relic/agent/samplers/memory_sampler.rb +141 -0
  37. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +23 -0
  38. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  39. data/lib/new_relic/agent/shim_agent.rb +21 -0
  40. data/lib/new_relic/agent/stats_engine.rb +22 -0
  41. data/lib/new_relic/agent/stats_engine/metric_stats.rb +116 -0
  42. data/lib/new_relic/agent/stats_engine/samplers.rb +74 -0
  43. data/lib/new_relic/agent/stats_engine/transactions.rb +154 -0
  44. data/lib/new_relic/agent/transaction_sampler.rb +315 -0
  45. data/lib/new_relic/agent/worker_loop.rb +118 -0
  46. data/lib/new_relic/commands/deployments.rb +145 -0
  47. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  48. data/lib/new_relic/control.rb +484 -0
  49. data/lib/new_relic/control/external.rb +13 -0
  50. data/lib/new_relic/control/merb.rb +24 -0
  51. data/lib/new_relic/control/rails.rb +151 -0
  52. data/lib/new_relic/control/ruby.rb +36 -0
  53. data/lib/new_relic/control/sinatra.rb +14 -0
  54. data/lib/new_relic/histogram.rb +89 -0
  55. data/lib/new_relic/local_environment.rb +299 -0
  56. data/lib/new_relic/merbtasks.rb +6 -0
  57. data/lib/new_relic/metric_data.rb +42 -0
  58. data/lib/new_relic/metric_parser.rb +124 -0
  59. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  60. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  61. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  62. data/lib/new_relic/metric_parser/controller.rb +54 -0
  63. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  64. data/lib/new_relic/metric_parser/errors.rb +6 -0
  65. data/lib/new_relic/metric_parser/external.rb +50 -0
  66. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  67. data/lib/new_relic/metric_parser/other_transaction.rb +15 -0
  68. data/lib/new_relic/metric_parser/view.rb +61 -0
  69. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  70. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  71. data/lib/new_relic/metric_spec.rb +67 -0
  72. data/lib/new_relic/metrics.rb +7 -0
  73. data/lib/new_relic/noticed_error.rb +23 -0
  74. data/lib/new_relic/rack/metric_app.rb +56 -0
  75. data/lib/new_relic/rack/mongrel_rpm.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +26 -0
  77. data/lib/new_relic/rack_app.rb +5 -0
  78. data/lib/new_relic/recipes.rb +82 -0
  79. data/lib/new_relic/stats.rb +362 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +671 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +276 -0
  84. data/lib/newrelic_rpm.rb +45 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +15 -0
  88. data/newrelic.yml +227 -0
  89. data/newrelic_rpm.gemspec +214 -0
  90. data/recipes/newrelic.rb +6 -0
  91. data/test/active_record_fixtures.rb +55 -0
  92. data/test/config/newrelic.yml +46 -0
  93. data/test/config/test_control.rb +38 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +268 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +254 -0
  96. data/test/new_relic/agent/agent_test_controller.rb +78 -0
  97. data/test/new_relic/agent/busy_calculator_test.rb +79 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  100. data/test/new_relic/agent/error_collector_test.rb +173 -0
  101. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  102. data/test/new_relic/agent/metric_data_test.rb +56 -0
  103. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  104. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  105. data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
  106. data/test/new_relic/agent/rpm_agent_test.rb +125 -0
  107. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  108. data/test/new_relic/agent/stats_engine/samplers_test.rb +88 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +177 -0
  111. data/test/new_relic/agent/testable_agent.rb +13 -0
  112. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  113. data/test/new_relic/agent/transaction_sample_test.rb +186 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +404 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +110 -0
  117. data/test/new_relic/delayed_job_test.rb +108 -0
  118. data/test/new_relic/deployments_api_test.rb +68 -0
  119. data/test/new_relic/environment_test.rb +75 -0
  120. data/test/new_relic/metric_parser_test.rb +172 -0
  121. data/test/new_relic/metric_spec_test.rb +177 -0
  122. data/test/new_relic/shim_agent_test.rb +9 -0
  123. data/test/new_relic/stats_test.rb +291 -0
  124. data/test/new_relic/version_number_test.rb +76 -0
  125. data/test/test_helper.rb +53 -0
  126. data/test/ui/newrelic_controller_test.rb +14 -0
  127. data/test/ui/newrelic_helper_test.rb +53 -0
  128. data/ui/controllers/newrelic_controller.rb +220 -0
  129. data/ui/helpers/google_pie_chart.rb +49 -0
  130. data/ui/helpers/newrelic_helper.rb +320 -0
  131. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  132. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  133. data/ui/views/newrelic/_sample.rhtml +19 -0
  134. data/ui/views/newrelic/_segment.rhtml +28 -0
  135. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  136. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  137. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  138. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  139. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  140. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  141. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  142. data/ui/views/newrelic/_table.rhtml +12 -0
  143. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  144. data/ui/views/newrelic/images/arrow-close.png +0 -0
  145. data/ui/views/newrelic/images/arrow-open.png +0 -0
  146. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  147. data/ui/views/newrelic/images/file_icon.png +0 -0
  148. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  149. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  150. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  151. data/ui/views/newrelic/images/textmate.png +0 -0
  152. data/ui/views/newrelic/index.rhtml +57 -0
  153. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  154. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  155. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  156. data/ui/views/newrelic/show_sample.rhtml +80 -0
  157. data/ui/views/newrelic/show_source.rhtml +3 -0
  158. data/ui/views/newrelic/stylesheets/style.css +484 -0
  159. data/ui/views/newrelic/threads.rhtml +52 -0
  160. metadata +238 -0
@@ -0,0 +1,59 @@
1
+
2
+ if defined? ActionController
3
+
4
+ case Rails::VERSION::STRING
5
+
6
+ when /^(1\.|2\.0)/ # Rails 1.* - 2.0
7
+ ActionController::Base.class_eval do
8
+ add_method_tracer :render, 'View/#{newrelic_metric_path}/Rendering'
9
+ end
10
+
11
+ when /^2\.1\./ # Rails 2.1
12
+ ActionView::PartialTemplate.class_eval do
13
+ add_method_tracer :render, 'View/#{path_without_extension[%r{^(/.*/)?(.*)$},2]}.#{@view.template_format}.#{extension}/Partial'
14
+ end
15
+ # this is for template rendering, as opposed to partial rendering.
16
+ ActionView::Template.class_eval do
17
+ add_method_tracer :render, 'View/#{path_without_extension[%r{^(/.*/)?(.*)$},2]}.#{@view.template_format}.#{extension}/Rendering'
18
+ end
19
+
20
+ when /^2\./ # Rails 2.2-2.*
21
+ ActionView::RenderablePartial.module_eval do
22
+ add_method_tracer :render_partial, 'View/#{path[%r{^(/.*/)?(.*)$},2]}/Partial'
23
+ end
24
+ ActionView::Template.class_eval do
25
+ add_method_tracer :render, 'View/#{path[%r{^(/.*/)?(.*)$},2]}/Rendering'
26
+ end
27
+ end unless NewRelic::Control.instance['disable_view_instrumentation']
28
+
29
+ ActionController::Base.class_eval do
30
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
31
+
32
+ # Compare with #alias_method_chain, which is not available in
33
+ # Rails 1.1:
34
+ alias_method :perform_action_without_newrelic_trace, :perform_action
35
+ alias_method :perform_action, :perform_action_with_newrelic_trace
36
+ private :perform_action
37
+
38
+ def self.newrelic_write_attr(attr_name, value) # :nodoc:
39
+ write_inheritable_attribute(attr_name, value)
40
+ end
41
+
42
+ def self.newrelic_read_attr(attr_name) # :nodoc:
43
+ read_inheritable_attribute(attr_name)
44
+ end
45
+
46
+ # determine the path that is used in the metric name for
47
+ # the called controller action
48
+ def newrelic_metric_path(action_name_override = nil)
49
+ action_part = action_name_override || action_name
50
+ if action_name_override || self.class.action_methods.include?(action_part)
51
+ "#{self.class.controller_path}/#{action_part}"
52
+ else
53
+ "#{self.class.controller_path}/(other)"
54
+ end
55
+ end
56
+ end
57
+ else
58
+ NewRelic::Agent.instance.log.debug "WARNING: ActionController instrumentation not added"
59
+ end
@@ -0,0 +1,27 @@
1
+ # NewRelic Agent instrumentation for WebServices
2
+
3
+ # Note Action Web Service is removed from default package in rails 2.0
4
+ if defined? ActionWebService
5
+
6
+ # instrumentation for Web Service martialing - XML RPC
7
+ ActionWebService::Protocol::XmlRpc::XmlRpcProtocol.class_eval do
8
+ add_method_tracer :decode_request, "WebService/Xml Rpc/XML Decode"
9
+ add_method_tracer :encode_request, "WebService/Xml Rpc/XML Encode"
10
+ add_method_tracer :decode_response, "WebService/Xml Rpc/XML Decode"
11
+ add_method_tracer :encode_response, "WebService/Xml Rpc/XML Encode"
12
+ end
13
+
14
+ # instrumentation for Web Service martialing - Soap
15
+ ActionWebService::Protocol::Soap::SoapProtocol.class_eval do
16
+ add_method_tracer :decode_request, "WebService/Soap/XML Decode"
17
+ add_method_tracer :encode_request, "WebService/Soap/XML Encode"
18
+ add_method_tracer :decode_response, "WebService/Soap/XML Decode"
19
+ add_method_tracer :encode_response, "WebService/Soap/XML Encode"
20
+ end
21
+
22
+ ActionController::Base.class_eval do
23
+ if method_defined? :perform_invocation
24
+ add_method_tracer :perform_invocation, 'WebService/#{controller_name}/#{args.first}'
25
+ end
26
+ end if defined? ActionController::Base
27
+ end
@@ -0,0 +1,24 @@
1
+
2
+ ActionController::Base.class_eval do
3
+
4
+ # Make a note of an exception associated with the currently executin
5
+ # controller action. Note that this used to be available on Object
6
+ # but we replaced that global method with NewRelic::Agent#notice_error.
7
+ # Use that one outside of controller actions.
8
+ def newrelic_notice_error(exception, custom_params = {})
9
+ NewRelic::Agent::Instrumentation::MetricFrame.notice_error exception, custom_params
10
+ end
11
+
12
+ def rescue_action_with_newrelic_trace(exception)
13
+ NewRelic::Agent::Instrumentation::MetricFrame.notice_error exception
14
+ rescue_action_without_newrelic_trace exception
15
+ end
16
+
17
+ # Compare with #alias_method_chain, which is not available in
18
+ # Rails 1.1:
19
+ alias_method :rescue_action_without_newrelic_trace, :rescue_action
20
+ alias_method :rescue_action, :rescue_action_with_newrelic_trace
21
+ protected :rescue_action
22
+
23
+ end if defined? ActionController
24
+
@@ -0,0 +1,39 @@
1
+ module NewRelic::Agent::Instrumentation
2
+ # NewRelic instrumentation for Sinatra applications. Sinatra actions will
3
+ # appear in the UI similar to controller actions, and have breakdown charts
4
+ # and transaction traces.
5
+ #
6
+ # The actions in the UI will correspond to the pattern expression used
7
+ # to match them. HTTP operations are not distinguished. Multiple matches
8
+ # will all be tracked as separate actions.
9
+ module Sinatra
10
+
11
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
12
+
13
+ def route_eval_with_newrelic(&block_arg)
14
+ path = unescape(@request.path_info)
15
+ name = path
16
+ # Go through each route and look for a match
17
+ if routes = self.class.routes[@request.request_method]
18
+ routes.detect do |pattern, keys, conditions, block|
19
+ if block_arg.equal? block
20
+ name = pattern.source
21
+ end
22
+ end
23
+ end
24
+ # strip of leading ^ and / chars and trailing $ and /
25
+ name.gsub!(%r{^[/^]*(.*?)[/\$]*$}, '\1')
26
+ name = 'root' if name.empty?
27
+ perform_action_with_newrelic_trace(:category => :sinatra, :name => name) do
28
+ route_eval_without_newrelic(&block_arg)
29
+ end
30
+ end
31
+ end
32
+
33
+ ::Sinatra::Base.class_eval do
34
+ include NewRelic::Agent::Instrumentation::Sinatra
35
+ alias route_eval_without_newrelic route_eval
36
+ alias route_eval route_eval_with_newrelic
37
+ end
38
+
39
+ end if defined?(Sinatra::Base)
@@ -0,0 +1,348 @@
1
+ require 'new_relic/control'
2
+ module NewRelic::Agent
3
+ # This module contains class methods added to support installing custom
4
+ # metric tracers and executing for individual metrics.
5
+ #
6
+ # == Examples
7
+ #
8
+ # When the agent initializes, it extends Module with these methods.
9
+ # However if you want to use the API in code that might get loaded
10
+ # before the agent is initialized you will need to require
11
+ # this file:
12
+ #
13
+ # require 'new_relic/agent/method_tracer'
14
+ # class A
15
+ # include NewRelic::Agent::MethodTracer
16
+ # def process
17
+ # ...
18
+ # end
19
+ # add_method_tracer :process
20
+ # end
21
+ #
22
+ # To instrument a class method:
23
+ #
24
+ # require 'new_relic/agent/method_tracer'
25
+ # class A
26
+ # extend NewRelic::Agent::MethodTracer
27
+ # def self.process
28
+ # ...
29
+ # end
30
+ # class << self
31
+ # add_method_tracer :process
32
+ # end
33
+ # end
34
+
35
+ module MethodTracer
36
+
37
+ def self.included clazz #:nodoc:
38
+ clazz.extend ClassMethods
39
+ clazz.send :include, InstanceMethods
40
+ end
41
+
42
+ def self.extended clazz #:nodoc:
43
+ clazz.extend ClassMethods
44
+ clazz.extend InstanceMethods
45
+ end
46
+
47
+ module InstanceMethods
48
+ # Deprecated: original method preserved for API backward compatibility.
49
+ # Use either #trace_execution_scoped or #trace_execution_unscoped
50
+ def trace_method_execution(metric_names, push_scope, produce_metric, deduct_call_time_from_parent, &block) #:nodoc:
51
+ if push_scope
52
+ trace_execution_scoped(metric_names, :metric => produce_metric,
53
+ :deduct_call_time_from_parent => deduct_call_time_from_parent, &block)
54
+ else
55
+ trace_execution_unscoped(metric_names, &block)
56
+ end
57
+ end
58
+
59
+ # Trace a given block with stats assigned to the given metric_name. It does not
60
+ # provide scoped measurements, meaning whatever is being traced will not 'blame the
61
+ # Controller'--that is to say appear in the breakdown chart.
62
+ # This is code is inlined in #add_method_tracer.
63
+ # * <tt>metric_names</tt> is a single name or an array of names of metrics
64
+ # * <tt>:force => true</tt> will force the metric to be captured even when
65
+ # tracing is disabled with NewRelic::Agent#disable_all_tracing
66
+ #
67
+ def trace_execution_unscoped(metric_names, options={})
68
+ return yield unless NewRelic::Agent.is_execution_traced?
69
+ t0 = Time.now.to_f
70
+ stats = Array(metric_names).map do | metric_name |
71
+ NewRelic::Agent.instance.stats_engine.get_stats_no_scope metric_name
72
+ end
73
+ begin
74
+ NewRelic::Agent.instance.push_trace_execution_flag(true) if options[:force]
75
+ yield
76
+ ensure
77
+ NewRelic::Agent.instance.pop_trace_execution_flag if options[:force]
78
+ duration = Time.now.to_f - t0 # for some reason this is 3 usec faster than Time - Time
79
+ stats.each { |stat| stat.trace_call(duration) }
80
+ end
81
+ end
82
+
83
+ EMPTY_ARRAY = [].freeze
84
+
85
+ # Deprecated. Use #trace_execution_scoped, a version with an options hash.
86
+ def trace_method_execution_with_scope(metric_names, produce_metric, deduct_call_time_from_parent, scoped_metric_only=false, &block) #:nodoc:
87
+ trace_execution_scoped(metric_names,
88
+ :metric => produce_metric,
89
+ :deduct_call_time_from_parent => deduct_call_time_from_parent,
90
+ :scoped_metric_only => scoped_metric_only, &block)
91
+ end
92
+
93
+ alias trace_method_execution_no_scope trace_execution_unscoped #:nodoc:
94
+
95
+ # Trace a given block with stats and keep track of the caller.
96
+ # See #add_method_tracer for a description of the arguments.
97
+ # +metric_names+ is either a single name or an array of metric names.
98
+ # If more than one metric is passed, the +produce_metric+ option only applies to the first. The
99
+ # others are always recorded. Only the first metric is pushed onto the scope stack.
100
+ #
101
+ # Generally you pass an array of metric names if you want to record the metric under additional
102
+ # categories, but generally this *should never ever be done*. Most of the time you can aggregate
103
+ # on the server.
104
+
105
+ def trace_execution_scoped(metric_names, options={})
106
+
107
+ return yield unless NewRelic::Agent.is_execution_traced? || options[:force]
108
+
109
+ produce_metric = options[:metric] != false
110
+ deduct_call_time_from_parent = options[:deduct_call_time_from_parent] != false
111
+ scoped_metric_only = produce_metric && options[:scoped_metric_only]
112
+ t0 = Time.now.to_f
113
+ if metric_names.instance_of? Array
114
+ first_name = metric_names.first
115
+ metric_stats = []
116
+ metric_stats << NewRelic::Agent.instance.stats_engine.get_stats(first_name, true, scoped_metric_only) if produce_metric
117
+ metric_names[1..-1].each do | name |
118
+ metric_stats << NewRelic::Agent.instance.stats_engine.get_stats_no_scope(name)
119
+ end
120
+ else
121
+ first_name = metric_names
122
+ if produce_metric
123
+ metric_stats = [NewRelic::Agent.instance.stats_engine.get_stats(first_name, true, scoped_metric_only)]
124
+ else
125
+ metric_stats = EMPTY_ARRAY
126
+ end
127
+ end
128
+
129
+ begin
130
+ # Keep a reference to the scope we are pushing so we can do a sanity check making
131
+ # sure when we pop we get the one we 'expected'
132
+ NewRelic::Agent.instance.push_trace_execution_flag(true) if options[:force]
133
+ expected_scope = NewRelic::Agent.instance.stats_engine.push_scope(first_name, t0, deduct_call_time_from_parent)
134
+ rescue => e
135
+ NewRelic::Control.instance.log.error("Caught exception in trace_method_execution header. Metric name = #{first_name}, exception = #{e}")
136
+ NewRelic::Control.instance.log.error(e.backtrace.join("\n"))
137
+ end
138
+
139
+ begin
140
+ yield
141
+ ensure
142
+ t1 = Time.now.to_f
143
+ duration = t1 - t0
144
+
145
+ begin
146
+ NewRelic::Agent.instance.pop_trace_execution_flag if options[:force]
147
+ if expected_scope
148
+ scope = NewRelic::Agent.instance.stats_engine.pop_scope expected_scope, duration, t1
149
+ exclusive = duration - scope.children_time
150
+ metric_stats.each { |stats| stats.trace_call(duration, exclusive) }
151
+ end
152
+ rescue => e
153
+ NewRelic::Control.instance.log.error("Caught exception in trace_method_execution footer. Metric name = #{first_name}, exception = #{e}")
154
+ NewRelic::Control.instance.log.error(e.backtrace.join("\n"))
155
+ end
156
+ end
157
+ end
158
+ end
159
+
160
+ module ClassMethods
161
+ # Add a method tracer to the specified method.
162
+ #
163
+ # === Common Options
164
+ #
165
+ # * <tt>:push_scope => false</tt> specifies this method tracer should not
166
+ # keep track of the caller; it will not show up in controller breakdown
167
+ # pie charts.
168
+ # * <tt>:metric => false</tt> specifies that no metric will be recorded.
169
+ # Instead the call will show up in transaction traces as well as traces
170
+ # shown in Developer Mode.
171
+ #
172
+ # === Uncommon Options
173
+ #
174
+ # * <tt>:scoped_metric_only => true</tt> indicates that the unscoped metric
175
+ # should not be recorded. Normally two metrics are potentially created
176
+ # on every invocation: the aggregate method where statistics for all calls
177
+ # of that metric are stored, and the "scoped metric" which records the
178
+ # statistics for invocations in a particular scope--generally a controller
179
+ # action. This option indicates that only the second type should be recorded.
180
+ # The effect is similar to <tt>:metric => false</tt> but in addition you
181
+ # will also see the invocation in breakdown pie charts.
182
+ # * <tt>:deduct_call_time_from_parent => false</tt> indicates that the method invocation
183
+ # time should never be deducted from the time reported as 'exclusive' in the
184
+ # caller. You would want to use this if you are tracing a recursive method
185
+ # or a method that might be called inside another traced method.
186
+ # * <tt>:code_header</tt> and <tt>:code_footer</tt> specify ruby code that
187
+ # is inserted into the tracer before and after the call.
188
+ # * <tt>:force = true</tt> will ensure the metric is captured even if called inside
189
+ # an untraced execution call. (See NewRelic::Agent#disable_all_tracing)
190
+ #
191
+ # === Overriding the metric name
192
+ #
193
+ # +metric_name_code+ is a string that is eval'd to get the
194
+ # name of the metric associated with the call, so if you want to
195
+ # use interpolaion evaluated at call time, then single quote
196
+ # the value like this:
197
+ #
198
+ # add_method_tracer :foo, 'Custom/#{self.class.name}/foo'
199
+ #
200
+ # This would name the metric according to the class of the runtime
201
+ # intance, as opposed to the class where +foo+ is defined.
202
+ #
203
+ # If not provided, the metric name will be <tt>Custom/ClassName/method_name</tt>.
204
+ #
205
+ # === Examples
206
+ #
207
+ # Instrument +foo+ only for custom views--will not show up in transaction traces or caller breakdown graphs:
208
+ #
209
+ # add_method_tracer :foo, :push_scope => false
210
+ #
211
+ # Instrument +foo+ just for transaction traces only:
212
+ #
213
+ # add_method_tracer :foo, :metric => false
214
+ #
215
+ # Instrument +foo+ so it shows up in transaction traces and caller breakdown graphs
216
+ # for actions:
217
+ #
218
+ # add_method_tracer :foo
219
+ #
220
+ # which is equivalent to:
221
+ #
222
+ # add_method_tracer :foo, 'Custom/#{self.class.name}/foo', :push_scope => true, :metric => true
223
+ #
224
+ # Instrument the class method +foo+ with the metric name 'Custom/People/fetch':
225
+ #
226
+ # class << self
227
+ # add_method_tracer :foo, 'Custom/People/fetch'
228
+ # end
229
+ #
230
+
231
+ def add_method_tracer(method_name, metric_name_code=nil, options = {})
232
+ # for backward compatibility:
233
+ if !options.is_a?(Hash)
234
+ options = {:push_scope => options}
235
+ end
236
+ # in case they omit the metric name code
237
+ if metric_name_code.is_a?(Hash)
238
+ options.merge(metric_name_code)
239
+ end
240
+ if (unrecognized = options.keys - [:force, :metric, :push_scope, :deduct_call_time_from_parent, :code_header, :code_footer, :scoped_metric_only]).any?
241
+ fail "Unrecognized options in add_method_tracer_call: #{unrecognized.join(', ')}"
242
+ end
243
+ # options[:push_scope] true if we are noting the scope of this for
244
+ # stats collection as well as the transaction tracing
245
+ options[:push_scope] = true if options[:push_scope].nil?
246
+ # options[:metric] true if you are tracking stats for a metric, otherwise
247
+ # it's just for transaction tracing.
248
+ options[:metric] = true if options[:metric].nil?
249
+ options[:force] = false if options[:force].nil?
250
+ options[:deduct_call_time_from_parent] = false if options[:deduct_call_time_from_parent].nil? && !options[:metric]
251
+ options[:deduct_call_time_from_parent] = true if options[:deduct_call_time_from_parent].nil?
252
+ options[:code_header] ||= ""
253
+ options[:code_footer] ||= ""
254
+ options[:scoped_metric_only] ||= false
255
+
256
+ klass = (self === Module) ? "self" : "self.class"
257
+ # Default to the class where the method is defined.
258
+ metric_name_code = "Custom/#{self.name}/#{method_name.to_s}" unless metric_name_code
259
+
260
+ unless method_defined?(method_name) || private_method_defined?(method_name)
261
+ NewRelic::Control.instance.log.warn("Did not trace #{self.name}##{method_name} because that method does not exist")
262
+ return
263
+ end
264
+
265
+ traced_method_name = _traced_method_name(method_name, metric_name_code)
266
+ if method_defined? traced_method_name
267
+ NewRelic::Control.instance.log.warn("Attempt to trace a method twice with the same metric: Method = #{method_name}, Metric Name = #{metric_name_code}")
268
+ return
269
+ end
270
+
271
+ fail "Can't add a tracer where push_scope is false and metric is false" if options[:push_scope] == false && !options[:metric]
272
+
273
+ header = ""
274
+ if !options[:force]
275
+ header << "return #{_untraced_method_name(method_name, metric_name_code)}(*args, &block) unless NewRelic::Agent.is_execution_traced?\n"
276
+ end
277
+ header << options[:code_header] if options[:code_header]
278
+ if options[:push_scope] == false
279
+ code = <<-CODE
280
+ def #{_traced_method_name(method_name, metric_name_code)}(*args, &block)
281
+ #{header}
282
+ t0 = Time.now.to_f
283
+ stats = NewRelic::Agent.instance.stats_engine.get_stats_no_scope "#{metric_name_code}"
284
+ begin
285
+ #{"NewRelic::Agent.instance.push_trace_execution_flag(true)\n" if options[:force]}
286
+ #{_untraced_method_name(method_name, metric_name_code)}(*args, &block)\n
287
+ ensure
288
+ #{"NewRelic::Agent.instance.pop_trace_execution_flag\n" if options[:force] }
289
+ duration = Time.now.to_f - t0
290
+ stats.trace_call(duration)
291
+ #{options[:code_footer]}
292
+ end
293
+ end
294
+ CODE
295
+ else
296
+ code = <<-CODE
297
+ def #{_traced_method_name(method_name, metric_name_code)}(*args, &block)
298
+ #{options[:code_header]}
299
+ result = #{klass}.trace_execution_scoped("#{metric_name_code}",
300
+ :metric => #{options[:metric]},
301
+ :forced => #{options[:force]},
302
+ :deduct_call_time_from_parent => #{options[:deduct_call_time_from_parent]},
303
+ :scoped_metric_only => #{options[:scoped_metric_only]}) do
304
+ #{_untraced_method_name(method_name, metric_name_code)}(*args, &block)
305
+ end
306
+ #{options[:code_footer]}
307
+ result
308
+ end
309
+ CODE
310
+ end
311
+ class_eval code, __FILE__, __LINE__
312
+
313
+ alias_method _untraced_method_name(method_name, metric_name_code), method_name
314
+ alias_method method_name, _traced_method_name(method_name, metric_name_code)
315
+
316
+ NewRelic::Control.instance.log.debug("Traced method: class = #{self.name}, method = #{method_name}, "+
317
+ "metric = '#{metric_name_code}', options: #{options.inspect}")
318
+ end
319
+
320
+ # For tests only because tracers must be removed in reverse-order
321
+ # from when they were added, or else other tracers that were added to the same method
322
+ # may get removed as well.
323
+ def remove_method_tracer(method_name, metric_name_code) # :nodoc:
324
+ return unless NewRelic::Control.instance.agent_enabled?
325
+
326
+ if method_defined? "#{_traced_method_name(method_name, metric_name_code)}"
327
+ alias_method method_name, "#{_untraced_method_name(method_name, metric_name_code)}"
328
+ undef_method "#{_traced_method_name(method_name, metric_name_code)}"
329
+ else
330
+ raise "No tracer for '#{metric_name_code}' on method '#{method_name}'"
331
+ end
332
+ end
333
+ private
334
+
335
+ def _untraced_method_name(method_name, metric_name)
336
+ "#{_sanitize_name(method_name)}_without_trace_#{_sanitize_name(metric_name)}"
337
+ end
338
+
339
+ def _traced_method_name(method_name, metric_name)
340
+ "#{_sanitize_name(method_name)}_with_trace_#{_sanitize_name(metric_name)}"
341
+ end
342
+
343
+ def _sanitize_name(name)
344
+ name.to_s.tr_s('^a-zA-Z0-9', '_')
345
+ end
346
+ end
347
+ end
348
+ end