newrelic_rpm 5.5.0.348 → 7.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (364) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +9 -0
  3. data/.yardopts +1 -0
  4. data/CHANGELOG.md +931 -0
  5. data/CONTRIBUTING.md +117 -19
  6. data/Gemfile +6 -2
  7. data/Guardfile +18 -1
  8. data/LICENSE +208 -38
  9. data/README.md +80 -84
  10. data/ROADMAP.md +24 -0
  11. data/Rakefile +2 -0
  12. data/THIRD_PARTY_NOTICES.md +213 -0
  13. data/bin/nrdebug +1 -1
  14. data/config.dot +1 -0
  15. data/init.rb +1 -1
  16. data/install.rb +1 -1
  17. data/lib/new_relic/agent.rb +111 -20
  18. data/lib/new_relic/agent/adaptive_sampler.rb +1 -1
  19. data/lib/new_relic/agent/agent.rb +182 -194
  20. data/lib/new_relic/agent/agent_logger.rb +5 -1
  21. data/lib/new_relic/agent/attribute_filter.rb +85 -25
  22. data/lib/new_relic/agent/attribute_processing.rb +1 -1
  23. data/lib/new_relic/agent/attributes.rb +152 -0
  24. data/lib/new_relic/agent/audit_logger.rb +11 -1
  25. data/lib/new_relic/agent/autostart.rb +19 -15
  26. data/lib/new_relic/agent/chained_call.rb +1 -1
  27. data/lib/new_relic/agent/commands/agent_command.rb +1 -1
  28. data/lib/new_relic/agent/commands/agent_command_router.rb +3 -22
  29. data/lib/new_relic/agent/commands/thread_profiler_session.rb +1 -1
  30. data/lib/new_relic/agent/configuration.rb +1 -1
  31. data/lib/new_relic/agent/configuration/default_source.rb +656 -188
  32. data/lib/new_relic/agent/configuration/dotted_hash.rb +1 -1
  33. data/lib/new_relic/agent/configuration/environment_source.rb +5 -3
  34. data/lib/new_relic/agent/configuration/event_harvest_config.rb +45 -0
  35. data/lib/new_relic/agent/configuration/high_security_source.rb +2 -1
  36. data/lib/new_relic/agent/configuration/manager.rb +17 -13
  37. data/lib/new_relic/agent/configuration/manual_source.rb +1 -1
  38. data/lib/new_relic/agent/configuration/mask_defaults.rb +1 -1
  39. data/lib/new_relic/agent/configuration/security_policy_source.rb +15 -1
  40. data/lib/new_relic/agent/configuration/server_source.rb +39 -4
  41. data/lib/new_relic/agent/configuration/yaml_source.rb +12 -7
  42. data/lib/new_relic/agent/connect/request_builder.rb +63 -0
  43. data/lib/new_relic/agent/connect/response_handler.rb +61 -0
  44. data/lib/new_relic/agent/custom_event_aggregator.rb +1 -1
  45. data/lib/new_relic/agent/database.rb +2 -3
  46. data/lib/new_relic/agent/database/explain_plan_helpers.rb +1 -1
  47. data/lib/new_relic/agent/database/obfuscation_helpers.rb +2 -2
  48. data/lib/new_relic/agent/database/obfuscator.rb +1 -1
  49. data/lib/new_relic/agent/database/postgres_explain_obfuscator.rb +1 -1
  50. data/lib/new_relic/agent/database_adapter.rb +33 -0
  51. data/lib/new_relic/agent/datastores.rb +7 -9
  52. data/lib/new_relic/agent/datastores/metric_helper.rb +2 -3
  53. data/lib/new_relic/agent/datastores/mongo.rb +2 -2
  54. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +3 -3
  55. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +1 -1
  56. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +9 -9
  57. data/lib/new_relic/agent/datastores/mongo/statement_formatter.rb +1 -1
  58. data/lib/new_relic/agent/datastores/redis.rb +1 -5
  59. data/lib/new_relic/agent/deprecator.rb +1 -1
  60. data/lib/new_relic/agent/distributed_tracing.rb +115 -32
  61. data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +3 -2
  62. data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +61 -46
  63. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +84 -0
  64. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  65. data/lib/new_relic/agent/{distributed_trace_payload.rb → distributed_tracing/distributed_trace_payload.rb} +32 -113
  66. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
  67. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
  68. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +126 -0
  69. data/lib/new_relic/agent/encoding_normalizer.rb +1 -1
  70. data/lib/new_relic/agent/error_collector.rb +88 -58
  71. data/lib/new_relic/agent/error_event_aggregator.rb +10 -7
  72. data/lib/new_relic/agent/error_filter.rb +167 -0
  73. data/lib/new_relic/agent/error_trace_aggregator.rb +2 -1
  74. data/lib/new_relic/agent/event_aggregator.rb +27 -33
  75. data/lib/new_relic/agent/event_buffer.rb +1 -1
  76. data/lib/new_relic/agent/event_listener.rb +1 -1
  77. data/lib/new_relic/agent/event_loop.rb +1 -1
  78. data/lib/new_relic/agent/external.rb +13 -11
  79. data/lib/new_relic/agent/guid_generator.rb +28 -0
  80. data/lib/new_relic/agent/harvester.rb +1 -1
  81. data/lib/new_relic/agent/heap.rb +1 -1
  82. data/lib/new_relic/agent/hostname.rb +16 -2
  83. data/lib/new_relic/agent/http_clients/abstract.rb +82 -0
  84. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +25 -20
  85. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +29 -14
  86. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +18 -22
  87. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +11 -12
  88. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +17 -5
  89. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +5 -7
  90. data/lib/new_relic/agent/http_clients/uri_util.rb +12 -11
  91. data/lib/new_relic/agent/instrumentation.rb +1 -1
  92. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +26 -43
  93. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +53 -73
  94. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +81 -56
  95. data/lib/new_relic/agent/instrumentation/active_job.rb +7 -8
  96. data/lib/new_relic/agent/instrumentation/active_merchant.rb +1 -1
  97. data/lib/new_relic/agent/instrumentation/active_record.rb +85 -38
  98. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +17 -6
  99. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +152 -0
  100. data/lib/new_relic/agent/instrumentation/active_record_prepend.rb +35 -11
  101. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +47 -51
  102. data/lib/new_relic/agent/instrumentation/active_storage.rb +23 -0
  103. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +63 -0
  104. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +10 -9
  105. data/lib/new_relic/agent/instrumentation/authlogic.rb +1 -1
  106. data/lib/new_relic/agent/instrumentation/bunny.rb +13 -132
  107. data/lib/new_relic/agent/instrumentation/bunny/chain.rb +45 -0
  108. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +152 -0
  109. data/lib/new_relic/agent/instrumentation/bunny/prepend.rb +35 -0
  110. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +11 -4
  111. data/lib/new_relic/agent/instrumentation/curb.rb +11 -183
  112. data/lib/new_relic/agent/instrumentation/curb/chain.rb +93 -0
  113. data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +222 -0
  114. data/lib/new_relic/agent/instrumentation/curb/prepend.rb +63 -0
  115. data/lib/new_relic/agent/instrumentation/data_mapper.rb +5 -3
  116. data/lib/new_relic/agent/instrumentation/delayed_job/chain.rb +38 -0
  117. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +53 -0
  118. data/lib/new_relic/agent/instrumentation/delayed_job/prepend.rb +34 -0
  119. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +9 -49
  120. data/lib/new_relic/agent/instrumentation/excon.rb +4 -3
  121. data/lib/new_relic/agent/instrumentation/excon/connection.rb +8 -5
  122. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +3 -2
  123. data/lib/new_relic/agent/instrumentation/grape.rb +14 -107
  124. data/lib/new_relic/agent/instrumentation/grape/chain.rb +25 -0
  125. data/lib/new_relic/agent/instrumentation/grape/instrumentation.rb +100 -0
  126. data/lib/new_relic/agent/instrumentation/grape/prepend.rb +17 -0
  127. data/lib/new_relic/agent/instrumentation/httpclient.rb +10 -30
  128. data/lib/new_relic/agent/instrumentation/httpclient/chain.rb +25 -0
  129. data/lib/new_relic/agent/instrumentation/httpclient/instrumentation.rb +38 -0
  130. data/lib/new_relic/agent/instrumentation/httpclient/prepend.rb +17 -0
  131. data/lib/new_relic/agent/instrumentation/httprb.rb +29 -0
  132. data/lib/new_relic/agent/instrumentation/httprb/chain.rb +22 -0
  133. data/lib/new_relic/agent/instrumentation/httprb/instrumentation.rb +30 -0
  134. data/lib/new_relic/agent/instrumentation/httprb/prepend.rb +15 -0
  135. data/lib/new_relic/agent/instrumentation/ignore_actions.rb +1 -1
  136. data/lib/new_relic/agent/instrumentation/memcache.rb +55 -68
  137. data/lib/new_relic/agent/instrumentation/memcache/chain.rb +16 -0
  138. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +39 -118
  139. data/lib/new_relic/agent/instrumentation/memcache/helper.rb +56 -0
  140. data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +88 -0
  141. data/lib/new_relic/agent/instrumentation/memcache/prepend.rb +88 -0
  142. data/lib/new_relic/agent/instrumentation/merb/controller.rb +1 -1
  143. data/lib/new_relic/agent/instrumentation/merb/errors.rb +1 -1
  144. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +4 -2
  145. data/lib/new_relic/agent/instrumentation/middleware_tracing.rb +20 -11
  146. data/lib/new_relic/agent/instrumentation/mongo.rb +17 -4
  147. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +36 -3
  148. data/lib/new_relic/agent/instrumentation/net_http.rb +44 -0
  149. data/lib/new_relic/agent/instrumentation/net_http/chain.rb +25 -0
  150. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +40 -0
  151. data/lib/new_relic/agent/instrumentation/net_http/prepend.rb +21 -0
  152. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +100 -0
  153. data/lib/new_relic/agent/instrumentation/padrino.rb +19 -52
  154. data/lib/new_relic/agent/instrumentation/padrino/chain.rb +34 -0
  155. data/lib/new_relic/agent/instrumentation/padrino/instrumentation.rb +27 -0
  156. data/lib/new_relic/agent/instrumentation/padrino/prepend.rb +20 -0
  157. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +1 -1
  158. data/lib/new_relic/agent/instrumentation/queue_time.rb +1 -1
  159. data/lib/new_relic/agent/instrumentation/rack.rb +30 -138
  160. data/lib/new_relic/agent/instrumentation/rack/chain.rb +58 -0
  161. data/lib/new_relic/agent/instrumentation/rack/helpers.rb +32 -0
  162. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +73 -0
  163. data/lib/new_relic/agent/instrumentation/rack/prepend.rb +37 -0
  164. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +1 -1
  165. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +1 -1
  166. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +1 -1
  167. data/lib/new_relic/agent/instrumentation/rails_middleware.rb +1 -1
  168. data/lib/new_relic/agent/instrumentation/{rails5 → rails_notifications}/action_cable.rb +6 -7
  169. data/lib/new_relic/agent/instrumentation/{rails5 → rails_notifications}/action_controller.rb +4 -4
  170. data/lib/new_relic/agent/instrumentation/{rails4 → rails_notifications}/action_view.rb +4 -4
  171. data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +1 -1
  172. data/lib/new_relic/agent/instrumentation/rake.rb +14 -155
  173. data/lib/new_relic/agent/instrumentation/rake/chain.rb +25 -0
  174. data/lib/new_relic/agent/instrumentation/rake/instrumentation.rb +144 -0
  175. data/lib/new_relic/agent/instrumentation/rake/prepend.rb +14 -0
  176. data/lib/new_relic/agent/instrumentation/redis.rb +13 -102
  177. data/lib/new_relic/agent/instrumentation/redis/chain.rb +34 -0
  178. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +65 -0
  179. data/lib/new_relic/agent/instrumentation/redis/prepend.rb +24 -0
  180. data/lib/new_relic/agent/instrumentation/resque.rb +22 -30
  181. data/lib/new_relic/agent/instrumentation/resque/chain.rb +22 -0
  182. data/lib/new_relic/agent/instrumentation/resque/helper.rb +19 -0
  183. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +35 -0
  184. data/lib/new_relic/agent/instrumentation/resque/prepend.rb +16 -0
  185. data/lib/new_relic/agent/instrumentation/sequel.rb +2 -2
  186. data/lib/new_relic/agent/instrumentation/sequel_helper.rb +1 -1
  187. data/lib/new_relic/agent/instrumentation/sidekiq.rb +48 -24
  188. data/lib/new_relic/agent/instrumentation/sinatra.rb +21 -160
  189. data/lib/new_relic/agent/instrumentation/sinatra/chain.rb +55 -0
  190. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +30 -35
  191. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +124 -0
  192. data/lib/new_relic/agent/instrumentation/sinatra/prepend.rb +33 -0
  193. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +1 -1
  194. data/lib/new_relic/agent/instrumentation/sunspot.rb +1 -1
  195. data/lib/new_relic/agent/instrumentation/typhoeus.rb +12 -74
  196. data/lib/new_relic/agent/instrumentation/typhoeus/chain.rb +22 -0
  197. data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +82 -0
  198. data/lib/new_relic/agent/instrumentation/typhoeus/prepend.rb +14 -0
  199. data/lib/new_relic/agent/internal_agent_error.rb +1 -1
  200. data/lib/new_relic/agent/javascript_instrumentor.rb +15 -10
  201. data/lib/new_relic/agent/log_once.rb +1 -1
  202. data/lib/new_relic/agent/logging.rb +139 -0
  203. data/lib/new_relic/agent/memory_logger.rb +1 -1
  204. data/lib/new_relic/agent/messaging.rb +15 -82
  205. data/lib/new_relic/agent/method_tracer.rb +10 -8
  206. data/lib/new_relic/agent/method_tracer_helpers.rb +5 -5
  207. data/lib/new_relic/agent/monitors.rb +27 -0
  208. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
  209. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
  210. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +4 -4
  211. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +4 -7
  212. data/lib/new_relic/agent/new_relic_service.rb +106 -40
  213. data/lib/new_relic/agent/new_relic_service/encoders.rb +1 -1
  214. data/lib/new_relic/agent/new_relic_service/json_marshaller.rb +1 -2
  215. data/lib/new_relic/agent/new_relic_service/marshaller.rb +6 -27
  216. data/lib/new_relic/agent/new_relic_service/security_policy_settings.rb +1 -1
  217. data/lib/new_relic/agent/noticible_error.rb +22 -0
  218. data/lib/new_relic/agent/null_logger.rb +1 -1
  219. data/lib/new_relic/agent/obfuscator.rb +1 -1
  220. data/lib/new_relic/agent/parameter_filtering.rb +19 -6
  221. data/lib/new_relic/agent/payload_metric_mapping.rb +1 -1
  222. data/lib/new_relic/agent/pipe_channel_manager.rb +1 -1
  223. data/lib/new_relic/agent/pipe_service.rb +1 -1
  224. data/lib/new_relic/agent/prepend_supportability.rb +1 -1
  225. data/lib/new_relic/agent/priority_sampled_buffer.rb +9 -4
  226. data/lib/new_relic/agent/range_extensions.rb +1 -1
  227. data/lib/new_relic/agent/rules_engine.rb +1 -1
  228. data/lib/new_relic/agent/rules_engine/replacement_rule.rb +1 -1
  229. data/lib/new_relic/agent/rules_engine/segment_terms_rule.rb +1 -1
  230. data/lib/new_relic/agent/sampler.rb +1 -1
  231. data/lib/new_relic/agent/sampler_collection.rb +1 -1
  232. data/lib/new_relic/agent/samplers/cpu_sampler.rb +1 -1
  233. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +1 -1
  234. data/lib/new_relic/agent/samplers/memory_sampler.rb +2 -2
  235. data/lib/new_relic/agent/samplers/object_sampler.rb +1 -1
  236. data/lib/new_relic/agent/samplers/vm_sampler.rb +1 -1
  237. data/lib/new_relic/agent/span_event_aggregator.rb +3 -5
  238. data/lib/new_relic/agent/span_event_primitive.rb +129 -65
  239. data/lib/new_relic/agent/sql_sampler.rb +12 -12
  240. data/lib/new_relic/agent/stats.rb +1 -1
  241. data/lib/new_relic/agent/stats_engine.rb +3 -3
  242. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +2 -2
  243. data/lib/new_relic/agent/stats_engine/stats_hash.rb +1 -1
  244. data/lib/new_relic/agent/supported_versions.rb +3 -3
  245. data/lib/new_relic/agent/synthetics_event_aggregator.rb +1 -1
  246. data/lib/new_relic/agent/system_info.rb +18 -4
  247. data/lib/new_relic/agent/threading/agent_thread.rb +2 -2
  248. data/lib/new_relic/agent/threading/backtrace_node.rb +1 -1
  249. data/lib/new_relic/agent/threading/backtrace_service.rb +4 -4
  250. data/lib/new_relic/agent/threading/thread_profile.rb +10 -24
  251. data/lib/new_relic/agent/timestamp_sampled_buffer.rb +1 -1
  252. data/lib/new_relic/agent/tracer.rb +509 -0
  253. data/lib/new_relic/agent/transaction.rb +143 -171
  254. data/lib/new_relic/agent/transaction/abstract_segment.rb +33 -6
  255. data/lib/new_relic/agent/transaction/datastore_segment.rb +1 -3
  256. data/lib/new_relic/agent/transaction/distributed_tracer.rb +177 -0
  257. data/lib/new_relic/agent/transaction/distributed_tracing.rb +59 -149
  258. data/lib/new_relic/agent/transaction/external_request_segment.rb +32 -39
  259. data/lib/new_relic/agent/transaction/message_broker_segment.rb +5 -12
  260. data/lib/new_relic/agent/transaction/request_attributes.rb +1 -1
  261. data/lib/new_relic/agent/transaction/segment.rb +28 -2
  262. data/lib/new_relic/agent/transaction/slowest_sample_buffer.rb +1 -1
  263. data/lib/new_relic/agent/transaction/synthetics_sample_buffer.rb +1 -1
  264. data/lib/new_relic/agent/transaction/trace.rb +6 -13
  265. data/lib/new_relic/agent/transaction/trace_builder.rb +1 -2
  266. data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
  267. data/lib/new_relic/agent/transaction/trace_node.rb +12 -8
  268. data/lib/new_relic/agent/transaction/tracing.rb +1 -100
  269. data/lib/new_relic/agent/transaction/transaction_sample_buffer.rb +1 -1
  270. data/lib/new_relic/agent/transaction_error_primitive.rb +11 -16
  271. data/lib/new_relic/agent/transaction_event_aggregator.rb +1 -1
  272. data/lib/new_relic/agent/transaction_event_primitive.rb +29 -40
  273. data/lib/new_relic/agent/transaction_event_recorder.rb +4 -4
  274. data/lib/new_relic/agent/transaction_metrics.rb +1 -1
  275. data/lib/new_relic/agent/transaction_sampler.rb +2 -6
  276. data/lib/new_relic/agent/transaction_time_aggregator.rb +50 -26
  277. data/lib/new_relic/agent/utilization/aws.rb +1 -1
  278. data/lib/new_relic/agent/utilization/azure.rb +1 -1
  279. data/lib/new_relic/agent/utilization/gcp.rb +1 -1
  280. data/lib/new_relic/agent/utilization/pcf.rb +1 -1
  281. data/lib/new_relic/agent/utilization/vendor.rb +1 -1
  282. data/lib/new_relic/agent/utilization_data.rb +37 -2
  283. data/lib/new_relic/agent/vm.rb +1 -1
  284. data/lib/new_relic/agent/vm/jruby_vm.rb +1 -1
  285. data/lib/new_relic/agent/vm/monotonic_gc_profiler.rb +1 -1
  286. data/lib/new_relic/agent/vm/mri_vm.rb +7 -5
  287. data/lib/new_relic/agent/vm/snapshot.rb +1 -1
  288. data/lib/new_relic/agent/worker_loop.rb +1 -1
  289. data/lib/new_relic/cli/command.rb +1 -1
  290. data/lib/new_relic/cli/commands/deployments.rb +2 -3
  291. data/lib/new_relic/cli/commands/install.rb +4 -3
  292. data/lib/new_relic/coerce.rb +32 -7
  293. data/lib/new_relic/collection_helper.rb +1 -1
  294. data/lib/new_relic/constants.rb +42 -0
  295. data/lib/new_relic/control.rb +1 -1
  296. data/lib/new_relic/control/class_methods.rb +8 -2
  297. data/lib/new_relic/control/frameworks.rb +1 -1
  298. data/lib/new_relic/control/frameworks/external.rb +1 -1
  299. data/lib/new_relic/control/frameworks/merb.rb +1 -1
  300. data/lib/new_relic/control/frameworks/rails.rb +12 -10
  301. data/lib/new_relic/control/frameworks/rails3.rb +1 -1
  302. data/lib/new_relic/control/frameworks/rails4.rb +1 -1
  303. data/lib/new_relic/control/frameworks/{rails5.rb → rails_notifications.rb} +2 -2
  304. data/lib/new_relic/control/frameworks/ruby.rb +1 -1
  305. data/lib/new_relic/control/frameworks/sinatra.rb +1 -1
  306. data/lib/new_relic/control/instance_methods.rb +12 -2
  307. data/lib/new_relic/control/instrumentation.rb +1 -1
  308. data/lib/new_relic/control/server_methods.rb +1 -1
  309. data/lib/new_relic/delayed_job_injection.rb +1 -1
  310. data/lib/new_relic/dependency_detection.rb +124 -14
  311. data/lib/new_relic/environment_report.rb +2 -4
  312. data/lib/new_relic/helper.rb +1 -1
  313. data/lib/new_relic/language_support.rb +1 -1
  314. data/lib/new_relic/latest_changes.rb +4 -4
  315. data/lib/new_relic/local_environment.rb +1 -1
  316. data/lib/new_relic/metric_data.rb +1 -1
  317. data/lib/new_relic/metric_spec.rb +1 -1
  318. data/lib/new_relic/noticed_error.rb +38 -21
  319. data/lib/new_relic/rack.rb +1 -1
  320. data/lib/new_relic/rack/agent_hooks.rb +1 -1
  321. data/lib/new_relic/rack/agent_middleware.rb +2 -2
  322. data/lib/new_relic/rack/browser_monitoring.rb +16 -9
  323. data/lib/new_relic/recipes.rb +1 -1
  324. data/lib/new_relic/recipes/capistrano3.rb +1 -1
  325. data/lib/new_relic/recipes/capistrano_legacy.rb +1 -1
  326. data/lib/new_relic/supportability_helper.rb +16 -1
  327. data/lib/new_relic/version.rb +3 -3
  328. data/lib/newrelic_rpm.rb +1 -1
  329. data/lib/sequel/extensions/newrelic_instrumentation.rb +1 -1
  330. data/lib/sequel/plugins/newrelic_instrumentation.rb +1 -1
  331. data/lib/tasks/all.rb +1 -1
  332. data/lib/tasks/config.html.erb +14 -25
  333. data/lib/tasks/config.rake +9 -9
  334. data/lib/tasks/multiverse.rb +35 -1
  335. data/lib/tasks/newrelic.rb +1 -1
  336. data/lib/tasks/tests.rake +6 -1
  337. data/newrelic_rpm.gemspec +22 -17
  338. data/recipes/newrelic.rb +1 -1
  339. data/test/agent_helper.rb +358 -78
  340. metadata +151 -66
  341. data/.travis.yml +0 -201
  342. data/cert/cacert.pem +0 -1177
  343. data/lib/new_relic/agent/commands/xray_session.rb +0 -55
  344. data/lib/new_relic/agent/commands/xray_session_collection.rb +0 -161
  345. data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
  346. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -41
  347. data/lib/new_relic/agent/http_clients/abstract_request.rb +0 -31
  348. data/lib/new_relic/agent/instrumentation/active_record_4.rb +0 -42
  349. data/lib/new_relic/agent/instrumentation/active_record_5.rb +0 -41
  350. data/lib/new_relic/agent/instrumentation/evented_subscriber.rb +0 -104
  351. data/lib/new_relic/agent/instrumentation/http.rb +0 -46
  352. data/lib/new_relic/agent/instrumentation/net.rb +0 -50
  353. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +0 -32
  354. data/lib/new_relic/agent/instrumentation/rails5/action_view.rb +0 -27
  355. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  356. data/lib/new_relic/agent/transaction/xray_sample_buffer.rb +0 -64
  357. data/lib/new_relic/agent/transaction_state.rb +0 -186
  358. data/lib/new_relic/build.rb +0 -2
  359. data/lib/new_relic/metrics.rb +0 -13
  360. data/lib/tasks/versions.html.erb +0 -28
  361. data/lib/tasks/versions.postface.html +0 -8
  362. data/lib/tasks/versions.preface.html +0 -9
  363. data/lib/tasks/versions.rake +0 -65
  364. data/lib/tasks/versions.txt.erb +0 -14
data/recipes/newrelic.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
- # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
4
 
5
5
  # The capistrano recipes in plugins are automatically
6
6
  # loaded from here. From gems, they are available from
data/test/agent_helper.rb CHANGED
@@ -1,30 +1,61 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
- # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
4
 
5
5
  # These helpers should not have any gem dependencies except on newrelic_rpm
6
6
  # itself, and should be usable from within any multiverse suite.
7
7
 
8
8
  require 'json'
9
+ require 'net/http'
10
+ begin
11
+ require 'net/http/status'
12
+ rescue LoadError
13
+ # NOP -- Net::HTTP::STATUS_CODES was introduced in Ruby 2.5
14
+ end
15
+
16
+ module MiniTest
17
+ module Assertions
18
+ # The failure message is backwards. This patch reverses the message
19
+ # Note: passing +msg+ caused two failure messages to be shown on failure
20
+ # and was more confusing than patching here.
21
+ def assert_match matcher, obj, msg = nil
22
+ msg = message(msg) { "Expected #{mu_pp obj} to match #{mu_pp matcher}" }
23
+ assert_respond_to matcher, :"=~"
24
+ matcher = Regexp.new Regexp.escape matcher if String === matcher
25
+ assert matcher =~ obj, msg
26
+ end
27
+ end
28
+ end
9
29
 
10
30
  class ArrayLogDevice
11
- def initialize( array=[] )
31
+ def initialize array=[]
12
32
  @array = array
13
33
  end
14
34
  attr_reader :array
15
35
 
16
- def write( message )
36
+ def write message
17
37
  @array << message
18
38
  end
19
39
 
20
40
  def close; end
21
41
  end
22
42
 
23
- def assert_between(floor, ceiling, value, message="expected #{floor} <= #{value} <= #{ceiling}")
43
+ def fake_guid length=16
44
+ NewRelic::Agent::GuidGenerator.generate_guid length
45
+ end
46
+
47
+ def assert_match matcher, obj, msg = nil
48
+ msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" }
49
+ assert_respond_to matcher, :"=~"
50
+ matcher = Regexp.new Regexp.escape matcher if String === matcher
51
+ assert matcher =~ obj, msg
52
+ end
53
+
54
+ def assert_between floor, ceiling, value, message="expected #{floor} <= #{value} <= #{ceiling}"
24
55
  assert((floor <= value && value <= ceiling), message)
25
56
  end
26
57
 
27
- def assert_in_delta(expected, actual, delta)
58
+ def assert_in_delta expected, actual, delta
28
59
  assert_between((expected - delta), (expected + delta), actual)
29
60
  end
30
61
 
@@ -36,7 +67,7 @@ def reset_error_traces!
36
67
  NewRelic::Agent.instance.error_collector.error_trace_aggregator.reset!
37
68
  end
38
69
 
39
- def assert_has_traced_error(error_class)
70
+ def assert_has_traced_error error_class
40
71
  errors = harvest_error_traces!
41
72
  assert \
42
73
  errors.find {|e| e.exception_class_name == error_class.name} != nil, \
@@ -55,6 +86,14 @@ def last_transaction_event
55
86
  harvest_transaction_events!.last.last
56
87
  end
57
88
 
89
+ def harvest_span_events!
90
+ NewRelic::Agent.instance.span_event_aggregator.harvest!
91
+ end
92
+
93
+ def last_span_event
94
+ harvest_span_events!.last.last
95
+ end
96
+
58
97
  def harvest_error_events!
59
98
  NewRelic::Agent.instance.error_collector.error_event_aggregator.harvest!
60
99
  end
@@ -63,38 +102,44 @@ def last_error_event
63
102
  harvest_error_events!.last.last
64
103
  end
65
104
 
66
- unless defined?( assert_block )
67
- def assert_block(*msgs)
105
+ unless defined? assert_block
106
+ def assert_block *msgs
68
107
  assert yield, *msgs
69
108
  end
70
109
  end
71
110
 
72
- unless defined?( assert_includes )
73
- def assert_includes( collection, member, msg=nil )
111
+ unless defined? assert_includes
112
+ def assert_includes collection, member, msg=nil
74
113
  msg = "Expected #{collection.inspect} to include #{member.inspect}"
75
114
  assert_block( msg ) { collection.include?(member) }
76
115
  end
77
116
  end
78
117
 
79
- unless defined?( assert_not_includes )
80
- def assert_not_includes( collection, member, msg=nil )
118
+ unless defined? assert_not_includes
119
+ def assert_not_includes collection, member, msg=nil
81
120
  msg = "Expected #{collection.inspect} not to include #{member.inspect}"
82
121
  assert !collection.include?(member), msg
83
122
  end
84
123
  end
85
124
 
86
- unless defined?( assert_empty )
87
- def assert_empty(collection, msg=nil)
125
+ unless defined? assert_empty
126
+ def assert_empty collection, msg=nil
88
127
  assert collection.empty?, msg
89
128
  end
90
129
  end
91
130
 
92
- def assert_equal_unordered(left, right)
131
+ def assert_equal_unordered left, right
93
132
  assert_equal(left.length, right.length, "Lengths don't match. #{left.length} != #{right.length}")
94
133
  left.each { |element| assert_includes(right, element) }
95
134
  end
96
135
 
97
- def assert_audit_log_contains(audit_log_contents, needle)
136
+ def assert_log_contains(log, message)
137
+ lines = log.array
138
+ failure_message = "Did not find '#{message}' in log. Log contained:\n#{lines.join('')}"
139
+ assert (lines.any? { |line| line.match(message) }), failure_message
140
+ end
141
+
142
+ def assert_audit_log_contains audit_log_contents, needle
98
143
  # Original request bodies dumped to the log have symbol keys, but once
99
144
  # they go through a dump/load, they're strings again, so we strip
100
145
  # double-quotes and colons from the log, and the strings we searching for.
@@ -112,7 +157,7 @@ end
112
157
  # orderings of the key/value pairs in Hashes that were embedded in the request
113
158
  # body). So, this method traverses an object graph and only makes assertions
114
159
  # about the terminal (non-Array-or-Hash) nodes therein.
115
- def assert_audit_log_contains_object(audit_log_contents, o, format = :json)
160
+ def assert_audit_log_contains_object audit_log_contents, o, format = :json
116
161
  case o
117
162
  when Hash
118
163
  o.each do |k,v|
@@ -130,12 +175,12 @@ def assert_audit_log_contains_object(audit_log_contents, o, format = :json)
130
175
  end
131
176
  end
132
177
 
133
- def compare_metrics(expected, actual)
178
+ def compare_metrics expected, actual
134
179
  actual.delete_if {|a| a.include?('GC/Transaction/') }
135
180
  assert_equal(expected.to_a.sort, actual.to_a.sort, "extra: #{(actual - expected).to_a.inspect}; missing: #{(expected - actual).to_a.inspect}")
136
181
  end
137
182
 
138
- def metric_spec_from_specish(specish)
183
+ def metric_spec_from_specish specish
139
184
  spec = case specish
140
185
  when String then NewRelic::MetricSpec.new(specish)
141
186
  when Array then NewRelic::MetricSpec.new(*specish)
@@ -143,7 +188,7 @@ def metric_spec_from_specish(specish)
143
188
  spec
144
189
  end
145
190
 
146
- def _normalize_metric_expectations(expectations)
191
+ def _normalize_metric_expectations expectations
147
192
  case expectations
148
193
  when Array
149
194
  hash = {}
@@ -157,7 +202,7 @@ def _normalize_metric_expectations(expectations)
157
202
  end
158
203
  end
159
204
 
160
- def dump_stats(stats)
205
+ def dump_stats stats
161
206
  str = " Call count: #{stats.call_count}\n"
162
207
  str << " Total call time: #{stats.total_call_time}\n"
163
208
  str << " Total exclusive time: #{stats.total_exclusive_time}\n"
@@ -170,7 +215,7 @@ def dump_stats(stats)
170
215
  str
171
216
  end
172
217
 
173
- def assert_stats_has_values(stats, expected_spec, expected_attrs)
218
+ def assert_stats_has_values stats, expected_spec, expected_attrs
174
219
  expected_attrs.each do |attr, expected_value|
175
220
  actual_value = stats.send(attr)
176
221
  if attr == :call_count
@@ -183,7 +228,7 @@ def assert_stats_has_values(stats, expected_spec, expected_attrs)
183
228
  end
184
229
  end
185
230
 
186
- def assert_metrics_recorded(expected)
231
+ def assert_metrics_recorded expected
187
232
  expected = _normalize_metric_expectations(expected)
188
233
  expected.each do |specish, expected_attrs|
189
234
  expected_spec = metric_spec_from_specish(specish)
@@ -212,9 +257,9 @@ end
212
257
  #
213
258
  # If you want to *allow* unexpected metrics matching certain patterns, use
214
259
  # the :ignore_filter option. This will allow you to specify a Regex that
215
- # whitelists broad swathes of metric territory (e.g. 'Supportability/').
260
+ # allowlists broad swathes of metric territory (e.g. 'Supportability/').
216
261
  #
217
- def assert_metrics_recorded_exclusive(expected, options={})
262
+ def assert_metrics_recorded_exclusive expected, options={}
218
263
  expected = _normalize_metric_expectations(expected)
219
264
  assert_metrics_recorded(expected)
220
265
 
@@ -252,7 +297,7 @@ def clear_metrics!
252
297
  NewRelic::Agent.instance.stats_engine.reset_for_test!
253
298
  end
254
299
 
255
- def assert_metrics_not_recorded(not_expected)
300
+ def assert_metrics_not_recorded not_expected
256
301
  not_expected = _normalize_metric_expectations(not_expected)
257
302
  found_but_not_expected = []
258
303
  not_expected.each do |specish, _|
@@ -266,7 +311,7 @@ end
266
311
 
267
312
  alias :refute_metrics_recorded :assert_metrics_not_recorded
268
313
 
269
- def assert_no_metrics_match(regex)
314
+ def assert_no_metrics_match regex
270
315
  matching_metrics = []
271
316
  NewRelic::Agent.instance.stats_engine.to_h.keys.map(&:to_s).each do |metric|
272
317
  matching_metrics << metric if metric.match regex
@@ -281,30 +326,30 @@ end
281
326
 
282
327
  alias :refute_metrics_match :assert_no_metrics_match
283
328
 
284
- def format_metric_spec_list(specs)
329
+ def format_metric_spec_list specs
285
330
  spec_strings = specs.map do |spec|
286
331
  "#{spec.name} (#{spec.scope.empty? ? '<unscoped>' : spec.scope})"
287
332
  end
288
333
  "[\n #{spec_strings.join(",\n ")}\n]"
289
334
  end
290
335
 
291
- def assert_truthy(expected, msg = nil)
336
+ def assert_truthy expected, msg=nil
292
337
  msg ||= "Expected #{expected.inspect} to be truthy"
293
338
  assert !!expected, msg
294
339
  end
295
340
 
296
- def assert_falsy(expected, msg = nil)
341
+ def assert_falsy expected, msg=nil
297
342
  msg ||= "Expected #{expected.inspect} to be falsy"
298
343
  assert !expected, msg
299
344
  end
300
345
 
301
- unless defined?( assert_false )
302
- def assert_false(expected)
346
+ unless defined? assert_false
347
+ def assert_false expected
303
348
  assert_equal false, expected
304
349
  end
305
350
  end
306
351
 
307
- unless defined?(refute)
352
+ unless defined? refute
308
353
  alias refute assert_false
309
354
  end
310
355
 
@@ -326,7 +371,7 @@ end
326
371
  # With a transaction name plus category:
327
372
  # in_transaction('foobar', :category => :controller) { ... }
328
373
  #
329
- def in_transaction(*args, &blk)
374
+ def in_transaction *args, &blk
330
375
  opts = (args.last && args.last.is_a?(Hash)) ? args.pop : {}
331
376
  category = (opts && opts.delete(:category)) || :other
332
377
 
@@ -334,10 +379,10 @@ def in_transaction(*args, &blk)
334
379
  name = opts.key?(:transaction_name) ? opts.delete(:transaction_name) :
335
380
  args.first || 'dummy'
336
381
 
337
- state = NewRelic::Agent::TransactionState.tl_get
382
+ state = NewRelic::Agent::Tracer.state
338
383
  txn = nil
339
384
 
340
- NewRelic::Agent::Transaction.wrap(state, name, category, opts) do
385
+ NewRelic::Agent::Tracer.in_transaction(name: name, category: category, options: opts) do
341
386
  txn = state.current_transaction
342
387
  yield state.current_transaction
343
388
  end
@@ -345,25 +390,71 @@ def in_transaction(*args, &blk)
345
390
  txn
346
391
  end
347
392
 
348
- def stub_transaction_guid(guid)
393
+ # Temporarily disables default transformer so tests with invalid inputs can be tried
394
+ def with_disabled_defaults_transformer key
395
+ begin
396
+ transformer = NewRelic::Agent::Configuration::DEFAULTS[key][:transform]
397
+ NewRelic::Agent::Configuration::DEFAULTS[key][:transform] = nil
398
+ yield
399
+ ensure
400
+ NewRelic::Agent::Configuration::DEFAULTS[key][:transform] = transformer
401
+ end
402
+ end
403
+
404
+ # Convenience wrapper to stand up a transaction and provide a segment within
405
+ # that transaction to work with. The same arguements as provided to in_transaction
406
+ # may be supplied.
407
+ def with_segment *args, &blk
408
+ segment = nil
409
+ txn = in_transaction(*args) do |txn|
410
+ segment = txn.current_segment
411
+ yield segment, txn
412
+ end
413
+ [segment, txn]
414
+ end
415
+
416
+ # building error attributes on segments are deferred until it's time
417
+ # to publish/harvest them as spans, so for testing, we'll explicitly
418
+ # build 'em as appropriate so we can test 'em
419
+ def build_deferred_error_attributes segment
420
+ return unless segment.noticed_error
421
+ return if segment.noticed_error_attributes.frozen?
422
+ segment.noticed_error.build_error_attributes
423
+ end
424
+
425
+ def capture_segment_with_error
426
+ begin
427
+ segment_with_error = nil
428
+ with_segment do |segment|
429
+ segment_with_error = segment
430
+ raise "oops!"
431
+ end
432
+ rescue Exception => exception
433
+ assert segment_with_error, "expected to have a segment_with_error"
434
+ build_deferred_error_attributes segment_with_error
435
+ return segment_with_error, exception
436
+ end
437
+ end
438
+
439
+ def stub_transaction_guid guid
349
440
  NewRelic::Agent::Transaction.tl_current.instance_variable_set(:@guid, guid)
350
441
  end
351
442
 
352
443
  # Convenience wrapper around in_transaction that sets the category so that it
353
444
  # looks like we are in a web transaction
354
- def in_web_transaction(name='dummy')
445
+ def in_web_transaction name='dummy'
355
446
  in_transaction(name, :category => :controller, :request => stub(:path => '/')) do |txn|
356
447
  yield txn
357
448
  end
358
449
  end
359
450
 
360
- def in_background_transaction(name='silly')
451
+ def in_background_transaction name='silly'
361
452
  in_transaction(name, :category => :task) do |txn|
362
453
  yield txn
363
454
  end
364
455
  end
365
456
 
366
- def refute_contains_request_params(attributes)
457
+ def refute_contains_request_params attributes
367
458
  attributes.keys.each do |key|
368
459
  refute_match(/^request\.parameters\./, key.to_s)
369
460
  end
@@ -382,7 +473,7 @@ def last_transaction_trace_request_params
382
473
  end
383
474
  end
384
475
 
385
- def find_sql_trace(metric_name)
476
+ def find_sql_trace metric_name
386
477
  NewRelic::Agent.agent.sql_sampler.sql_traces.values.detect do |trace|
387
478
  trace.database_metric_name == metric_name
388
479
  end
@@ -392,7 +483,7 @@ def last_sql_trace
392
483
  NewRelic::Agent.agent.sql_sampler.sql_traces.values.last
393
484
  end
394
485
 
395
- def find_last_transaction_node(transaction_sample=nil)
486
+ def find_last_transaction_node transaction_sample=nil
396
487
  if transaction_sample
397
488
  root_node = transaction_sample.root_node
398
489
  else
@@ -405,7 +496,7 @@ def find_last_transaction_node(transaction_sample=nil)
405
496
  return last_node
406
497
  end
407
498
 
408
- def find_node_with_name(transaction_sample, name)
499
+ def find_node_with_name transaction_sample, name
409
500
  transaction_sample.root_node.each_node do |node|
410
501
  if node.metric_name == name
411
502
  return node
@@ -415,7 +506,7 @@ def find_node_with_name(transaction_sample, name)
415
506
  nil
416
507
  end
417
508
 
418
- def find_node_with_name_matching(transaction_sample, regex)
509
+ def find_node_with_name_matching transaction_sample, regex
419
510
  transaction_sample.root_node.each_node do |node|
420
511
  if node.metric_name.match regex
421
512
  return node
@@ -425,7 +516,7 @@ def find_node_with_name_matching(transaction_sample, regex)
425
516
  nil
426
517
  end
427
518
 
428
- def find_all_nodes_with_name_matching(transaction_sample, regexes)
519
+ def find_all_nodes_with_name_matching transaction_sample, regexes
429
520
  regexes = [regexes].flatten
430
521
  matching_nodes = []
431
522
 
@@ -440,7 +531,7 @@ def find_all_nodes_with_name_matching(transaction_sample, regexes)
440
531
  matching_nodes
441
532
  end
442
533
 
443
- def with_config(config_hash, at_start=true)
534
+ def with_config config_hash, at_start=true
444
535
  config = NewRelic::Agent::Configuration::DottedHash.new(config_hash, true)
445
536
  NewRelic::Agent.config.add_config_for_testing(config, at_start)
446
537
  NewRelic::Agent.instance.refresh_attribute_filter
@@ -452,13 +543,20 @@ def with_config(config_hash, at_start=true)
452
543
  end
453
544
  end
454
545
 
455
- def with_config_low_priority(config_hash)
546
+ def with_server_source config_hash, at_start=true
547
+ with_config config_hash, at_start do
548
+ NewRelic::Agent.config.notify_server_source_added
549
+ yield
550
+ end
551
+ end
552
+
553
+ def with_config_low_priority config_hash
456
554
  with_config(config_hash, false) do
457
555
  yield
458
556
  end
459
557
  end
460
558
 
461
- def with_transaction_renaming_rules(rule_specs)
559
+ def with_transaction_renaming_rules rule_specs
462
560
  original_engine = NewRelic::Agent.agent.instance_variable_get(:@transaction_rules)
463
561
  begin
464
562
  new_engine = NewRelic::Agent::RulesEngine.create_transaction_rules('transaction_name_rules' => rule_specs)
@@ -484,7 +582,7 @@ unless Time.respond_to?(:__original_now)
484
582
  end
485
583
  end
486
584
 
487
- def nr_freeze_time(now=Time.now)
585
+ def nr_freeze_time now=Time.now
488
586
  Time.__frozen_now = now
489
587
  end
490
588
 
@@ -492,11 +590,11 @@ def nr_unfreeze_time
492
590
  Time.__frozen_now = nil
493
591
  end
494
592
 
495
- def advance_time(seconds)
593
+ def advance_time seconds
496
594
  Time.__frozen_now = Time.now + seconds
497
595
  end
498
596
 
499
- def with_constant_defined(constant_symbol, implementation=Module.new)
597
+ def with_constant_defined constant_symbol, implementation=Module.new
500
598
  const_path = constant_path(constant_symbol.to_s)
501
599
 
502
600
  if const_path
@@ -516,12 +614,12 @@ def with_constant_defined(constant_symbol, implementation=Module.new)
516
614
  end
517
615
  end
518
616
 
519
- def constant_path(name, opts={})
617
+ def constant_path name, opts={}
520
618
  allow_partial = opts[:allow_partial]
521
619
  path = [Object]
522
620
  parts = name.gsub(/^::/, '').split('::')
523
621
  parts.each do |part|
524
- if !path.last.const_defined?(part)
622
+ if !path.last.constants.include?(part.to_sym)
525
623
  return allow_partial ? path : nil
526
624
  end
527
625
  path << path.last.const_get(part)
@@ -529,11 +627,17 @@ def constant_path(name, opts={})
529
627
  path
530
628
  end
531
629
 
532
- def undefine_constant(constant_symbol)
533
- const_path = constant_path(constant_symbol.to_s)
534
- return yield unless const_path
535
- parent = const_path[-2]
536
- const_name = constant_symbol.to_s.gsub(/.*::/, '')
630
+ def get_parent constant_name
631
+ parent_name = constant_name.gsub(/::[^:]*$/, '')
632
+ const_path = constant_path(parent_name)
633
+ const_path ? const_path[-1] : nil
634
+ end
635
+
636
+ def undefine_constant constant_symbol
637
+ const_str = constant_symbol.to_s
638
+ parent = get_parent(const_str)
639
+ const_name = const_str.gsub(/.*::/, '')
640
+ return yield unless parent && parent.constants.include?(const_name.to_sym)
537
641
  removed_constant = parent.send(:remove_const, const_name)
538
642
  yield
539
643
  ensure
@@ -553,11 +657,11 @@ ensure
553
657
  NewRelic::Agent.logger = orig_logger
554
658
  end
555
659
 
556
- def create_agent_command(args = {})
660
+ def create_agent_command args={}
557
661
  NewRelic::Agent::Commands::AgentCommand.new([-1, { "name" => "command_name", "arguments" => args}])
558
662
  end
559
663
 
560
- def wait_for_backtrace_service_poll(opts={})
664
+ def wait_for_backtrace_service_poll opts={}
561
665
  defaults = {
562
666
  :timeout => 10.0,
563
667
  :service => NewRelic::Agent.agent.agent_command_router.backtrace_service,
@@ -583,7 +687,7 @@ def wait_for_backtrace_service_poll(opts={})
583
687
  end
584
688
  end
585
689
 
586
- def with_array_logger(level=:info)
690
+ def with_array_logger level=:info
587
691
  orig_logger = NewRelic::Agent.logger
588
692
  config = { :log_level => level }
589
693
  logdev = ArrayLogDevice.new
@@ -599,20 +703,90 @@ ensure
599
703
  NewRelic::Agent.logger = orig_logger
600
704
  end
601
705
 
602
- def with_environment(env)
603
- old_env = {}
604
- env.each do |key, val|
605
- old_env[key] = ENV[key]
606
- ENV[key] = val.to_s
706
+ # The EnvUpdater was introduced due to random fails in JRuby environment
707
+ # whereby attempting to set ENV[key] = some_value randomly failed.
708
+ # It is conjectured that this is thread related, but may also be
709
+ # a core bug in the JVM implementation of Ruby. Root cause was not
710
+ # discovered, but it was found that a combination of retrying and using
711
+ # mutex lock around the update operation was the only consistently working
712
+ # solution as the error continued to surface without the mutex and
713
+ # retry alone wasn't enough, either.
714
+ #
715
+ # JRUBY: oraclejdk8 + jruby-9.2.6.0
716
+ #
717
+ # NOTE: Singleton pattern to ensure one mutex lock for all threads
718
+ class EnvUpdater
719
+ MAX_RETRIES = 5
720
+
721
+ def initialize
722
+ @mutex = Mutex.new
607
723
  end
608
- begin
724
+
725
+ # Will attempt the given block up to MAX_RETRIES before
726
+ # surfacing the exception down the chain.
727
+ def with_retry retry_limit=MAX_RETRIES
728
+ retries ||= 0
729
+ sleep(retries)
609
730
  yield
610
- ensure
611
- old_env.each { |key, old_val| ENV[key] = old_val }
731
+ rescue
732
+ (retries += 1) < retry_limit ? retry : raise
733
+ end
734
+
735
+ # Locks and updates the ENV
736
+ def safe_update env
737
+ with_retry do
738
+ @mutex.synchronize do
739
+ env.each{ |key, val| ENV[key] = val.to_s }
740
+ end
741
+ end
742
+ end
743
+
744
+ # Locks and restores the ENV
745
+ def safe_restore old_env
746
+ with_retry do
747
+ @mutex.synchronize do
748
+ old_env.each{ |key, val| val ? ENV[key] = val : ENV.delete(key) }
749
+ end
750
+ end
751
+ end
752
+
753
+ # Singleton pattern implemented via @@instance
754
+ def self.instance
755
+ @@instance ||= EnvUpdater.new
756
+ end
757
+
758
+ def self.safe_update env
759
+ instance.safe_update env
612
760
  end
761
+
762
+ def self.safe_restore old_env
763
+ instance.safe_restore old_env
764
+ end
765
+
766
+ # Effectively saves current ENV settings for given env's key/values,
767
+ # runs given block, then restores ENV to original state before returning.
768
+ def self.inject env, &block
769
+ old_env = {}
770
+ env.each{ |key, val| old_env[key] = ENV[key] }
771
+ begin
772
+ safe_update(env)
773
+ yield
774
+ ensure
775
+ safe_restore(old_env)
776
+ end
777
+ end
778
+
779
+ # must call instance here to ensure only one @mutex for all threads.
780
+ instance
781
+ end
782
+
783
+ # Changes ENV settings to given and runs given block and restores ENV
784
+ # to original values before returning.
785
+ def with_environment env, &block
786
+ EnvUpdater.inject(env) { yield }
613
787
  end
614
788
 
615
- def with_argv(argv)
789
+ def with_argv argv
616
790
  old_argv = ARGV.dup
617
791
  ARGV.clear
618
792
  ARGV.concat(argv)
@@ -625,7 +799,7 @@ def with_argv(argv)
625
799
  end
626
800
  end
627
801
 
628
- def with_ignore_error_filter(filter, &blk)
802
+ def with_ignore_error_filter filter, &blk
629
803
  original_filter = NewRelic::Agent.ignore_error_filter
630
804
  NewRelic::Agent.ignore_error_filter(&filter)
631
805
 
@@ -634,7 +808,7 @@ ensure
634
808
  NewRelic::Agent::ErrorCollector.ignore_error_filter = original_filter
635
809
  end
636
810
 
637
- def json_dump_and_encode(object)
811
+ def json_dump_and_encode object
638
812
  Base64.encode64(::JSON.dump(object))
639
813
  end
640
814
 
@@ -642,7 +816,7 @@ def get_last_analytics_event
642
816
  NewRelic::Agent.agent.transaction_event_aggregator.harvest![1].last
643
817
  end
644
818
 
645
- def swap_instance_method(target, method_name, new_method_implementation, &blk)
819
+ def swap_instance_method target, method_name, new_method_implementation, &blk
646
820
  old_method_implementation = target.instance_method(method_name)
647
821
  target.send(:define_method, method_name, new_method_implementation)
648
822
  yield
@@ -657,7 +831,7 @@ def cross_agent_tests_dir
657
831
  File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'cross_agent_tests'))
658
832
  end
659
833
 
660
- def load_cross_agent_test(name)
834
+ def load_cross_agent_test name
661
835
  test_file_path = File.join(cross_agent_tests_dir, "#{name}.json")
662
836
  data = File.read(test_file_path)
663
837
  data.gsub!('callCount', 'call_count')
@@ -666,13 +840,13 @@ def load_cross_agent_test(name)
666
840
  data
667
841
  end
668
842
 
669
- def each_cross_agent_test(options)
843
+ def each_cross_agent_test options
670
844
  options = {:dir => nil, :pattern => "*"}.update(options)
671
845
  path = File.join [cross_agent_tests_dir, options[:dir], options[:pattern]].compact
672
846
  Dir.glob(path).each { |file| yield file}
673
847
  end
674
848
 
675
- def assert_event_attributes(event, test_name, expected_attributes, non_expected_attributes)
849
+ def assert_event_attributes event, test_name, expected_attributes, non_expected_attributes
676
850
  incorrect_attributes = []
677
851
 
678
852
  event_attrs = event[0]
@@ -696,10 +870,116 @@ def assert_event_attributes(event, test_name, expected_attributes, non_expected_
696
870
  assert(incorrect_attributes.empty?, msg)
697
871
 
698
872
  non_expected_attributes.each do |name|
699
- assert_nil(event_attrs[name], "Found value '#{event_attrs[name]}' for attribute '#{name}', but expected nothing in #{test_name}")
873
+ refute event_attrs[name], "Found value '#{event_attrs[name]}' for attribute '#{name}', but expected nothing in #{test_name}"
700
874
  end
701
875
  end
702
876
 
703
- def attributes_for(sample, type)
877
+ def attributes_for sample, type
704
878
  sample.attributes.instance_variable_get("@#{type}_attributes")
705
879
  end
880
+
881
+ def uncache_trusted_account_key
882
+ NewRelic::Agent::Transaction::TraceContext::AccountHelpers.instance_variable_set :@trace_state_entry_key, nil
883
+ end
884
+
885
+ def reset_buffers_and_caches
886
+ NewRelic::Agent.drop_buffered_data
887
+ uncache_trusted_account_key
888
+ end
889
+
890
+ def message_for_status_code code
891
+ # Net::HTTP::STATUS_CODES was introduced in Ruby 2.5
892
+ if defined?(Net::HTTP::STATUS_CODES)
893
+ return Net::HTTP::STATUS_CODES[code]
894
+ end
895
+
896
+ case code
897
+ when 200 then "OK"
898
+ when 404 then "Not Found"
899
+ when 403 then "Forbidden"
900
+ else "Unknown"
901
+ end
902
+ end
903
+
904
+ # wraps the given headers in a Net::HTTPResponse which has accompanying
905
+ # http status code associated with it.
906
+ # a "status_code" may be passed in the headers to alter the HTTP Status Code
907
+ # that is wrapped in the response.
908
+ def mock_http_response headers, wrap_it=true
909
+ status_code = (headers.delete("status_code") || 200).to_i
910
+ net_http_resp = Net::HTTPResponse.new(1.0, status_code, message_for_status_code(status_code))
911
+ headers.each do |key, value|
912
+ net_http_resp.add_field key.to_s, value
913
+ end
914
+ return net_http_resp unless wrap_it
915
+ NewRelic::Agent::HTTPClients::NetHTTPResponse.new(net_http_resp)
916
+ end
917
+
918
+ # +expected+ can be a string or regular expression
919
+ def assert_match_or_equal expected, value
920
+ if expected.is_a?(Regexp)
921
+ assert_match expected, value
922
+ else
923
+ assert_equal expected, value
924
+ end
925
+ end
926
+
927
+ # selects the last segment with a noticed_error and checks
928
+ # the expectations against it.
929
+ def assert_segment_noticed_error txn, segment_name, error_classes, error_message
930
+ error_segment = txn.segments.reverse.detect{|s| s.noticed_error}
931
+ assert error_segment, "Expected at least one segment with a noticed_error"
932
+
933
+ assert_match_or_equal segment_name, error_segment.name
934
+
935
+ noticed_error = error_segment.noticed_error
936
+
937
+ assert_match_or_equal error_classes, noticed_error.exception_class_name
938
+ assert_match_or_equal error_message, noticed_error.message
939
+ end
940
+
941
+ def assert_transaction_noticed_error txn, error_classes
942
+ refute_empty txn.exceptions, "Expected transaction to notice the error"
943
+ assert_match_or_equal error_classes, txn.exceptions.keys.first.class.name
944
+ end
945
+
946
+ def refute_transaction_noticed_error txn, error_class
947
+ error_segment = txn.segments.reverse.detect{|s| s.noticed_error}
948
+ assert error_segment, "Expected at least one segment with a noticed_error"
949
+ assert_empty txn.exceptions, "Expected transaction to NOT notice any segment errors"
950
+ end
951
+
952
+ def refute_raises *exp
953
+ msg = "#{exp.pop}.\n" if String === exp.last
954
+
955
+ begin
956
+ yield
957
+ rescue MiniTest::Skip => e
958
+ puts "SKIP REPORTS: #{e.inspect}"
959
+ return e if exp.include? MiniTest::Skip
960
+ raise e
961
+ rescue Exception => e
962
+ puts "EXCEPTION RAISED: #{e.inspect}\n#{e.backtrace}"
963
+ exp = exp.first if exp.size == 1
964
+ flunk msg || "unexpected exception raised: #{e}"
965
+ end
966
+ end
967
+
968
+ def assert_implements instance, method, *args
969
+ fail_message = "expected #{instance.class}##{method} method to be implemented"
970
+ refute_raises NotImplementedError, fail_message do
971
+ instance.send(method, *args)
972
+ end
973
+ end
974
+
975
+ def defer_testing_to_min_supported_rails test_file, min_rails_version, supports_jruby=true
976
+ if defined?(::Rails) &&
977
+ defined?(::Rails::VERSION::STRING) &&
978
+ (::Rails::VERSION::STRING.to_f >= min_rails_version) &&
979
+ (supports_jruby || !NewRelic::LanguageSupport.jruby?)
980
+
981
+ yield
982
+ else
983
+ puts "Skipping tests in #{test_file} because Rails >= #{min_rails_version} is unavailable"
984
+ end
985
+ end