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,89 @@
1
+ module NewRelic
2
+ module Agent
3
+ class StatsEngine
4
+ module Shim # :nodoc:
5
+ def add_sampler(*args); end
6
+ def add_harvest_sampler(*args); end
7
+ def start_sampler_thread(*args); end
8
+ end
9
+
10
+ module Samplers
11
+
12
+ # By default a sampler polls on harvest time, once a minute. However you can
13
+ # override #use_harvest_sampler? to return false and it will sample
14
+ # every POLL_PERIOD seconds on a background thread.
15
+ POLL_PERIOD = 20
16
+
17
+ def start_sampler_thread
18
+
19
+ return if @sampler_thread && @sampler_thread.alive?
20
+
21
+ # start up a thread that will periodically poll for metric samples
22
+ return if periodic_samplers.empty?
23
+
24
+ @sampler_thread = Thread.new do
25
+ while true do
26
+ begin
27
+ sleep POLL_PERIOD
28
+ poll periodic_samplers
29
+ end
30
+ end
31
+ end
32
+ @sampler_thread['newrelic_label'] = 'Sampler Tasks'
33
+ end
34
+
35
+ private
36
+
37
+ def add_sampler_to(sampler_array, sampler)
38
+ raise "Sampler #{sampler.inspect} is already registered. Don't call add_sampler directly anymore." if sampler_array.include?(sampler)
39
+ sampler_array << sampler
40
+ sampler.stats_engine = self
41
+ end
42
+
43
+ def log_added_sampler(type, sampler)
44
+ log.debug "Adding #{type} sampler: #{sampler.inspect}"
45
+ end
46
+
47
+ public
48
+
49
+ # Add an instance of Sampler to be invoked about every 10 seconds on a background
50
+ # thread.
51
+ def add_sampler(sampler)
52
+ add_sampler_to(periodic_samplers, sampler)
53
+ log_added_sampler('periodic', sampler)
54
+ end
55
+
56
+ # Add a sampler to be invoked just before each harvest.
57
+ def add_harvest_sampler(sampler)
58
+ add_sampler_to(harvest_samplers, sampler)
59
+ log_added_sampler('harvest-time', sampler)
60
+ end
61
+
62
+ private
63
+
64
+ # Call poll on each of the samplers. Remove
65
+ # the sampler if it raises.
66
+ def poll(samplers)
67
+ samplers.delete_if do |sampled_item|
68
+ begin
69
+ sampled_item.poll
70
+ false # it's okay. don't delete it.
71
+ rescue Exception => e
72
+ log.error "Removing #{sampled_item} from list"
73
+ log.error e
74
+ log.debug e.backtrace.to_s
75
+ true # remove the sampler
76
+ end
77
+ end
78
+ end
79
+
80
+ def harvest_samplers
81
+ @harvest_samplers ||= []
82
+ end
83
+ def periodic_samplers
84
+ @periodic_samplers ||= []
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,184 @@
1
+ module NewRelic
2
+ module Agent
3
+ class StatsEngine
4
+
5
+ # Defines methods that stub out the stats engine methods
6
+ # when the agent is disabled
7
+
8
+ class ScopeStackElement
9
+ attr_reader :name, :deduct_call_time_from_parent
10
+ attr_accessor :children_time
11
+ def initialize(name, deduct_call_time)
12
+ @name = name
13
+ @deduct_call_time_from_parent = deduct_call_time
14
+ @children_time = 0
15
+ end
16
+ end
17
+
18
+ module Transactions
19
+ module Shim # :nodoc:
20
+ def start_transaction(*args); end
21
+ def end_transaction; end
22
+ def push_scope(*args); end
23
+ def transaction_sampler=(*args); end
24
+ def scope_name=(*args); end
25
+ def scope_name; end
26
+ def pop_scope(*args); end
27
+ end
28
+
29
+ def transaction_sampler= sampler
30
+ fail "Can't add a scope listener midflight in a transaction" if scope_stack.any?
31
+ @transaction_sampler = sampler
32
+ end
33
+
34
+ def remove_transaction_sampler(l)
35
+ @transaction_sampler = nil
36
+ end
37
+
38
+ def push_scope(metric, time = Time.now.to_f, deduct_call_time_from_parent = true)
39
+
40
+ stack = scope_stack
41
+ if collecting_gc?
42
+ if stack.empty?
43
+ # reset the gc time so we only include gc time spent during this call
44
+ @last_gc_timestamp = gc_time
45
+ @last_gc_count = gc_collections
46
+ else
47
+ capture_gc_time
48
+ end
49
+ end
50
+ @transaction_sampler.notice_push_scope metric, time if @transaction_sampler
51
+ scope = ScopeStackElement.new(metric, deduct_call_time_from_parent)
52
+ stack.push scope
53
+ scope
54
+ end
55
+
56
+ def pop_scope(expected_scope, duration, time=Time.now.to_f)
57
+ capture_gc_time if collecting_gc?
58
+ stack = scope_stack
59
+ scope = stack.pop
60
+ fail "unbalanced pop from blame stack, got #{scope ? scope.name : 'nil'}, expected #{expected_scope ? expected_scope.name : 'nil'}" if scope != expected_scope
61
+
62
+ if !stack.empty?
63
+ if scope.deduct_call_time_from_parent
64
+ stack.last.children_time += duration
65
+ else
66
+ stack.last.children_time += scope.children_time
67
+ end
68
+ end
69
+ @transaction_sampler.notice_pop_scope(scope.name, time) if @transaction_sampler
70
+ scope
71
+ end
72
+
73
+ def peek_scope
74
+ scope_stack.last
75
+ end
76
+
77
+ # set the name of the transaction for the current thread, which will be used
78
+ # to define the scope of all traced methods called on this thread until the
79
+ # scope stack is empty.
80
+ #
81
+ # currently the transaction name is the name of the controller action that
82
+ # is invoked via the dispatcher, but conceivably we could use other transaction
83
+ # names in the future if the traced application does more than service http request
84
+ # via controller actions
85
+ def scope_name=(transaction)
86
+ Thread::current[:newrelic_scope_name] = transaction
87
+ Thread::current[:newrelic_most_recent_transaction] = transaction
88
+ end
89
+
90
+ def scope_name
91
+ Thread::current[:newrelic_scope_name]
92
+ end
93
+
94
+ # Start a new transaction, unless one is already in progress
95
+ def start_transaction(name = nil)
96
+ Thread::current[:newrelic_scope_stack] ||= []
97
+ self.scope_name = name if name
98
+ end
99
+
100
+ # Try to clean up gracefully, otherwise we leave things hanging around on thread locals.
101
+ # If it looks like a transaction is still in progress, then maybe this is an inner transaction
102
+ # and is ignored.
103
+ #
104
+ def end_transaction
105
+ stack = scope_stack
106
+
107
+ if stack && stack.empty?
108
+ Thread::current[:newrelic_scope_stack] = nil
109
+ Thread::current[:newrelic_scope_name] = nil
110
+ end
111
+ end
112
+
113
+ private
114
+
115
+ # Make sure we don't do this in a multi-threaded environment
116
+ def collecting_gc?
117
+ if !defined?(@@collecting_gc)
118
+ @@collecting_gc = false
119
+ if !NewRelic::Control.instance.multi_threaded?
120
+ @@collecting_gc = true if GC.respond_to?(:time) && GC.respond_to?(:collections) # 1.8.x
121
+ @@collecting_gc = true if defined?(GC::Profiler) && GC::Profiler.enabled? # 1.9.2
122
+ end
123
+ end
124
+ @@collecting_gc
125
+ end
126
+
127
+ # The total number of times the garbage collector has run since
128
+ # profiling was enabled
129
+ def gc_collections
130
+ if GC.respond_to?(:count)
131
+ GC.count
132
+ elsif GC.respond_to?(:collections)
133
+ GC.collections
134
+ end
135
+ end
136
+
137
+ # The total amount of time taken by garbage collection since
138
+ # profiling was enabled
139
+ def gc_time
140
+ if GC.respond_to?(:time)
141
+ GC.time
142
+ elsif defined?(GC::Profiler) && GC::Profiler.respond_to?(:total_time)
143
+ # The 1.9 profiler returns a time in usec
144
+ GC::Profiler.total_time * 1000000.0
145
+ end
146
+ end
147
+
148
+ # Assumes collecting_gc?
149
+ def capture_gc_time
150
+ # Skip this if we are already in this segment
151
+ return if !scope_stack.empty? && scope_stack.last.name == "GC/cumulative"
152
+ num_calls = gc_collections - @last_gc_count
153
+ elapsed = (gc_time - @last_gc_timestamp).to_f
154
+ @last_gc_timestamp = gc_time
155
+ @last_gc_count = gc_collections
156
+
157
+ if defined?(GC::Profiler)
158
+ GC::Profiler.clear
159
+ @last_gc_timestamp = 0
160
+ end
161
+
162
+ if num_calls > 0
163
+ # µs to seconds
164
+ elapsed = elapsed / 1000000.0
165
+ # Allocate the GC time to a scope as if the GC just ended
166
+ # right now.
167
+ time = Time.now.to_f
168
+ gc_scope = push_scope("GC/cumulative", time - elapsed)
169
+ # GC stats are collected into a blamed metric which allows
170
+ # us to show the stats controller by controller
171
+ gc_stats = NewRelic::Agent.get_stats(gc_scope.name, true)
172
+ gc_stats.record_multiple_data_points(elapsed, num_calls)
173
+ pop_scope(gc_scope, elapsed, time)
174
+ end
175
+ end
176
+
177
+ def scope_stack
178
+ Thread::current[:newrelic_scope_stack] ||= []
179
+ end
180
+
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,101 @@
1
+ require 'new_relic/collection_helper'
2
+ require 'new_relic/transaction_sample'
3
+ require 'new_relic/control'
4
+ require 'new_relic/agent/instrumentation/metric_frame'
5
+ module NewRelic
6
+ module Agent
7
+ # a builder is created with every sampled transaction, to dynamically
8
+ # generate the sampled data. It is a thread-local object, and is not
9
+ # accessed by any other thread so no need for synchronization.
10
+ class TransactionSampleBuilder
11
+ attr_reader :current_segment, :sample
12
+
13
+ include NewRelic::CollectionHelper
14
+
15
+ def initialize(time=Time.now)
16
+ @sample = NewRelic::TransactionSample.new(time.to_f)
17
+ @sample_start = time.to_f
18
+ @current_segment = @sample.root_segment
19
+ end
20
+
21
+ def sample_id
22
+ @sample.sample_id
23
+ end
24
+ def ignored?
25
+ @ignore || @sample.params[:path].nil?
26
+ end
27
+ def ignore_transaction
28
+ @ignore = true
29
+ end
30
+ def trace_entry(metric_name, time)
31
+ segment = @sample.create_segment(time.to_f - @sample_start, metric_name)
32
+ @current_segment.add_called_segment(segment)
33
+ @current_segment = segment
34
+ end
35
+
36
+ def trace_exit(metric_name, time)
37
+ if metric_name != @current_segment.metric_name
38
+ fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
39
+ end
40
+ @current_segment.end_trace(time.to_f - @sample_start)
41
+ @current_segment = @current_segment.parent_segment
42
+ end
43
+
44
+ def finish_trace(time)
45
+ # This should never get called twice, but in a rare case that we can't reproduce in house it does.
46
+ # log forensics and return gracefully
47
+ if @sample.frozen?
48
+ log = NewRelic::Control.instance.log
49
+ log.error "Unexpected double-freeze of Transaction Trace Object: \n#{@sample.to_s}"
50
+ return
51
+ end
52
+ @sample.root_segment.end_trace(time.to_f - @sample_start)
53
+ @sample.params[:custom_params] = normalize_params(NewRelic::Agent::Instrumentation::MetricFrame.custom_parameters)
54
+ @sample.freeze
55
+ @current_segment = nil
56
+ end
57
+
58
+ def scope_depth
59
+ depth = -1 # have to account for the root
60
+ current = @current_segment
61
+
62
+ while(current)
63
+ depth += 1
64
+ current = current.parent_segment
65
+ end
66
+
67
+ depth
68
+ end
69
+
70
+ def freeze
71
+ @sample.freeze unless sample.frozen?
72
+ end
73
+
74
+ def set_profile(profile)
75
+ @sample.profile = profile
76
+ end
77
+
78
+ def set_transaction_info(path, uri, params)
79
+ @sample.params[:path] = path
80
+
81
+ if NewRelic::Control.instance.capture_params
82
+ params = normalize_params params
83
+
84
+ @sample.params[:request_params].merge!(params)
85
+ @sample.params[:request_params].delete :controller
86
+ @sample.params[:request_params].delete :action
87
+ end
88
+ @sample.params[:uri] ||= uri || params[:uri]
89
+ end
90
+
91
+ def set_transaction_cpu_time(cpu_time)
92
+ @sample.params[:cpu_time] = cpu_time
93
+ end
94
+
95
+ def sample
96
+ @sample
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,378 @@
1
+ require 'new_relic/agent'
2
+ require 'new_relic/control'
3
+ require 'new_relic/agent/transaction_sample_builder'
4
+ module NewRelic
5
+ module Agent
6
+
7
+ class TransactionSampler
8
+
9
+ # Module defining methods stubbed out when the agent is disabled
10
+ module Shim #:nodoc:
11
+ def notice_first_scope_push(*args); end
12
+ def notice_push_scope(*args); end
13
+ def notice_pop_scope(*args); end
14
+ def notice_scope_empty(*args); end
15
+ end
16
+
17
+ BUILDER_KEY = :transaction_sample_builder
18
+
19
+ attr_accessor :stack_trace_threshold, :random_sampling, :sampling_rate
20
+ attr_reader :samples, :last_sample, :disabled
21
+
22
+ def initialize
23
+ # @samples is an array of recent samples up to @max_samples in
24
+ # size - it's only used by developer mode
25
+ @samples = []
26
+ @max_samples = 100
27
+
28
+ # @harvest_count is a count of harvests used for random
29
+ # sampling - we pull 1 @random_sample in every @sampling_rate harvests
30
+ @harvest_count = 0
31
+ @random_sample = nil
32
+ @sampling_rate = 10
33
+
34
+ # @segment_limit and @stack_trace_threshold come from the
35
+ # configuration file, with built-in defaults that should
36
+ # suffice for most customers
37
+ config = NewRelic::Control.instance
38
+ sampler_config = config.fetch('transaction_tracer', {})
39
+ @segment_limit = sampler_config.fetch('limit_segments', 4000)
40
+ @stack_trace_threshold = sampler_config.fetch('stack_trace_threshold', 0.500).to_f
41
+
42
+ # This lock is used to synchronize access to the @last_sample
43
+ # and related variables. It can become necessary on JRuby or
44
+ # any 'honest-to-god'-multithreaded system
45
+ @samples_lock = Mutex.new
46
+ end
47
+
48
+ # Returns the current sample id, delegated from `builder`
49
+ def current_sample_id
50
+ b=builder
51
+ b and b.sample_id
52
+ end
53
+
54
+ # Enable the transaction sampler - this also registers it with
55
+ # the statistics engine.
56
+ def enable
57
+ @disabled = false
58
+ NewRelic::Agent.instance.stats_engine.transaction_sampler = self
59
+ end
60
+
61
+ # Disable the transaction sampler - this also deregisters it
62
+ # with the statistics engine.
63
+ def disable
64
+ @disabled = true
65
+ NewRelic::Agent.instance.stats_engine.remove_transaction_sampler(self)
66
+ end
67
+
68
+ # Set with an integer value n, this takes one in every n
69
+ # harvested samples. It also resets the harvest count to a
70
+ # random integer between 0 and (n-1)
71
+ def sampling_rate=(val)
72
+ @sampling_rate = val.to_i
73
+ @harvest_count = rand(val.to_i).to_i
74
+ end
75
+
76
+
77
+ # Creates a new transaction sample builder, unless the
78
+ # transaction sampler is disabled. Takes a time parameter for
79
+ # the start of the transaction sample
80
+ def notice_first_scope_push(time)
81
+ start_builder(time.to_f) unless disabled
82
+ end
83
+
84
+ # This delegates to the builder to create a new open transaction
85
+ # segment for the specified scope, beginning at the optionally
86
+ # specified time.
87
+ #
88
+ # Note that in developer mode, this captures a stacktrace for
89
+ # the beginning of each segment, which can be fairly slow
90
+ def notice_push_scope(scope, time=Time.now)
91
+ return unless builder
92
+
93
+ builder.trace_entry(scope, time.to_f)
94
+
95
+ capture_segment_trace if NewRelic::Control.instance.developer_mode?
96
+ end
97
+
98
+ # in developer mode, capture the stack trace with the segment.
99
+ # this is cpu and memory expensive and therefore should not be
100
+ # turned on in production mode
101
+ def capture_segment_trace
102
+ return unless NewRelic::Control.instance.developer_mode?
103
+ segment = builder.current_segment
104
+ if segment
105
+ # Strip stack frames off the top that match /new_relic/agent/
106
+ trace = caller
107
+ while trace.first =~/\/lib\/new_relic\/agent\//
108
+ trace.shift
109
+ end
110
+
111
+ trace = trace[0..39] if trace.length > 40
112
+ segment[:backtrace] = trace
113
+ end
114
+ end
115
+
116
+ # Defaults to zero, otherwise delegated to the transaction
117
+ # sample builder
118
+ def scope_depth
119
+ return 0 unless builder
120
+
121
+ builder.scope_depth
122
+ end
123
+
124
+ # Informs the transaction sample builder about the end of a
125
+ # traced scope
126
+ def notice_pop_scope(scope, time = Time.now)
127
+ return unless builder
128
+ raise "frozen already???" if builder.sample.frozen?
129
+ builder.trace_exit(scope, time.to_f)
130
+ end
131
+
132
+ # This is called when we are done with the transaction. We've
133
+ # unwound the stack to the top level. It also clears the
134
+ # transaction sample builder so that it won't continue to have
135
+ # scopes appended to it.
136
+ #
137
+ # It sets various instance variables to the finished sample,
138
+ # depending on which settings are active. See `store_sample`
139
+ def notice_scope_empty(time=Time.now)
140
+
141
+ last_builder = builder
142
+ return unless last_builder
143
+
144
+ last_builder.finish_trace(time.to_f)
145
+ clear_builder
146
+ return if last_builder.ignored?
147
+
148
+ @samples_lock.synchronize do
149
+ # NB this instance variable may be used elsewhere, it's not
150
+ # just a side effect
151
+ @last_sample = last_builder.sample
152
+ store_sample(@last_sample)
153
+ end
154
+ end
155
+
156
+ # Samples can be stored in three places: the random sample
157
+ # variable, when random sampling is active, the developer mode
158
+ # @samples array, and the @slowest_sample variable if it is
159
+ # slower than the current occupant of that slot
160
+ def store_sample(sample)
161
+ store_random_sample(sample)
162
+ store_sample_for_developer_mode(sample)
163
+ store_slowest_sample(sample)
164
+ end
165
+
166
+ # Only active when random sampling is true - this is very rarely
167
+ # used. Always store the most recent sample so that random
168
+ # sampling can pick a few of the samples to store, upon harvest
169
+ def store_random_sample(sample)
170
+ if @random_sampling
171
+ @random_sample = sample
172
+ end
173
+ end
174
+
175
+ # Samples take up a ton of memory, so we only store a lot of
176
+ # them in developer mode - we truncate to @max_samples
177
+ def store_sample_for_developer_mode(sample)
178
+ return unless NewRelic::Control.instance.developer_mode?
179
+ @samples = [] unless @samples
180
+ @samples << sample
181
+ truncate_samples
182
+ end
183
+
184
+ # Sets @slowest_sample to the passed in sample if it is slower
185
+ # than the current sample in @slowest_sample
186
+ def store_slowest_sample(sample)
187
+ @slowest_sample = sample if slowest_sample?(@slowest_sample, sample)
188
+ end
189
+
190
+ # Checks to see if the old sample exists, or if it's duration is
191
+ # less than the new sample
192
+ def slowest_sample?(old_sample, new_sample)
193
+ old_sample.nil? || (new_sample.duration > old_sample.duration)
194
+ end
195
+
196
+ # Smashes the @samples array down to the length of @max_samples
197
+ # by taking the last @max_samples elements of the array
198
+ def truncate_samples
199
+ if @samples.length > @max_samples
200
+ @samples = @samples[-@max_samples..-1]
201
+ end
202
+ end
203
+
204
+ # Delegates to the builder to store the path, uri, and
205
+ # parameters if the sampler is active
206
+ def notice_transaction(path, uri=nil, params={})
207
+ builder.set_transaction_info(path, uri, params) if !disabled && builder
208
+ end
209
+
210
+ # Tells the builder to ignore a transaction, if we are currently
211
+ # creating one. Only causes the sample to be ignored upon end of
212
+ # the transaction, and does not change the metrics gathered
213
+ # outside of the sampler
214
+ def ignore_transaction
215
+ builder.ignore_transaction if builder
216
+ end
217
+
218
+ # For developer mode profiling support - delegates to the builder
219
+ def notice_profile(profile)
220
+ builder.set_profile(profile) if builder
221
+ end
222
+
223
+ # Sets the CPU time used by a transaction, delegates to the builder
224
+ def notice_transaction_cpu_time(cpu_time)
225
+ builder.set_transaction_cpu_time(cpu_time) if builder
226
+ end
227
+
228
+ MAX_DATA_LENGTH = 16384
229
+ # This method is used to record metadata into the currently
230
+ # active segment like a sql query, memcache key, or Net::HTTP uri
231
+ #
232
+ # duration is seconds, float value.
233
+ def notice_extra_data(message, duration, key, config=nil, config_key=nil)
234
+ return unless builder
235
+ segment = builder.current_segment
236
+ if segment
237
+ segment[key] = truncate_message(append_new_message(segment[key], message))
238
+ segment[config_key] = config if config_key
239
+ append_backtrace(segment, duration)
240
+ end
241
+ end
242
+
243
+ private :notice_extra_data
244
+
245
+ # Truncates the message to `MAX_DATA_LENGTH` if needed, and
246
+ # appends an ellipsis because it makes the trucation clearer in
247
+ # the UI
248
+ def truncate_message(message)
249
+ if message.length > (MAX_DATA_LENGTH - 4)
250
+ message[0..MAX_DATA_LENGTH - 4] + '...'
251
+ else
252
+ message
253
+ end
254
+ end
255
+
256
+ # Allows the addition of multiple pieces of metadata to one
257
+ # segment - i.e. traced method calls multiple sql queries
258
+ def append_new_message(old_message, message)
259
+ if old_message
260
+ old_message + ";\n" + message
261
+ else
262
+ message
263
+ end
264
+ end
265
+
266
+ # Appends a backtrace to a segment if that segment took longer
267
+ # than the specified duration
268
+ def append_backtrace(segment, duration)
269
+ segment[:backtrace] = caller.join("\n") if duration >= @stack_trace_threshold
270
+ end
271
+
272
+ # some statements (particularly INSERTS with large BLOBS
273
+ # may be very large; we should trim them to a maximum usable length
274
+ # config is the driver configuration for the connection
275
+ # duration is seconds, float value.
276
+ def notice_sql(sql, config, duration)
277
+ if NewRelic::Agent.is_sql_recorded?
278
+ notice_extra_data(sql, duration, :sql, config, :connection_config)
279
+ end
280
+ end
281
+
282
+ # Adds non-sql metadata to a segment - generally the memcached
283
+ # key
284
+ #
285
+ # duration is seconds, float value.
286
+ def notice_nosql(key, duration)
287
+ notice_extra_data(key, duration, :key)
288
+ end
289
+
290
+ # Every 1/n harvests, adds the most recent sample to the harvest
291
+ # array if it exists. Makes sure that the random sample is not
292
+ # also the slowest sample for this harvest by `uniq!`ing the
293
+ # result array
294
+ #
295
+ # random sampling is very, very seldom used
296
+ def add_random_sample_to(result)
297
+ return unless @random_sampling && @sampling_rate && @sampling_rate.to_i > 0
298
+ @harvest_count += 1
299
+ if (@harvest_count.to_i % @sampling_rate.to_i) == 0
300
+ result << @random_sample if @random_sample
301
+ end
302
+ result.uniq!
303
+ nil # don't assume this method returns anything
304
+ end
305
+
306
+ # Returns an array of slow samples, with either one or two
307
+ # elements - one element unless random sampling is enabled. The
308
+ # sample returned will be the slowest sample among those
309
+ # available during this harvest
310
+ def add_samples_to(result, slow_threshold)
311
+ if @slowest_sample && @slowest_sample.duration >= slow_threshold
312
+ result << @slowest_sample
313
+ end
314
+ result.compact!
315
+ result = result.sort_by { |x| x.duration }
316
+ result = result[-1..-1] || []
317
+ add_random_sample_to(result)
318
+ result
319
+ end
320
+
321
+ # get the set of collected samples, merging into previous samples,
322
+ # and clear the collected sample list. Truncates samples to a
323
+ # specified @segment_limit to save memory and bandwith
324
+ # transmitting samples to the server.
325
+ def harvest(previous = [], slow_threshold = 2.0)
326
+ return [] if disabled
327
+ result = Array(previous)
328
+ @samples_lock.synchronize do
329
+ result = add_samples_to(result, slow_threshold)
330
+ # clear previous transaction samples
331
+ @slowest_sample = nil
332
+ @random_sample = nil
333
+ @last_sample = nil
334
+ end
335
+ # Truncate the samples at 2100 segments. The UI will clamp them at 2000 segments anyway.
336
+ # This will save us memory and bandwidth.
337
+ result.each { |sample| sample.truncate(@segment_limit) }
338
+ result
339
+ end
340
+
341
+ # reset samples without rebooting the web server
342
+ def reset!
343
+ @samples = []
344
+ @last_sample = nil
345
+ @random_sample = nil
346
+ @slowest_sample = nil
347
+ end
348
+
349
+ private
350
+
351
+ # Checks to see if the transaction sampler is disabled, if
352
+ # transaction trace recording is disabled by a thread local, or
353
+ # if execution is untraced - if so it clears the transaction
354
+ # sample builder from the thread local, otherwise it generates a
355
+ # new transaction sample builder with the stated time as a
356
+ # starting point and saves it in the thread local variable
357
+ def start_builder(time=nil)
358
+ if disabled || !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced?
359
+ clear_builder
360
+ else
361
+ Thread::current[BUILDER_KEY] ||= TransactionSampleBuilder.new(time)
362
+ end
363
+ end
364
+
365
+ # The current thread-local transaction sample builder
366
+ def builder
367
+ Thread::current[BUILDER_KEY]
368
+ end
369
+
370
+ # Sets the thread local variable storing the transaction sample
371
+ # builder to nil to clear it
372
+ def clear_builder
373
+ Thread::current[BUILDER_KEY] = nil
374
+ end
375
+
376
+ end
377
+ end
378
+ end