wd_newrelic_rpm 3.3.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. data/CHANGELOG +591 -0
  2. data/LICENSE +64 -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 +467 -0
  14. data/lib/new_relic/agent/agent.rb +1325 -0
  15. data/lib/new_relic/agent/beacon_configuration.rb +121 -0
  16. data/lib/new_relic/agent/browser_monitoring.rb +142 -0
  17. data/lib/new_relic/agent/busy_calculator.rb +99 -0
  18. data/lib/new_relic/agent/chained_call.rb +13 -0
  19. data/lib/new_relic/agent/database.rb +223 -0
  20. data/lib/new_relic/agent/error_collector.rb +251 -0
  21. data/lib/new_relic/agent/instrumentation.rb +9 -0
  22. data/lib/new_relic/agent/instrumentation/active_merchant.rb +29 -0
  23. data/lib/new_relic/agent/instrumentation/active_record.rb +137 -0
  24. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +68 -0
  25. data/lib/new_relic/agent/instrumentation/authlogic.rb +19 -0
  26. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +443 -0
  27. data/lib/new_relic/agent/instrumentation/data_mapper.rb +238 -0
  28. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +52 -0
  29. data/lib/new_relic/agent/instrumentation/memcache.rb +80 -0
  30. data/lib/new_relic/agent/instrumentation/merb/controller.rb +41 -0
  31. data/lib/new_relic/agent/instrumentation/merb/errors.rb +29 -0
  32. data/lib/new_relic/agent/instrumentation/metric_frame.rb +353 -0
  33. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +80 -0
  34. data/lib/new_relic/agent/instrumentation/net.rb +29 -0
  35. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +37 -0
  36. data/lib/new_relic/agent/instrumentation/queue_time.rb +210 -0
  37. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  38. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +114 -0
  39. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +42 -0
  40. data/lib/new_relic/agent/instrumentation/rails/errors.rb +42 -0
  41. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +180 -0
  42. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +37 -0
  43. data/lib/new_relic/agent/instrumentation/sinatra.rb +78 -0
  44. data/lib/new_relic/agent/instrumentation/sunspot.rb +29 -0
  45. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +21 -0
  46. data/lib/new_relic/agent/method_tracer.rb +528 -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 +143 -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/sql_sampler.rb +286 -0
  54. data/lib/new_relic/agent/stats_engine.rb +26 -0
  55. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +123 -0
  56. data/lib/new_relic/agent/stats_engine/metric_stats.rb +187 -0
  57. data/lib/new_relic/agent/stats_engine/samplers.rb +95 -0
  58. data/lib/new_relic/agent/stats_engine/transactions.rb +125 -0
  59. data/lib/new_relic/agent/transaction_info.rb +74 -0
  60. data/lib/new_relic/agent/transaction_sample_builder.rb +116 -0
  61. data/lib/new_relic/agent/transaction_sampler.rb +468 -0
  62. data/lib/new_relic/agent/worker_loop.rb +89 -0
  63. data/lib/new_relic/collection_helper.rb +77 -0
  64. data/lib/new_relic/command.rb +85 -0
  65. data/lib/new_relic/commands/deployments.rb +105 -0
  66. data/lib/new_relic/commands/install.rb +80 -0
  67. data/lib/new_relic/control.rb +46 -0
  68. data/lib/new_relic/control/class_methods.rb +53 -0
  69. data/lib/new_relic/control/configuration.rb +206 -0
  70. data/lib/new_relic/control/frameworks.rb +10 -0
  71. data/lib/new_relic/control/frameworks/external.rb +16 -0
  72. data/lib/new_relic/control/frameworks/merb.rb +31 -0
  73. data/lib/new_relic/control/frameworks/rails.rb +164 -0
  74. data/lib/new_relic/control/frameworks/rails3.rb +75 -0
  75. data/lib/new_relic/control/frameworks/ruby.rb +42 -0
  76. data/lib/new_relic/control/frameworks/sinatra.rb +20 -0
  77. data/lib/new_relic/control/instance_methods.rb +179 -0
  78. data/lib/new_relic/control/instrumentation.rb +100 -0
  79. data/lib/new_relic/control/logging_methods.rb +143 -0
  80. data/lib/new_relic/control/profiling.rb +25 -0
  81. data/lib/new_relic/control/server_methods.rb +114 -0
  82. data/lib/new_relic/data_serialization.rb +151 -0
  83. data/lib/new_relic/delayed_job_injection.rb +51 -0
  84. data/lib/new_relic/language_support.rb +73 -0
  85. data/lib/new_relic/local_environment.rb +428 -0
  86. data/lib/new_relic/merbtasks.rb +6 -0
  87. data/lib/new_relic/metric_data.rb +51 -0
  88. data/lib/new_relic/metric_spec.rb +76 -0
  89. data/lib/new_relic/metrics.rb +9 -0
  90. data/lib/new_relic/noticed_error.rb +29 -0
  91. data/lib/new_relic/rack/browser_monitoring.rb +76 -0
  92. data/lib/new_relic/rack/developer_mode.rb +268 -0
  93. data/lib/new_relic/recipes.rb +77 -0
  94. data/lib/new_relic/stats.rb +335 -0
  95. data/lib/new_relic/timer_lib.rb +27 -0
  96. data/lib/new_relic/transaction_analysis.rb +77 -0
  97. data/lib/new_relic/transaction_analysis/segment_summary.rb +49 -0
  98. data/lib/new_relic/transaction_sample.rb +261 -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 +203 -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 +312 -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/fixtures/proc_cpuinfo.txt +575 -0
  116. data/test/new_relic/agent/agent/connect_test.rb +403 -0
  117. data/test/new_relic/agent/agent/start_test.rb +255 -0
  118. data/test/new_relic/agent/agent/start_worker_thread_test.rb +153 -0
  119. data/test/new_relic/agent/agent_test.rb +140 -0
  120. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  121. data/test/new_relic/agent/agent_test_controller_test.rb +382 -0
  122. data/test/new_relic/agent/apdex_from_server_test.rb +9 -0
  123. data/test/new_relic/agent/beacon_configuration_test.rb +111 -0
  124. data/test/new_relic/agent/browser_monitoring_test.rb +323 -0
  125. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  126. data/test/new_relic/agent/database_test.rb +149 -0
  127. data/test/new_relic/agent/error_collector/notice_error_test.rb +257 -0
  128. data/test/new_relic/agent/error_collector_test.rb +192 -0
  129. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +576 -0
  130. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +34 -0
  131. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +11 -0
  132. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +171 -0
  133. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +50 -0
  134. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +84 -0
  135. data/test/new_relic/agent/instrumentation/queue_time_test.rb +382 -0
  136. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  137. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +184 -0
  138. data/test/new_relic/agent/memcache_instrumentation_test.rb +143 -0
  139. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +164 -0
  140. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +234 -0
  141. data/test/new_relic/agent/method_tracer_test.rb +386 -0
  142. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  143. data/test/new_relic/agent/rpm_agent_test.rb +149 -0
  144. data/test/new_relic/agent/sampler_test.rb +19 -0
  145. data/test/new_relic/agent/shim_agent_test.rb +20 -0
  146. data/test/new_relic/agent/sql_sampler_test.rb +192 -0
  147. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +150 -0
  148. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +82 -0
  149. data/test/new_relic/agent/stats_engine/samplers_test.rb +99 -0
  150. data/test/new_relic/agent/stats_engine_test.rb +220 -0
  151. data/test/new_relic/agent/transaction_info_test.rb +13 -0
  152. data/test/new_relic/agent/transaction_sample_builder_test.rb +219 -0
  153. data/test/new_relic/agent/transaction_sampler_test.rb +967 -0
  154. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  155. data/test/new_relic/agent_test.rb +187 -0
  156. data/test/new_relic/collection_helper_test.rb +149 -0
  157. data/test/new_relic/command/deployments_test.rb +68 -0
  158. data/test/new_relic/control/class_methods_test.rb +62 -0
  159. data/test/new_relic/control/configuration_test.rb +84 -0
  160. data/test/new_relic/control/logging_methods_test.rb +185 -0
  161. data/test/new_relic/control_test.rb +256 -0
  162. data/test/new_relic/data_serialization_test.rb +208 -0
  163. data/test/new_relic/delayed_job_injection_test.rb +16 -0
  164. data/test/new_relic/local_environment_test.rb +85 -0
  165. data/test/new_relic/metric_data_test.rb +125 -0
  166. data/test/new_relic/metric_parser/metric_parser_test.rb +11 -0
  167. data/test/new_relic/metric_spec_test.rb +95 -0
  168. data/test/new_relic/rack/all_test.rb +11 -0
  169. data/test/new_relic/rack/browser_monitoring_test.rb +142 -0
  170. data/test/new_relic/rack/developer_mode_helper_test.rb +141 -0
  171. data/test/new_relic/rack/developer_mode_test.rb +74 -0
  172. data/test/new_relic/stats_test.rb +411 -0
  173. data/test/new_relic/transaction_analysis/segment_summary_test.rb +91 -0
  174. data/test/new_relic/transaction_analysis_test.rb +121 -0
  175. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  176. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  177. data/test/new_relic/transaction_sample/segment_test.rb +389 -0
  178. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  179. data/test/new_relic/transaction_sample_subtest_test.rb +56 -0
  180. data/test/new_relic/transaction_sample_test.rb +177 -0
  181. data/test/new_relic/version_number_test.rb +89 -0
  182. data/test/script/build_test_gem.sh +51 -0
  183. data/test/script/ci.sh +94 -0
  184. data/test/script/ci_bench.sh +52 -0
  185. data/test/test_contexts.rb +29 -0
  186. data/test/test_helper.rb +155 -0
  187. data/ui/helpers/developer_mode_helper.rb +357 -0
  188. data/ui/helpers/google_pie_chart.rb +48 -0
  189. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  190. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  191. data/ui/views/newrelic/_sample.rhtml +20 -0
  192. data/ui/views/newrelic/_segment.rhtml +28 -0
  193. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  194. data/ui/views/newrelic/_segment_row.rhtml +12 -0
  195. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  196. data/ui/views/newrelic/_show_sample_sql.rhtml +24 -0
  197. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  198. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  199. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  200. data/ui/views/newrelic/_table.rhtml +12 -0
  201. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  202. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  203. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  204. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  205. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  206. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  207. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  208. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  209. data/ui/views/newrelic/file/images/textmate.png +0 -0
  210. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  211. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  212. data/ui/views/newrelic/file/stylesheets/style.css +490 -0
  213. data/ui/views/newrelic/index.rhtml +71 -0
  214. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  215. data/ui/views/newrelic/show_sample.rhtml +80 -0
  216. data/ui/views/newrelic/show_source.rhtml +3 -0
  217. data/ui/views/newrelic/threads.rhtml +53 -0
  218. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +5 -0
  219. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +67 -0
  220. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection/version.rb +3 -0
  221. data/vendor/gems/metric_parser-0.1.0.pre1/lib/metric_parser.rb +1 -0
  222. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +64 -0
  223. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +14 -0
  224. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +31 -0
  225. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +33 -0
  226. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +89 -0
  227. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +7 -0
  228. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +46 -0
  229. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +67 -0
  230. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +43 -0
  231. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +17 -0
  232. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +48 -0
  233. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +24 -0
  234. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +28 -0
  235. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +17 -0
  236. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/errors.rb +11 -0
  237. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +55 -0
  238. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +40 -0
  239. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +20 -0
  240. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +7 -0
  241. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +31 -0
  242. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +17 -0
  243. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +34 -0
  244. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +7 -0
  245. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +55 -0
  246. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +135 -0
  247. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +27 -0
  248. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +40 -0
  249. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +7 -0
  250. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +7 -0
  251. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +7 -0
  252. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +27 -0
  253. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +15 -0
  254. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +54 -0
  255. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_controller.rb +6 -0
  256. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_view.rb +6 -0
  257. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +20 -0
  258. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +20 -0
  259. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/version.rb +5 -0
  260. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +70 -0
  261. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_frontend.rb +18 -0
  262. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_service.rb +14 -0
  263. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +133 -0
  264. metadata +376 -0
@@ -0,0 +1,74 @@
1
+ require 'erb'
2
+
3
+ module NewRelic
4
+ module Agent
5
+ class TransactionInfo
6
+
7
+ attr_accessor :token, :capture_deep_tt, :transaction_name
8
+ attr_reader :start_time
9
+
10
+ def initialize
11
+ @guid = ""
12
+ @transaction_name = "(unknown)"
13
+ @start_time = Time.now
14
+ end
15
+
16
+ def force_persist_sample?(sample)
17
+ token && sample.duration > NewRelic::Control.instance.apdex_t
18
+ end
19
+
20
+ def include_guid?
21
+ token && duration > NewRelic::Control.instance.apdex_t
22
+ end
23
+
24
+ def guid
25
+ @guid
26
+ end
27
+
28
+ def guid=(value)
29
+ @guid = value
30
+ end
31
+
32
+ def duration
33
+ Time.now - start_time
34
+ end
35
+
36
+ def self.get()
37
+ Thread.current[:newrelic_transaction_info] ||= TransactionInfo.new
38
+ end
39
+
40
+ def self.set(instance)
41
+ Thread.current[:newrelic_transaction_info] = instance
42
+ end
43
+
44
+ def self.clear
45
+ Thread.current[:newrelic_transaction_info] = nil
46
+ end
47
+
48
+ # clears any existing transaction info object and initializes a new one.
49
+ # This starts the timer for the transaction.
50
+ def self.reset(request=nil)
51
+ clear
52
+ instance = get
53
+ instance.token = get_token(request)
54
+ end
55
+
56
+ def self.get_token(request)
57
+ return nil unless request
58
+
59
+ agent_flag = request.cookies['NRAGENT']
60
+ if agent_flag
61
+ s = agent_flag.split("=")
62
+ if s.length == 2
63
+ if s[0] == "tk" && s[1]
64
+ ERB::Util.h(s[1])
65
+ end
66
+ end
67
+ else
68
+ nil
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,116 @@
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
+ attr_accessor :segment_limit
13
+
14
+ include NewRelic::CollectionHelper
15
+
16
+ def initialize(time=Time.now)
17
+ @sample = NewRelic::TransactionSample.new(time.to_f)
18
+ @sample_start = time.to_f
19
+ @current_segment = @sample.root_segment
20
+ @segment_limit = NewRelic::Control.instance.fetch('transaction_tracer', {}) \
21
+ .fetch('limit_segments', 4000)
22
+ end
23
+
24
+ def sample_id
25
+ @sample.sample_id
26
+ end
27
+
28
+ def ignored?
29
+ @ignore || @sample.params[:path].nil?
30
+ end
31
+
32
+ def ignore_transaction
33
+ @ignore = true
34
+ end
35
+
36
+ def trace_entry(metric_name, time)
37
+ if @sample.count_segments < @segment_limit
38
+ segment = @sample.create_segment(time.to_f - @sample_start, metric_name)
39
+ @current_segment.add_called_segment(segment)
40
+ @current_segment = segment
41
+ if @sample.count_segments == @segment_limit
42
+ NewRelic::Control.instance.log.debug("Segment limit of #{@segment_limit} reached, ceasing collection.")
43
+ end
44
+ @current_segment
45
+ end
46
+ end
47
+
48
+ def trace_exit(metric_name, time)
49
+ return unless @sample.count_segments < @segment_limit
50
+ if metric_name != @current_segment.metric_name
51
+ fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
52
+ end
53
+ @current_segment.end_trace(time.to_f - @sample_start)
54
+ @current_segment = @current_segment.parent_segment
55
+ end
56
+
57
+ def finish_trace(time)
58
+ # This should never get called twice, but in a rare case that we can't reproduce in house it does.
59
+ # log forensics and return gracefully
60
+ if @sample.frozen?
61
+ log = NewRelic::Control.instance.log
62
+ log.error "Unexpected double-freeze of Transaction Trace Object: \n#{@sample.to_s}"
63
+ return
64
+ end
65
+ @sample.root_segment.end_trace(time.to_f - @sample_start)
66
+ @sample.params[:custom_params] = normalize_params(NewRelic::Agent::Instrumentation::MetricFrame.custom_parameters)
67
+
68
+ @sample.force_persist = NewRelic::Agent::TransactionInfo.get.force_persist_sample?(sample)
69
+ @sample.freeze
70
+ @current_segment = nil
71
+ end
72
+
73
+ def scope_depth
74
+ depth = -1 # have to account for the root
75
+ current = @current_segment
76
+
77
+ while(current)
78
+ depth += 1
79
+ current = current.parent_segment
80
+ end
81
+
82
+ depth
83
+ end
84
+
85
+ def freeze
86
+ @sample.freeze unless sample.frozen?
87
+ end
88
+
89
+ def set_profile(profile)
90
+ @sample.profile = profile
91
+ end
92
+
93
+ def set_transaction_info(path, uri, params)
94
+ @sample.params[:path] = path
95
+
96
+ if NewRelic::Control.instance.capture_params
97
+ params = normalize_params params
98
+
99
+ @sample.params[:request_params].merge!(params)
100
+ @sample.params[:request_params].delete :controller
101
+ @sample.params[:request_params].delete :action
102
+ end
103
+ @sample.params[:uri] ||= uri || params[:uri]
104
+ end
105
+
106
+ def set_transaction_cpu_time(cpu_time)
107
+ @sample.params[:cpu_time] = cpu_time
108
+ end
109
+
110
+ def sample
111
+ @sample
112
+ end
113
+
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,468 @@
1
+ require 'new_relic/agent'
2
+ require 'new_relic/control'
3
+ require 'new_relic/agent/transaction_sample_builder'
4
+
5
+ module NewRelic
6
+ module Agent
7
+
8
+ # This class contains the logic of sampling a transaction -
9
+ # creation and modification of transaction samples
10
+ class TransactionSampler
11
+
12
+ # Module defining methods stubbed out when the agent is disabled
13
+ module Shim #:nodoc:
14
+ def notice_first_scope_push(*args); end
15
+ def notice_push_scope(*args); end
16
+ def notice_pop_scope(*args); end
17
+ def notice_scope_empty(*args); end
18
+ end
19
+
20
+ BUILDER_KEY = :transaction_sample_builder
21
+
22
+ attr_accessor :stack_trace_threshold, :random_sampling, :sampling_rate
23
+ attr_accessor :explain_threshold, :explain_enabled, :transaction_threshold
24
+ attr_accessor :slow_capture_threshold
25
+ attr_reader :samples, :last_sample, :disabled
26
+
27
+ def initialize
28
+
29
+ # @samples is an array of recent samples up to @max_samples in
30
+ # size - it's only used by developer mode
31
+ @samples = []
32
+ @force_persist = []
33
+ @max_samples = 100
34
+
35
+ # @harvest_count is a count of harvests used for random
36
+ # sampling - we pull 1 @random_sample in every @sampling_rate harvests
37
+ @harvest_count = 0
38
+ @random_sample = nil
39
+ @sampling_rate = 10
40
+ @slow_capture_threshold = 2.0
41
+ configure!
42
+
43
+ # This lock is used to synchronize access to the @last_sample
44
+ # and related variables. It can become necessary on JRuby or
45
+ # any 'honest-to-god'-multithreaded system
46
+ @samples_lock = Mutex.new
47
+ end
48
+
49
+ def configure!
50
+ # @segment_limit and @stack_trace_threshold come from the
51
+ # configuration file, with built-in defaults that should
52
+ # suffice for most customers
53
+
54
+ # enable if config.fetch('enabled', true)
55
+
56
+ @segment_limit = config.fetch('limit_segments', 4000)
57
+ @stack_trace_threshold = config.fetch('stack_trace_threshold', 0.500).to_f
58
+ @explain_threshold = config.fetch('explain_threshold', 0.5).to_f
59
+ @explain_enabled = config.fetch('explain_enabled', true)
60
+ @transaction_threshold = config.fetch('transation_threshold', 2.0)
61
+
62
+ # Configure the sample storage policy. Create a list of methods to be called.
63
+ @store_sampler_methods = [ :store_random_sample, :store_slowest_sample ]
64
+ if NewRelic::Control.instance.developer_mode?
65
+ @store_sampler_methods << :store_sample_for_developer_mode
66
+ end
67
+ end
68
+
69
+ def config
70
+ NewRelic::Control.instance.fetch('transaction_tracer', {})
71
+ end
72
+
73
+ # Returns the current sample id, delegated from `builder`
74
+ def current_sample_id
75
+ b=builder
76
+ b and b.sample_id
77
+ end
78
+
79
+ # Enable the transaction sampler - this also registers it with
80
+ # the statistics engine.
81
+ def enable
82
+ @disabled = false
83
+ NewRelic::Agent.instance.stats_engine.transaction_sampler = self
84
+ end
85
+
86
+ # Disable the transaction sampler - this also deregisters it
87
+ # with the statistics engine.
88
+ def disable
89
+ @disabled = true
90
+ NewRelic::Agent.instance.stats_engine.remove_transaction_sampler(self)
91
+ end
92
+
93
+ def enabled?
94
+ !@disabled
95
+ end
96
+
97
+ # Set with an integer value n, this takes one in every n
98
+ # harvested samples. It also resets the harvest count to a
99
+ # random integer between 0 and (n-1)
100
+ def sampling_rate=(val)
101
+ @sampling_rate = val.to_i
102
+ @harvest_count = rand(val.to_i).to_i
103
+ end
104
+
105
+
106
+ # Creates a new transaction sample builder, unless the
107
+ # transaction sampler is disabled. Takes a time parameter for
108
+ # the start of the transaction sample
109
+ def notice_first_scope_push(time)
110
+ start_builder(time.to_f) unless disabled
111
+ end
112
+
113
+ # This delegates to the builder to create a new open transaction
114
+ # segment for the specified scope, beginning at the optionally
115
+ # specified time.
116
+ #
117
+ # Note that in developer mode, this captures a stacktrace for
118
+ # the beginning of each segment, which can be fairly slow
119
+ def notice_push_scope(scope, time=Time.now)
120
+ return unless builder
121
+
122
+ builder.trace_entry(scope, time.to_f)
123
+
124
+ capture_segment_trace if NewRelic::Control.instance.developer_mode?
125
+ end
126
+
127
+ # in developer mode, capture the stack trace with the segment.
128
+ # this is cpu and memory expensive and therefore should not be
129
+ # turned on in production mode
130
+ def capture_segment_trace
131
+ return unless NewRelic::Control.instance.developer_mode?
132
+ segment = builder.current_segment
133
+ if segment
134
+ # Strip stack frames off the top that match /new_relic/agent/
135
+ trace = caller
136
+ while trace.first =~/\/lib\/new_relic\/agent\//
137
+ trace.shift
138
+ end
139
+
140
+ trace = trace[0..39] if trace.length > 40
141
+ segment[:backtrace] = trace
142
+ end
143
+ end
144
+
145
+ # Defaults to zero, otherwise delegated to the transaction
146
+ # sample builder
147
+ def scope_depth
148
+ return 0 unless builder
149
+
150
+ builder.scope_depth
151
+ end
152
+
153
+ # Informs the transaction sample builder about the end of a
154
+ # traced scope
155
+ def notice_pop_scope(scope, time = Time.now)
156
+ return unless builder
157
+ raise "frozen already???" if builder.sample.frozen?
158
+ builder.trace_exit(scope, time.to_f)
159
+ end
160
+
161
+ # This is called when we are done with the transaction. We've
162
+ # unwound the stack to the top level. It also clears the
163
+ # transaction sample builder so that it won't continue to have
164
+ # scopes appended to it.
165
+ #
166
+ # It sets various instance variables to the finished sample,
167
+ # depending on which settings are active. See `store_sample`
168
+ def notice_scope_empty(time=Time.now)
169
+
170
+ last_builder = builder
171
+ return unless last_builder
172
+
173
+ last_builder.finish_trace(time.to_f)
174
+ clear_builder
175
+ return if last_builder.ignored?
176
+
177
+ @samples_lock.synchronize do
178
+ # NB this instance variable may be used elsewhere, it's not
179
+ # just a side effect
180
+ @last_sample = last_builder.sample
181
+ store_sample(@last_sample)
182
+ end
183
+ end
184
+
185
+ # Samples can be stored in three places: the random sample
186
+ # variable, when random sampling is active, the developer mode
187
+ # @samples array, and the @slowest_sample variable if it is
188
+ # slower than the current occupant of that slot
189
+ def store_sample(sample)
190
+ @store_sampler_methods.each{|sym| send sym, sample}
191
+ if NewRelic::Agent::TransactionInfo.get.force_persist_sample?(sample)
192
+ store_force_persist(sample)
193
+ end
194
+
195
+ end
196
+
197
+ # Only active when random sampling is true - this is very rarely
198
+ # used. Always store the most recent sample so that random
199
+ # sampling can pick a few of the samples to store, upon harvest
200
+ def store_random_sample(sample)
201
+ if @random_sampling
202
+ @random_sample = sample
203
+ end
204
+ end
205
+
206
+ def store_force_persist(sample)
207
+ @force_persist << sample
208
+
209
+ # WARNING - this clamp should be configurable
210
+ if @force_persist.length > 15
211
+ @force_persist.sort! {|a,b| b.duration <=> a.duration}
212
+ @force_persist = @force_persist[0..14]
213
+ end
214
+ end
215
+
216
+ # Samples take up a ton of memory, so we only store a lot of
217
+ # them in developer mode - we truncate to @max_samples
218
+ def store_sample_for_developer_mode(sample)
219
+ return unless NewRelic::Control.instance.developer_mode?
220
+ @samples = [] unless @samples
221
+ @samples << sample
222
+ truncate_samples
223
+ end
224
+
225
+ # Sets @slowest_sample to the passed in sample if it is slower
226
+ # than the current sample in @slowest_sample
227
+ def store_slowest_sample(sample)
228
+ if slowest_sample?(@slowest_sample, sample)
229
+ @slowest_sample = sample
230
+ end
231
+ end
232
+
233
+ # Checks to see if the old sample exists, or if it's duration is
234
+ # less than the new sample
235
+ def slowest_sample?(old_sample, new_sample)
236
+ old_sample.nil? || (new_sample.duration > old_sample.duration)
237
+ end
238
+
239
+ # Smashes the @samples array down to the length of @max_samples
240
+ # by taking the last @max_samples elements of the array
241
+ def truncate_samples
242
+ if @samples.length > @max_samples
243
+ @samples = @samples[-@max_samples..-1]
244
+ end
245
+ end
246
+
247
+ # Delegates to the builder to store the path, uri, and
248
+ # parameters if the sampler is active
249
+ def notice_transaction(path, uri=nil, params={})
250
+ builder.set_transaction_info(path, uri, params) if !disabled && builder
251
+ end
252
+
253
+ # Tells the builder to ignore a transaction, if we are currently
254
+ # creating one. Only causes the sample to be ignored upon end of
255
+ # the transaction, and does not change the metrics gathered
256
+ # outside of the sampler
257
+ def ignore_transaction
258
+ builder.ignore_transaction if builder
259
+ end
260
+
261
+ # For developer mode profiling support - delegates to the builder
262
+ def notice_profile(profile)
263
+ builder.set_profile(profile) if builder
264
+ end
265
+
266
+ # Sets the CPU time used by a transaction, delegates to the builder
267
+ def notice_transaction_cpu_time(cpu_time)
268
+ builder.set_transaction_cpu_time(cpu_time) if builder
269
+ end
270
+
271
+ MAX_DATA_LENGTH = 16384
272
+ # This method is used to record metadata into the currently
273
+ # active segment like a sql query, memcache key, or Net::HTTP uri
274
+ #
275
+ # duration is seconds, float value.
276
+ def notice_extra_data(message, duration, key, config=nil, config_key=nil)
277
+ return unless builder
278
+ segment = builder.current_segment
279
+ if segment
280
+ new_message = truncate_message(append_new_message(segment[key],
281
+ message))
282
+ if key == :sql && config.respond_to?(:has_key?) && config.has_key?(:adapter)
283
+ segment[key] = Database::Statement.new(new_message)
284
+ segment[key].adapter = config[:adapter]
285
+ else
286
+ segment[key] = new_message
287
+ end
288
+ segment[config_key] = config if config_key
289
+ append_backtrace(segment, duration)
290
+ end
291
+ end
292
+
293
+ private :notice_extra_data
294
+
295
+ # Truncates the message to `MAX_DATA_LENGTH` if needed, and
296
+ # appends an ellipsis because it makes the trucation clearer in
297
+ # the UI
298
+ def truncate_message(message)
299
+ if message.length > (MAX_DATA_LENGTH - 4)
300
+ message[0..MAX_DATA_LENGTH - 4] + '...'
301
+ else
302
+ message
303
+ end
304
+ end
305
+
306
+ # Allows the addition of multiple pieces of metadata to one
307
+ # segment - i.e. traced method calls multiple sql queries
308
+ def append_new_message(old_message, message)
309
+ if old_message
310
+ old_message + ";\n" + message
311
+ else
312
+ message
313
+ end
314
+ end
315
+
316
+ # Appends a backtrace to a segment if that segment took longer
317
+ # than the specified duration
318
+ def append_backtrace(segment, duration)
319
+ segment[:backtrace] = caller.join("\n") if (duration >= @stack_trace_threshold || Thread.current[:capture_deep_tt])
320
+ end
321
+
322
+ # some statements (particularly INSERTS with large BLOBS
323
+ # may be very large; we should trim them to a maximum usable length
324
+ # config is the driver configuration for the connection
325
+ # duration is seconds, float value.
326
+ def notice_sql(sql, config, duration)
327
+ if NewRelic::Agent.is_sql_recorded?
328
+ notice_extra_data(sql, duration, :sql, config, :connection_config)
329
+ end
330
+ end
331
+
332
+ # Adds non-sql metadata to a segment - generally the memcached
333
+ # key
334
+ #
335
+ # duration is seconds, float value.
336
+ def notice_nosql(key, duration)
337
+ notice_extra_data(key, duration, :key)
338
+ end
339
+
340
+ # Every 1/n harvests, adds the most recent sample to the harvest
341
+ # array if it exists. Makes sure that the random sample is not
342
+ # also the slowest sample for this harvest by `uniq!`ing the
343
+ # result array
344
+ #
345
+ # random sampling is very, very seldom used
346
+ def add_random_sample_to(result)
347
+ return unless @random_sampling && @sampling_rate && @sampling_rate.to_i > 0
348
+ @harvest_count += 1
349
+ if (@harvest_count.to_i % @sampling_rate.to_i) == 0
350
+ result << @random_sample if @random_sample
351
+ @harvest_count = 0
352
+ end
353
+ nil # don't assume this method returns anything
354
+ end
355
+
356
+ def add_force_persist_to(result)
357
+ result.concat(@force_persist)
358
+ @force_persist = []
359
+ end
360
+
361
+ # Returns an array of slow samples, with either one or two
362
+ # elements - one element unless random sampling is enabled. The
363
+ # sample returned will be the slowest sample among those
364
+ # available during this harvest
365
+ def add_samples_to(result, slow_threshold)
366
+
367
+ # pull out force persist
368
+ force_persist = result.select {|sample| sample.force_persist} || []
369
+ result.reject! {|sample| sample.force_persist}
370
+
371
+ force_persist.each {|sample| store_force_persist(sample)}
372
+
373
+
374
+ # Now get the slowest sample
375
+ if @slowest_sample && @slowest_sample.duration >= slow_threshold
376
+ result << @slowest_sample
377
+ end
378
+
379
+ result.compact!
380
+ result = result.sort_by { |x| x.duration }
381
+ result = result[-1..-1] || [] # take the slowest sample
382
+
383
+ add_random_sample_to(result)
384
+ add_force_persist_to(result)
385
+
386
+ result.uniq
387
+ end
388
+
389
+ # get the set of collected samples, merging into previous samples,
390
+ # and clear the collected sample list. Truncates samples to a
391
+ # specified @segment_limit to save memory and bandwith
392
+ # transmitting samples to the server.
393
+ def harvest(previous = [], slow_threshold = 2.0)
394
+ return [] if disabled
395
+ result = Array(previous)
396
+
397
+ @samples_lock.synchronize do
398
+ result = add_samples_to(result, slow_threshold)
399
+
400
+ # clear previous transaction samples
401
+ @slowest_sample = nil
402
+ @random_sample = nil
403
+ @last_sample = nil
404
+ end
405
+
406
+ # Clamp the number of TTs we'll keep in memory and send
407
+ #
408
+ result = clamp_number_tts(result, 20) if result.length > 20
409
+
410
+ # Truncate the samples at 2100 segments. The UI will clamp them at 2000 segments anyway.
411
+ # This will save us memory and bandwidth.
412
+ result.each { |sample| sample.truncate(@segment_limit) }
413
+ result
414
+ end
415
+
416
+ # JON - THIS CODE NEEDS A UNIT TEST
417
+ def clamp_number_tts(tts, limit)
418
+ tts.sort! do |a,b|
419
+ if a.force_persist && b.force_persist
420
+ b.duration <=> a.duration
421
+ elsif a.force_persist
422
+ -1
423
+ elsif b.force_persist
424
+ 1
425
+ else
426
+ b.duration <=> a.duration
427
+ end
428
+ end
429
+
430
+ tts[0..(limit-1)]
431
+ end
432
+
433
+ # reset samples without rebooting the web server
434
+ def reset!
435
+ @samples = []
436
+ @last_sample = nil
437
+ @random_sample = nil
438
+ @slowest_sample = nil
439
+ end
440
+
441
+ # Checks to see if the transaction sampler is disabled, if
442
+ # transaction trace recording is disabled by a thread local, or
443
+ # if execution is untraced - if so it clears the transaction
444
+ # sample builder from the thread local, otherwise it generates a
445
+ # new transaction sample builder with the stated time as a
446
+ # starting point and saves it in the thread local variable
447
+ def start_builder(time=nil)
448
+ if disabled || !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced?
449
+ clear_builder
450
+ else
451
+ Thread::current[BUILDER_KEY] ||= TransactionSampleBuilder.new(time)
452
+ end
453
+ end
454
+
455
+ # The current thread-local transaction sample builder
456
+ def builder
457
+ Thread::current[BUILDER_KEY]
458
+ end
459
+
460
+ # Sets the thread local variable storing the transaction sample
461
+ # builder to nil to clear it
462
+ def clear_builder
463
+ Thread::current[BUILDER_KEY] = nil
464
+ end
465
+
466
+ end
467
+ end
468
+ end