sundawg_newrelic_rpm 3.5.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (409) hide show
  1. data/.gitignore +22 -0
  2. data/.project +23 -0
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +969 -0
  5. data/GUIDELINES_FOR_CONTRIBUTING.md +76 -0
  6. data/Gemfile +20 -0
  7. data/LICENSE +87 -0
  8. data/README.md +195 -0
  9. data/Rakefile +58 -0
  10. data/bin/mongrel_rpm +33 -0
  11. data/bin/newrelic +13 -0
  12. data/bin/newrelic_cmd +5 -0
  13. data/cert/cacert.pem +118 -0
  14. data/cert/oldsite.pem +28 -0
  15. data/cert/site.pem +27 -0
  16. data/config.dot +290 -0
  17. data/config/database.yml +5 -0
  18. data/gem-public_cert.pem +20 -0
  19. data/init.rb +31 -0
  20. data/install.rb +9 -0
  21. data/lib/conditional_vendored_dependency_detection.rb +3 -0
  22. data/lib/conditional_vendored_metric_parser.rb +5 -0
  23. data/lib/new_relic/agent.rb +488 -0
  24. data/lib/new_relic/agent/agent.rb +1069 -0
  25. data/lib/new_relic/agent/agent_logger.rb +173 -0
  26. data/lib/new_relic/agent/audit_logger.rb +72 -0
  27. data/lib/new_relic/agent/beacon_configuration.rb +107 -0
  28. data/lib/new_relic/agent/browser_monitoring.rb +195 -0
  29. data/lib/new_relic/agent/busy_calculator.rb +112 -0
  30. data/lib/new_relic/agent/chained_call.rb +13 -0
  31. data/lib/new_relic/agent/configuration.rb +74 -0
  32. data/lib/new_relic/agent/configuration/defaults.rb +132 -0
  33. data/lib/new_relic/agent/configuration/environment_source.rb +49 -0
  34. data/lib/new_relic/agent/configuration/manager.rb +148 -0
  35. data/lib/new_relic/agent/configuration/mask_defaults.rb +11 -0
  36. data/lib/new_relic/agent/configuration/server_source.rb +31 -0
  37. data/lib/new_relic/agent/configuration/yaml_source.rb +67 -0
  38. data/lib/new_relic/agent/cross_app_monitor.rb +239 -0
  39. data/lib/new_relic/agent/cross_app_tracing.rb +281 -0
  40. data/lib/new_relic/agent/database.rb +254 -0
  41. data/lib/new_relic/agent/error_collector.rb +266 -0
  42. data/lib/new_relic/agent/event_listener.rb +43 -0
  43. data/lib/new_relic/agent/instrumentation.rb +9 -0
  44. data/lib/new_relic/agent/instrumentation/active_merchant.rb +29 -0
  45. data/lib/new_relic/agent/instrumentation/active_record.rb +134 -0
  46. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +68 -0
  47. data/lib/new_relic/agent/instrumentation/authlogic.rb +19 -0
  48. data/lib/new_relic/agent/instrumentation/browser_monitoring_timings.rb +51 -0
  49. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +485 -0
  50. data/lib/new_relic/agent/instrumentation/data_mapper.rb +230 -0
  51. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +52 -0
  52. data/lib/new_relic/agent/instrumentation/memcache.rb +80 -0
  53. data/lib/new_relic/agent/instrumentation/merb/controller.rb +41 -0
  54. data/lib/new_relic/agent/instrumentation/merb/errors.rb +29 -0
  55. data/lib/new_relic/agent/instrumentation/metric_frame.rb +356 -0
  56. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +80 -0
  57. data/lib/new_relic/agent/instrumentation/net.rb +31 -0
  58. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +23 -0
  59. data/lib/new_relic/agent/instrumentation/queue_time.rb +68 -0
  60. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  61. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +114 -0
  62. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +42 -0
  63. data/lib/new_relic/agent/instrumentation/rails/errors.rb +42 -0
  64. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +180 -0
  65. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +41 -0
  66. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +145 -0
  67. data/lib/new_relic/agent/instrumentation/rails4/errors.rb +45 -0
  68. data/lib/new_relic/agent/instrumentation/resque.rb +81 -0
  69. data/lib/new_relic/agent/instrumentation/sinatra.rb +107 -0
  70. data/lib/new_relic/agent/instrumentation/sunspot.rb +29 -0
  71. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +22 -0
  72. data/lib/new_relic/agent/method_tracer.rb +530 -0
  73. data/lib/new_relic/agent/new_relic_service.rb +513 -0
  74. data/lib/new_relic/agent/pipe_channel_manager.rb +175 -0
  75. data/lib/new_relic/agent/pipe_service.rb +63 -0
  76. data/lib/new_relic/agent/rules_engine.rb +72 -0
  77. data/lib/new_relic/agent/sampler.rb +50 -0
  78. data/lib/new_relic/agent/samplers/cpu_sampler.rb +58 -0
  79. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +86 -0
  80. data/lib/new_relic/agent/samplers/memory_sampler.rb +141 -0
  81. data/lib/new_relic/agent/samplers/object_sampler.rb +26 -0
  82. data/lib/new_relic/agent/shim_agent.rb +28 -0
  83. data/lib/new_relic/agent/sql_sampler.rb +251 -0
  84. data/lib/new_relic/agent/stats.rb +149 -0
  85. data/lib/new_relic/agent/stats_engine.rb +30 -0
  86. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +116 -0
  87. data/lib/new_relic/agent/stats_engine/metric_stats.rb +161 -0
  88. data/lib/new_relic/agent/stats_engine/samplers.rb +97 -0
  89. data/lib/new_relic/agent/stats_engine/stats_hash.rb +58 -0
  90. data/lib/new_relic/agent/stats_engine/transactions.rb +145 -0
  91. data/lib/new_relic/agent/thread.rb +32 -0
  92. data/lib/new_relic/agent/thread_profiler.rb +319 -0
  93. data/lib/new_relic/agent/transaction_info.rb +119 -0
  94. data/lib/new_relic/agent/transaction_sample_builder.rb +126 -0
  95. data/lib/new_relic/agent/transaction_sampler.rb +459 -0
  96. data/lib/new_relic/agent/worker_loop.rb +96 -0
  97. data/lib/new_relic/coerce.rb +37 -0
  98. data/lib/new_relic/collection_helper.rb +77 -0
  99. data/lib/new_relic/command.rb +85 -0
  100. data/lib/new_relic/commands/deployments.rb +114 -0
  101. data/lib/new_relic/commands/install.rb +80 -0
  102. data/lib/new_relic/control.rb +44 -0
  103. data/lib/new_relic/control/class_methods.rb +58 -0
  104. data/lib/new_relic/control/frameworks.rb +10 -0
  105. data/lib/new_relic/control/frameworks/external.rb +16 -0
  106. data/lib/new_relic/control/frameworks/merb.rb +25 -0
  107. data/lib/new_relic/control/frameworks/rails.rb +173 -0
  108. data/lib/new_relic/control/frameworks/rails3.rb +71 -0
  109. data/lib/new_relic/control/frameworks/rails4.rb +23 -0
  110. data/lib/new_relic/control/frameworks/ruby.rb +21 -0
  111. data/lib/new_relic/control/frameworks/sinatra.rb +20 -0
  112. data/lib/new_relic/control/instance_methods.rb +145 -0
  113. data/lib/new_relic/control/instrumentation.rb +95 -0
  114. data/lib/new_relic/control/profiling.rb +25 -0
  115. data/lib/new_relic/control/server_methods.rb +80 -0
  116. data/lib/new_relic/delayed_job_injection.rb +51 -0
  117. data/lib/new_relic/helper.rb +66 -0
  118. data/lib/new_relic/language_support.rb +92 -0
  119. data/lib/new_relic/latest_changes.rb +31 -0
  120. data/lib/new_relic/local_environment.rb +385 -0
  121. data/lib/new_relic/merbtasks.rb +6 -0
  122. data/lib/new_relic/metric_data.rb +70 -0
  123. data/lib/new_relic/metric_spec.rb +80 -0
  124. data/lib/new_relic/metrics.rb +9 -0
  125. data/lib/new_relic/noticed_error.rb +59 -0
  126. data/lib/new_relic/okjson.rb +599 -0
  127. data/lib/new_relic/rack.rb +4 -0
  128. data/lib/new_relic/rack/agent_hooks.rb +20 -0
  129. data/lib/new_relic/rack/browser_monitoring.rb +85 -0
  130. data/lib/new_relic/rack/developer_mode.rb +271 -0
  131. data/lib/new_relic/rack/error_collector.rb +66 -0
  132. data/lib/new_relic/recipes.rb +99 -0
  133. data/lib/new_relic/timer_lib.rb +27 -0
  134. data/lib/new_relic/transaction_analysis.rb +77 -0
  135. data/lib/new_relic/transaction_analysis/segment_summary.rb +49 -0
  136. data/lib/new_relic/transaction_sample.rb +272 -0
  137. data/lib/new_relic/transaction_sample/composite_segment.rb +27 -0
  138. data/lib/new_relic/transaction_sample/fake_segment.rb +9 -0
  139. data/lib/new_relic/transaction_sample/segment.rb +205 -0
  140. data/lib/new_relic/transaction_sample/summary_segment.rb +21 -0
  141. data/lib/new_relic/url_rule.rb +14 -0
  142. data/lib/new_relic/version.rb +66 -0
  143. data/lib/newrelic_rpm.rb +49 -0
  144. data/lib/tasks/all.rb +4 -0
  145. data/lib/tasks/install.rake +7 -0
  146. data/lib/tasks/tests.rake +17 -0
  147. data/newrelic.yml +227 -0
  148. data/newrelic_rpm.gemspec +50 -0
  149. data/recipes/newrelic.rb +6 -0
  150. data/test/active_record_fixtures.rb +77 -0
  151. data/test/config/newrelic.yml +49 -0
  152. data/test/config/test_control.rb +48 -0
  153. data/test/fixtures/proc_cpuinfo.txt +575 -0
  154. data/test/intentional_fail.rb +10 -0
  155. data/test/multiverse/.gitignore +11 -0
  156. data/test/multiverse/README.md +85 -0
  157. data/test/multiverse/lib/multiverse/color.rb +13 -0
  158. data/test/multiverse/lib/multiverse/envfile.rb +66 -0
  159. data/test/multiverse/lib/multiverse/environment.rb +16 -0
  160. data/test/multiverse/lib/multiverse/output_collector.rb +29 -0
  161. data/test/multiverse/lib/multiverse/runner.rb +44 -0
  162. data/test/multiverse/lib/multiverse/suite.rb +164 -0
  163. data/test/multiverse/script/run_one +3 -0
  164. data/test/multiverse/script/runner +9 -0
  165. data/test/multiverse/suites/active_record/Envfile +13 -0
  166. data/test/multiverse/suites/active_record/ar_method_aliasing.rb +94 -0
  167. data/test/multiverse/suites/active_record/config/newrelic.yml +22 -0
  168. data/test/multiverse/suites/active_record/encoding_test.rb +26 -0
  169. data/test/multiverse/suites/agent_only/Envfile +9 -0
  170. data/test/multiverse/suites/agent_only/audit_log_test.rb +97 -0
  171. data/test/multiverse/suites/agent_only/config/newrelic.yml +23 -0
  172. data/test/multiverse/suites/agent_only/cross_application_tracing_test.rb +56 -0
  173. data/test/multiverse/suites/agent_only/http_response_code_test.rb +53 -0
  174. data/test/multiverse/suites/agent_only/key_transactions_test.rb +66 -0
  175. data/test/multiverse/suites/agent_only/logging_test.rb +163 -0
  176. data/test/multiverse/suites/agent_only/marshaling_test.rb +96 -0
  177. data/test/multiverse/suites/agent_only/method_visibility_test.rb +98 -0
  178. data/test/multiverse/suites/agent_only/no_dns_resolv.rb +17 -0
  179. data/test/multiverse/suites/agent_only/pipe_manager_test.rb +33 -0
  180. data/test/multiverse/suites/agent_only/rename_rule_test.rb +57 -0
  181. data/test/multiverse/suites/agent_only/rum_instrumentation_test.rb +81 -0
  182. data/test/multiverse/suites/agent_only/service_timeout_test.rb +32 -0
  183. data/test/multiverse/suites/agent_only/ssl_test.rb +22 -0
  184. data/test/multiverse/suites/agent_only/start_up_test.rb +21 -0
  185. data/test/multiverse/suites/agent_only/test_trace_method_with_punctuation.rb +30 -0
  186. data/test/multiverse/suites/agent_only/test_trace_transaction_with_punctuation.rb +32 -0
  187. data/test/multiverse/suites/agent_only/testing_app.rb +17 -0
  188. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +92 -0
  189. data/test/multiverse/suites/config_file_loading/Envfile +7 -0
  190. data/test/multiverse/suites/config_file_loading/config_file_loading_test.rb +106 -0
  191. data/test/multiverse/suites/datamapper/Envfile +8 -0
  192. data/test/multiverse/suites/datamapper/config/newrelic.yml +22 -0
  193. data/test/multiverse/suites/datamapper/encoding_test.rb +36 -0
  194. data/test/multiverse/suites/rails/Envfile +18 -0
  195. data/test/multiverse/suites/rails/app.rb +49 -0
  196. data/test/multiverse/suites/rails/app/views/foos/_foo.html.haml +1 -0
  197. data/test/multiverse/suites/rails/app/views/views/_a_partial.html.erb +1 -0
  198. data/test/multiverse/suites/rails/app/views/views/_mid_partial.html.erb +1 -0
  199. data/test/multiverse/suites/rails/app/views/views/_top_partial.html.erb +3 -0
  200. data/test/multiverse/suites/rails/app/views/views/deep_partial.html.erb +3 -0
  201. data/test/multiverse/suites/rails/app/views/views/haml_view.html.haml +6 -0
  202. data/test/multiverse/suites/rails/app/views/views/index.html.erb +4 -0
  203. data/test/multiverse/suites/rails/config/newrelic.yml +32 -0
  204. data/test/multiverse/suites/rails/error_tracing_test.rb +211 -0
  205. data/test/multiverse/suites/rails/gc_instrumentation_test.rb +79 -0
  206. data/test/multiverse/suites/rails/queue_time_test.rb +55 -0
  207. data/test/multiverse/suites/rails/view_instrumentation_test.rb +205 -0
  208. data/test/multiverse/suites/resque/Envfile +24 -0
  209. data/test/multiverse/suites/resque/Rakefile +8 -0
  210. data/test/multiverse/suites/resque/config/newrelic.yml +22 -0
  211. data/test/multiverse/suites/resque/instrumentation_test.rb +151 -0
  212. data/test/multiverse/suites/resque/resque_setup.rb +15 -0
  213. data/test/multiverse/suites/sinatra/Envfile +15 -0
  214. data/test/multiverse/suites/sinatra/config/newrelic.yml +24 -0
  215. data/test/multiverse/suites/sinatra/sinatra_error_tracing_test.rb +38 -0
  216. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +77 -0
  217. data/test/multiverse/suites/sinatra/sinatra_routes_test.rb +46 -0
  218. data/test/multiverse/suites/sinatra/sinatra_test.rb +139 -0
  219. data/test/multiverse/test/multiverse_test.rb +55 -0
  220. data/test/multiverse/test/suite_examples/one/a/Envfile +3 -0
  221. data/test/multiverse/test/suite_examples/one/a/a_test.rb +11 -0
  222. data/test/multiverse/test/suite_examples/one/a/config/newrelic.yml +24 -0
  223. data/test/multiverse/test/suite_examples/one/b/Envfile +3 -0
  224. data/test/multiverse/test/suite_examples/one/b/b_test.rb +11 -0
  225. data/test/multiverse/test/suite_examples/one/b/config/newrelic.yml +24 -0
  226. data/test/multiverse/test/suite_examples/three/a/Envfile +2 -0
  227. data/test/multiverse/test/suite_examples/three/a/fail_test.rb +6 -0
  228. data/test/multiverse/test/suite_examples/three/b/Envfile +2 -0
  229. data/test/multiverse/test/suite_examples/three/b/win_test.rb +6 -0
  230. data/test/multiverse/test/suite_examples/two/a/Envfile +1 -0
  231. data/test/multiverse/test/suite_examples/two/a/fail_test.rb +6 -0
  232. data/test/new_relic/agent/agent/connect_test.rb +295 -0
  233. data/test/new_relic/agent/agent/start_test.rb +205 -0
  234. data/test/new_relic/agent/agent/start_worker_thread_test.rb +88 -0
  235. data/test/new_relic/agent/agent_logger_test.rb +153 -0
  236. data/test/new_relic/agent/agent_test.rb +301 -0
  237. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  238. data/test/new_relic/agent/agent_test_controller_test.rb +381 -0
  239. data/test/new_relic/agent/apdex_from_server_test.rb +9 -0
  240. data/test/new_relic/agent/audit_logger_test.rb +105 -0
  241. data/test/new_relic/agent/beacon_configuration_test.rb +107 -0
  242. data/test/new_relic/agent/browser_monitoring_test.rb +395 -0
  243. data/test/new_relic/agent/busy_calculator_test.rb +96 -0
  244. data/test/new_relic/agent/configuration/environment_source_test.rb +79 -0
  245. data/test/new_relic/agent/configuration/manager_test.rb +232 -0
  246. data/test/new_relic/agent/configuration/server_source_test.rb +50 -0
  247. data/test/new_relic/agent/configuration/yaml_source_test.rb +85 -0
  248. data/test/new_relic/agent/cross_app_monitor_test.rb +237 -0
  249. data/test/new_relic/agent/database_test.rb +194 -0
  250. data/test/new_relic/agent/error_collector/notice_error_test.rb +268 -0
  251. data/test/new_relic/agent/error_collector_test.rb +246 -0
  252. data/test/new_relic/agent/event_listener_test.rb +67 -0
  253. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +606 -0
  254. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +66 -0
  255. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +107 -0
  256. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +11 -0
  257. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +171 -0
  258. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +151 -0
  259. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +461 -0
  260. data/test/new_relic/agent/instrumentation/queue_time_test.rb +97 -0
  261. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  262. data/test/new_relic/agent/instrumentation/sinatra_test.rb +25 -0
  263. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +182 -0
  264. data/test/new_relic/agent/memcache_instrumentation_test.rb +143 -0
  265. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +158 -0
  266. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +222 -0
  267. data/test/new_relic/agent/method_tracer_test.rb +389 -0
  268. data/test/new_relic/agent/mock_scope_listener.rb +26 -0
  269. data/test/new_relic/agent/new_relic_service_test.rb +571 -0
  270. data/test/new_relic/agent/pipe_channel_manager_test.rb +131 -0
  271. data/test/new_relic/agent/pipe_service_test.rb +133 -0
  272. data/test/new_relic/agent/rpm_agent_test.rb +115 -0
  273. data/test/new_relic/agent/rules_engine_test.rb +82 -0
  274. data/test/new_relic/agent/sampler_test.rb +19 -0
  275. data/test/new_relic/agent/shim_agent_test.rb +16 -0
  276. data/test/new_relic/agent/sql_sampler_test.rb +244 -0
  277. data/test/new_relic/agent/stats_engine/gc_profiler_test.rb +85 -0
  278. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +180 -0
  279. data/test/new_relic/agent/stats_engine/samplers_test.rb +99 -0
  280. data/test/new_relic/agent/stats_engine_test.rb +184 -0
  281. data/test/new_relic/agent/stats_hash_test.rb +93 -0
  282. data/test/new_relic/agent/stats_test.rb +197 -0
  283. data/test/new_relic/agent/thread_profiler_test.rb +582 -0
  284. data/test/new_relic/agent/thread_test.rb +89 -0
  285. data/test/new_relic/agent/threaded_test.rb +65 -0
  286. data/test/new_relic/agent/transaction_info_test.rb +106 -0
  287. data/test/new_relic/agent/transaction_sample_builder_test.rb +227 -0
  288. data/test/new_relic/agent/transaction_sampler_test.rb +968 -0
  289. data/test/new_relic/agent/worker_loop_test.rb +84 -0
  290. data/test/new_relic/agent_test.rb +296 -0
  291. data/test/new_relic/coerce_test.rb +65 -0
  292. data/test/new_relic/collection_helper_test.rb +148 -0
  293. data/test/new_relic/command/deployments_test.rb +84 -0
  294. data/test/new_relic/control/class_methods_test.rb +44 -0
  295. data/test/new_relic/control/frameworks/rails_test.rb +26 -0
  296. data/test/new_relic/control_test.rb +194 -0
  297. data/test/new_relic/delayed_job_injection_test.rb +21 -0
  298. data/test/new_relic/dispatcher_test.rb +54 -0
  299. data/test/new_relic/fake_collector.rb +389 -0
  300. data/test/new_relic/framework_test.rb +53 -0
  301. data/test/new_relic/helper_test.rb +24 -0
  302. data/test/new_relic/load_test.rb +13 -0
  303. data/test/new_relic/local_environment_test.rb +85 -0
  304. data/test/new_relic/metric_data_test.rb +183 -0
  305. data/test/new_relic/metric_parser/metric_parser_test.rb +11 -0
  306. data/test/new_relic/metric_spec_test.rb +95 -0
  307. data/test/new_relic/noticed_error_test.rb +32 -0
  308. data/test/new_relic/rack/agent_hooks_test.rb +30 -0
  309. data/test/new_relic/rack/all_test.rb +11 -0
  310. data/test/new_relic/rack/browser_monitoring_test.rb +152 -0
  311. data/test/new_relic/rack/developer_mode_helper_test.rb +141 -0
  312. data/test/new_relic/rack/developer_mode_test.rb +80 -0
  313. data/test/new_relic/rack/error_collector_test.rb +90 -0
  314. data/test/new_relic/transaction_analysis/segment_summary_test.rb +91 -0
  315. data/test/new_relic/transaction_analysis_test.rb +121 -0
  316. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  317. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  318. data/test/new_relic/transaction_sample/segment_test.rb +415 -0
  319. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  320. data/test/new_relic/transaction_sample_subtest_test.rb +56 -0
  321. data/test/new_relic/transaction_sample_test.rb +250 -0
  322. data/test/new_relic/version_number_test.rb +97 -0
  323. data/test/script/build_test_gem.sh +57 -0
  324. data/test/script/ci.sh +168 -0
  325. data/test/script/ci_agent-tests_runner.sh +82 -0
  326. data/test/script/ci_bench.sh +52 -0
  327. data/test/script/ci_multiverse_runner.sh +63 -0
  328. data/test/test_contexts.rb +30 -0
  329. data/test/test_helper.rb +240 -0
  330. data/ui/helpers/developer_mode_helper.rb +367 -0
  331. data/ui/helpers/google_pie_chart.rb +49 -0
  332. data/ui/views/layouts/newrelic_default.rhtml +48 -0
  333. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  334. data/ui/views/newrelic/_sample.rhtml +20 -0
  335. data/ui/views/newrelic/_segment.rhtml +28 -0
  336. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  337. data/ui/views/newrelic/_segment_row.rhtml +12 -0
  338. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  339. data/ui/views/newrelic/_show_sample_sql.rhtml +24 -0
  340. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  341. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  342. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  343. data/ui/views/newrelic/_table.rhtml +12 -0
  344. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  345. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  346. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  347. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  348. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  349. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  350. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  351. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  352. data/ui/views/newrelic/file/images/textmate.png +0 -0
  353. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  354. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  355. data/ui/views/newrelic/file/stylesheets/style.css +490 -0
  356. data/ui/views/newrelic/index.rhtml +71 -0
  357. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  358. data/ui/views/newrelic/show_sample.rhtml +80 -0
  359. data/ui/views/newrelic/show_source.rhtml +3 -0
  360. data/ui/views/newrelic/threads.rhtml +45 -0
  361. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +5 -0
  362. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +67 -0
  363. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection/version.rb +3 -0
  364. data/vendor/gems/metric_parser-0.1.0.pre1/.specification +116 -0
  365. data/vendor/gems/metric_parser-0.1.0.pre1/lib/metric_parser.rb +1 -0
  366. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +64 -0
  367. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +14 -0
  368. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +31 -0
  369. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +33 -0
  370. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +89 -0
  371. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +7 -0
  372. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +46 -0
  373. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +67 -0
  374. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +43 -0
  375. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +17 -0
  376. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +48 -0
  377. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +24 -0
  378. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +28 -0
  379. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +17 -0
  380. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/errors.rb +11 -0
  381. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +55 -0
  382. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +40 -0
  383. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +20 -0
  384. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +7 -0
  385. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +31 -0
  386. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +17 -0
  387. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +34 -0
  388. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +7 -0
  389. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +55 -0
  390. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +135 -0
  391. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +27 -0
  392. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +40 -0
  393. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +7 -0
  394. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +7 -0
  395. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +7 -0
  396. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_init.rb +7 -0
  397. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +27 -0
  398. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +15 -0
  399. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +54 -0
  400. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_controller.rb +6 -0
  401. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_view.rb +6 -0
  402. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +20 -0
  403. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +20 -0
  404. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/version.rb +5 -0
  405. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +70 -0
  406. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_frontend.rb +18 -0
  407. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_service.rb +14 -0
  408. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +133 -0
  409. metadata +494 -0
@@ -0,0 +1,6 @@
1
+ namespace :newrelic do
2
+ desc "Install the developer mode newrelic.yml file"
3
+ task :default do
4
+ load File.expand_path(File.join(__FILE__,"..","..","install.rb"))
5
+ end
6
+ end
@@ -0,0 +1,70 @@
1
+ require 'new_relic/coerce'
2
+
3
+ module NewRelic
4
+ class MetricData
5
+ # nil, or a NewRelic::MetricSpec object if we have no cached ID
6
+ attr_accessor :metric_spec
7
+ # nil or a cached integer ID for the metric from the collector.
8
+ attr_accessor :metric_id
9
+ # the actual statistics object
10
+ attr_accessor :stats
11
+
12
+ def initialize(metric_spec, stats, metric_id)
13
+ @metric_spec = metric_spec
14
+ self.stats = stats
15
+ self.metric_id = metric_id
16
+ end
17
+
18
+ def eql?(o)
19
+ (metric_spec.eql? o.metric_spec) && (stats.eql? o.stats)
20
+ end
21
+
22
+ def original_spec
23
+ @original_spec || @metric_spec
24
+ end
25
+
26
+ # assigns a new metric spec, and retains the old metric spec as
27
+ # @original_spec if it exists currently
28
+ def metric_spec= new_spec
29
+ @original_spec = @metric_spec if @metric_spec
30
+ @metric_spec = new_spec
31
+ end
32
+
33
+ def hash
34
+ metric_spec.hash ^ stats.hash
35
+ end
36
+
37
+ # Serialize with all attributes, but if the metric id is not nil, then don't send the metric spec
38
+ def to_json(*a)
39
+ %Q[{"metric_spec":#{metric_id ? 'null' : metric_spec.to_json},"stats":{"total_exclusive_time":#{stats.total_exclusive_time},"min_call_time":#{stats.min_call_time},"call_count":#{stats.call_count},"sum_of_squares":#{stats.sum_of_squares},"total_call_time":#{stats.total_call_time},"max_call_time":#{stats.max_call_time}},"metric_id":#{metric_id ? metric_id : 'null'}}]
40
+ end
41
+
42
+ def to_s
43
+ if metric_spec
44
+ "#{metric_spec.name}(#{metric_spec.scope}): #{stats}"
45
+ else
46
+ "#{metric_id}: #{stats}"
47
+ end
48
+ end
49
+
50
+ def inspect
51
+ "#<MetricData metric_spec:#{metric_spec.inspect}, stats:#{stats.inspect}, metric_id:#{metric_id.inspect}>"
52
+ end
53
+
54
+ include NewRelic::Coerce
55
+
56
+ def to_collector_array(encoder=nil)
57
+ stat_key = metric_id || { 'name' => metric_spec.name, 'scope' => metric_spec.scope }
58
+ [ stat_key,
59
+ [
60
+ int(stats.call_count, stat_key),
61
+ float(stats.total_call_time, stat_key),
62
+ float(stats.total_exclusive_time, stat_key),
63
+ float(stats.min_call_time, stat_key),
64
+ float(stats.max_call_time, stat_key),
65
+ float(stats.sum_of_squares, stat_key)
66
+ ]
67
+ ]
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,80 @@
1
+ # this struct uniquely defines a metric, optionally inside
2
+ # the call scope of another metric
3
+ class NewRelic::MetricSpec
4
+ attr_accessor :name
5
+ attr_accessor :scope
6
+
7
+ # the maximum length of a metric name or metric scope
8
+ MAX_LENGTH = 255
9
+ LENGTH_RANGE = (0...MAX_LENGTH)
10
+ # Need a "zero-arg" constructor so it can be instantiated from java (using
11
+ # jruby) for sending responses to ruby agents from the java collector.
12
+ #
13
+ def initialize(metric_name = '', metric_scope = nil)
14
+ self.name = (metric_name || '') && metric_name[LENGTH_RANGE]
15
+ if metric_scope
16
+ self.scope = metric_scope && metric_scope[LENGTH_RANGE]
17
+ else
18
+ self.scope = ''
19
+ end
20
+ end
21
+
22
+ # truncates the name and scope to the MAX_LENGTH
23
+ def truncate!
24
+ self.name = name[LENGTH_RANGE] if name && name.size > MAX_LENGTH
25
+ self.scope = scope[LENGTH_RANGE] if scope && scope.size > MAX_LENGTH
26
+ end
27
+
28
+ def ==(o)
29
+ self.eql?(o)
30
+ end
31
+
32
+ def eql? o
33
+ self.class == o.class &&
34
+ name.eql?(o.name) &&
35
+ # coerce scope to a string and compare
36
+ scope.to_s == o.scope.to_s
37
+ end
38
+
39
+ def hash
40
+ h = name.hash
41
+ h ^= scope.hash unless scope.nil?
42
+ h
43
+ end
44
+ # return a new metric spec if the given regex
45
+ # matches the name or scope.
46
+ def sub(pattern, replacement, apply_to_scope = true)
47
+ ::NewRelic::Agent.logger.warn("The sub method on metric specs is deprecated") rescue nil
48
+ return nil if name !~ pattern &&
49
+ (!apply_to_scope || scope.nil? || scope !~ pattern)
50
+ new_name = name.sub(pattern, replacement)[LENGTH_RANGE]
51
+
52
+ if apply_to_scope
53
+ new_scope = (scope && scope.sub(pattern, replacement)[LENGTH_RANGE])
54
+ else
55
+ new_scope = scope
56
+ end
57
+
58
+ self.class.new new_name, new_scope
59
+ end
60
+
61
+ def to_s
62
+ return name if scope.empty?
63
+ "#{name}:#{scope}"
64
+ end
65
+
66
+ def inspect
67
+ "#<NewRelic::MetricSpec '#{name}':'#{scope}'>"
68
+ end
69
+
70
+ def to_json(*a)
71
+ {'name' => name,
72
+ 'scope' => scope}.to_json(*a)
73
+ end
74
+
75
+ def <=>(o)
76
+ namecmp = self.name <=> o.name
77
+ return namecmp if namecmp != 0
78
+ return (self.scope || '') <=> (o.scope || '')
79
+ end
80
+ end
@@ -0,0 +1,9 @@
1
+ module NewRelic
2
+ module Metrics
3
+ CONTROLLER = "Controller"
4
+ DISPATCHER = "HttpDispatcher"
5
+ ACTIVE_RECORD = "ActiveRecord"
6
+ USER_TIME = "CPU/User Time"
7
+ MEMORY = "Memory/Physical"
8
+ end
9
+ end
@@ -0,0 +1,59 @@
1
+ require 'new_relic/helper'
2
+
3
+ # This class encapsulates an error that was noticed by New Relic in a managed app.
4
+ class NewRelic::NoticedError
5
+ extend NewRelic::CollectionHelper
6
+ attr_accessor :path, :timestamp, :params, :exception_class, :message
7
+ attr_reader :exception_id
8
+
9
+ def initialize(path, data, exception, timestamp = Time.now)
10
+ @exception_id = exception.object_id
11
+ @path = path
12
+ @params = NewRelic::NoticedError.normalize_params(data)
13
+
14
+ @exception_class = exception.is_a?(Exception) ? exception.class.name : 'Error'
15
+
16
+ if exception.respond_to?('original_exception')
17
+ @message = exception.original_exception.message.to_s
18
+ else
19
+ @message = (exception || '<no message>').to_s
20
+ end
21
+
22
+ unless @message.is_a?(String)
23
+ # In pre-1.9.3, Exception.new({}).to_s.class != String
24
+ # That is, Exception#to_s may not return a String instance if one wasn't
25
+ # passed in upon creation of the Exception. So, try to generate a useful
26
+ # String representation of the exception message, falling back to failsafe
27
+ @message = String(@message.inspect) rescue '<unknown message type>'
28
+ end
29
+
30
+ # clamp long messages to 4k so that we don't send a lot of
31
+ # overhead across the wire
32
+ @message = @message[0..4095] if @message.length > 4096
33
+
34
+ # obfuscate error message if necessary
35
+ if NewRelic::Agent.config[:high_security]
36
+ @message = NewRelic::Agent::Database.obfuscate_sql(@message)
37
+ end
38
+
39
+ @timestamp = timestamp
40
+ end
41
+
42
+ def ==(other)
43
+ if other.respond_to?(:exception_id)
44
+ @exception_id == other.exception_id
45
+ else
46
+ false
47
+ end
48
+ end
49
+
50
+ include NewRelic::Coerce
51
+
52
+ def to_collector_array(encoder=nil)
53
+ [ NewRelic::Helper.time_to_millis(@timestamp),
54
+ string(@path),
55
+ string(@message),
56
+ string(@exception_class),
57
+ @params ]
58
+ end
59
+ end
@@ -0,0 +1,599 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011, 2012 Keith Rarick
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ # See https://github.com/kr/okjson for updates.
24
+
25
+ require 'stringio'
26
+
27
+ # Some parts adapted from
28
+ # http://golang.org/src/pkg/json/decode.go and
29
+ # http://golang.org/src/pkg/utf8/utf8.go
30
+ module NewRelic
31
+ module OkJson
32
+ Upstream = 'LTD7LBKLZWFF7OZK'
33
+ extend self
34
+
35
+
36
+ # Decodes a json document in string s and
37
+ # returns the corresponding ruby value.
38
+ # String s must be valid UTF-8. If you have
39
+ # a string in some other encoding, convert
40
+ # it first.
41
+ #
42
+ # String values in the resulting structure
43
+ # will be UTF-8.
44
+ def decode(s)
45
+ ts = lex(s)
46
+ v, ts = textparse(ts)
47
+ if ts.length > 0
48
+ raise Error, 'trailing garbage'
49
+ end
50
+ v
51
+ end
52
+
53
+
54
+ # Parses a "json text" in the sense of RFC 4627.
55
+ # Returns the parsed value and any trailing tokens.
56
+ # Note: this is almost the same as valparse,
57
+ # except that it does not accept atomic values.
58
+ def textparse(ts)
59
+ if ts.length < 0
60
+ raise Error, 'empty'
61
+ end
62
+
63
+ typ, _, val = ts[0]
64
+ case typ
65
+ when '{' then objparse(ts)
66
+ when '[' then arrparse(ts)
67
+ else
68
+ raise Error, "unexpected #{val.inspect}"
69
+ end
70
+ end
71
+
72
+
73
+ # Parses a "value" in the sense of RFC 4627.
74
+ # Returns the parsed value and any trailing tokens.
75
+ def valparse(ts)
76
+ if ts.length < 0
77
+ raise Error, 'empty'
78
+ end
79
+
80
+ typ, _, val = ts[0]
81
+ case typ
82
+ when '{' then objparse(ts)
83
+ when '[' then arrparse(ts)
84
+ when :val,:str then [val, ts[1..-1]]
85
+ else
86
+ raise Error, "unexpected #{val.inspect}"
87
+ end
88
+ end
89
+
90
+
91
+ # Parses an "object" in the sense of RFC 4627.
92
+ # Returns the parsed value and any trailing tokens.
93
+ def objparse(ts)
94
+ ts = eat('{', ts)
95
+ obj = {}
96
+
97
+ if ts[0][0] == '}'
98
+ return obj, ts[1..-1]
99
+ end
100
+
101
+ k, v, ts = pairparse(ts)
102
+ obj[k] = v
103
+
104
+ if ts[0][0] == '}'
105
+ return obj, ts[1..-1]
106
+ end
107
+
108
+ loop do
109
+ ts = eat(',', ts)
110
+
111
+ k, v, ts = pairparse(ts)
112
+ obj[k] = v
113
+
114
+ if ts[0][0] == '}'
115
+ return obj, ts[1..-1]
116
+ end
117
+ end
118
+ end
119
+
120
+
121
+ # Parses a "member" in the sense of RFC 4627.
122
+ # Returns the parsed values and any trailing tokens.
123
+ def pairparse(ts)
124
+ (typ, _, k), ts = ts[0], ts[1..-1]
125
+ if typ != :str
126
+ raise Error, "unexpected #{k.inspect}"
127
+ end
128
+ ts = eat(':', ts)
129
+ v, ts = valparse(ts)
130
+ [k, v, ts]
131
+ end
132
+
133
+
134
+ # Parses an "array" in the sense of RFC 4627.
135
+ # Returns the parsed value and any trailing tokens.
136
+ def arrparse(ts)
137
+ ts = eat('[', ts)
138
+ arr = []
139
+
140
+ if ts[0][0] == ']'
141
+ return arr, ts[1..-1]
142
+ end
143
+
144
+ v, ts = valparse(ts)
145
+ arr << v
146
+
147
+ if ts[0][0] == ']'
148
+ return arr, ts[1..-1]
149
+ end
150
+
151
+ loop do
152
+ ts = eat(',', ts)
153
+
154
+ v, ts = valparse(ts)
155
+ arr << v
156
+
157
+ if ts[0][0] == ']'
158
+ return arr, ts[1..-1]
159
+ end
160
+ end
161
+ end
162
+
163
+
164
+ def eat(typ, ts)
165
+ if ts[0][0] != typ
166
+ raise Error, "expected #{typ} (got #{ts[0].inspect})"
167
+ end
168
+ ts[1..-1]
169
+ end
170
+
171
+
172
+ # Scans s and returns a list of json tokens,
173
+ # excluding white space (as defined in RFC 4627).
174
+ def lex(s)
175
+ ts = []
176
+ while s.length > 0
177
+ typ, lexeme, val = tok(s)
178
+ if typ == nil
179
+ raise Error, "invalid character at #{s[0,10].inspect}"
180
+ end
181
+ if typ != :space
182
+ ts << [typ, lexeme, val]
183
+ end
184
+ s = s[lexeme.length..-1]
185
+ end
186
+ ts
187
+ end
188
+
189
+
190
+ # Scans the first token in s and
191
+ # returns a 3-element list, or nil
192
+ # if s does not begin with a valid token.
193
+ #
194
+ # The first list element is one of
195
+ # '{', '}', ':', ',', '[', ']',
196
+ # :val, :str, and :space.
197
+ #
198
+ # The second element is the lexeme.
199
+ #
200
+ # The third element is the value of the
201
+ # token for :val and :str, otherwise
202
+ # it is the lexeme.
203
+ def tok(s)
204
+ case s[0]
205
+ when ?{ then ['{', s[0,1], s[0,1]]
206
+ when ?} then ['}', s[0,1], s[0,1]]
207
+ when ?: then [':', s[0,1], s[0,1]]
208
+ when ?, then [',', s[0,1], s[0,1]]
209
+ when ?[ then ['[', s[0,1], s[0,1]]
210
+ when ?] then [']', s[0,1], s[0,1]]
211
+ when ?n then nulltok(s)
212
+ when ?t then truetok(s)
213
+ when ?f then falsetok(s)
214
+ when ?" then strtok(s)
215
+ when Spc then [:space, s[0,1], s[0,1]]
216
+ when ?\t then [:space, s[0,1], s[0,1]]
217
+ when ?\n then [:space, s[0,1], s[0,1]]
218
+ when ?\r then [:space, s[0,1], s[0,1]]
219
+ else numtok(s)
220
+ end
221
+ end
222
+
223
+
224
+ def nulltok(s); s[0,4] == 'null' ? [:val, 'null', nil] : [] end
225
+ def truetok(s); s[0,4] == 'true' ? [:val, 'true', true] : [] end
226
+ def falsetok(s); s[0,5] == 'false' ? [:val, 'false', false] : [] end
227
+
228
+
229
+ def numtok(s)
230
+ m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
231
+ if m && m.begin(0) == 0
232
+ if m[3] && !m[2]
233
+ [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
234
+ elsif m[2]
235
+ [:val, m[0], Float(m[0])]
236
+ else
237
+ [:val, m[0], Integer(m[0])]
238
+ end
239
+ else
240
+ []
241
+ end
242
+ end
243
+
244
+
245
+ def strtok(s)
246
+ m = /"([^"\\]|\\["\/\\bfnrt]|\\u[0-9a-fA-F]{4})*"/.match(s)
247
+ if ! m
248
+ raise Error, "invalid string literal at #{abbrev(s)}"
249
+ end
250
+ [:str, m[0], unquote(m[0])]
251
+ end
252
+
253
+
254
+ def abbrev(s)
255
+ t = s[0,10]
256
+ p = t['`']
257
+ t = t[0,p] if p
258
+ t = t + '...' if t.length < s.length
259
+ '`' + t + '`'
260
+ end
261
+
262
+
263
+ # Converts a quoted json string literal q into a UTF-8-encoded string.
264
+ # The rules are different than for Ruby, so we cannot use eval.
265
+ # Unquote will raise an error if q contains control characters.
266
+ def unquote(q)
267
+ q = q[1...-1]
268
+ a = q.dup # allocate a big enough string
269
+ rubydoesenc = false
270
+ # In ruby >= 1.9, a[w] is a codepoint, not a byte.
271
+ if a.class.method_defined?(:force_encoding)
272
+ a.force_encoding('UTF-8')
273
+ rubydoesenc = true
274
+ end
275
+ r, w = 0, 0
276
+ while r < q.length
277
+ c = q[r]
278
+ case true
279
+ when c == ?\\
280
+ r += 1
281
+ if r >= q.length
282
+ raise Error, "string literal ends with a \"\\\": \"#{q}\""
283
+ end
284
+
285
+ case q[r]
286
+ when ?",?\\,?/,?'
287
+ a[w] = q[r]
288
+ r += 1
289
+ w += 1
290
+ when ?b,?f,?n,?r,?t
291
+ a[w] = Unesc[q[r]]
292
+ r += 1
293
+ w += 1
294
+ when ?u
295
+ r += 1
296
+ uchar = begin
297
+ hexdec4(q[r,4])
298
+ rescue RuntimeError => e
299
+ raise Error, "invalid escape sequence \\u#{q[r,4]}: #{e}"
300
+ end
301
+ r += 4
302
+ if surrogate? uchar
303
+ if q.length >= r+6
304
+ uchar1 = hexdec4(q[r+2,4])
305
+ uchar = subst(uchar, uchar1)
306
+ if uchar != Ucharerr
307
+ # A valid pair; consume.
308
+ r += 6
309
+ end
310
+ end
311
+ end
312
+ if rubydoesenc
313
+ a[w] = '' << uchar
314
+ w += 1
315
+ else
316
+ w += ucharenc(a, w, uchar)
317
+ end
318
+ else
319
+ raise Error, "invalid escape char #{q[r]} in \"#{q}\""
320
+ end
321
+ when c == ?", c < Spc
322
+ raise Error, "invalid character in string literal \"#{q}\""
323
+ else
324
+ # Copy anything else byte-for-byte.
325
+ # Valid UTF-8 will remain valid UTF-8.
326
+ # Invalid UTF-8 will remain invalid UTF-8.
327
+ # In ruby >= 1.9, c is a codepoint, not a byte,
328
+ # in which case this is still what we want.
329
+ a[w] = c
330
+ r += 1
331
+ w += 1
332
+ end
333
+ end
334
+ a[0,w]
335
+ end
336
+
337
+
338
+ # Encodes unicode character u as UTF-8
339
+ # bytes in string a at position i.
340
+ # Returns the number of bytes written.
341
+ def ucharenc(a, i, u)
342
+ case true
343
+ when u <= Uchar1max
344
+ a[i] = (u & 0xff).chr
345
+ 1
346
+ when u <= Uchar2max
347
+ a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
348
+ a[i+1] = (Utagx | (u&Umaskx)).chr
349
+ 2
350
+ when u <= Uchar3max
351
+ a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
352
+ a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
353
+ a[i+2] = (Utagx | (u&Umaskx)).chr
354
+ 3
355
+ else
356
+ a[i+0] = (Utag4 | ((u>>18)&0xff)).chr
357
+ a[i+1] = (Utagx | ((u>>12)&Umaskx)).chr
358
+ a[i+2] = (Utagx | ((u>>6)&Umaskx)).chr
359
+ a[i+3] = (Utagx | (u&Umaskx)).chr
360
+ 4
361
+ end
362
+ end
363
+
364
+
365
+ def hexdec4(s)
366
+ if s.length != 4
367
+ raise Error, 'short'
368
+ end
369
+ (nibble(s[0])<<12) | (nibble(s[1])<<8) | (nibble(s[2])<<4) | nibble(s[3])
370
+ end
371
+
372
+
373
+ def subst(u1, u2)
374
+ if Usurr1 <= u1 && u1 < Usurr2 && Usurr2 <= u2 && u2 < Usurr3
375
+ return ((u1-Usurr1)<<10) | (u2-Usurr2) + Usurrself
376
+ end
377
+ return Ucharerr
378
+ end
379
+
380
+
381
+ def surrogate?(u)
382
+ Usurr1 <= u && u < Usurr3
383
+ end
384
+
385
+
386
+ def nibble(c)
387
+ case true
388
+ when ?0 <= c && c <= ?9 then c.ord - ?0.ord
389
+ when ?a <= c && c <= ?z then c.ord - ?a.ord + 10
390
+ when ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
391
+ else
392
+ raise Error, "invalid hex code #{c}"
393
+ end
394
+ end
395
+
396
+
397
+ # Encodes x into a json text. It may contain only
398
+ # Array, Hash, String, Numeric, true, false, nil.
399
+ # (Note, this list excludes Symbol.)
400
+ # X itself must be an Array or a Hash.
401
+ # No other value can be encoded, and an error will
402
+ # be raised if x contains any other value, such as
403
+ # Nan, Infinity, Symbol, and Proc, or if a Hash key
404
+ # is not a String.
405
+ # Strings contained in x must be valid UTF-8.
406
+ def encode(x)
407
+ case x
408
+ when Hash then objenc(x)
409
+ when Array then arrenc(x)
410
+ else
411
+ raise Error, 'root value must be an Array or a Hash'
412
+ end
413
+ end
414
+
415
+
416
+ def valenc(x)
417
+ case x
418
+ when Hash then objenc(x)
419
+ when Array then arrenc(x)
420
+ when String then strenc(x)
421
+ when Numeric then numenc(x)
422
+ when true then "true"
423
+ when false then "false"
424
+ when nil then "null"
425
+ else
426
+ raise Error, "cannot encode #{x.class}: #{x.inspect}"
427
+ end
428
+ end
429
+
430
+
431
+ def objenc(x)
432
+ '{' + x.map{|k,v| keyenc(k) + ':' + valenc(v)}.join(',') + '}'
433
+ end
434
+
435
+
436
+ def arrenc(a)
437
+ '[' + a.map{|x| valenc(x)}.join(',') + ']'
438
+ end
439
+
440
+
441
+ def keyenc(k)
442
+ case k
443
+ when String then strenc(k)
444
+ else
445
+ raise Error, "Hash key is not a string: #{k.inspect}"
446
+ end
447
+ end
448
+
449
+
450
+ def strenc(s)
451
+ t = StringIO.new
452
+ t.putc(?")
453
+ r = 0
454
+
455
+ # In ruby >= 1.9, s[r] is a codepoint, not a byte.
456
+ rubydoesenc = s.class.method_defined?(:encoding)
457
+
458
+ while r < s.length
459
+ case s[r]
460
+ when ?" then t.print('\\"')
461
+ when ?\\ then t.print('\\\\')
462
+ when ?\b then t.print('\\b')
463
+ when ?\f then t.print('\\f')
464
+ when ?\n then t.print('\\n')
465
+ when ?\r then t.print('\\r')
466
+ when ?\t then t.print('\\t')
467
+ else
468
+ c = s[r]
469
+ case true
470
+ when rubydoesenc
471
+ begin
472
+ c.ord # will raise an error if c is invalid UTF-8
473
+ t.write(c)
474
+ rescue
475
+ t.write(Ustrerr)
476
+ end
477
+ when Spc <= c && c <= ?~
478
+ t.putc(c)
479
+ else
480
+ n = ucharcopy(t, s, r) # ensure valid UTF-8 output
481
+ r += n - 1 # r is incremented below
482
+ end
483
+ end
484
+ r += 1
485
+ end
486
+ t.putc(?")
487
+ t.string
488
+ end
489
+
490
+
491
+ def numenc(x)
492
+ if ((x.nan? || x.infinite?) rescue false)
493
+ raise Error, "Numeric cannot be represented: #{x}"
494
+ end
495
+ "#{x}"
496
+ end
497
+
498
+
499
+ # Copies the valid UTF-8 bytes of a single character
500
+ # from string s at position i to I/O object t, and
501
+ # returns the number of bytes copied.
502
+ # If no valid UTF-8 char exists at position i,
503
+ # ucharcopy writes Ustrerr and returns 1.
504
+ def ucharcopy(t, s, i)
505
+ n = s.length - i
506
+ raise Utf8Error if n < 1
507
+
508
+ c0 = s[i].ord
509
+
510
+ # 1-byte, 7-bit sequence?
511
+ if c0 < Utagx
512
+ t.putc(c0)
513
+ return 1
514
+ end
515
+
516
+ raise Utf8Error if c0 < Utag2 # unexpected continuation byte?
517
+
518
+ raise Utf8Error if n < 2 # need continuation byte
519
+ c1 = s[i+1].ord
520
+ raise Utf8Error if c1 < Utagx || Utag2 <= c1
521
+
522
+ # 2-byte, 11-bit sequence?
523
+ if c0 < Utag3
524
+ raise Utf8Error if ((c0&Umask2)<<6 | (c1&Umaskx)) <= Uchar1max
525
+ t.putc(c0)
526
+ t.putc(c1)
527
+ return 2
528
+ end
529
+
530
+ # need second continuation byte
531
+ raise Utf8Error if n < 3
532
+
533
+ c2 = s[i+2].ord
534
+ raise Utf8Error if c2 < Utagx || Utag2 <= c2
535
+
536
+ # 3-byte, 16-bit sequence?
537
+ if c0 < Utag4
538
+ u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
539
+ raise Utf8Error if u <= Uchar2max
540
+ t.putc(c0)
541
+ t.putc(c1)
542
+ t.putc(c2)
543
+ return 3
544
+ end
545
+
546
+ # need third continuation byte
547
+ raise Utf8Error if n < 4
548
+ c3 = s[i+3].ord
549
+ raise Utf8Error if c3 < Utagx || Utag2 <= c3
550
+
551
+ # 4-byte, 21-bit sequence?
552
+ if c0 < Utag5
553
+ u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
554
+ raise Utf8Error if u <= Uchar3max
555
+ t.putc(c0)
556
+ t.putc(c1)
557
+ t.putc(c2)
558
+ t.putc(c3)
559
+ return 4
560
+ end
561
+
562
+ raise Utf8Error
563
+ rescue Utf8Error
564
+ t.write(Ustrerr)
565
+ return 1
566
+ end
567
+
568
+
569
+ class Utf8Error < ::StandardError
570
+ end
571
+
572
+
573
+ class Error < ::StandardError
574
+ end
575
+
576
+
577
+ Utagx = 0x80 # 1000 0000
578
+ Utag2 = 0xc0 # 1100 0000
579
+ Utag3 = 0xe0 # 1110 0000
580
+ Utag4 = 0xf0 # 1111 0000
581
+ Utag5 = 0xF8 # 1111 1000
582
+ Umaskx = 0x3f # 0011 1111
583
+ Umask2 = 0x1f # 0001 1111
584
+ Umask3 = 0x0f # 0000 1111
585
+ Umask4 = 0x07 # 0000 0111
586
+ Uchar1max = (1<<7) - 1
587
+ Uchar2max = (1<<11) - 1
588
+ Uchar3max = (1<<16) - 1
589
+ Ucharerr = 0xFFFD # unicode "replacement char"
590
+ Ustrerr = "\xef\xbf\xbd" # unicode "replacement char"
591
+ Usurrself = 0x10000
592
+ Usurr1 = 0xd800
593
+ Usurr2 = 0xdc00
594
+ Usurr3 = 0xe000
595
+
596
+ Spc = ' '[0]
597
+ Unesc = {?b=>?\b, ?f=>?\f, ?n=>?\n, ?r=>?\r, ?t=>?\t}
598
+ end
599
+ end