techarch-newrelic_rpm 2.10.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/CHANGELOG +335 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +159 -0
  4. data/README.md +138 -0
  5. data/Rakefile +22 -0
  6. data/bin/mongrel_rpm +33 -0
  7. data/bin/newrelic_cmd +4 -0
  8. data/cert/cacert.pem +34 -0
  9. data/init.rb +38 -0
  10. data/install.rb +45 -0
  11. data/lib/new_relic/agent.rb +281 -0
  12. data/lib/new_relic/agent/agent.rb +636 -0
  13. data/lib/new_relic/agent/chained_call.rb +13 -0
  14. data/lib/new_relic/agent/collection_helper.rb +66 -0
  15. data/lib/new_relic/agent/error_collector.rb +121 -0
  16. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  17. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +88 -0
  18. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  19. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +437 -0
  20. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  21. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +167 -0
  22. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  23. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  24. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  25. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  26. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  27. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  28. data/lib/new_relic/agent/instrumentation/rack.rb +108 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  31. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +41 -0
  32. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  34. data/lib/new_relic/agent/method_tracer.rb +349 -0
  35. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  36. data/lib/new_relic/agent/sampler.rb +12 -0
  37. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  38. data/lib/new_relic/agent/samplers/memory_sampler.rb +138 -0
  39. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  40. data/lib/new_relic/agent/shim_agent.rb +21 -0
  41. data/lib/new_relic/agent/stats_engine.rb +22 -0
  42. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  43. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  44. data/lib/new_relic/agent/stats_engine/transactions.rb +152 -0
  45. data/lib/new_relic/agent/transaction_sampler.rb +310 -0
  46. data/lib/new_relic/agent/worker_loop.rb +118 -0
  47. data/lib/new_relic/commands/deployments.rb +145 -0
  48. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  49. data/lib/new_relic/control.rb +473 -0
  50. data/lib/new_relic/control/external.rb +13 -0
  51. data/lib/new_relic/control/merb.rb +22 -0
  52. data/lib/new_relic/control/rails.rb +145 -0
  53. data/lib/new_relic/control/ruby.rb +36 -0
  54. data/lib/new_relic/control/sinatra.rb +14 -0
  55. data/lib/new_relic/histogram.rb +89 -0
  56. data/lib/new_relic/local_environment.rb +328 -0
  57. data/lib/new_relic/merbtasks.rb +6 -0
  58. data/lib/new_relic/metric_data.rb +42 -0
  59. data/lib/new_relic/metric_parser.rb +124 -0
  60. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  61. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  62. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  63. data/lib/new_relic/metric_parser/controller.rb +54 -0
  64. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  65. data/lib/new_relic/metric_parser/errors.rb +6 -0
  66. data/lib/new_relic/metric_parser/external.rb +50 -0
  67. data/lib/new_relic/metric_parser/mem_cache.rb +12 -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 +52 -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/newrelic.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +25 -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 +361 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +666 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +313 -0
  84. data/lib/newrelic_rpm.rb +40 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +13 -0
  88. data/newrelic.yml +227 -0
  89. data/recipes/newrelic.rb +6 -0
  90. data/techarch-newrelic_rpm.gemspec +32 -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 +39 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +264 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +119 -0
  97. data/test/new_relic/agent/agent_test_controller.rb +44 -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/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +172 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  103. data/test/new_relic/agent/metric_data_test.rb +56 -0
  104. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  105. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  106. data/test/new_relic/agent/net_instrumentation_test.rb +63 -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 +81 -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 +126 -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 +385 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +113 -0
  117. data/test/new_relic/deployments_api_test.rb +68 -0
  118. data/test/new_relic/environment_test.rb +75 -0
  119. data/test/new_relic/metric_parser_test.rb +172 -0
  120. data/test/new_relic/metric_spec_test.rb +177 -0
  121. data/test/new_relic/shim_agent_test.rb +9 -0
  122. data/test/new_relic/stats_test.rb +291 -0
  123. data/test/new_relic/version_number_test.rb +74 -0
  124. data/test/test_helper.rb +38 -0
  125. data/test/ui/newrelic_controller_test.rb +14 -0
  126. data/test/ui/newrelic_helper_test.rb +53 -0
  127. data/ui/controllers/newrelic_controller.rb +220 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +317 -0
  130. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  131. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  132. data/ui/views/newrelic/_sample.rhtml +19 -0
  133. data/ui/views/newrelic/_segment.rhtml +28 -0
  134. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  135. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  136. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  137. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  138. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  139. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  140. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  141. data/ui/views/newrelic/_table.rhtml +12 -0
  142. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  143. data/ui/views/newrelic/images/arrow-close.png +0 -0
  144. data/ui/views/newrelic/images/arrow-open.png +0 -0
  145. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  146. data/ui/views/newrelic/images/file_icon.png +0 -0
  147. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  148. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  149. data/ui/views/newrelic/images/textmate.png +0 -0
  150. data/ui/views/newrelic/index.rhtml +57 -0
  151. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  152. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  153. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  154. data/ui/views/newrelic/show_sample.rhtml +80 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +484 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +330 -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,167 @@
1
+ # We have to patch the mongrel dispatcher live since the classes
2
+ # aren't defined when our instrumentation loads.
3
+ # To use this module, you should monkey patch a method newrelic_response_code
4
+ # which will return the response status code when the dispatcher finishes.
5
+ module NewRelic::Agent::Instrumentation
6
+ module DispatcherInstrumentation
7
+ extend self
8
+ def newrelic_dispatcher_start
9
+ # This call might be entered twice in the same call sequence so we ignore subsequent calls.
10
+ return if _dispatcher_start_time
11
+ # Put the current time on the thread. Can't put in @ivar because this could
12
+ # be a class or instance context
13
+ start = Time.now.to_f
14
+ self._dispatcher_start_time = start
15
+ BusyCalculator.dispatcher_start start
16
+ _detect_upstream_wait
17
+
18
+ NewRelic::Agent.agent.start_transaction
19
+
20
+ # Reset the flag indicating the controller action should be ignored.
21
+ # It may be set by the action to either true or false or left nil meaning false
22
+ Thread.current[:newrelic_ignore_controller] = nil
23
+ end
24
+
25
+ def newrelic_dispatcher_finish
26
+ NewRelic::Agent.agent.end_transaction
27
+ return unless started = _dispatcher_start_time
28
+ dispatcher_end_time = Time.now.to_f
29
+ BusyCalculator.dispatcher_finish dispatcher_end_time
30
+ unless Thread.current[:newrelic_ignore_controller]
31
+ elapsed_time = dispatcher_end_time - started
32
+ # Store the response header
33
+ if response_code = newrelic_response_code
34
+ NewRelic::Agent.agent.stats_engine.get_stats_no_scope("HTTP/Response/#{response_code}").trace_call(elapsed_time)
35
+ end
36
+ # Store the response time
37
+ _dispatch_stat.trace_call(elapsed_time)
38
+ NewRelic::Agent.instance.histogram.process(elapsed_time)
39
+ end
40
+ # ensure we don't record it twice
41
+ self._dispatcher_start_time = nil
42
+ self._request_start_time = nil
43
+ end
44
+ # Should be implemented in the dispatcher class
45
+ def newrelic_response_code; end
46
+
47
+ def newrelic_request_headers
48
+ self.respond_to?(:request) && self.request.respond_to?(:headers) && self.request.headers
49
+ end
50
+
51
+ # Used only when no before/after callbacks are available with
52
+ # the dispatcher, such as Rails before 2.0
53
+ def dispatch_newrelic(*args)
54
+ newrelic_dispatcher_start
55
+ begin
56
+ dispatch_without_newrelic(*args)
57
+ ensure
58
+ newrelic_dispatcher_finish
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def _dispatcher_start_time
65
+ Thread.current[:newrelic_dispatcher_start]
66
+ end
67
+
68
+ def _dispatcher_start_time= newval
69
+ Thread.current[:newrelic_dispatcher_start] = newval
70
+ end
71
+
72
+ def _request_start_time
73
+ Thread.current[:newrelic_request_start]
74
+ end
75
+ def _request_start_time=(newval)
76
+ Thread.current[:newrelic_request_start] = newval
77
+ end
78
+
79
+ def _detect_upstream_wait
80
+ return if _request_start_time
81
+ if newrelic_request_headers
82
+ entry_time = newrelic_request_headers['HTTP_X_REQUEST_START'] and
83
+ entry_time = entry_time[/t=(\d+)/, 1 ] and
84
+ http_entry_time = entry_time.to_f/1e6
85
+ end
86
+ # If we didn't find the custom header, look for the mongrel timestamp:a
87
+ http_entry_time ||= Thread.current[:started_on] and http_entry_time = http_entry_time.to_f
88
+
89
+ if http_entry_time
90
+ self._request_start_time = http_entry_time
91
+ queue_stat = NewRelic::Agent.agent.stats_engine.get_stats_no_scope 'WebFrontend/Mongrel/Average Queue Time'
92
+ queue_stat.trace_call(_dispatcher_start_time - http_entry_time)
93
+ end
94
+ end
95
+
96
+
97
+ def _dispatch_stat
98
+ NewRelic::Agent.agent.stats_engine.get_stats_no_scope 'HttpDispatcher'
99
+ end
100
+
101
+ # This won't work with Rails 2.2 multi-threading
102
+ module BusyCalculator
103
+ extend self
104
+ # the fraction of the sample period that the dispatcher was busy
105
+
106
+ def dispatcher_start(time)
107
+ @lock.synchronize do
108
+ @entrypoint_stack.push time
109
+ end
110
+ end
111
+
112
+ def dispatcher_finish(time)
113
+ @lock.synchronize do
114
+ NewRelic::Control.instance.log.error("Stack underflow tracking dispatcher entry and exit!\n #{caller.join(" \n")}") and return if @entrypoint_stack.empty?
115
+ @accumulator += (time - @entrypoint_stack.pop)
116
+ end
117
+ end
118
+
119
+ def busy_count
120
+ @entrypoint_stack.size
121
+ end
122
+ def reset
123
+ @entrypoint_stack = []
124
+ @lock = Mutex.new
125
+ @accumulator = 0
126
+ @harvest_start = Time.now.to_f
127
+ end
128
+
129
+ self.reset
130
+
131
+ # Called before uploading to to the server to collect current busy stats.
132
+ def harvest_busy
133
+ busy = 0
134
+ t0 = Time.now.to_f
135
+ @lock.synchronize do
136
+ busy = @accumulator
137
+ @accumulator = 0
138
+
139
+ # Walk through the stack and capture all times up to
140
+ # now for entrypoints
141
+ @entrypoint_stack.size.times do |frame|
142
+ busy += (t0 - @entrypoint_stack[frame])
143
+ @entrypoint_stack[frame] = t0
144
+ end
145
+
146
+ end
147
+
148
+ busy = 0.0 if busy < 0.0 # don't go below 0%
149
+
150
+ time_window = (t0 - @harvest_start)
151
+ time_window = 1.0 if time_window == 0.0 # protect against divide by zero
152
+
153
+ busy = busy / time_window
154
+
155
+ instance_busy_stats.record_data_point busy unless busy == 0
156
+ @harvest_start = t0
157
+ end
158
+ private
159
+ def instance_busy_stats
160
+ # Late binding on the Instance/busy stats
161
+ NewRelic::Agent.agent.stats_engine.get_stats_no_scope 'Instance/Busy'
162
+ end
163
+
164
+ end
165
+
166
+ end
167
+ end
@@ -0,0 +1,24 @@
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://dev.robotcoop.com/Libraries/memcache-client/ (Gem: memcache-client)
6
+ unless NewRelic::Control.instance['disable_memcache_instrumentation']
7
+ MemCache.class_eval do
8
+ add_method_tracer :get, 'MemCache/read' if self.method_defined? :get
9
+ add_method_tracer :get_multi, 'MemCache/read' if self.method_defined? :get_multi
10
+ %w[set add incr decr delete].each do | method |
11
+ add_method_tracer method, 'MemCache/write' if self.method_defined? method
12
+ end
13
+ end if defined? MemCache
14
+
15
+ # Support for libmemcached through Evan Weaver's memcached wrapper
16
+ # http://blog.evanweaver.com/files/doc/fauna/memcached/classes/Memcached.html
17
+ Memcached.class_eval do
18
+ add_method_tracer :get, 'MemCache/read' if self.method_defined? :get
19
+ %w[set add increment decrement delete replace append prepend cas].each do | method |
20
+ add_method_tracer method, "MemCache/write" if self.method_defined? method
21
+ end
22
+ end if defined? Memcached
23
+
24
+ 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(action)
22
+ "#{controller_name}/#{action}"
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,13 @@
1
+ require 'merb-core/dispatch/dispatcher'
2
+ # NewRelic RPM instrumentation for http request dispatching (Routes mapping)
3
+ Merb::Request.class_eval do
4
+
5
+ include NewRelic::Agent::Instrumentation::DispatcherInstrumentation
6
+
7
+ alias_method :dispatch_without_newrelic, :handle
8
+ alias_method :handle, :dispatch_newrelic
9
+ def newrelic_response_code
10
+ # Don't have an easy way to get the HTTP status from here yet
11
+ nil
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ # Hook in the notification to merb
2
+ error_notifier = Proc.new {
3
+ if request.exceptions #check that there's actually an exception
4
+ NewRelic::Agent.agent.error_collector.notice_error(request.exceptions.first, request, "#{params[:controller]}/#{params[:action]}", params)
5
+ end
6
+ }
7
+ Merb::Dispatcher::DefaultException.before error_notifier
8
+ Exceptions.before error_notifier
@@ -0,0 +1,23 @@
1
+ # ActiveMerchant Instrumentation.
2
+
3
+ if defined? Net::HTTP
4
+ Net::HTTP.class_eval do
5
+ def request_with_newrelic_trace(*args, &block)
6
+ if Thread::current[:newrelic_transaction_name].nil?
7
+ self.class.trace_execution_unscoped(["External/#{@address}/Net::HTTP/#{args[0].method}",
8
+ "External/#{@address}/all",
9
+ "External/allOther"]) do
10
+ request_without_newrelic_trace(*args, &block)
11
+ end
12
+ else
13
+ self.class.trace_execution_scoped(["External/#{@address}/Net::HTTP/#{args[0].method}",
14
+ "External/#{@address}/all",
15
+ "External/allWeb"]) do
16
+ request_without_newrelic_trace(*args, &block)
17
+ end
18
+ end
19
+ end
20
+ alias request_without_newrelic_trace request
21
+ alias request request_with_newrelic_trace
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ if defined?(PhusionPassenger)
2
+ NewRelic::Control.instance.log.debug "Installing Passenger event hooks."
3
+
4
+ PhusionPassenger.on_event(:stopping_worker_process) do
5
+ NewRelic::Control.instance.log.info "Passenger stopping this process, shutdown the agent."
6
+ NewRelic::Agent.instance.shutdown
7
+ end
8
+
9
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
10
+ if forked
11
+ # We want to reset the stats from the stats engine in case any carried
12
+ # over into the spawned process. Don't clear them in case any were
13
+ # cached.
14
+ NewRelic::Agent.instance.stats_engine.reset_stats
15
+ else
16
+ # We're in conservative spawning mode. We don't need to do anything.
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,108 @@
1
+
2
+
3
+ require 'new_relic/agent/instrumentation/controller_instrumentation'
4
+
5
+ module NewRelic
6
+ module Agent
7
+ module Instrumentation
8
+ # == Instrumentation for Rack
9
+ #
10
+ # New Relic will instrument a #call method as if it were a controller
11
+ # action, collecting transaction traces and errors. The middleware will
12
+ # be identified only by it's class, so if you want to instrument multiple
13
+ # actions in a middleware, you need to use
14
+ # NewRelic::Agent::Instrumentation::ControllerInstrumentation::ClassMethods#add_transaction_tracer
15
+ #
16
+ # Example:
17
+ # require 'new_relic/agent/instrumentation/rack'
18
+ # class Middleware
19
+ # def call(env)
20
+ # ...
21
+ # end
22
+ # # Do the include after the call method is defined:
23
+ # include NewRelic::Agent::Instrumentation::Rack
24
+ # end
25
+ #
26
+ # == Instrumenting Metal
27
+ #
28
+ # If you are using Metal, be sure and extend the your Metal class with the
29
+ # Rack instrumentation:
30
+ #
31
+ # require 'new_relic/agent/instrumentation/rack'
32
+ # class MetalApp
33
+ # def self.call(env)
34
+ # ...
35
+ # end
36
+ # # Do the include after the call method is defined:
37
+ # extend NewRelic::Agent::Instrumentation::Rack
38
+ # end
39
+ #
40
+ # == Overriding the metric name
41
+ #
42
+ # By default the middleware is identified only by its class, but if you want to
43
+ # be more specific and pass in name, then omit including the Rack instrumentation
44
+ # and instead follow this example:
45
+ #
46
+ # require 'new_relic/agent/instrumentation/controller_instrumentation'
47
+ # class Middleware
48
+ # include NewRelic::Agent::Instrumentation::ControllerInstrumentation
49
+ # def call(env)
50
+ # ...
51
+ # end
52
+ # add_transaction_tracer :call, :category => :rack, :name => 'my app'
53
+ # end
54
+ #
55
+ # == Cascading or chained calls
56
+ #
57
+ # You should avoid instrumenting nested calls. So if you are using Rack::Cascade
58
+ # to fall through the action, or you are chaining through to the next middleware
59
+ # which will have it's own controller instrumentation, then you will want to
60
+ # instrument only when you are the endpoint.
61
+ #
62
+ # In these cases, you should not include or extend the Rack module but instead
63
+ # include NewRelic::Agent::Instrumentation::ControllerInstrumentation.
64
+ # Here's how that might look:
65
+ #
66
+ # require 'new_relic/agent/instrumentation/controller_instrumentation'
67
+ # class MetalApp
68
+ # extend NewRelic::Agent::Instrumentation::ControllerInstrumentation
69
+ # def self.call(env)
70
+ # if should_do_my_thing?
71
+ # perform_action_with_newrelic_trace(:category => :rack) do
72
+ # return my_response(env)
73
+ # end
74
+ # else
75
+ # return [404, {"Content-Type" => "text/html"}, ["Not Found"]]
76
+ # end
77
+ # end
78
+ # end
79
+ #
80
+ module Rack
81
+ def newrelic_request_headers
82
+ @newrelic_env
83
+ end
84
+ def call_with_newrelic(*args)
85
+ @newrelic_env = args.first
86
+ perform_action_with_newrelic_trace(:category => :rack) do
87
+ call_without_newrelic(*args)
88
+ end
89
+ end
90
+ def self.included middleware #:nodoc:
91
+ middleware.class_eval do
92
+ alias call_without_newrelic call
93
+ alias call call_with_newrelic
94
+ end
95
+ end
96
+ include ControllerInstrumentation
97
+ def self.extended middleware #:nodoc:
98
+ middleware.class_eval do
99
+ class << self
100
+ alias call_without_newrelic call
101
+ alias call call_with_newrelic
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end