ghazel-newrelic_rpm 3.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/CHANGELOG +525 -0
  2. data/LICENSE +37 -0
  3. data/README.rdoc +179 -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 +118 -0
  8. data/cert/oldsite.pem +28 -0
  9. data/cert/site.pem +27 -0
  10. data/install.rb +9 -0
  11. data/lib/conditional_vendored_dependency_detection.rb +3 -0
  12. data/lib/conditional_vendored_metric_parser.rb +5 -0
  13. data/lib/new_relic/agent.rb +423 -0
  14. data/lib/new_relic/agent/agent.rb +1040 -0
  15. data/lib/new_relic/agent/beacon_configuration.rb +61 -0
  16. data/lib/new_relic/agent/browser_monitoring.rb +83 -0
  17. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  18. data/lib/new_relic/agent/chained_call.rb +13 -0
  19. data/lib/new_relic/agent/error_collector.rb +203 -0
  20. data/lib/new_relic/agent/instrumentation.rb +9 -0
  21. data/lib/new_relic/agent/instrumentation/active_merchant.rb +21 -0
  22. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +62 -0
  23. data/lib/new_relic/agent/instrumentation/authlogic.rb +13 -0
  24. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +413 -0
  25. data/lib/new_relic/agent/instrumentation/data_mapper.rb +232 -0
  26. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +44 -0
  27. data/lib/new_relic/agent/instrumentation/memcache.rb +56 -0
  28. data/lib/new_relic/agent/instrumentation/merb/controller.rb +36 -0
  29. data/lib/new_relic/agent/instrumentation/merb/errors.rb +23 -0
  30. data/lib/new_relic/agent/instrumentation/metric_frame.rb +326 -0
  31. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +84 -0
  32. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  33. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +34 -0
  34. data/lib/new_relic/agent/instrumentation/queue_time.rb +210 -0
  35. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  36. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +83 -0
  37. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +36 -0
  38. data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +108 -0
  39. data/lib/new_relic/agent/instrumentation/rails/errors.rb +36 -0
  40. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +60 -0
  41. data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +112 -0
  42. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +31 -0
  43. data/lib/new_relic/agent/instrumentation/sinatra.rb +54 -0
  44. data/lib/new_relic/agent/instrumentation/sunspot.rb +23 -0
  45. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +16 -0
  46. data/lib/new_relic/agent/method_tracer.rb +422 -0
  47. data/lib/new_relic/agent/sampler.rb +50 -0
  48. data/lib/new_relic/agent/samplers/cpu_sampler.rb +58 -0
  49. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +40 -0
  50. data/lib/new_relic/agent/samplers/memory_sampler.rb +144 -0
  51. data/lib/new_relic/agent/samplers/object_sampler.rb +26 -0
  52. data/lib/new_relic/agent/shim_agent.rb +29 -0
  53. data/lib/new_relic/agent/stats_engine.rb +24 -0
  54. data/lib/new_relic/agent/stats_engine/metric_stats.rb +182 -0
  55. data/lib/new_relic/agent/stats_engine/samplers.rb +89 -0
  56. data/lib/new_relic/agent/stats_engine/transactions.rb +184 -0
  57. data/lib/new_relic/agent/transaction_sample_builder.rb +101 -0
  58. data/lib/new_relic/agent/transaction_sampler.rb +378 -0
  59. data/lib/new_relic/agent/worker_loop.rb +81 -0
  60. data/lib/new_relic/collection_helper.rb +69 -0
  61. data/lib/new_relic/command.rb +85 -0
  62. data/lib/new_relic/commands/deployments.rb +105 -0
  63. data/lib/new_relic/commands/install.rb +91 -0
  64. data/lib/new_relic/control.rb +48 -0
  65. data/lib/new_relic/control/class_methods.rb +47 -0
  66. data/lib/new_relic/control/configuration.rb +157 -0
  67. data/lib/new_relic/control/frameworks/external.rb +16 -0
  68. data/lib/new_relic/control/frameworks/merb.rb +30 -0
  69. data/lib/new_relic/control/frameworks/rails.rb +151 -0
  70. data/lib/new_relic/control/frameworks/rails3.rb +70 -0
  71. data/lib/new_relic/control/frameworks/ruby.rb +42 -0
  72. data/lib/new_relic/control/frameworks/sinatra.rb +23 -0
  73. data/lib/new_relic/control/instance_methods.rb +154 -0
  74. data/lib/new_relic/control/instrumentation.rb +81 -0
  75. data/lib/new_relic/control/logging_methods.rb +88 -0
  76. data/lib/new_relic/control/profiling.rb +25 -0
  77. data/lib/new_relic/control/server_methods.rb +104 -0
  78. data/lib/new_relic/data_serialization.rb +84 -0
  79. data/lib/new_relic/delayed_job_injection.rb +38 -0
  80. data/lib/new_relic/histogram.rb +91 -0
  81. data/lib/new_relic/local_environment.rb +391 -0
  82. data/lib/new_relic/merbtasks.rb +6 -0
  83. data/lib/new_relic/metric_data.rb +46 -0
  84. data/lib/new_relic/metric_spec.rb +73 -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/browser_monitoring.rb +68 -0
  88. data/lib/new_relic/rack/developer_mode.rb +267 -0
  89. data/lib/new_relic/rack/metric_app.rb +65 -0
  90. data/lib/new_relic/rack/mongrel_rpm.ru +28 -0
  91. data/lib/new_relic/rack/newrelic.yml +27 -0
  92. data/lib/new_relic/rack_app.rb +6 -0
  93. data/lib/new_relic/recipes.rb +73 -0
  94. data/lib/new_relic/stats.rb +378 -0
  95. data/lib/new_relic/timer_lib.rb +27 -0
  96. data/lib/new_relic/transaction_analysis.rb +76 -0
  97. data/lib/new_relic/transaction_analysis/segment_summary.rb +47 -0
  98. data/lib/new_relic/transaction_sample.rb +272 -0
  99. data/lib/new_relic/transaction_sample/composite_segment.rb +27 -0
  100. data/lib/new_relic/transaction_sample/fake_segment.rb +9 -0
  101. data/lib/new_relic/transaction_sample/segment.rb +251 -0
  102. data/lib/new_relic/transaction_sample/summary_segment.rb +21 -0
  103. data/lib/new_relic/url_rule.rb +14 -0
  104. data/lib/new_relic/version.rb +55 -0
  105. data/lib/newrelic_rpm.rb +49 -0
  106. data/lib/tasks/all.rb +4 -0
  107. data/lib/tasks/install.rake +7 -0
  108. data/lib/tasks/tests.rake +19 -0
  109. data/newrelic.yml +265 -0
  110. data/newrelic_rpm.gemspec +330 -0
  111. data/recipes/newrelic.rb +6 -0
  112. data/test/active_record_fixtures.rb +77 -0
  113. data/test/config/newrelic.yml +48 -0
  114. data/test/config/test_control.rb +48 -0
  115. data/test/new_relic/agent/agent/connect_test.rb +316 -0
  116. data/test/new_relic/agent/agent/start_test.rb +340 -0
  117. data/test/new_relic/agent/agent/start_worker_thread_test.rb +155 -0
  118. data/test/new_relic/agent/agent_test.rb +173 -0
  119. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  120. data/test/new_relic/agent/agent_test_controller_test.rb +366 -0
  121. data/test/new_relic/agent/apdex_from_server_test.rb +9 -0
  122. data/test/new_relic/agent/beacon_configuration_test.rb +104 -0
  123. data/test/new_relic/agent/browser_monitoring_test.rb +274 -0
  124. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  125. data/test/new_relic/agent/error_collector/notice_error_test.rb +257 -0
  126. data/test/new_relic/agent/error_collector_test.rb +168 -0
  127. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +522 -0
  128. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +36 -0
  129. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +11 -0
  130. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +179 -0
  131. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +50 -0
  132. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +84 -0
  133. data/test/new_relic/agent/instrumentation/queue_time_test.rb +387 -0
  134. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  135. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +184 -0
  136. data/test/new_relic/agent/memcache_instrumentation_test.rb +107 -0
  137. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +164 -0
  138. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +234 -0
  139. data/test/new_relic/agent/method_tracer_test.rb +386 -0
  140. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  141. data/test/new_relic/agent/rpm_agent_test.rb +149 -0
  142. data/test/new_relic/agent/sampler_test.rb +19 -0
  143. data/test/new_relic/agent/shim_agent_test.rb +20 -0
  144. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +150 -0
  145. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +81 -0
  146. data/test/new_relic/agent/stats_engine/samplers_test.rb +99 -0
  147. data/test/new_relic/agent/stats_engine_test.rb +185 -0
  148. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  149. data/test/new_relic/agent/transaction_sampler_test.rb +945 -0
  150. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  151. data/test/new_relic/collection_helper_test.rb +127 -0
  152. data/test/new_relic/command/deployments_test.rb +68 -0
  153. data/test/new_relic/control/class_methods_test.rb +62 -0
  154. data/test/new_relic/control/logging_methods_test.rb +157 -0
  155. data/test/new_relic/control_test.rb +195 -0
  156. data/test/new_relic/data_serialization_test.rb +70 -0
  157. data/test/new_relic/local_environment_test.rb +72 -0
  158. data/test/new_relic/metric_data_test.rb +125 -0
  159. data/test/new_relic/metric_spec_test.rb +95 -0
  160. data/test/new_relic/rack/all_test.rb +11 -0
  161. data/test/new_relic/rack/browser_monitoring_test.rb +84 -0
  162. data/test/new_relic/rack/developer_mode_test.rb +43 -0
  163. data/test/new_relic/stats_test.rb +426 -0
  164. data/test/new_relic/transaction_analysis/segment_summary_test.rb +77 -0
  165. data/test/new_relic/transaction_analysis_test.rb +121 -0
  166. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  167. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  168. data/test/new_relic/transaction_sample/segment_test.rb +454 -0
  169. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  170. data/test/new_relic/transaction_sample_subtest_test.rb +56 -0
  171. data/test/new_relic/transaction_sample_test.rb +170 -0
  172. data/test/new_relic/version_number_test.rb +89 -0
  173. data/test/test_contexts.rb +29 -0
  174. data/test/test_helper.rb +144 -0
  175. data/ui/helpers/developer_mode_helper.rb +351 -0
  176. data/ui/helpers/google_pie_chart.rb +49 -0
  177. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  178. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  179. data/ui/views/newrelic/_sample.rhtml +20 -0
  180. data/ui/views/newrelic/_segment.rhtml +28 -0
  181. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  182. data/ui/views/newrelic/_segment_row.rhtml +12 -0
  183. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  184. data/ui/views/newrelic/_show_sample_sql.rhtml +24 -0
  185. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  186. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  187. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  188. data/ui/views/newrelic/_table.rhtml +12 -0
  189. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  190. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  191. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  192. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  193. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  194. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  195. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  196. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  197. data/ui/views/newrelic/file/images/textmate.png +0 -0
  198. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  199. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  200. data/ui/views/newrelic/file/stylesheets/style.css +490 -0
  201. data/ui/views/newrelic/index.rhtml +71 -0
  202. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  203. data/ui/views/newrelic/show_sample.rhtml +80 -0
  204. data/ui/views/newrelic/show_source.rhtml +3 -0
  205. data/ui/views/newrelic/threads.rhtml +53 -0
  206. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +19 -0
  207. data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
  208. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +57 -0
  209. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection/version.rb +3 -0
  210. data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
  211. data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
  212. data/vendor/gems/metric_parser-0.1.0.pre1/lib/metric_parser.rb +1 -0
  213. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +64 -0
  214. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +14 -0
  215. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +31 -0
  216. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +33 -0
  217. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +89 -0
  218. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +7 -0
  219. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +46 -0
  220. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +67 -0
  221. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +43 -0
  222. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +17 -0
  223. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +48 -0
  224. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +24 -0
  225. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +28 -0
  226. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +17 -0
  227. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/errors.rb +11 -0
  228. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +55 -0
  229. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +40 -0
  230. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +20 -0
  231. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +7 -0
  232. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +31 -0
  233. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +17 -0
  234. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +34 -0
  235. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +7 -0
  236. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +55 -0
  237. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +122 -0
  238. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +27 -0
  239. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +40 -0
  240. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +7 -0
  241. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +7 -0
  242. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +7 -0
  243. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +27 -0
  244. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +15 -0
  245. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +54 -0
  246. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_controller.rb +6 -0
  247. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_view.rb +6 -0
  248. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +20 -0
  249. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +20 -0
  250. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/version.rb +5 -0
  251. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +66 -0
  252. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_frontend.rb +18 -0
  253. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_service.rb +14 -0
  254. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +133 -0
  255. metadata +399 -0
@@ -0,0 +1,27 @@
1
+ # Copyright: (C) 2008 David Vollbracht & Philippe Hanrigou
2
+
3
+ # This code was borrowed from the system_timer gem under the terms
4
+ # of the Ruby license. It has been slightly modified.
5
+
6
+ # Defines the constant TimerLib to the appropriate timeout library
7
+ module NewRelic #:nodoc:
8
+
9
+ begin
10
+ # Try to use the SystemTimer gem instead of Ruby's timeout library
11
+ # when running on Ruby 1.8.x. See:
12
+ # http://ph7spot.com/articles/system_timer
13
+ # We don't want to bother trying to load SystemTimer on jruby,
14
+ # ruby 1.9+ and rbx.
15
+ if !defined?(RUBY_ENGINE) || (RUBY_ENGINE == 'ruby' && RUBY_VERSION < '1.9.0')
16
+ require 'system_timer'
17
+ TimerLib = SystemTimer
18
+ else
19
+ require 'timeout'
20
+ TimerLib = Timeout
21
+ end
22
+ rescue LoadError => e
23
+ require 'timeout'
24
+ TimerLib = Timeout
25
+ end
26
+
27
+ end
@@ -0,0 +1,76 @@
1
+ require 'new_relic/transaction_analysis/segment_summary'
2
+ # Add these methods to TransactionSample that enable performance analysis in the user interface.
3
+ module NewRelic
4
+ module TransactionAnalysis
5
+ def database_time
6
+ time_percentage(/^Database\/.*/)
7
+ end
8
+
9
+ def render_time
10
+ time_percentage(/^View\/.*/)
11
+ end
12
+
13
+ # return the data that breaks down the performance of the transaction
14
+ # as an array of SegmentSummary objects. If a limit is specified, then
15
+ # limit the data set to the top n
16
+ def breakdown_data(limit = nil)
17
+ metric_hash = {}
18
+ each_segment do |segment|
19
+ unless segment == root_segment
20
+ metric_name = segment.metric_name
21
+ metric_hash[metric_name] ||= SegmentSummary.new(metric_name, self)
22
+ metric_hash[metric_name] << segment
23
+ end
24
+ end
25
+
26
+ data = metric_hash.values
27
+
28
+ data.sort! do |x,y|
29
+ y.exclusive_time <=> x.exclusive_time
30
+ end
31
+
32
+ if limit && data.length > limit
33
+ data = data[0..limit - 1]
34
+ end
35
+
36
+ # add one last segment for the remaining time if any
37
+ remainder = duration
38
+ data.each do |segment|
39
+ remainder -= segment.exclusive_time
40
+ end
41
+
42
+ if (remainder*1000).round > 0
43
+ remainder_summary = SegmentSummary.new('Remainder', self)
44
+ remainder_summary.total_time = remainder_summary.exclusive_time = remainder
45
+ remainder_summary.call_count = 1
46
+ data << remainder_summary
47
+ end
48
+
49
+ data
50
+ end
51
+
52
+ # return an array of sql statements executed by this transaction
53
+ # each element in the array contains [sql, parent_segment_metric_name, duration]
54
+ def sql_segments(show_non_sql_segments = true)
55
+ segments = []
56
+ each_segment do |segment|
57
+ segments << segment if segment[:sql] || segment[:sql_obfuscated] || (show_non_sql_segments && segment[:key])
58
+ end
59
+ segments
60
+ end
61
+
62
+ private
63
+ def time_percentage(regex)
64
+ total = 0
65
+ each_segment do |segment|
66
+ if regex =~ segment.metric_name
67
+ total += segment.duration
68
+ end
69
+ end
70
+ fraction = 100.0 * total / duration
71
+ # percent value rounded to two digits:
72
+ return (100 * fraction).round / 100.0
73
+ end
74
+ end
75
+ end
76
+
@@ -0,0 +1,47 @@
1
+ module NewRelic
2
+ module TransactionAnalysis
3
+ # summarizes performance data for all calls to segments
4
+ # with the same metric_name
5
+ class SegmentSummary
6
+ attr_accessor :metric_name, :total_time, :exclusive_time, :call_count
7
+ def initialize(metric_name, sample)
8
+ @metric_name = metric_name
9
+ @total_time, @exclusive_time, @call_count = 0,0,0
10
+ @sample = sample
11
+ end
12
+
13
+ def <<(segment)
14
+ if metric_name != segment.metric_name
15
+ raise ArgumentError, "Metric Name Mismatch: #{segment.metric_name} != #{metric_name}"
16
+ end
17
+
18
+ @total_time += segment.duration
19
+ @exclusive_time += segment.exclusive_duration
20
+ @call_count += 1
21
+ end
22
+
23
+ def average_time
24
+ @total_time / @call_count
25
+ end
26
+
27
+ def average_exclusive_time
28
+ @exclusive_time / @call_count
29
+ end
30
+
31
+ def exclusive_time_percentage
32
+ return 0 unless @exclusive_time && @sample.duration && @sample.duration > 0
33
+ @exclusive_time / @sample.duration
34
+ end
35
+
36
+ def total_time_percentage
37
+ return 0 unless @total_time && @sample.duration && @sample.duration > 0
38
+ @total_time / @sample.duration
39
+ end
40
+
41
+ def ui_name
42
+ return @metric_name if @metric_name == 'Remainder'
43
+ NewRelic::MetricParser::MetricParser.parse(@metric_name).developer_name
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,272 @@
1
+ require 'new_relic/transaction_sample/segment'
2
+ require 'new_relic/transaction_sample/summary_segment'
3
+ require 'new_relic/transaction_sample/fake_segment'
4
+ require 'new_relic/transaction_sample/composite_segment'
5
+ module NewRelic
6
+ COLLAPSE_SEGMENTS_THRESHOLD = 2
7
+
8
+ MYSQL_EXPLAIN_COLUMNS = [
9
+ "Id",
10
+ "Select Type",
11
+ "Table",
12
+ "Type",
13
+ "Possible Keys",
14
+ "Key",
15
+ "Key Length",
16
+ "Ref",
17
+ "Rows",
18
+ "Extra"
19
+ ].freeze
20
+
21
+ class TransactionSample
22
+
23
+ attr_accessor :params, :root_segment
24
+ attr_accessor :profile
25
+ attr_reader :root_segment
26
+ attr_reader :params
27
+ attr_reader :sample_id
28
+
29
+ @@start_time = Time.now
30
+
31
+ include TransactionAnalysis
32
+
33
+ class << self
34
+ def obfuscate_sql(sql)
35
+ NewRelic::Agent.instance.obfuscator.call(sql)
36
+ end
37
+
38
+ def get_connection(config)
39
+ @@connections ||= {}
40
+
41
+ connection = @@connections[config]
42
+
43
+ return connection if connection
44
+
45
+ begin
46
+ connection = ActiveRecord::Base.send("#{config[:adapter]}_connection", config)
47
+ @@connections[config] = connection
48
+ rescue => e
49
+ NewRelic::Agent.agent.log.error("Caught exception #{e} trying to get connection to DB for explain. Control: #{config}")
50
+ NewRelic::Agent.agent.log.error(e.backtrace.join("\n"))
51
+ nil
52
+ end
53
+ end
54
+
55
+ def close_connections
56
+ @@connections ||= {}
57
+ @@connections.values.each do |connection|
58
+ begin
59
+ connection.disconnect!
60
+ rescue
61
+ end
62
+ end
63
+
64
+ @@connections = {}
65
+ end
66
+
67
+ end
68
+
69
+ def initialize(time = Time.now.to_f, sample_id = nil)
70
+ @sample_id = sample_id || object_id
71
+ @start_time = time
72
+ @root_segment = create_segment 0.0, "ROOT"
73
+ @params = {}
74
+ @params[:request_params] = {}
75
+ end
76
+
77
+ def count_segments
78
+ @root_segment.count_segments - 1 # don't count the root segment
79
+ end
80
+
81
+ def truncate(max)
82
+ count = count_segments
83
+ return if count < max
84
+ @root_segment.truncate(max + 1)
85
+
86
+ ensure_segment_count_set(count)
87
+ end
88
+
89
+ def ensure_segment_count_set(count)
90
+ params[:segment_count] ||= count
91
+ end
92
+
93
+ # offset from start of app
94
+ def timestamp
95
+ @start_time - @@start_time.to_f
96
+ end
97
+
98
+ # Used in the server only
99
+ def to_json(options = {}) #:nodoc:
100
+ map = {:sample_id => @sample_id,
101
+ :start_time => @start_time,
102
+ :root_segment => @root_segment}
103
+ if @params && !@params.empty?
104
+ map[:params] = @params
105
+ end
106
+ map.to_json
107
+ end
108
+
109
+ def start_time
110
+ Time.at(@start_time)
111
+ end
112
+
113
+ def path_string
114
+ @root_segment.path_string
115
+ end
116
+
117
+ def create_segment(relative_timestamp, metric_name, segment_id = nil)
118
+ raise TypeError.new("Frozen Transaction Sample") if frozen?
119
+ NewRelic::TransactionSample::Segment.new(relative_timestamp, metric_name, segment_id)
120
+ end
121
+
122
+ def duration
123
+ root_segment.duration
124
+ end
125
+
126
+ def each_segment(&block)
127
+ @root_segment.each_segment(&block)
128
+ end
129
+
130
+ def to_s_compact
131
+ @root_segment.to_s_compact
132
+ end
133
+
134
+ def find_segment(id)
135
+ @root_segment.find_segment(id)
136
+ end
137
+
138
+ def to_s
139
+ s = "Transaction Sample collected at #{start_time}\n"
140
+ s << " {\n"
141
+ s << " Path: #{params[:path]} \n"
142
+
143
+ params.each do |k,v|
144
+ next if k == :path
145
+ s << " #{k}: " <<
146
+ case v
147
+ when Enumerable then v.map(&:to_s).sort.join("; ")
148
+ when String then v
149
+ when Float then '%6.3s' % v
150
+ when nil then ''
151
+ else
152
+ raise "unexpected value type for #{k}: '#{v}' (#{v.class})"
153
+ end << "\n"
154
+ end
155
+ s << " }\n\n"
156
+ s << @root_segment.to_debug_str(0)
157
+ end
158
+
159
+ # return a new transaction sample that treats segments
160
+ # with the given regular expression in their name as if they
161
+ # were never called at all. This allows us to strip out segments
162
+ # from traces captured in development environment that would not
163
+ # normally show up in production (like Rails/Application Code Loading)
164
+ def omit_segments_with(regex)
165
+ regex = Regexp.new(regex)
166
+
167
+ sample = TransactionSample.new(@start_time, sample_id)
168
+
169
+ params.each {|k,v| sample.params[k] = v}
170
+
171
+ delta = build_segment_with_omissions(sample, 0.0, @root_segment, sample.root_segment, regex)
172
+ sample.root_segment.end_trace(@root_segment.exit_timestamp - delta)
173
+ sample.profile = self.profile
174
+ sample
175
+ end
176
+
177
+ # Return a new transaction sample that can be sent to the New
178
+ # Relic service. This involves potentially one or more of the
179
+ # following options
180
+ #
181
+ # :explain_sql : run EXPLAIN on all queries whose response times equal the value for this key
182
+ # (for example :explain_sql => 2.0 would explain everything over 2 seconds. 0.0 would explain everything.)
183
+ # :keep_backtraces : keep backtraces, significantly increasing size of trace (off by default)
184
+ # :record_sql => [ :raw | :obfuscated] : copy over the sql, obfuscating if necessary
185
+ def prepare_to_send(options={})
186
+ sample = TransactionSample.new(@start_time, sample_id)
187
+
188
+ sample.params.merge! self.params
189
+
190
+ begin
191
+ build_segment_for_transfer(sample, @root_segment, sample.root_segment, options)
192
+ ensure
193
+ self.class.close_connections
194
+ end
195
+
196
+ sample.root_segment.end_trace(@root_segment.exit_timestamp)
197
+ sample
198
+ end
199
+
200
+ def params=(params)
201
+ @params = params
202
+ end
203
+
204
+ private
205
+
206
+ def build_segment_with_omissions(new_sample, time_delta, source_segment, target_segment, regex)
207
+ source_segment.called_segments.each do |source_called_segment|
208
+ # if this segment's metric name matches the given regular expression, bail
209
+ # here and increase the amount of time that we reduce the target sample with
210
+ # by this omitted segment's duration.
211
+ do_omit = regex =~ source_called_segment.metric_name
212
+
213
+ if do_omit
214
+ time_delta += source_called_segment.duration
215
+ else
216
+ target_called_segment = new_sample.create_segment(
217
+ source_called_segment.entry_timestamp - time_delta,
218
+ source_called_segment.metric_name,
219
+ source_called_segment.segment_id)
220
+
221
+ target_segment.add_called_segment target_called_segment
222
+ source_called_segment.params.each do |k,v|
223
+ target_called_segment[k]=v
224
+ end
225
+
226
+ time_delta = build_segment_with_omissions(
227
+ new_sample, time_delta, source_called_segment, target_called_segment, regex)
228
+ target_called_segment.end_trace(source_called_segment.exit_timestamp - time_delta)
229
+ end
230
+ end
231
+
232
+ return time_delta
233
+ end
234
+
235
+ # see prepare_to_send for what we do with options
236
+ def build_segment_for_transfer(new_sample, source_segment, target_segment, options)
237
+ source_segment.called_segments.each do |source_called_segment|
238
+ target_called_segment = new_sample.create_segment(
239
+ source_called_segment.entry_timestamp,
240
+ source_called_segment.metric_name,
241
+ source_called_segment.segment_id)
242
+
243
+ target_segment.add_called_segment target_called_segment
244
+ source_called_segment.params.each do |k,v|
245
+ case k
246
+ when :backtrace
247
+ target_called_segment[k]=v if options[:keep_backtraces]
248
+ when :sql
249
+ # run an EXPLAIN on this sql if specified.
250
+ if options[:record_sql] && options[:record_sql] && options[:explain_sql] && source_called_segment.duration > options[:explain_sql].to_f
251
+ target_called_segment[:explanation] = source_called_segment.explain_sql
252
+ end
253
+
254
+ target_called_segment[:sql] = case options[:record_sql]
255
+ when :raw then v
256
+ when :obfuscated then TransactionSample.obfuscate_sql(v)
257
+ else raise "Invalid value for record_sql: #{options[:record_sql]}"
258
+ end if options[:record_sql]
259
+ when :connection_config
260
+ # don't copy it
261
+ else
262
+ target_called_segment[k]=v
263
+ end
264
+ end
265
+
266
+ build_segment_for_transfer(new_sample, source_called_segment, target_called_segment, options)
267
+ target_called_segment.end_trace(source_called_segment.exit_timestamp)
268
+ end
269
+ end
270
+
271
+ end
272
+ end
@@ -0,0 +1,27 @@
1
+ require 'new_relic/transaction_sample'
2
+ require 'new_relic/transaction_sample/segment'
3
+ require 'new_relic/transaction_sample/summary_segment'
4
+ module NewRelic
5
+ class TransactionSample
6
+ class CompositeSegment < Segment
7
+ attr_reader :detail_segments
8
+
9
+ def initialize(segments)
10
+ summary = SummarySegment.new(segments.first)
11
+ super summary.entry_timestamp, "Repeating pattern (#{segments.length} repeats)", nil
12
+
13
+ summary.end_trace(segments.last.exit_timestamp)
14
+
15
+ @detail_segments = segments.clone
16
+
17
+ add_called_segment(summary)
18
+ end_trace summary.exit_timestamp
19
+ end
20
+
21
+ def detail_segments=(segments)
22
+ @detail_segments = segments
23
+ end
24
+
25
+ end
26
+ end
27
+ end