wd_newrelic_rpm 3.3.4.1 → 3.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (290) hide show
  1. data/.gitignore +20 -0
  2. data/.project +23 -0
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +317 -39
  5. data/GUIDELINES_FOR_CONTRIBUTING.md +76 -0
  6. data/Gemfile +16 -0
  7. data/LICENSE +1 -1
  8. data/{README.rdoc → README.md} +71 -55
  9. data/Rakefile +62 -0
  10. data/bin/mongrel_rpm +1 -1
  11. data/config.dot +290 -0
  12. data/config/database.yml +5 -0
  13. data/init.rb +31 -0
  14. data/lib/new_relic/agent.rb +31 -60
  15. data/lib/new_relic/agent/agent.rb +277 -502
  16. data/lib/new_relic/agent/agent_logger.rb +165 -0
  17. data/lib/new_relic/agent/audit_logger.rb +72 -0
  18. data/lib/new_relic/agent/beacon_configuration.rb +36 -50
  19. data/lib/new_relic/agent/browser_monitoring.rb +114 -61
  20. data/lib/new_relic/agent/busy_calculator.rb +14 -6
  21. data/lib/new_relic/agent/configuration.rb +74 -0
  22. data/lib/new_relic/agent/configuration/defaults.rb +126 -0
  23. data/lib/new_relic/agent/configuration/environment_source.rb +49 -0
  24. data/lib/new_relic/agent/configuration/manager.rb +136 -0
  25. data/lib/new_relic/agent/configuration/mask_defaults.rb +10 -0
  26. data/lib/new_relic/agent/configuration/server_source.rb +27 -0
  27. data/lib/new_relic/agent/configuration/yaml_source.rb +63 -0
  28. data/lib/new_relic/agent/cross_process_monitoring.rb +43 -0
  29. data/lib/new_relic/agent/database.rb +39 -26
  30. data/lib/new_relic/agent/error_collector.rb +48 -49
  31. data/lib/new_relic/agent/instrumentation/active_merchant.rb +1 -1
  32. data/lib/new_relic/agent/instrumentation/active_record.rb +4 -7
  33. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +1 -1
  34. data/lib/new_relic/agent/instrumentation/authlogic.rb +1 -1
  35. data/lib/new_relic/agent/instrumentation/browser_monitoring_timings.rb +41 -0
  36. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +35 -12
  37. data/lib/new_relic/agent/instrumentation/data_mapper.rb +4 -12
  38. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +2 -2
  39. data/lib/new_relic/agent/instrumentation/memcache.rb +6 -6
  40. data/lib/new_relic/agent/instrumentation/merb/controller.rb +1 -1
  41. data/lib/new_relic/agent/instrumentation/merb/errors.rb +1 -1
  42. data/lib/new_relic/agent/instrumentation/metric_frame.rb +4 -14
  43. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +1 -1
  44. data/lib/new_relic/agent/instrumentation/net.rb +1 -1
  45. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +6 -20
  46. data/lib/new_relic/agent/instrumentation/queue_time.rb +2 -2
  47. data/lib/new_relic/agent/instrumentation/rack.rb +1 -1
  48. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +7 -7
  49. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +1 -1
  50. data/lib/new_relic/agent/instrumentation/rails/errors.rb +1 -1
  51. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +5 -5
  52. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +6 -2
  53. data/lib/new_relic/agent/instrumentation/resque.rb +81 -0
  54. data/lib/new_relic/agent/instrumentation/sinatra.rb +21 -10
  55. data/lib/new_relic/agent/instrumentation/sunspot.rb +1 -1
  56. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +5 -4
  57. data/lib/new_relic/agent/method_tracer.rb +11 -9
  58. data/lib/new_relic/agent/new_relic_service.rb +379 -0
  59. data/lib/new_relic/agent/pipe_channel_manager.rb +175 -0
  60. data/lib/new_relic/agent/pipe_service.rb +58 -0
  61. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +86 -0
  62. data/lib/new_relic/agent/samplers/memory_sampler.rb +6 -8
  63. data/lib/new_relic/agent/sql_sampler.rb +31 -74
  64. data/lib/new_relic/agent/stats_engine.rb +0 -5
  65. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +40 -24
  66. data/lib/new_relic/agent/stats_engine/metric_stats.rb +89 -14
  67. data/lib/new_relic/agent/stats_engine/samplers.rb +9 -7
  68. data/lib/new_relic/agent/stats_engine/transactions.rb +20 -12
  69. data/lib/new_relic/agent/thread.rb +32 -0
  70. data/lib/new_relic/agent/thread_profiler.rb +306 -0
  71. data/lib/new_relic/agent/transaction_info.rb +24 -4
  72. data/lib/new_relic/agent/transaction_sample_builder.rb +12 -13
  73. data/lib/new_relic/agent/transaction_sampler.rb +49 -65
  74. data/lib/new_relic/agent/worker_loop.rb +31 -25
  75. data/lib/new_relic/collection_helper.rb +1 -1
  76. data/lib/new_relic/commands/deployments.rb +19 -10
  77. data/lib/new_relic/control.rb +1 -3
  78. data/lib/new_relic/control/class_methods.rb +10 -5
  79. data/lib/new_relic/control/frameworks/merb.rb +0 -6
  80. data/lib/new_relic/control/frameworks/rails.rb +30 -45
  81. data/lib/new_relic/control/frameworks/rails3.rb +23 -18
  82. data/lib/new_relic/control/frameworks/rails4.rb +23 -0
  83. data/lib/new_relic/control/frameworks/ruby.rb +2 -23
  84. data/lib/new_relic/control/instance_methods.rb +35 -73
  85. data/lib/new_relic/control/instrumentation.rb +7 -12
  86. data/lib/new_relic/control/server_methods.rb +17 -19
  87. data/lib/new_relic/delayed_job_injection.rb +2 -2
  88. data/lib/new_relic/helper.rb +34 -0
  89. data/lib/new_relic/language_support.rb +56 -37
  90. data/lib/new_relic/local_environment.rb +32 -67
  91. data/lib/new_relic/metric_data.rb +10 -2
  92. data/lib/new_relic/metric_spec.rb +7 -3
  93. data/lib/new_relic/noticed_error.rb +32 -9
  94. data/lib/new_relic/rack.rb +4 -0
  95. data/lib/new_relic/rack/browser_monitoring.rb +34 -25
  96. data/lib/new_relic/rack/developer_mode.rb +3 -0
  97. data/lib/new_relic/rack/error_collector.rb +56 -0
  98. data/lib/new_relic/stats.rb +9 -7
  99. data/lib/new_relic/transaction_sample.rb +19 -18
  100. data/lib/new_relic/transaction_sample/segment.rb +13 -15
  101. data/lib/new_relic/version.rb +19 -3
  102. data/lib/newrelic_rpm.rb +1 -1
  103. data/lib/tasks/tests.rake +6 -8
  104. data/newrelic.yml +15 -32
  105. data/newrelic_rpm.gemspec +224 -43
  106. data/newrelic_rpm.gemspec.erb +54 -0
  107. data/test/config/newrelic.yml +4 -3
  108. data/test/config/test_control.rb +18 -18
  109. data/test/fixtures/gemspec_no_build.rb +442 -0
  110. data/test/fixtures/gemspec_with_build.rb +442 -0
  111. data/test/fixtures/gemspec_with_build_and_stage.rb +442 -0
  112. data/test/intentional_fail.rb +10 -0
  113. data/test/multiverse/.gitignore +10 -0
  114. data/test/multiverse/README.md +85 -0
  115. data/test/multiverse/lib/multiverse/color.rb +13 -0
  116. data/test/multiverse/lib/multiverse/envfile.rb +66 -0
  117. data/test/multiverse/lib/multiverse/environment.rb +16 -0
  118. data/test/multiverse/lib/multiverse/output_collector.rb +29 -0
  119. data/test/multiverse/lib/multiverse/runner.rb +44 -0
  120. data/test/multiverse/lib/multiverse/suite.rb +162 -0
  121. data/test/multiverse/script/run_one +3 -0
  122. data/test/multiverse/script/runner +9 -0
  123. data/test/multiverse/suites/active_record/Envfile +13 -0
  124. data/test/multiverse/suites/active_record/ar_method_aliasing.rb +94 -0
  125. data/test/multiverse/suites/active_record/config/newrelic.yml +22 -0
  126. data/test/multiverse/suites/active_record/encoding_test.rb +26 -0
  127. data/test/multiverse/suites/agent_only/Envfile +4 -0
  128. data/test/multiverse/suites/agent_only/audit_log_test.rb +99 -0
  129. data/test/multiverse/suites/agent_only/config/newrelic.yml +22 -0
  130. data/test/multiverse/suites/agent_only/http_response_code_test.rb +53 -0
  131. data/test/multiverse/suites/agent_only/marshaling_test.rb +109 -0
  132. data/test/multiverse/suites/agent_only/method_visibility_test.rb +98 -0
  133. data/test/multiverse/suites/agent_only/pipe_manager_test.rb +33 -0
  134. data/test/multiverse/suites/agent_only/service_timeout_test.rb +29 -0
  135. data/test/multiverse/suites/agent_only/test_trace_method_with_punctuation.rb +30 -0
  136. data/test/multiverse/suites/agent_only/test_trace_transaction_with_punctuation.rb +32 -0
  137. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +80 -0
  138. data/test/multiverse/suites/config_file_loading/Envfile +7 -0
  139. data/test/multiverse/suites/config_file_loading/config_file_loading_test.rb +106 -0
  140. data/test/multiverse/suites/datamapper/Envfile +8 -0
  141. data/test/multiverse/suites/datamapper/config/newrelic.yml +22 -0
  142. data/test/multiverse/suites/datamapper/encoding_test.rb +36 -0
  143. data/test/multiverse/suites/logging/Envfile +4 -0
  144. data/test/multiverse/suites/logging/config/newrelic.yml +22 -0
  145. data/test/multiverse/suites/logging/logging_test.rb +143 -0
  146. data/test/multiverse/suites/monitor_mode_false/Envfile +2 -0
  147. data/test/multiverse/suites/monitor_mode_false/config/newrelic.yml +25 -0
  148. data/test/multiverse/suites/monitor_mode_false/no_dns_resolv.rb +29 -0
  149. data/test/multiverse/suites/no_load/Envfile +2 -0
  150. data/test/multiverse/suites/no_load/config/newrelic.yml +22 -0
  151. data/test/multiverse/suites/no_load/start_up_test.rb +14 -0
  152. data/test/multiverse/suites/rails_3_error_tracing/Envfile +15 -0
  153. data/test/multiverse/suites/rails_3_error_tracing/config/newrelic.yml +165 -0
  154. data/test/multiverse/suites/rails_3_error_tracing/error_tracing_test.rb +236 -0
  155. data/test/multiverse/suites/rails_3_gc/Envfile +8 -0
  156. data/test/multiverse/suites/rails_3_gc/config/newrelic.yml +167 -0
  157. data/test/multiverse/suites/rails_3_gc/instrumentation_test.rb +92 -0
  158. data/test/multiverse/suites/rails_3_queue_time/Envfile +15 -0
  159. data/test/multiverse/suites/rails_3_queue_time/config/newrelic.yml +165 -0
  160. data/test/multiverse/suites/rails_3_queue_time/queue_time_test.rb +75 -0
  161. data/test/multiverse/suites/rails_3_views/.gitignore +3 -0
  162. data/test/multiverse/suites/rails_3_views/Envfile +16 -0
  163. data/test/multiverse/suites/rails_3_views/app/views/foos/_foo.html.haml +1 -0
  164. data/test/multiverse/suites/rails_3_views/app/views/test/_a_partial.html.erb +1 -0
  165. data/test/multiverse/suites/rails_3_views/app/views/test/_mid_partial.html.erb +1 -0
  166. data/test/multiverse/suites/rails_3_views/app/views/test/_top_partial.html.erb +3 -0
  167. data/test/multiverse/suites/rails_3_views/app/views/test/deep_partial.html.erb +3 -0
  168. data/test/multiverse/suites/rails_3_views/app/views/test/haml_view.html.haml +6 -0
  169. data/test/multiverse/suites/rails_3_views/app/views/test/index.html.erb +4 -0
  170. data/test/multiverse/suites/rails_3_views/config/newrelic.yml +164 -0
  171. data/test/multiverse/suites/rails_3_views/view_instrumentation_test.rb +245 -0
  172. data/test/multiverse/suites/resque/Envfile +21 -0
  173. data/test/multiverse/suites/resque/config/newrelic.yml +22 -0
  174. data/test/multiverse/suites/resque/dump.rdb +0 -0
  175. data/test/multiverse/suites/resque/instrumentation_test.rb +73 -0
  176. data/test/multiverse/suites/rum_auto_instrumentation/Envfile +4 -0
  177. data/test/multiverse/suites/rum_auto_instrumentation/config/newrelic.yml +24 -0
  178. data/test/multiverse/suites/rum_auto_instrumentation/responses/worst_case_small.html +5000 -0
  179. data/test/multiverse/suites/rum_auto_instrumentation/sanity_test.rb +102 -0
  180. data/test/multiverse/suites/sinatra/Envfile +13 -0
  181. data/test/multiverse/suites/sinatra/config/newrelic.yml +24 -0
  182. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +77 -0
  183. data/test/multiverse/suites/sinatra/sinatra_routes_test.rb +46 -0
  184. data/test/multiverse/suites/sinatra/sinatra_test.rb +55 -0
  185. data/test/multiverse/test/multiverse_test.rb +55 -0
  186. data/test/multiverse/test/suite_examples/one/a/Envfile +3 -0
  187. data/test/multiverse/test/suite_examples/one/a/a_test.rb +11 -0
  188. data/test/multiverse/test/suite_examples/one/a/config/newrelic.yml +24 -0
  189. data/test/multiverse/test/suite_examples/one/b/Envfile +3 -0
  190. data/test/multiverse/test/suite_examples/one/b/b_test.rb +11 -0
  191. data/test/multiverse/test/suite_examples/one/b/config/newrelic.yml +24 -0
  192. data/test/multiverse/test/suite_examples/three/a/Envfile +2 -0
  193. data/test/multiverse/test/suite_examples/three/a/fail_test.rb +6 -0
  194. data/test/multiverse/test/suite_examples/three/b/Envfile +2 -0
  195. data/test/multiverse/test/suite_examples/three/b/win_test.rb +6 -0
  196. data/test/multiverse/test/suite_examples/two/a/Envfile +1 -0
  197. data/test/multiverse/test/suite_examples/two/a/fail_test.rb +6 -0
  198. data/test/new_relic/agent/agent/connect_test.rb +151 -227
  199. data/test/new_relic/agent/agent/start_test.rb +68 -118
  200. data/test/new_relic/agent/agent/start_worker_thread_test.rb +12 -74
  201. data/test/new_relic/agent/agent_logger_test.rb +153 -0
  202. data/test/new_relic/agent/agent_test.rb +116 -30
  203. data/test/new_relic/agent/agent_test_controller.rb +1 -1
  204. data/test/new_relic/agent/agent_test_controller_test.rb +42 -10
  205. data/test/new_relic/agent/audit_logger_test.rb +105 -0
  206. data/test/new_relic/agent/beacon_configuration_test.rb +63 -67
  207. data/test/new_relic/agent/browser_monitoring_test.rb +151 -79
  208. data/test/new_relic/agent/busy_calculator_test.rb +7 -0
  209. data/test/new_relic/agent/configuration/environment_source_test.rb +79 -0
  210. data/test/new_relic/agent/configuration/manager_test.rb +204 -0
  211. data/test/new_relic/agent/configuration/server_source_test.rb +45 -0
  212. data/test/new_relic/agent/configuration/yaml_source_test.rb +75 -0
  213. data/test/new_relic/agent/cross_process_monitoring_test.rb +77 -0
  214. data/test/new_relic/agent/database_test.rb +12 -11
  215. data/test/new_relic/agent/error_collector/notice_error_test.rb +64 -53
  216. data/test/new_relic/agent/error_collector_test.rb +33 -19
  217. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +60 -30
  218. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +39 -0
  219. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +1 -1
  220. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +6 -0
  221. data/test/new_relic/agent/instrumentation/queue_time_test.rb +14 -0
  222. data/test/new_relic/agent/instrumentation/sinatra_test.rb +25 -0
  223. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -2
  224. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +4 -10
  225. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +3 -15
  226. data/test/new_relic/agent/method_tracer_test.rb +7 -6
  227. data/test/new_relic/agent/mock_scope_listener.rb +3 -0
  228. data/test/new_relic/agent/new_relic_service_test.rb +376 -0
  229. data/test/new_relic/agent/pipe_channel_manager_test.rb +131 -0
  230. data/test/new_relic/agent/pipe_service_test.rb +113 -0
  231. data/test/new_relic/agent/rpm_agent_test.rb +27 -50
  232. data/test/new_relic/agent/sql_sampler_test.rb +81 -56
  233. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +3 -20
  234. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +12 -1
  235. data/test/new_relic/agent/stats_engine_test.rb +17 -8
  236. data/test/new_relic/agent/thread_profiler_test.rb +537 -0
  237. data/test/new_relic/agent/thread_test.rb +89 -0
  238. data/test/new_relic/agent/threaded_test.rb +65 -0
  239. data/test/new_relic/agent/transaction_info_test.rb +45 -4
  240. data/test/new_relic/agent/transaction_sample_builder_test.rb +8 -7
  241. data/test/new_relic/agent/transaction_sampler_test.rb +193 -204
  242. data/test/new_relic/agent/worker_loop_test.rb +26 -11
  243. data/test/new_relic/agent_test.rb +113 -33
  244. data/test/new_relic/collection_helper_test.rb +7 -8
  245. data/test/new_relic/command/deployments_test.rb +18 -2
  246. data/test/new_relic/control/class_methods_test.rb +0 -18
  247. data/test/new_relic/control/frameworks/rails_test.rb +26 -0
  248. data/test/new_relic/control_test.rb +96 -137
  249. data/test/new_relic/delayed_job_injection_test.rb +6 -1
  250. data/test/new_relic/dispatcher_test.rb +54 -0
  251. data/test/new_relic/fake_collector.rb +283 -0
  252. data/test/new_relic/fake_service.rb +53 -0
  253. data/test/new_relic/fakes_sending_data.rb +30 -0
  254. data/test/new_relic/framework_test.rb +53 -0
  255. data/test/new_relic/load_test.rb +13 -0
  256. data/test/new_relic/local_environment_test.rb +11 -11
  257. data/test/new_relic/metric_data_test.rb +45 -16
  258. data/test/new_relic/noticed_error_test.rb +24 -0
  259. data/test/new_relic/rack/browser_monitoring_test.rb +20 -10
  260. data/test/new_relic/rack/developer_mode_test.rb +13 -7
  261. data/test/new_relic/rack/error_collector_test.rb +74 -0
  262. data/test/new_relic/stats_test.rb +10 -0
  263. data/test/new_relic/transaction_sample/segment_test.rb +23 -4
  264. data/test/new_relic/transaction_sample_test.rb +47 -2
  265. data/test/new_relic/version_number_test.rb +32 -0
  266. data/test/script/build_test_gem.sh +9 -3
  267. data/test/script/ci.sh +108 -35
  268. data/test/script/ci_agent-tests_runner.sh +82 -0
  269. data/test/script/ci_multiverse_runner.sh +63 -0
  270. data/test/test_contexts.rb +1 -0
  271. data/test/test_helper.rb +68 -18
  272. data/ui/helpers/developer_mode_helper.rb +21 -11
  273. data/ui/views/layouts/newrelic_default.rhtml +1 -0
  274. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  275. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  276. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  277. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  278. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  279. data/ui/views/newrelic/show_sample.rhtml +1 -1
  280. data/ui/views/newrelic/threads.rhtml +2 -10
  281. data/vendor/gems/metric_parser-0.1.0.pre1/.specification +116 -0
  282. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_init.rb +7 -0
  283. metadata +191 -65
  284. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +0 -40
  285. data/lib/new_relic/control/configuration.rb +0 -206
  286. data/lib/new_relic/control/logging_methods.rb +0 -143
  287. data/lib/new_relic/data_serialization.rb +0 -151
  288. data/test/new_relic/control/configuration_test.rb +0 -84
  289. data/test/new_relic/control/logging_methods_test.rb +0 -185
  290. data/test/new_relic/data_serialization_test.rb +0 -208
@@ -0,0 +1,165 @@
1
+ require 'logger'
2
+
3
+ module NewRelic
4
+ module Agent
5
+ class AgentLogger
6
+
7
+ def initialize(config, root = "", override_logger=nil)
8
+ create_log(config, root, override_logger)
9
+ set_log_level!(config)
10
+ set_log_format!
11
+
12
+ gather_startup_logs
13
+ end
14
+
15
+ def fatal(*msgs)
16
+ @log.fatal(format_messages(msgs))
17
+ end
18
+
19
+ def error(*msgs)
20
+ @log.error(format_messages(msgs))
21
+ end
22
+
23
+ def warn(*msgs)
24
+ @log.warn(format_messages(msgs))
25
+ end
26
+
27
+ def info(*msgs)
28
+ @log.info(format_messages(msgs))
29
+ end
30
+
31
+ def debug(*msgs)
32
+ @log.debug(format_messages(msgs))
33
+ end
34
+
35
+ # Allows for passing exceptions in explicitly, which format with backtrace
36
+ def format_messages(msgs)
37
+ msgs.map do |msg|
38
+ if msg.respond_to?(:backtrace)
39
+ "#{msg.class}: #{msg}\n\t#{(msg.backtrace || []).join("\n\t")}"
40
+ else
41
+ msg
42
+ end
43
+ end.join("\n")
44
+ end
45
+
46
+ def create_log(config, root, override_logger)
47
+ if !override_logger.nil?
48
+ @log = override_logger
49
+ elsif config[:agent_enabled] == false
50
+ create_null_logger
51
+ else
52
+ if wants_stdout(config)
53
+ @log = ::Logger.new(STDOUT)
54
+ else
55
+ create_log_to_file(config, root)
56
+ end
57
+ end
58
+ end
59
+
60
+ def create_log_to_file(config, root)
61
+ path = find_or_create_file_path(config[:log_file_path], root)
62
+ if path.nil?
63
+ @log = ::Logger.new(STDOUT)
64
+ warn("Error creating log directory #{config[:log_file_path]}, using standard out for logging.")
65
+ else
66
+ file_path = "#{path}/#{config[:log_file_name]}"
67
+ begin
68
+ @log = ::Logger.new(file_path)
69
+ rescue => e
70
+ @log = ::Logger.new(STDOUT)
71
+ warn("Failed creating logger for file #{file_path}, using standard out for logging.", e)
72
+ end
73
+ end
74
+ end
75
+
76
+ def create_null_logger
77
+ null_path = ["/dev/null", "NUL"].detect{|f| File.exists?(f)}
78
+ @log = ::Logger.new(null_path)
79
+ end
80
+
81
+ def wants_stdout(config)
82
+ config[:log_file_path].upcase == "STDOUT"
83
+ end
84
+
85
+ def find_or_create_file_path(path_setting, root)
86
+ for abs_path in [ File.expand_path(path_setting),
87
+ File.expand_path(File.join(root, path_setting)) ] do
88
+ if File.directory?(abs_path) || (Dir.mkdir(abs_path) rescue nil)
89
+ return abs_path[%r{^(.*?)/?$}]
90
+ end
91
+ end
92
+ nil
93
+ end
94
+
95
+ def set_log_level!(config)
96
+ @log.level = AgentLogger.log_level_for(config.fetch(:log_level))
97
+ end
98
+
99
+ LOG_LEVELS = {
100
+ "debug" => Logger::DEBUG,
101
+ "info" => Logger::INFO,
102
+ "warn" => Logger::WARN,
103
+ "error" => Logger::ERROR,
104
+ "fatal" => Logger::FATAL,
105
+ }
106
+
107
+ def self.log_level_for(level)
108
+ LOG_LEVELS.fetch(level.to_s.downcase, Logger::INFO)
109
+ end
110
+
111
+ def set_log_format!
112
+ def @log.format_message(severity, timestamp, progname, msg)
113
+ prefix = @logdev.dev == STDOUT ? '** [NewRelic]' : ''
114
+ prefix + "[#{timestamp.strftime("%m/%d/%y %H:%M:%S %z")} #{Socket.gethostname} (#{$$})] #{severity} : #{msg}\n"
115
+ end
116
+ end
117
+
118
+ def gather_startup_logs
119
+ StartupLogger.instance.dump(self)
120
+ end
121
+ end
122
+
123
+ # BBase class for startup logging and testing in multiverse
124
+ class MemoryLogger
125
+ def initialize
126
+ @messages = []
127
+ end
128
+
129
+ attr_accessor :messages, :level
130
+
131
+ def fatal(*msgs)
132
+ messages << [:fatal, msgs]
133
+ end
134
+
135
+ def error(*msgs)
136
+ messages << [:error, msgs]
137
+ end
138
+
139
+ def warn(*msgs)
140
+ messages << [:warn, msgs]
141
+ end
142
+
143
+ def info(*msgs)
144
+ messages << [:info, msgs]
145
+ end
146
+
147
+ def debug(*msgs)
148
+ messages << [:debug, msgs]
149
+ end
150
+
151
+ def dump(logger)
152
+ messages.each do |msg|
153
+ logger.send(msg[0], msg[1])
154
+ end
155
+ messages.clear
156
+ end
157
+ end
158
+
159
+ # In an effort to not lose messages during startup, we trap them in memory
160
+ # The real logger will then dump its contents out when it arrives.
161
+ class StartupLogger < MemoryLogger
162
+ include Singleton
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,72 @@
1
+ require 'logger'
2
+ require 'fileutils'
3
+
4
+ module NewRelic
5
+ module Agent
6
+ class AuditLogger
7
+ def initialize(config)
8
+ @config = config
9
+ @enabled = @config[:'audit_log.enabled']
10
+ @encoder = NewRelic::Agent::NewRelicService::Encoders::Identity
11
+ end
12
+
13
+ attr_writer :enabled
14
+
15
+ def enabled?
16
+ @enabled
17
+ end
18
+
19
+ def setup?
20
+ !@log.nil?
21
+ end
22
+
23
+ def log_request(uri, data, marshaller)
24
+ if enabled?
25
+ setup_logger unless setup?
26
+ request_body = if marshaller.class.human_readable?
27
+ marshaller.dump(data, :encoder => @encoder)
28
+ else
29
+ marshaller.prepare(data, :encoder => @encoder).inspect
30
+ end
31
+ @log.info("REQUEST: #{uri}")
32
+ @log.info("REQUEST BODY: #{request_body}")
33
+ end
34
+ rescue SystemCallError => e
35
+ ::NewRelic::Agent.logger.warn("Failed writing to audit log: #{e}")
36
+ end
37
+
38
+ def setup_logger
39
+ path = ensure_log_path
40
+ @log = ::Logger.new(path || "/dev/null")
41
+ @log.formatter = log_formatter
42
+ ::NewRelic::Agent.logger.info("Audit log enabled at '#{path}'") if path
43
+ end
44
+
45
+ def ensure_log_path
46
+ path = File.expand_path(@config[:'audit_log.path'])
47
+ log_dir = File.dirname(path)
48
+
49
+ begin
50
+ FileUtils.mkdir_p(log_dir)
51
+ FileUtils.touch(path)
52
+ rescue SystemCallError => e
53
+ msg = "Audit log disabled, failed opening log at '#{path}': #{e}"
54
+ ::NewRelic::Agent.logger.warn(msg)
55
+ path = nil
56
+ end
57
+
58
+ path
59
+ end
60
+
61
+ def log_formatter
62
+ if @formatter.nil?
63
+ @formatter = Logger::Formatter.new
64
+ def @formatter.call(severity, time, progname, msg)
65
+ "[#{time} #{Socket.gethostname} (#{$$})] : #{msg}\n"
66
+ end
67
+ end
68
+ @formatter
69
+ end
70
+ end
71
+ end
72
+ end
@@ -12,81 +12,69 @@ module NewRelic
12
12
  # the static portion of the RUM footer - this part does not vary
13
13
  # by which request is in progress
14
14
  attr_reader :browser_timing_static_footer
15
-
16
- # the application id we include in the javascript -
17
- # crossreferences with the application id on the collectors
18
- attr_reader :application_id
19
15
 
20
- # the key used for browser monitoring. This is different from
21
- # the account key
22
- attr_reader :browser_monitoring_key
23
-
24
- # which beacon we should report to - set by startup of the agent
25
- attr_reader :beacon
26
-
27
- # whether RUM is enabled or not - determined based on server and
28
- # local config
29
- attr_reader :rum_enabled
30
-
31
- # whether JSONP is used to communicate with the Beacon or not
32
- attr_reader :rum_jsonp
33
-
34
16
  # RUM footer command used for 'finish' - based on whether JSONP is
35
17
  # being used. 'nrfj' for JSONP, otherwise 'nrf2'
36
18
  attr_reader :finish_command
37
-
19
+
38
20
  # A static javascript header that is identical for every account
39
21
  # and application
40
22
  JS_HEADER = "<script type=\"text/javascript\">var NREUMQ=NREUMQ||[];NREUMQ.push([\"mark\",\"firstbyte\",new Date().getTime()]);</script>"
41
-
23
+
42
24
  # Creates a new browser configuration data. Argument is a hash
43
25
  # of configuration values from the server
44
- def initialize(connect_data)
45
- @browser_monitoring_key = connect_data['browser_key']
46
- @application_id = connect_data['application_id']
47
- @beacon = connect_data['beacon']
48
- @rum_enabled = connect_data['rum.enabled']
49
- @rum_enabled = true if @rum_enabled.nil?
50
- NewRelic::Control.instance.log.warn("Real User Monitoring is disabled for this agent. Edit your configuration to change this.") unless @rum_enabled
26
+ def initialize
51
27
  @browser_timing_header = build_browser_timing_header
52
- NewRelic::Control.instance.log.debug("Browser timing header: #{@browser_timing_header.inspect}")
53
- @browser_timing_static_footer = build_load_file_js(connect_data)
54
- NewRelic::Control.instance.log.debug("Browser timing static footer: #{@browser_timing_static_footer.inspect}")
55
- @rum_jsonp = connect_data['rum.jsonp']
56
- @rum_jsonp = true if @rum_jsonp.nil?
57
- NewRelic::Control.instance.log.debug("Real User Monitoring is using JSONP protocol") if @rum_jsonp
58
- @finish_command = @rum_jsonp ? 'nrfj' : 'nrf2'
28
+ ::NewRelic::Agent.logger.debug("Browser timing header: #{@browser_timing_header.inspect}")
29
+ @browser_timing_static_footer = build_load_file_js
30
+ ::NewRelic::Agent.logger.debug("Browser timing static footer: #{@browser_timing_static_footer.inspect}")
31
+ if Agent.config[:'rum.jsonp']
32
+ ::NewRelic::Agent.logger.debug("Real User Monitoring is using JSONP protocol")
33
+ @finish_command = 'nrfj'
34
+ else
35
+ @finish_command = 'nrf2'
36
+ end
37
+
38
+ if !Agent.config[:'rum.enabled']
39
+ ::NewRelic::Agent.logger.debug("Real User Monitoring is disabled for this agent. Edit your configuration to change this.")
40
+ end
41
+ end
42
+
43
+ def enabled?
44
+ Agent.config[:'rum.enabled'] && !!Agent.config[:beacon]
59
45
  end
60
-
46
+
61
47
  # returns a memoized version of the bytes in the license key for
62
48
  # obscuring transaction names in the javascript
63
49
  def license_bytes
64
50
  if @license_bytes.nil?
65
51
  @license_bytes = []
66
- NewRelic::Control.instance.license_key.each_byte {|byte| @license_bytes << byte}
52
+ Agent.config[:license_key].each_byte {|byte| @license_bytes << byte}
67
53
  end
68
54
  @license_bytes
69
55
  end
70
-
56
+
71
57
  # returns a snippet of text that does not change
72
58
  # per-transaction. Is empty when rum is disabled, or we are not
73
59
  # including the episodes file dynamically (i.e. the user
74
60
  # includes it themselves)
75
- def build_load_file_js(connect_data)
61
+ def build_load_file_js
76
62
  js = <<-EOS
77
63
  if (!NREUMQ.f) { NREUMQ.f=function() {
78
64
  NREUMQ.push(["load",new Date().getTime()]);
79
65
  EOS
80
-
81
- if connect_data.fetch('rum.load_episodes_file', true)
82
- episodes_url = connect_data.fetch('episodes_url', '')
66
+
67
+ if Agent.config[:'rum.load_episodes_file'] &&
68
+ Agent.config[:'rum.load_episodes_file'] != ''
83
69
  js << <<-EOS
84
- var e=document.createElement(\"script\");
85
- e.type=\"text/javascript\";e.async=true;e.src=\"#{episodes_url}\";
70
+ var e=document.createElement("script");
71
+ e.type="text/javascript";
72
+ e.src=(("http:"===document.location.protocol)?"http:":"https:") + "//" +
73
+ "#{Agent.config[:episodes_file]}";
86
74
  document.body.appendChild(e);
87
75
  EOS
88
76
  end
89
-
77
+
90
78
  js << <<-EOS
91
79
  if(NREUMQ.a)NREUMQ.a();
92
80
  };
@@ -101,12 +89,12 @@ EOS
101
89
  def javascript_header
102
90
  JS_HEADER.dup
103
91
  end
104
-
92
+
105
93
  # Returns the header string, properly html-safed if needed
106
94
  def build_browser_timing_header
107
- return "" if !@rum_enabled
108
- return "" if @browser_monitoring_key.nil?
109
-
95
+ return "" if !enabled?
96
+ return "" if Agent.config[:browser_key].nil?
97
+
110
98
  value = javascript_header
111
99
  if value.respond_to?(:html_safe)
112
100
  value.html_safe
@@ -117,5 +105,3 @@ EOS
117
105
  end
118
106
  end
119
107
  end
120
-
121
-
@@ -5,25 +5,22 @@ module NewRelic
5
5
  # This module contains support for Real User Monitoring - the
6
6
  # javascript generation and configuration
7
7
  module BrowserMonitoring
8
-
9
-
10
8
  class DummyMetricFrame
11
9
  def initialize
12
10
  @attributes = {}
13
11
  end
14
-
12
+
15
13
  def user_attributes
16
14
  @attributes
17
15
  end
18
-
16
+
19
17
  def queue_time
20
18
  0.0
21
19
  end
22
20
  end
23
-
21
+
24
22
  @@dummy_metric_frame = DummyMetricFrame.new
25
-
26
-
23
+
27
24
  # This method returns a string suitable for inclusion in a page
28
25
  # - known as 'manual instrumentation' for Real User
29
26
  # Monitoring. Can return either a script tag with associated
@@ -34,10 +31,11 @@ module NewRelic
34
31
  # page as is reasonably possible - that is, before any style or
35
32
  # javascript inclusions, but after any header-related meta tags
36
33
  def browser_timing_header
37
- return "" if NewRelic::Agent.instance.beacon_configuration.nil?
38
- return "" if !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced?
39
-
40
- NewRelic::Agent.instance.beacon_configuration.browser_timing_header
34
+ if insert_js?
35
+ NewRelic::Agent.instance.beacon_configuration.browser_timing_header
36
+ else
37
+ ""
38
+ end
41
39
  end
42
40
 
43
41
  # This method returns a string suitable for inclusion in a page
@@ -49,73 +47,140 @@ module NewRelic
49
47
  # This is the footer string - it should be placed as low in the
50
48
  # page as is reasonably possible.
51
49
  def browser_timing_footer
52
- config = NewRelic::Agent.instance.beacon_configuration
53
- return "" if config.nil? || !config.rum_enabled || config.browser_monitoring_key.nil?
54
- return "" if !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced?
55
- generate_footer_js(config)
50
+ if insert_js?
51
+ generate_footer_js(NewRelic::Agent.instance.beacon_configuration)
52
+ else
53
+ ""
54
+ end
56
55
  end
57
56
 
58
- private
57
+ module_function
59
58
 
60
- def generate_footer_js(config)
61
- if browser_monitoring_start_time
62
- application_id = config.application_id
63
- beacon = config.beacon
64
- license_key = config.browser_monitoring_key
59
+ def obfuscate(config, text)
60
+ obfuscated = ""
61
+ key_bytes = config.license_bytes
62
+ index = 0
63
+ text.each_byte{|byte|
64
+ obfuscated.concat((byte ^ key_bytes[index % 13].to_i))
65
+ index+=1
66
+ }
65
67
 
66
- footer_js_string(config, beacon, license_key, application_id)
67
- else
68
- ''
69
- end
68
+ [obfuscated].pack("m0").gsub("\n", '')
70
69
  end
71
70
 
72
71
  def browser_monitoring_transaction_name
73
72
  NewRelic::Agent::TransactionInfo.get.transaction_name
74
73
  end
75
74
 
75
+ def browser_monitoring_queue_time
76
+ clamp_to_positive((current_metric_frame.queue_time.to_f * 1000.0).round)
77
+ end
78
+
79
+ def browser_monitoring_app_time
80
+ clamp_to_positive(((Time.now - browser_monitoring_start_time).to_f * 1000.0).round)
81
+ end
82
+
83
+ def current_metric_frame
84
+ Thread.current[:last_metric_frame] || @@dummy_metric_frame
85
+ end
86
+
87
+ def clamp_to_positive(value)
88
+ return 0.0 if value < 0
89
+ value
90
+ end
91
+
76
92
  def browser_monitoring_start_time
77
93
  NewRelic::Agent::TransactionInfo.get.start_time
78
94
  end
79
-
95
+
96
+ def self.timings
97
+ NewRelic::Agent::Instrumentation::BrowserMonitoringTimings.new(
98
+ current_metric_frame.queue_time,
99
+ NewRelic::Agent::TransactionInfo.get)
100
+ end
101
+
102
+ def insert_mobile_response_header(request, response)
103
+ if mobile_header_found_in?(request) &&
104
+ NewRelic::Agent.instance.beacon_configuration
105
+
106
+ config = NewRelic::Agent.instance.beacon_configuration
107
+
108
+ response['X-NewRelic-Beacon-Url'] = beacon_url(request)
109
+
110
+ payload = %[ ["#{Agent.config[:application_id]}","#{obfuscate(config, browser_monitoring_transaction_name)}",#{browser_monitoring_queue_time},#{browser_monitoring_app_time}] ]
111
+ response['X-NewRelic-App-Server-Metrics'] = payload
112
+ end
113
+ end
114
+
115
+ def mobile_header_found_in?(request)
116
+ headers = ['HTTP_X_NEWRELIC_MOBILE_TRACE', 'X_NEWRELIC_MOBILE_TRACE',
117
+ 'X-NewRelic-Mobile-Trace']
118
+ headers.inject(false){|i,m| i || (request.env[m] == 'true')}
119
+ end
120
+
121
+ def beacon_url(request)
122
+ "#{request.scheme || 'http'}://#{Agent.config[:beacon]}/mobile/1/#{Agent.config[:browser_key]}"
123
+ end
124
+
125
+ private
126
+
127
+ # Check whether RUM header and footer should be generated. Log the
128
+ # reason if they shouldn't.
129
+ def insert_js?
130
+ if NewRelic::Agent.instance.beacon_configuration.nil?
131
+ ::NewRelic::Agent.logger.debug "Beacon configuration is nil. Skipping browser instrumentation."
132
+ false
133
+ elsif ! NewRelic::Agent.instance.beacon_configuration.enabled?
134
+ ::NewRelic::Agent.logger.debug "Beacon configuration is disabled. Skipping browser instrumentation."
135
+ ::NewRelic::Agent.logger.debug NewRelic::Agent.instance.beacon_configuration.inspect
136
+ false
137
+ elsif Agent.config[:browser_key].nil? || Agent.config[:browser_key].empty?
138
+ ::NewRelic::Agent.logger.debug "Browser key is not set. Skipping browser instrumentation."
139
+ false
140
+ elsif ! NewRelic::Agent.is_transaction_traced?
141
+ ::NewRelic::Agent.logger.debug "Transaction is not traced. Skipping browser instrumentation."
142
+ false
143
+ elsif ! NewRelic::Agent.is_execution_traced?
144
+ ::NewRelic::Agent.logger.debug "Execution is not traced. Skipping browser instrumentation."
145
+ false
146
+ elsif NewRelic::Agent::TransactionInfo.get.ignore_end_user?
147
+ ::NewRelic::Agent.logger.debug "Ignore end user for this transaction is set. Skipping browser instrumentation."
148
+ false
149
+ else
150
+ true
151
+ end
152
+ end
153
+
154
+ def generate_footer_js(config)
155
+ if browser_monitoring_start_time
156
+ footer_js_string(config)
157
+ else
158
+ ''
159
+ end
160
+ end
161
+
80
162
  def metric_frame_attribute(key)
81
163
  current_metric_frame.user_attributes[key] || ""
82
164
  end
83
-
84
- def current_metric_frame
85
- Thread.current[:last_metric_frame] || @@dummy_metric_frame
86
- end
87
-
165
+
88
166
  def tt_guid
89
167
  txn = NewRelic::Agent::TransactionInfo.get
90
168
  return txn.guid if txn.include_guid?
91
169
  ""
92
170
  end
93
-
171
+
94
172
  def tt_token
95
173
  return NewRelic::Agent::TransactionInfo.get.token
96
174
  end
97
-
98
- def clamp_to_positive(value)
99
- return 0.0 if value < 0
100
- value
101
- end
102
-
103
- def browser_monitoring_app_time
104
- clamp_to_positive(((Time.now - browser_monitoring_start_time).to_f * 1000.0).round)
105
- end
106
175
 
107
- def browser_monitoring_queue_time
108
- clamp_to_positive((current_metric_frame.queue_time.to_f * 1000.0).round)
109
- end
110
-
111
- def footer_js_string(config, beacon, license_key, application_id)
176
+ def footer_js_string(config)
112
177
  obfuscated_transaction_name = obfuscate(config, browser_monitoring_transaction_name)
113
-
178
+
114
179
  user = obfuscate(config, metric_frame_attribute(:user))
115
180
  account = obfuscate(config, metric_frame_attribute(:account))
116
181
  product = obfuscate(config, metric_frame_attribute(:product))
117
-
118
- html_safe_if_needed("<script type=\"text/javascript\">#{config.browser_timing_static_footer}NREUMQ.push([\"#{config.finish_command}\",\"#{beacon}\",\"#{license_key}\",#{application_id},\"#{obfuscated_transaction_name}\",#{browser_monitoring_queue_time},#{browser_monitoring_app_time},new Date().getTime(),\"#{tt_guid}\",\"#{tt_token}\",\"#{user}\",\"#{account}\",\"#{product}\"])</script>")
182
+
183
+ html_safe_if_needed(%'<script type="text/javascript">#{config.browser_timing_static_footer}NREUMQ.push(["#{config.finish_command}","#{Agent.config[:beacon]}","#{Agent.config[:browser_key]}","#{Agent.config[:application_id]}","#{obfuscated_transaction_name}",#{browser_monitoring_queue_time},#{browser_monitoring_app_time},new Date().getTime(),"#{tt_guid}","#{tt_token}","#{user}","#{account}","#{product}"]);</script>')
119
184
  end
120
185
 
121
186
  def html_safe_if_needed(string)
@@ -125,18 +190,6 @@ module NewRelic
125
190
  string
126
191
  end
127
192
  end
128
-
129
- def obfuscate(config, text)
130
- obfuscated = ""
131
- key_bytes = config.license_bytes
132
- index = 0
133
- text.each_byte{|byte|
134
- obfuscated.concat((byte ^ key_bytes[index % 13].to_i))
135
- index+=1
136
- }
137
-
138
- [obfuscated].pack("m0").gsub("\n", '')
139
- end
140
193
  end
141
194
  end
142
195
  end