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,49 @@
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, :current_nest_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
+ @current_nest_count = 0
12
+ end
13
+
14
+ def <<(segment)
15
+ if metric_name != segment.metric_name
16
+ raise ArgumentError, "Metric Name Mismatch: #{segment.metric_name} != #{metric_name}"
17
+ end
18
+
19
+ # a nested segment should use the sum of the top level totals
20
+ @total_time += segment.duration if current_nest_count == 0
21
+ @exclusive_time += segment.exclusive_duration
22
+ @call_count += 1
23
+ end
24
+
25
+ def average_time
26
+ @total_time / @call_count
27
+ end
28
+
29
+ def average_exclusive_time
30
+ @exclusive_time / @call_count
31
+ end
32
+
33
+ def exclusive_time_percentage
34
+ return 0 unless @exclusive_time && @sample.duration && @sample.duration > 0
35
+ @exclusive_time / @sample.duration
36
+ end
37
+
38
+ def total_time_percentage
39
+ return 0 unless @total_time && @sample.duration && @sample.duration > 0
40
+ @total_time / @sample.duration
41
+ end
42
+
43
+ def ui_name
44
+ return @metric_name if @metric_name == 'Remainder'
45
+ NewRelic::MetricParser::MetricParser.parse(@metric_name).developer_name
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,261 @@
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
+ # the number of segments that need to exist before we roll them up
7
+ # into one segment with multiple executions
8
+ COLLAPSE_SEGMENTS_THRESHOLD = 2
9
+
10
+ class TransactionSample
11
+
12
+ attr_accessor :params, :root_segment, :profile, :force_persist, :guid
13
+ attr_reader :root_segment, :params, :sample_id
14
+
15
+ @@start_time = Time.now
16
+
17
+ include TransactionAnalysis
18
+
19
+ def initialize(time = Time.now.to_f, sample_id = nil)
20
+ @sample_id = sample_id || object_id
21
+ @start_time = time
22
+ @params = { :segment_count => -1, :request_params => {} }
23
+ @segment_count = -1
24
+ @root_segment = create_segment 0.0, "ROOT"
25
+
26
+ @guid = generate_guid
27
+ NewRelic::Agent::TransactionInfo.get.guid = @guid
28
+ end
29
+
30
+ def count_segments
31
+ @segment_count
32
+ end
33
+
34
+ # Truncates the transaction sample to a maximum length determined
35
+ # by the passed-in parameter. Operates recursively on the entire
36
+ # tree of transaction segments in a depth-first manner
37
+ def truncate(max)
38
+ return if @segment_count < max
39
+ @root_segment.truncate(max + 1)
40
+ @segment_count = max
41
+ end
42
+
43
+ # makes sure that the parameter cache for segment count is set to
44
+ # the correct value
45
+ def ensure_segment_count_set(count)
46
+ params[:segment_count] ||= count
47
+ end
48
+
49
+ # offset from start of app
50
+ def timestamp
51
+ @start_time - @@start_time.to_f
52
+ end
53
+
54
+ # Used in the server only
55
+ def to_json(options = {}) #:nodoc:
56
+ map = {:sample_id => @sample_id,
57
+ :start_time => @start_time,
58
+ :root_segment => @root_segment}
59
+ if @params && !@params.empty?
60
+ map[:params] = @params
61
+ end
62
+ map.to_json
63
+ end
64
+
65
+ def start_time
66
+ Time.at(@start_time)
67
+ end
68
+
69
+ def path_string
70
+ @root_segment.path_string
71
+ end
72
+
73
+ def create_segment(relative_timestamp, metric_name, segment_id = nil)
74
+ raise TypeError.new("Frozen Transaction Sample") if frozen?
75
+ @params[:segment_count] += 1
76
+ @segment_count += 1
77
+ NewRelic::TransactionSample::Segment.new(relative_timestamp, metric_name, segment_id)
78
+ end
79
+
80
+ def duration
81
+ root_segment.duration
82
+ end
83
+
84
+ # Iterates recursively over each segment in the entire transaction
85
+ # sample tree
86
+ def each_segment(&block)
87
+ @root_segment.each_segment(&block)
88
+ end
89
+
90
+ # Iterates recursively over each segment in the entire transaction
91
+ # sample tree while keeping track of nested segments
92
+ def each_segment_with_nest_tracking(&block)
93
+ @root_segment.each_segment_with_nest_tracking(&block)
94
+ end
95
+
96
+ def to_s_compact
97
+ @root_segment.to_s_compact
98
+ end
99
+
100
+ # Searches the tree recursively for the segment with the given
101
+ # id. note that this is an internal id, not an ActiveRecord id
102
+ def find_segment(id)
103
+ @root_segment.find_segment(id)
104
+ end
105
+
106
+ def to_s
107
+ s = "Transaction Sample collected at #{start_time}\n"
108
+ s << " {\n"
109
+ s << " Path: #{params[:path]} \n"
110
+
111
+ params.each do |k,v|
112
+ next if k == :path
113
+ s << " #{k}: " <<
114
+ case v
115
+ when Enumerable then v.map(&:to_s).sort.join("; ")
116
+ when String then v
117
+ when Float then '%6.3s' % v
118
+ when Fixnum then v.to_s
119
+ when nil then ''
120
+ else
121
+ raise "unexpected value type for #{k}: '#{v}' (#{v.class})"
122
+ end << "\n"
123
+ end
124
+ s << " }\n\n"
125
+ s << @root_segment.to_debug_str(0)
126
+ end
127
+
128
+ # return a new transaction sample that treats segments
129
+ # with the given regular expression in their name as if they
130
+ # were never called at all. This allows us to strip out segments
131
+ # from traces captured in development environment that would not
132
+ # normally show up in production (like Rails/Application Code Loading)
133
+ def omit_segments_with(regex)
134
+ regex = Regexp.new(regex)
135
+
136
+ sample = TransactionSample.new(@start_time, sample_id)
137
+
138
+ sample.params = params.dup
139
+ sample.params[:segment_count] = 0
140
+
141
+ delta = build_segment_with_omissions(sample, 0.0, @root_segment, sample.root_segment, regex)
142
+ sample.root_segment.end_trace(@root_segment.exit_timestamp - delta)
143
+ sample.profile = self.profile
144
+ sample
145
+ end
146
+
147
+ # Return a new transaction sample that can be sent to the New
148
+ # Relic service. This involves potentially one or more of the
149
+ # following options
150
+ #
151
+ # :explain_sql : run EXPLAIN on all queries whose response times equal the value for this key
152
+ # (for example :explain_sql => 2.0 would explain everything over 2 seconds. 0.0 would explain everything.)
153
+ # :keep_backtraces : keep backtraces, significantly increasing size of trace (off by default)
154
+ # :record_sql => [ :raw | :obfuscated] : copy over the sql, obfuscating if necessary
155
+ def prepare_to_send(options={})
156
+ sample = TransactionSample.new(@start_time, sample_id)
157
+
158
+ sample.params.merge! self.params
159
+ sample.guid = self.guid
160
+ sample.force_persist = self.force_persist if self.force_persist
161
+
162
+ begin
163
+ build_segment_for_transfer(sample, @root_segment, sample.root_segment, options)
164
+ ensure
165
+ NewRelic::Agent::Database.close_connections
166
+ end
167
+
168
+ sample.root_segment.end_trace(@root_segment.exit_timestamp)
169
+ sample
170
+ end
171
+
172
+ def params=(params)
173
+ @params = params
174
+ end
175
+
176
+ private
177
+
178
+
179
+
180
+ HEX_DIGITS = (0..15).map{|i| i.to_s(16)}
181
+ # generate a random 64 bit uuid
182
+ def generate_guid
183
+ guid = ''
184
+ HEX_DIGITS.each do |a|
185
+ guid << HEX_DIGITS[rand(16)]
186
+ end
187
+ guid
188
+ end
189
+
190
+ # This is badly in need of refactoring
191
+ def build_segment_with_omissions(new_sample, time_delta, source_segment, target_segment, regex)
192
+ source_segment.called_segments.each do |source_called_segment|
193
+ # if this segment's metric name matches the given regular expression, bail
194
+ # here and increase the amount of time that we reduce the target sample with
195
+ # by this omitted segment's duration.
196
+ do_omit = regex =~ source_called_segment.metric_name
197
+
198
+ if do_omit
199
+ time_delta += source_called_segment.duration
200
+ else
201
+ target_called_segment = new_sample.create_segment(
202
+ source_called_segment.entry_timestamp - time_delta,
203
+ source_called_segment.metric_name,
204
+ source_called_segment.segment_id)
205
+
206
+ target_segment.add_called_segment target_called_segment
207
+ source_called_segment.params.each do |k,v|
208
+ target_called_segment[k]=v
209
+ end
210
+
211
+ time_delta = build_segment_with_omissions(
212
+ new_sample, time_delta, source_called_segment, target_called_segment, regex)
213
+ target_called_segment.end_trace(source_called_segment.exit_timestamp - time_delta)
214
+ end
215
+ end
216
+
217
+ return time_delta
218
+ end
219
+
220
+ # see prepare_to_send for what we do with options
221
+ #
222
+ # This is badly in need of refactoring
223
+ def build_segment_for_transfer(new_sample, source_segment, target_segment, options)
224
+ source_segment.called_segments.each do |source_called_segment|
225
+ target_called_segment = new_sample.create_segment(
226
+ source_called_segment.entry_timestamp,
227
+ source_called_segment.metric_name,
228
+ source_called_segment.segment_id)
229
+
230
+ target_segment.add_called_segment target_called_segment
231
+ source_called_segment.params.each do |k,v|
232
+ case k
233
+ when :backtrace
234
+ target_called_segment[k]=v if options[:keep_backtraces]
235
+ when :sql
236
+ # run an EXPLAIN on this sql if specified.
237
+ if options[:record_sql] && options[:record_sql] &&
238
+ options[:explain_sql] &&
239
+ source_called_segment.duration > options[:explain_sql].to_f
240
+ target_called_segment[:explain_plan] = source_called_segment.explain_sql
241
+ end
242
+
243
+ target_called_segment[:sql] = case options[:record_sql]
244
+ when :raw then v
245
+ when :obfuscated then NewRelic::Agent::Database.obfuscate_sql(v)
246
+ else raise "Invalid value for record_sql: #{options[:record_sql]}"
247
+ end.to_s if options[:record_sql]
248
+ when :connection_config
249
+ # don't copy it
250
+ else
251
+ target_called_segment[k]=v
252
+ end
253
+ end
254
+
255
+ build_segment_for_transfer(new_sample, source_called_segment, target_called_segment, options)
256
+ target_called_segment.end_trace(source_called_segment.exit_timestamp)
257
+ end
258
+ end
259
+
260
+ end
261
+ 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
@@ -0,0 +1,9 @@
1
+ require 'new_relic/transaction_sample'
2
+ require 'new_relic/transaction_sample/segment'
3
+ module NewRelic
4
+ class TransactionSample
5
+ class FakeSegment < Segment
6
+ public :parent_segment=
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,203 @@
1
+ require 'new_relic/transaction_sample'
2
+ module NewRelic
3
+ class TransactionSample
4
+ class Segment
5
+ attr_reader :entry_timestamp
6
+ # The exit timestamp will be relative except for the outermost sample which will
7
+ # have a timestamp.
8
+ attr_reader :exit_timestamp
9
+ attr_reader :parent_segment
10
+ attr_reader :metric_name
11
+ attr_reader :segment_id
12
+
13
+ def initialize(timestamp, metric_name, segment_id)
14
+ @entry_timestamp = timestamp
15
+ @metric_name = metric_name || '<unknown>'
16
+ @segment_id = segment_id || object_id
17
+ end
18
+
19
+ # sets the final timestamp on a segment to indicate the exit
20
+ # point of the segment
21
+ def end_trace(timestamp)
22
+ @exit_timestamp = timestamp
23
+ end
24
+
25
+ def add_called_segment(s)
26
+ @called_segments ||= []
27
+ @called_segments << s
28
+ s.parent_segment = self
29
+ end
30
+
31
+ def to_s
32
+ to_debug_str(0)
33
+ end
34
+
35
+ def to_json(options={})
36
+ hash = {
37
+ :entry_timestamp => @entry_timestamp,
38
+ :exit_timestamp => @exit_timestamp,
39
+ :metric_name => @metric_name,
40
+ :segment_id => @segment_id,
41
+ }
42
+
43
+ hash[:called_segments] = called_segments if !called_segments.empty?
44
+ hash[:params] = @params if @params && !@params.empty?
45
+
46
+ hash.to_json
47
+ end
48
+
49
+ def path_string
50
+ "#{metric_name}[#{called_segments.collect {|segment| segment.path_string }.join('')}]"
51
+ end
52
+ def to_s_compact
53
+ str = ""
54
+ str << metric_name
55
+ if called_segments.any?
56
+ str << "{#{called_segments.map { | cs | cs.to_s_compact }.join(",")}}"
57
+ end
58
+ str
59
+ end
60
+ def to_debug_str(depth)
61
+ tab = " " * depth
62
+ s = tab.clone
63
+ s << ">> #{'%3i ms' % (@entry_timestamp*1000)} [#{self.class.name.split("::").last}] #{metric_name} \n"
64
+ unless params.empty?
65
+ params.each do |k,v|
66
+ s << "#{tab} -#{'%-16s' % k}: #{v.to_s[0..80]}\n"
67
+ end
68
+ end
69
+ called_segments.each do |cs|
70
+ s << cs.to_debug_str(depth + 1)
71
+ end
72
+ s << tab + "<< "
73
+ s << case @exit_timestamp
74
+ when nil then ' n/a'
75
+ when Numeric then '%3i ms' % (@exit_timestamp*1000)
76
+ else @exit_timestamp.to_s
77
+ end
78
+ s << " #{metric_name}\n"
79
+ end
80
+
81
+ def called_segments
82
+ @called_segments || []
83
+ end
84
+
85
+ # return the total duration of this segment
86
+ def duration
87
+ (@exit_timestamp - @entry_timestamp).to_f
88
+ end
89
+
90
+ # return the duration of this segment without
91
+ # including the time in the called segments
92
+ def exclusive_duration
93
+ d = duration
94
+
95
+ called_segments.each do |segment|
96
+ d -= segment.duration
97
+ end
98
+ d
99
+ end
100
+
101
+ def count_segments
102
+ count = 1
103
+ called_segments.each { | seg | count += seg.count_segments }
104
+ count
105
+ end
106
+
107
+ # Walk through the tree and truncate the segments in a
108
+ # depth-first manner
109
+ def truncate(max)
110
+ return 1 unless @called_segments
111
+ total, self.called_segments = truncate_each_child(max - 1)
112
+ total+1
113
+ end
114
+
115
+ def truncate_each_child(max)
116
+ total = 0
117
+ accumulator = []
118
+ called_segments.each { | s |
119
+ if total == max
120
+ true
121
+ else
122
+ total += s.truncate(max - total)
123
+ accumulator << s
124
+ end
125
+ }
126
+ total
127
+ [total, accumulator]
128
+ end
129
+
130
+ def []=(key, value)
131
+ # only create a parameters field if a parameter is set; this will save
132
+ # bandwidth etc as most segments have no parameters
133
+ params[key] = value
134
+ end
135
+
136
+ def [](key)
137
+ params[key]
138
+ end
139
+
140
+ def params
141
+ @params ||= {}
142
+ end
143
+
144
+ # call the provided block for this segment and each
145
+ # of the called segments
146
+ def each_segment(&block)
147
+ block.call self
148
+
149
+ if @called_segments
150
+ @called_segments.each do |segment|
151
+ segment.each_segment(&block)
152
+ end
153
+ end
154
+ end
155
+
156
+ # call the provided block for this segment and each
157
+ # of the called segments while keeping track of nested segments
158
+ def each_segment_with_nest_tracking(&block)
159
+ summary = block.call self
160
+ summary.current_nest_count += 1 if summary
161
+
162
+ if @called_segments
163
+ @called_segments.each do |segment|
164
+ segment.each_segment_with_nest_tracking(&block)
165
+ end
166
+ end
167
+
168
+ summary.current_nest_count -= 1 if summary
169
+ end
170
+
171
+ def find_segment(id)
172
+ return self if @segment_id == id
173
+ called_segments.each do |segment|
174
+ found = segment.find_segment(id)
175
+ return found if found
176
+ end
177
+ nil
178
+ end
179
+
180
+ def explain_sql
181
+ NewRelic::Agent::Database.explain_sql(params[:sql],
182
+ params[:connection_config])
183
+ end
184
+
185
+ def obfuscated_sql
186
+ NewRelic::Agent::Database.obfuscate_sql(params[:sql])
187
+ end
188
+
189
+ def params=(p)
190
+ @params = p
191
+ end
192
+
193
+ def called_segments=(segments)
194
+ @called_segments = segments
195
+ end
196
+
197
+ protected
198
+ def parent_segment=(s)
199
+ @parent_segment = s
200
+ end
201
+ end
202
+ end
203
+ end