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,25 @@
1
+ module NewRelic
2
+ class Control
3
+ module Profiling
4
+
5
+ # A flag used in dev mode to indicate if profiling is available
6
+ def profiling?
7
+ @profiling
8
+ end
9
+
10
+ def profiling_available?
11
+ @profiling_available ||=
12
+ begin
13
+ require 'ruby-prof'
14
+ true
15
+ rescue LoadError; end
16
+ end
17
+ # Set the flag for capturing profiles in dev mode. If RubyProf is not
18
+ # loaded a true value is ignored.
19
+ def profiling=(val)
20
+ @profiling = profiling_available? && val && defined?(RubyProf)
21
+ end
22
+ end
23
+ include Profiling
24
+ end
25
+ end
@@ -0,0 +1,114 @@
1
+ module NewRelic
2
+ class Control
3
+
4
+ # Structs holding info for the remote server and proxy server
5
+ class Server < Struct.new :name, :port, :ip #:nodoc:
6
+ def to_s; "#{name}:#{port}"; end
7
+ end
8
+
9
+ ProxyServer = Struct.new :name, :port, :user, :password #:nodoc:
10
+
11
+ # Contains methods that deal with connecting to the server
12
+ module ServerMethods
13
+
14
+ def server
15
+ @remote_server ||= server_from_host(nil)
16
+ end
17
+
18
+ # the server we should contact for api requests, like uploading
19
+ # deployments and the like
20
+ def api_server
21
+ api_host = self['api_host'] || 'rpm.newrelic.com'
22
+ @api_server ||=
23
+ NewRelic::Control::Server.new \
24
+ api_host,
25
+ (self['api_port'] || self['port'] || (use_ssl? ? 443 : 80)).to_i,
26
+ nil
27
+ end
28
+
29
+ # a new instances of the proxy server - this passes through if
30
+ # there is no proxy, otherwise it has proxy configuration
31
+ # information pulled from the config file
32
+ def proxy_server
33
+ @proxy_server ||=
34
+ NewRelic::Control::ProxyServer.new self['proxy_host'], self['proxy_port'], self['proxy_user'], self['proxy_pass']
35
+ end
36
+
37
+ # turns a hostname into an ip address and returns a
38
+ # NewRelic::Control::Server that contains the configuration info
39
+ def server_from_host(hostname=nil)
40
+ host = hostname || self['host'] || 'collector.newrelic.com'
41
+
42
+ # if the host is not an IP address, turn it into one
43
+ NewRelic::Control::Server.new host, (self['port'] || (use_ssl? ? 443 : 80)).to_i, convert_to_ip_address(host)
44
+ end
45
+
46
+ # Check to see if we need to look up the IP address
47
+ # If it's an IP address already, we pass it through.
48
+ # If it's nil, or localhost, we don't bother.
49
+ # Otherwise, use `resolve_ip_address` to find one
50
+ def convert_to_ip_address(host)
51
+ # here we leave it as a host name since the cert verification
52
+ # needs it in host form
53
+ return host if verify_certificate?
54
+ return nil if host.nil? || host.downcase == "localhost"
55
+ ip = resolve_ip_address(host)
56
+ log.info "Resolved #{host} to #{ip}"
57
+ ip
58
+ end
59
+
60
+ # Look up the ip address of the host using the pure ruby lookup
61
+ # to prevent blocking. If that fails, fall back to the regular
62
+ # IPSocket library. Return nil if we can't find the host ip
63
+ # address and don't have a good default.
64
+ def resolve_ip_address(host)
65
+ Resolv.getaddress(host)
66
+ rescue => e
67
+ log.warn("DNS Error caching IP address: #{e}")
68
+ log.debug(e.backtrace.join("\n "))
69
+ begin
70
+ log.info("Trying native DNS lookup since Resolv failed")
71
+ IPSocket.getaddress(host)
72
+ rescue => e
73
+ log.error("Could not look up server address: #{e}")
74
+ nil
75
+ end
76
+ end
77
+
78
+ # The path to the certificate file used to verify the SSL
79
+ # connection if verify_peer is enabled
80
+ def cert_file_path
81
+ File.expand_path(File.join(newrelic_root, 'cert', 'cacert.pem'))
82
+ end
83
+
84
+ # Return the Net::HTTP with proxy configuration given the NewRelic::Control::Server object.
85
+ # Default is the collector but for api calls you need to pass api_server
86
+ #
87
+ # Experimental support for SSL verification:
88
+ # swap 'VERIFY_NONE' for 'VERIFY_PEER' line to try it out
89
+ # If verification fails, uncomment the 'http.ca_file' line
90
+ # and it will use the included certificate.
91
+ def http_connection(host = nil)
92
+ host ||= server
93
+ # Proxy returns regular HTTP if @proxy_host is nil (the default)
94
+ http_class = Net::HTTP::Proxy(proxy_server.name, proxy_server.port,
95
+ proxy_server.user, proxy_server.password)
96
+ http = http_class.new(host.ip || host.name, host.port)
97
+ log.debug("Http Connection opened to #{host.ip||host.name}:#{host.port}")
98
+ if use_ssl?
99
+ http.use_ssl = true
100
+ if verify_certificate?
101
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
102
+ http.ca_file = cert_file_path
103
+ else
104
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
105
+ end
106
+ end
107
+ http
108
+ end
109
+ end
110
+
111
+ include ServerMethods
112
+ end
113
+ end
114
+
@@ -0,0 +1,151 @@
1
+ require 'fileutils'
2
+ require 'new_relic/language_support'
3
+
4
+ module NewRelic
5
+ # Handles serialization of data to disk, to save on contacting the
6
+ # server. Lowers both server and client overhead, if the disk is not overloaded
7
+ class DataSerialization
8
+ include NewRelic::LanguageSupport::DataSerialization
9
+
10
+ module ClassMethods
11
+ # Check whether the store is too large, too old, or the
12
+ # pid file is too old. If so, we should send the data
13
+ # right away. If not, we presumably store it for later sending
14
+ # (handled elsewhere)
15
+ def should_send_data?
16
+ NewRelic::Control.instance.disable_serialization? || store_too_large? ||
17
+ store_too_old? || pid_too_old? ||
18
+ NewRelic::LanguageSupport.using_version?('1.8.6')
19
+ rescue => e
20
+ NewRelic::Control.instance.disable_serialization = true
21
+ NewRelic::Control.instance.log.warn("Disabling serialization: #{e.message}")
22
+ true
23
+ end
24
+
25
+ # A combined locked read/write from the store file - reduces
26
+ # contention by not acquiring the lock and file handle twice
27
+ def read_and_write_to_file
28
+ with_locked_store do |f|
29
+ result = (yield get_data_from_file(f))
30
+ f.rewind
31
+ f.truncate(0)
32
+ write_contents_nonblockingly(f, dump(result)) if result
33
+ end
34
+ rescue Errno::ENOENT => e
35
+ NewRelic::Control.instance.log.warn(e.message)
36
+ end
37
+
38
+ # touches the age file that determines whether we should send
39
+ # data now or not
40
+ def update_last_sent!
41
+ FileUtils.touch(pid_file_path)
42
+ end
43
+
44
+ def pid_too_old?
45
+ return true unless File.exists?(pid_file_path)
46
+ age = (Time.now.to_i - File.mtime(pid_file_path).to_i)
47
+ NewRelic::Control.instance.log.debug("Pid was #{age} seconds old, sending data") if age > 60
48
+ age > 60
49
+ end
50
+
51
+ def store_too_old?
52
+ return true unless File.exists?(file_path)
53
+ age = (Time.now.to_i - File.mtime(file_path).to_i)
54
+ NewRelic::Control.instance.log.debug("Store was #{age} seconds old, sending data") if age > 60
55
+ age > 50
56
+ end
57
+
58
+ def store_too_large?
59
+ return true unless File.exists?(file_path)
60
+ size = File.size(file_path) > max_size
61
+ NewRelic::Control.instance.log.debug("Store was oversize, sending data") if size
62
+ size
63
+ end
64
+
65
+ private
66
+
67
+ def open_arguments
68
+ if defined?(Encoding)
69
+ [file_path, File::RDWR | File::CREAT, {:internal_encoding => nil}]
70
+ else
71
+ [file_path, File::RDWR | File::CREAT]
72
+ end
73
+ end
74
+
75
+ def with_locked_store
76
+ File.open(*open_arguments) do |f|
77
+ f.flock(File::LOCK_EX)
78
+ begin
79
+ yield(f)
80
+ ensure
81
+ f.flock(File::LOCK_UN)
82
+ end
83
+ end
84
+ rescue => e
85
+ NewRelic::Control.instance.log.error("Error serializing data to disk: #{e.inspect}")
86
+ NewRelic::Control.instance.log.debug(e.backtrace.split("\n"))
87
+ # re-raise so that serialization will be disabled higher up the stack
88
+ raise e
89
+ end
90
+
91
+ def get_data_from_file(f)
92
+ data = read_until_eof_error(f)
93
+ result = load(data)
94
+ f.truncate(0)
95
+ result
96
+ end
97
+
98
+ def write_contents_nonblockingly(f, string)
99
+ result = 0
100
+ while (result < string.length)
101
+ result += f.write_nonblock(string)
102
+ end
103
+ rescue Errno::EAGAIN, Errno::EINTR
104
+ IO.select(nil, [f])
105
+ retry
106
+ end
107
+
108
+ def read_until_eof_error(f)
109
+ accumulator = ""
110
+ while(true)
111
+ accumulator << f.read_nonblock(10_000)
112
+ end
113
+ rescue Errno::EAGAIN, Errno::EINTR
114
+ IO.select([f])
115
+ retry
116
+ rescue EOFError
117
+ accumulator
118
+ end
119
+
120
+ def max_size
121
+ 10_000
122
+ end
123
+
124
+ def dump(object)
125
+ Marshal.dump(object.clone)
126
+ end
127
+
128
+ def load(dump)
129
+ if dump.respond_to?(:size) && dump.size == 0
130
+ NewRelic::Control.instance.log.debug("Spool file empty.")
131
+ return nil
132
+ end
133
+ Marshal.load(dump)
134
+ rescue ArgumentError, TypeError => e
135
+ NewRelic::Control.instance.log.error("Error loading data from newrelic_agent_store.db: #{e.inspect}")
136
+ NewRelic::Control.instance.log.debug(e.backtrace.inspect)
137
+ nil
138
+ end
139
+
140
+ def file_path
141
+ "#{NewRelic::Control.instance.log_path}/newrelic_agent_store.db"
142
+ end
143
+
144
+ def pid_file_path
145
+ "#{NewRelic::Control.instance.log_path}/newrelic_agent_store.pid"
146
+ end
147
+ end
148
+ extend ClassMethods
149
+ end
150
+ end
151
+
@@ -0,0 +1,51 @@
1
+ require 'dependency_detection'
2
+ # This installs some code to manually start the agent when a delayed
3
+ # job worker starts. It's not really instrumentation. It's more like
4
+ # a hook from DJ to the Ruby Agent so it gets loaded at the time the
5
+ # Ruby Agent initializes, which must be before the DJ worker
6
+ # initializes. Loaded from control.rb
7
+ module NewRelic
8
+ module DelayedJobInjection
9
+ extend self
10
+ attr_accessor :worker_name
11
+ end
12
+ end
13
+
14
+ DependencyDetection.defer do
15
+ @name = :delayed_job_injection
16
+
17
+ depends_on do
18
+ defined?(::Delayed) && defined?(::Delayed::Worker) && !NewRelic::Control.instance['disable_dj']
19
+ end
20
+
21
+ executes do
22
+ if NewRelic::Agent.respond_to?(:logger)
23
+ NewRelic::Agent.logger.debug 'Installing DelayedJob instrumentation hooks'
24
+ end
25
+ end
26
+
27
+ executes do
28
+ Delayed::Worker.class_eval do
29
+ def initialize_with_new_relic(*args)
30
+ initialize_without_new_relic(*args)
31
+ worker_name = case
32
+ when self.respond_to?(:name) then self.name
33
+ when self.class.respond_to?(:default_name) then self.class.default_name
34
+ end
35
+ dispatcher_instance_id = worker_name || "host:#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}"
36
+ say "New Relic Ruby Agent Monitoring DJ worker #{dispatcher_instance_id}"
37
+ NewRelic::DelayedJobInjection.worker_name = worker_name
38
+ NewRelic::Control.instance.init_plugin :dispatcher => :delayed_job, :dispatcher_instance_id => dispatcher_instance_id
39
+ end
40
+
41
+ alias initialize_without_new_relic initialize
42
+ alias initialize initialize_with_new_relic
43
+ end
44
+ end
45
+ end
46
+
47
+ # If Rails is defined, this gets called in an after_initialize hook
48
+ # see NewRelic::Control::Frameworks::Rails#init_config
49
+ unless defined?(Rails)
50
+ DependencyDetection.detect!
51
+ end
@@ -0,0 +1,73 @@
1
+ module NewRelic::LanguageSupport
2
+ extend self
3
+
4
+ module DataSerialization
5
+ def self.included(base)
6
+ # need to disable GC during marshal load in 1.8.7
7
+ if NewRelic::LanguageSupport.using_version?('1.8.7') &&
8
+ !NewRelic::LanguageSupport.using_engine?('jruby') &&
9
+ !NewRelic::LanguageSupport.using_engine?('rbx')
10
+ base.class_eval do
11
+ def self.load(*args)
12
+ if defined?(::GC) && ::GC.respond_to?(:disable)
13
+ ::GC.disable
14
+ val = super
15
+ ::GC.enable
16
+ val
17
+ else
18
+ super
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ module Control
27
+ def self.included(base)
28
+ # need to use syck rather than psych when possible
29
+ if defined?(::YAML::ENGINE)
30
+ if !NewRelic::LanguageSupport.using_engine?('jruby') &&
31
+ (NewRelic::LanguageSupport.using_version?('1.9.1') ||
32
+ NewRelic::LanguageSupport.using_version?('1.9.2'))
33
+ base.class_eval do
34
+ def load_newrelic_yml(*args)
35
+ yamler = ::YAML::ENGINE.yamler
36
+ ::YAML::ENGINE.yamler = 'syck'
37
+ val = super
38
+ ::YAML::ENGINE.yamler = yamler
39
+ val
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ module SynchronizedHash
48
+ def self.included(base)
49
+ # need to lock iteration of stats hash in 1.9.x
50
+ if NewRelic::LanguageSupport.using_version?('1.9') ||
51
+ NewRelic::LanguageSupport.using_engine?('jruby')
52
+ base.class_eval do
53
+ def each(*args, &block)
54
+ @lock.synchronize { super }
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def using_engine?(engine)
62
+ if defined?(::RUBY_ENGINE)
63
+ ::RUBY_ENGINE == engine
64
+ else
65
+ engine == 'ruby'
66
+ end
67
+ end
68
+
69
+ def using_version?(version)
70
+ numbers = version.split('.')
71
+ numbers == ::RUBY_VERSION.split('.')[0, numbers.size]
72
+ end
73
+ end
@@ -0,0 +1,428 @@
1
+ require 'set'
2
+ require 'new_relic/version'
3
+
4
+ module NewRelic
5
+ # An instance of LocalEnvironment is responsible for determining
6
+ # three things:
7
+ #
8
+ # * Framework - :rails, :rails3, :merb, :ruby, :external, :test
9
+ # * Dispatcher - A supported dispatcher, or nil (:mongrel, :thin, :passenger, :webrick, etc)
10
+ # * Dispatcher Instance ID, which distinguishes agents on a single host from each other
11
+ #
12
+ # If the environment can't be determined, it will be set to
13
+ # nil and dispatcher_instance_id will have nil.
14
+ #
15
+ # NewRelic::LocalEnvironment should be accessed through NewRelic::Control#env (via the NewRelic::Control singleton).
16
+ class LocalEnvironment
17
+
18
+ # mongrel, thin, webrick, or possibly nil
19
+ attr_accessor :dispatcher
20
+ # used to distinguish instances of a dispatcher from each other, may be nil
21
+ attr_writer :dispatcher_instance_id
22
+ # rails, rails3, merb, external, ruby, test, etc
23
+ attr_accessor :framework
24
+ # The number of cpus, if detected, or nil - many platforms do not
25
+ # support this :(
26
+ attr_reader :processors
27
+ alias environment dispatcher
28
+
29
+ def initialize
30
+ # Extend self with any any submodules of LocalEnvironment. These can override
31
+ # the discover methods to discover new framworks and dispatchers.
32
+ NewRelic::LocalEnvironment.constants.each do | const |
33
+ mod = NewRelic::LocalEnvironment.const_get const
34
+ self.extend mod if mod.instance_of? Module
35
+ end
36
+
37
+ discover_framework
38
+ discover_dispatcher
39
+ @dispatcher = nil if @dispatcher == :none
40
+ @gems = Set.new
41
+ @plugins = Set.new
42
+ @config = Hash.new
43
+ end
44
+
45
+ # Add the given key/value pair to the app environment
46
+ # settings. Must pass either a value or a block. Block
47
+ # is called to get the value and any raised errors are
48
+ # silently ignored.
49
+ def append_environment_value(name, value = nil)
50
+ value = yield if block_given?
51
+ @config[name] = value if value
52
+ rescue
53
+ # puts "#{e}\n #{e.backtrace.join("\n ")}"
54
+ raise if @framework == :test
55
+ end
56
+
57
+ # yields to the block and appends the returned value to the list
58
+ # of gems - this catches errors that might be raised in the block
59
+ def append_gem_list
60
+ @gems += yield
61
+ rescue => e
62
+ # puts "#{e}\n #{e.backtrace.join("\n ")}"
63
+ raise if @framework == :test
64
+ end
65
+
66
+ # yields to the block and appends the returned value to the list
67
+ # of plugins - this catches errors that might be raised in the block
68
+ def append_plugin_list
69
+ @plugins += yield
70
+ rescue
71
+ # puts "#{e}\n #{e.backtrace.join("\n ")}"
72
+ raise if @framework == :test
73
+ end
74
+
75
+ # An instance id pulled from either @dispatcher_instance_id or by
76
+ # splitting out the first part of the running file
77
+ def dispatcher_instance_id
78
+ if @dispatcher_instance_id.nil?
79
+ if @dispatcher.nil?
80
+ @dispatcher_instance_id = File.basename($0).split(".").first
81
+ end
82
+ end
83
+ @dispatcher_instance_id
84
+ end
85
+
86
+ # Interrogates some common ruby constants for useful information
87
+ # about what kind of ruby environment the agent is running in
88
+ def gather_ruby_info
89
+ append_environment_value('Ruby version'){ RUBY_VERSION }
90
+ append_environment_value('Ruby description'){ RUBY_DESCRIPTION } if defined? ::RUBY_DESCRIPTION
91
+ append_environment_value('Ruby platform') { RUBY_PLATFORM }
92
+ append_environment_value('Ruby patchlevel') { RUBY_PATCHLEVEL }
93
+ # room here for other ruby implementations, when.
94
+ if defined? ::JRUBY_VERSION
95
+ gather_jruby_info
96
+ end
97
+ end
98
+
99
+ # like gather_ruby_info but for the special case of JRuby
100
+ def gather_jruby_info
101
+ append_environment_value('JRuby version') { JRUBY_VERSION }
102
+ append_environment_value('Java VM version') { ENV_JAVA['java.vm.version']}
103
+ end
104
+
105
+ # See what the number of cpus is, works only on some linux variants
106
+ def gather_cpu_info(proc_file='/proc/cpuinfo')
107
+ return unless File.readable? proc_file
108
+ @processors = append_environment_value('Processors') do
109
+ cpuinfo = ''
110
+ File.open(proc_file) do |f|
111
+ loop do
112
+ begin
113
+ cpuinfo << f.read_nonblock(4096).strip
114
+ rescue EOFError
115
+ break
116
+ rescue Errno::EWOULDBLOCK, Errno::EAGAIN
117
+ cpuinfo = ''
118
+ break # don't select file handle, just give up
119
+ end
120
+ end
121
+ end
122
+ processors = cpuinfo.split("\n").select {|line| line =~ /^processor\s*:/ }.size
123
+
124
+ if processors == 0
125
+ processors = 1 # assume there is at least one processor
126
+ NewRelic::Agent.logger.warn("Cannot determine the number of processors in #{proc_file}")
127
+ end
128
+ processors
129
+ end
130
+ end
131
+
132
+ # Grabs the architecture string from either `uname -p` or the env
133
+ # variable PROCESSOR_ARCHITECTURE
134
+ def gather_architecture_info
135
+ append_environment_value('Arch') { `uname -p` } ||
136
+ append_environment_value('Arch') { ENV['PROCESSOR_ARCHITECTURE'] }
137
+ end
138
+
139
+ # gathers OS info from either `uname -v`, `uname -s`, or the OS
140
+ # env variable
141
+ def gather_os_info
142
+ append_environment_value('OS version') { `uname -v` }
143
+ append_environment_value('OS') { `uname -s` } ||
144
+ append_environment_value('OS') { ENV['OS'] }
145
+ end
146
+
147
+ # Gathers the architecture and cpu info
148
+ def gather_system_info
149
+ gather_architecture_info
150
+ gather_cpu_info
151
+ end
152
+
153
+ # Looks for a capistrano file indicating the current revision
154
+ def gather_revision_info
155
+ rev_file = File.join(NewRelic::Control.instance.root, "REVISION")
156
+ if File.readable?(rev_file) && File.size(rev_file) < 64
157
+ append_environment_value('Revision') do
158
+ File.open(rev_file) { | file | file.readline.strip }
159
+ end
160
+ end
161
+ end
162
+
163
+ # The name of the AR database adapter for the current environment and
164
+ # the current schema version
165
+ def gather_ar_adapter_info
166
+
167
+ append_environment_value 'Database adapter' do
168
+ if defined?(ActiveRecord) && defined?(ActiveRecord::Base) &&
169
+ ActiveRecord::Base.respond_to?(:configurations)
170
+ config = ActiveRecord::Base.configurations[NewRelic::Control.instance.env]
171
+ if config
172
+ config['adapter']
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ # Datamapper version
179
+ def gather_dm_adapter_info
180
+ append_environment_value 'DataMapper version' do
181
+ require 'dm-core/version'
182
+ DataMapper::VERSION
183
+ end
184
+ end
185
+
186
+ # sensing for which adapter is defined, then appends the relevant
187
+ # config information
188
+ def gather_db_info
189
+ # room here for more database adapters, when.
190
+ if defined? ::ActiveRecord
191
+ gather_ar_adapter_info
192
+ end
193
+ if defined? ::DataMapper
194
+ gather_dm_adapter_info
195
+ end
196
+ end
197
+
198
+ # Collect base statistics about the environment and record them for
199
+ # comparison and change detection.
200
+ def gather_environment_info
201
+ append_environment_value 'Framework', @framework.to_s
202
+ append_environment_value 'Dispatcher', @dispatcher.to_s if @dispatcher
203
+ append_environment_value 'Dispatcher instance id', @dispatcher_instance_id if @dispatcher_instance_id
204
+ append_environment_value('Environment') { NewRelic::Control.instance.env }
205
+
206
+ # miscellaneous other helpful debugging information
207
+ gather_ruby_info
208
+ gather_system_info
209
+ gather_revision_info
210
+ gather_db_info
211
+ end
212
+
213
+ # Take a snapshot of the environment information for this application
214
+ # Returns an associative array
215
+ def snapshot
216
+ i = @config.to_a
217
+ i << [ 'Plugin List', @plugins.to_a] if not @plugins.empty?
218
+ i << [ 'Gems', @gems.to_a] if not @gems.empty?
219
+ i
220
+ end
221
+
222
+ # it's a working jruby if it has the runtime method, and object
223
+ # space is enabled
224
+ def working_jruby?
225
+ !(defined?(::JRuby) && JRuby.respond_to?(:runtime) && !JRuby.runtime.is_object_space_enabled)
226
+ end
227
+
228
+ # Runs through all the objects in ObjectSpace to find the first one that
229
+ # match the provided class
230
+ def find_class_in_object_space(klass)
231
+ ObjectSpace.each_object(klass) do |x|
232
+ return x
233
+ end
234
+ return nil
235
+ end
236
+
237
+ # Sets the @mongrel instance variable if we can find a Mongrel::HttpServer
238
+ def mongrel
239
+ return @mongrel if @mongrel
240
+ if defined?(::Mongrel) && defined?(::Mongrel::HttpServer) && working_jruby?
241
+ @mongrel = find_class_in_object_space(::Mongrel::HttpServer)
242
+ end
243
+ @mongrel
244
+ end
245
+
246
+ private
247
+
248
+ # Although you can override the framework with NEWRELIC_DISPATCHER this
249
+ # is not advisable since it implies certain api's being available.
250
+ def discover_dispatcher
251
+ @dispatcher ||= ENV['NEWRELIC_DISPATCHER'] && ENV['NEWRELIC_DISPATCHER'].to_sym
252
+ @dispatcher ||= ENV['NEW_RELIC_DISPATCHER'] && ENV['NEW_RELIC_DISPATCHER'].to_sym
253
+ dispatchers = %w[passenger torquebox glassfish thin mongrel litespeed webrick fastcgi unicorn sinatra]
254
+ while dispatchers.any? && @dispatcher.nil?
255
+ send 'check_for_'+(dispatchers.shift)
256
+ end
257
+ end
258
+
259
+ def discover_framework
260
+ # Although you can override the framework with NEWRELIC_FRAMEWORK this
261
+ # is not advisable since it implies certain api's being available.
262
+ #
263
+ # Note that the odd defined? sequence is necessary to work around a bug in an older version
264
+ # of JRuby.
265
+ @framework ||= case
266
+ when ENV['NEWRELIC_FRAMEWORK'] then ENV['NEWRELIC_FRAMEWORK'].to_sym
267
+ when ENV['NEW_RELIC_FRAMEWORK'] then ENV['NEW_RELIC_FRAMEWORK'].to_sym
268
+ when defined?(::NewRelic::TEST) then :test
269
+ when defined?(::Merb) && defined?(::Merb::Plugins) then :merb
270
+ when defined?(::Rails) then check_rails_version
271
+ when defined?(::Sinatra) && defined?(::Sinatra::Base) then :sinatra
272
+ when defined?(::NewRelic::IA) then :external
273
+ else :ruby
274
+ end
275
+ end
276
+
277
+ def check_rails_version
278
+ if Rails::VERSION::MAJOR < 3
279
+ :rails
280
+ else
281
+ :rails3
282
+ end
283
+ end
284
+
285
+ def check_for_torquebox
286
+ return unless defined?(::JRuby) &&
287
+ ( org.torquebox::TorqueBox rescue nil)
288
+ @dispatcher = :torquebox
289
+ end
290
+
291
+ def check_for_glassfish
292
+ return unless defined?(::JRuby) &&
293
+ (((com.sun.grizzly.jruby.rack.DefaultRackApplicationFactory rescue nil) &&
294
+ defined?(com::sun::grizzly::jruby::rack::DefaultRackApplicationFactory)) ||
295
+ (jruby_rack? && defined?(::GlassFish::Server)))
296
+ @dispatcher = :glassfish
297
+ end
298
+
299
+ def check_for_trinidad
300
+ return unless defined?(::JRuby) && jruby_rack? && defined?(::Trinidad::Server)
301
+ @dispatcher = :trinidad
302
+ end
303
+
304
+ def jruby_rack?
305
+ ((org.jruby.rack.DefaultRackApplicationFactory rescue nil) &&
306
+ defined?(org::jruby::rack::DefaultRackApplicationFactory))
307
+ end
308
+
309
+ def check_for_webrick
310
+ return unless defined?(::WEBrick) && defined?(::WEBrick::VERSION)
311
+ @dispatcher = :webrick
312
+ if defined?(::OPTIONS) && OPTIONS.respond_to?(:fetch)
313
+ # OPTIONS is set by script/server
314
+ @dispatcher_instance_id = OPTIONS.fetch(:port)
315
+ end
316
+ @dispatcher_instance_id = default_port unless @dispatcher_instance_id
317
+ end
318
+
319
+ def check_for_fastcgi
320
+ return unless defined?(::FCGI)
321
+ @dispatcher = :fastcgi
322
+ end
323
+
324
+ # this case covers starting by mongrel_rails
325
+ def check_for_mongrel
326
+ return unless defined?(::Mongrel) && defined?(::Mongrel::HttpServer)
327
+ @dispatcher = :mongrel
328
+
329
+ # Get the port from the server if it's started
330
+
331
+ if mongrel && mongrel.respond_to?(:port)
332
+ @dispatcher_instance_id = mongrel.port.to_s
333
+ end
334
+
335
+ # Get the port from the configurator if one was created
336
+ if @dispatcher_instance_id.nil? && defined?(::Mongrel::Configurator)
337
+ ObjectSpace.each_object(Mongrel::Configurator) do |mongrel|
338
+ @dispatcher_instance_id = mongrel.defaults[:port] && mongrel.defaults[:port].to_s
339
+ end unless defined?(::JRuby) && !JRuby.runtime.is_object_space_enabled
340
+ end
341
+
342
+ # Still can't find the port. Let's look at ARGV to fall back
343
+ @dispatcher_instance_id = default_port if @dispatcher_instance_id.nil?
344
+ end
345
+
346
+ def check_for_unicorn
347
+ if (defined?(::Unicorn) && defined?(::Unicorn::HttpServer)) && working_jruby?
348
+ v = find_class_in_object_space(::Unicorn::HttpServer)
349
+ @dispatcher = :unicorn if v
350
+ end
351
+ end
352
+
353
+ def check_for_sinatra
354
+ return unless defined?(::Sinatra) && defined?(::Sinatra::Base)
355
+
356
+ begin
357
+ version = ::Sinatra::VERSION
358
+ rescue
359
+ $stderr.puts("Error determining Sinatra version")
360
+ end
361
+
362
+ if ::NewRelic::VersionNumber.new('0.9.2') > version
363
+ $stderr.puts("Your Sinatra version is #{version}, we highly recommend upgrading to >=0.9.2")
364
+ end
365
+
366
+ @dispatcher = :sinatra
367
+ end
368
+
369
+ def check_for_thin
370
+ if defined?(::Thin) && defined?(::Thin::Server)
371
+ # This case covers the thin web dispatcher
372
+ # Same issue as above- we assume only one instance per process
373
+ ObjectSpace.each_object(Thin::Server) do |thin_dispatcher|
374
+ @dispatcher = :thin
375
+ backend = thin_dispatcher.backend
376
+ # We need a way to uniquely identify and distinguish agents. The port
377
+ # works for this. When using sockets, use the socket file name.
378
+ if backend.respond_to? :port
379
+ @dispatcher_instance_id = backend.port
380
+ elsif backend.respond_to? :socket
381
+ @dispatcher_instance_id = backend.socket
382
+ else
383
+ raise "Unknown thin backend: #{backend}"
384
+ end
385
+ end # each thin instance
386
+ end
387
+ if defined?(::Thin) && defined?(::Thin::VERSION) && !@dispatcher
388
+ @dispatcher = :thin
389
+ @dispatcher_instance_id = default_port
390
+ end
391
+ end
392
+
393
+ def check_for_litespeed
394
+ if caller.pop =~ /fcgi-bin\/RailsRunner\.rb/
395
+ @dispatcher = :litespeed
396
+ end
397
+ end
398
+
399
+ def check_for_passenger
400
+ if (defined?(::Passenger) && defined?(::Passenger::AbstractServer)) || defined?(::IN_PHUSION_PASSENGER)
401
+ @dispatcher = :passenger
402
+ end
403
+ end
404
+
405
+
406
+ def default_port
407
+ require 'optparse'
408
+ # If nothing else is found, use the 3000 default
409
+ default_port = 3000
410
+ OptionParser.new do |opts|
411
+ opts.on("-p", "--port=port", String) { | p | default_port = p }
412
+ opts.parse(ARGV.clone) rescue nil
413
+ end
414
+ default_port
415
+ end
416
+
417
+ public
418
+ # outputs a human-readable description
419
+ def to_s
420
+ s = "LocalEnvironment["
421
+ s << @framework.to_s
422
+ s << ";dispatcher=#{@dispatcher}" if @dispatcher
423
+ s << ";instance=#{@dispatcher_instance_id}" if @dispatcher_instance_id
424
+ s << "]"
425
+ end
426
+
427
+ end
428
+ end