newrelic_rpm 6.15.0 → 9.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (485) hide show
  1. checksums.yaml +4 -4
  2. data/.build_ignore +26 -0
  3. data/CHANGELOG.md +4176 -2789
  4. data/CONTRIBUTING.md +32 -13
  5. data/Gemfile +1 -2
  6. data/LICENSE +0 -6
  7. data/README.md +29 -22
  8. data/Rakefile +33 -35
  9. data/THIRD_PARTY_NOTICES.md +14 -199
  10. data/Thorfile +5 -0
  11. data/bin/newrelic +4 -9
  12. data/bin/newrelic_rpm +15 -0
  13. data/bin/nrdebug +85 -62
  14. data/init.rb +6 -8
  15. data/install.rb +2 -2
  16. data/lib/new_relic/agent/adaptive_sampler.rb +13 -9
  17. data/lib/new_relic/agent/agent.rb +99 -941
  18. data/lib/new_relic/agent/agent_helpers/connect.rb +222 -0
  19. data/lib/new_relic/agent/agent_helpers/harvest.rb +153 -0
  20. data/lib/new_relic/agent/agent_helpers/shutdown.rb +72 -0
  21. data/lib/new_relic/agent/agent_helpers/special_startup.rb +74 -0
  22. data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +175 -0
  23. data/lib/new_relic/agent/agent_helpers/startup.rb +202 -0
  24. data/lib/new_relic/agent/agent_helpers/transmit.rb +76 -0
  25. data/lib/new_relic/agent/agent_logger.rb +27 -23
  26. data/lib/new_relic/agent/attribute_filter.rb +67 -50
  27. data/lib/new_relic/agent/attribute_pre_filtering.rb +109 -0
  28. data/lib/new_relic/agent/attribute_processing.rb +9 -9
  29. data/lib/new_relic/agent/attributes.rb +5 -4
  30. data/lib/new_relic/agent/audit_logger.rb +13 -8
  31. data/lib/new_relic/agent/autostart.rb +21 -20
  32. data/lib/new_relic/agent/chained_call.rb +1 -1
  33. data/lib/new_relic/agent/commands/agent_command.rb +4 -4
  34. data/lib/new_relic/agent/commands/agent_command_router.rb +15 -13
  35. data/lib/new_relic/agent/commands/thread_profiler_session.rb +16 -14
  36. data/lib/new_relic/agent/configuration/default_source.rb +1608 -1182
  37. data/lib/new_relic/agent/configuration/dotted_hash.rb +6 -5
  38. data/lib/new_relic/agent/configuration/environment_source.rb +13 -11
  39. data/lib/new_relic/agent/configuration/event_harvest_config.rb +40 -17
  40. data/lib/new_relic/agent/configuration/high_security_source.rb +11 -12
  41. data/lib/new_relic/agent/configuration/manager.rb +103 -75
  42. data/lib/new_relic/agent/configuration/manual_source.rb +1 -1
  43. data/lib/new_relic/agent/configuration/mask_defaults.rb +3 -3
  44. data/lib/new_relic/agent/configuration/security_policy_source.rb +92 -95
  45. data/lib/new_relic/agent/configuration/server_source.rb +43 -41
  46. data/lib/new_relic/agent/configuration/yaml_source.rb +47 -10
  47. data/lib/new_relic/agent/configuration.rb +1 -1
  48. data/lib/new_relic/agent/connect/request_builder.rb +18 -18
  49. data/lib/new_relic/agent/connect/response_handler.rb +5 -8
  50. data/lib/new_relic/agent/custom_event_aggregator.rb +42 -16
  51. data/lib/new_relic/agent/database/explain_plan_helpers.rb +5 -6
  52. data/lib/new_relic/agent/database/obfuscation_helpers.rb +16 -15
  53. data/lib/new_relic/agent/database/obfuscator.rb +4 -4
  54. data/lib/new_relic/agent/database/postgres_explain_obfuscator.rb +3 -3
  55. data/lib/new_relic/agent/database.rb +45 -53
  56. data/lib/new_relic/agent/database_adapter.rb +3 -1
  57. data/lib/new_relic/agent/datastores/metric_helper.rb +21 -22
  58. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +7 -6
  59. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +17 -21
  60. data/lib/new_relic/agent/datastores/mongo.rb +6 -11
  61. data/lib/new_relic/agent/datastores/nosql_obfuscator.rb +41 -0
  62. data/lib/new_relic/agent/datastores/redis.rb +9 -15
  63. data/lib/new_relic/agent/datastores.rb +13 -15
  64. data/lib/new_relic/agent/deprecator.rb +1 -1
  65. data/lib/new_relic/agent/distributed_tracing/cross_app_payload.rb +10 -10
  66. data/lib/new_relic/agent/distributed_tracing/cross_app_tracing.rb +45 -40
  67. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +22 -22
  68. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +23 -23
  69. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +52 -54
  70. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +10 -11
  71. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +82 -83
  72. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +42 -41
  73. data/lib/new_relic/agent/distributed_tracing.rb +33 -101
  74. data/lib/new_relic/agent/encoding_normalizer.rb +4 -2
  75. data/lib/new_relic/agent/error_collector.rb +105 -58
  76. data/lib/new_relic/agent/error_event_aggregator.rb +4 -5
  77. data/lib/new_relic/agent/error_filter.rb +174 -0
  78. data/lib/new_relic/agent/error_trace_aggregator.rb +9 -7
  79. data/lib/new_relic/agent/event_aggregator.rb +22 -21
  80. data/lib/new_relic/agent/event_buffer.rb +7 -8
  81. data/lib/new_relic/agent/event_listener.rb +1 -2
  82. data/lib/new_relic/agent/event_loop.rb +27 -25
  83. data/lib/new_relic/agent/external.rb +11 -44
  84. data/lib/new_relic/agent/guid_generator.rb +13 -11
  85. data/lib/new_relic/agent/harvester.rb +5 -8
  86. data/lib/new_relic/agent/heap.rb +8 -9
  87. data/lib/new_relic/agent/hostname.rb +21 -14
  88. data/lib/new_relic/agent/http_clients/abstract.rb +22 -31
  89. data/lib/new_relic/agent/http_clients/async_http_wrappers.rb +80 -0
  90. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +14 -17
  91. data/lib/new_relic/agent/http_clients/ethon_wrappers.rb +109 -0
  92. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +15 -17
  93. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +7 -6
  94. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +8 -9
  95. data/lib/new_relic/agent/http_clients/httpx_wrappers.rb +91 -0
  96. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +13 -8
  97. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +12 -13
  98. data/lib/new_relic/agent/http_clients/uri_util.rb +3 -5
  99. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +9 -22
  100. data/lib/new_relic/agent/instrumentation/action_controller_other_subscriber.rb +42 -0
  101. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +41 -33
  102. data/lib/new_relic/agent/instrumentation/action_dispatch.rb +31 -0
  103. data/lib/new_relic/agent/instrumentation/action_dispatch_subscriber.rb +64 -0
  104. data/lib/new_relic/agent/instrumentation/action_mailbox.rb +30 -0
  105. data/lib/new_relic/agent/instrumentation/action_mailbox_subscriber.rb +33 -0
  106. data/lib/new_relic/agent/instrumentation/action_mailer.rb +30 -0
  107. data/lib/new_relic/agent/instrumentation/action_mailer_subscriber.rb +85 -0
  108. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +20 -21
  109. data/lib/new_relic/agent/instrumentation/active_job.rb +33 -13
  110. data/lib/new_relic/agent/instrumentation/active_job_subscriber.rb +41 -0
  111. data/lib/new_relic/agent/instrumentation/active_merchant.rb +20 -6
  112. data/lib/new_relic/agent/instrumentation/active_record.rb +41 -38
  113. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +87 -75
  114. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +54 -66
  115. data/lib/new_relic/agent/instrumentation/active_record_prepend.rb +6 -6
  116. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +30 -22
  117. data/lib/new_relic/agent/instrumentation/active_storage.rb +7 -3
  118. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +8 -33
  119. data/lib/new_relic/agent/instrumentation/active_support.rb +27 -0
  120. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/chain.rb +69 -0
  121. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +13 -0
  122. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/prepend.rb +37 -0
  123. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb +23 -0
  124. data/lib/new_relic/agent/instrumentation/active_support_logger/chain.rb +23 -0
  125. data/lib/new_relic/agent/instrumentation/active_support_logger/instrumentation.rb +24 -0
  126. data/lib/new_relic/agent/instrumentation/active_support_logger/prepend.rb +12 -0
  127. data/lib/new_relic/agent/instrumentation/active_support_logger.rb +26 -0
  128. data/lib/new_relic/agent/instrumentation/active_support_subscriber.rb +41 -0
  129. data/lib/new_relic/agent/instrumentation/async_http/chain.rb +23 -0
  130. data/lib/new_relic/agent/instrumentation/async_http/instrumentation.rb +37 -0
  131. data/lib/new_relic/agent/instrumentation/async_http/prepend.rb +15 -0
  132. data/lib/new_relic/agent/instrumentation/async_http.rb +28 -0
  133. data/lib/new_relic/agent/instrumentation/bunny/chain.rb +45 -0
  134. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +159 -0
  135. data/lib/new_relic/agent/instrumentation/bunny/prepend.rb +35 -0
  136. data/lib/new_relic/agent/instrumentation/bunny.rb +11 -154
  137. data/lib/new_relic/agent/instrumentation/concurrent_ruby/chain.rb +36 -0
  138. data/lib/new_relic/agent/instrumentation/concurrent_ruby/instrumentation.rb +20 -0
  139. data/lib/new_relic/agent/instrumentation/concurrent_ruby/prepend.rb +27 -0
  140. data/lib/new_relic/agent/instrumentation/concurrent_ruby.rb +31 -0
  141. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +74 -63
  142. data/lib/new_relic/agent/instrumentation/curb/chain.rb +91 -0
  143. data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +225 -0
  144. data/lib/new_relic/agent/instrumentation/curb/prepend.rb +61 -0
  145. data/lib/new_relic/agent/instrumentation/curb.rb +14 -246
  146. data/lib/new_relic/agent/instrumentation/custom_events.rb +12 -0
  147. data/lib/new_relic/agent/instrumentation/custom_events_subscriber.rb +38 -0
  148. data/lib/new_relic/agent/instrumentation/delayed_job/chain.rb +36 -0
  149. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +51 -0
  150. data/lib/new_relic/agent/instrumentation/delayed_job/prepend.rb +33 -0
  151. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +30 -53
  152. data/lib/new_relic/agent/instrumentation/elasticsearch/chain.rb +29 -0
  153. data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +70 -0
  154. data/lib/new_relic/agent/instrumentation/elasticsearch/prepend.rb +13 -0
  155. data/lib/new_relic/agent/instrumentation/elasticsearch.rb +31 -0
  156. data/lib/new_relic/agent/instrumentation/ethon/chain.rb +39 -0
  157. data/lib/new_relic/agent/instrumentation/ethon/instrumentation.rb +105 -0
  158. data/lib/new_relic/agent/instrumentation/ethon/prepend.rb +35 -0
  159. data/lib/new_relic/agent/instrumentation/ethon.rb +39 -0
  160. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +11 -7
  161. data/lib/new_relic/agent/instrumentation/excon.rb +27 -29
  162. data/lib/new_relic/agent/instrumentation/fiber/chain.rb +27 -0
  163. data/lib/new_relic/agent/instrumentation/fiber/instrumentation.rb +20 -0
  164. data/lib/new_relic/agent/instrumentation/fiber/prepend.rb +25 -0
  165. data/lib/new_relic/agent/instrumentation/fiber.rb +25 -0
  166. data/lib/new_relic/agent/instrumentation/grape/chain.rb +24 -0
  167. data/lib/new_relic/agent/instrumentation/grape/instrumentation.rb +104 -0
  168. data/lib/new_relic/agent/instrumentation/grape/prepend.rb +17 -0
  169. data/lib/new_relic/agent/instrumentation/grape.rb +16 -117
  170. data/lib/new_relic/agent/instrumentation/grpc/client/chain.rb +97 -0
  171. data/lib/new_relic/agent/instrumentation/grpc/client/instrumentation.rb +94 -0
  172. data/lib/new_relic/agent/instrumentation/grpc/client/prepend.rb +111 -0
  173. data/lib/new_relic/agent/instrumentation/grpc/client/request_wrapper.rb +30 -0
  174. data/lib/new_relic/agent/instrumentation/grpc/helper.rb +32 -0
  175. data/lib/new_relic/agent/instrumentation/grpc/server/chain.rb +69 -0
  176. data/lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb +138 -0
  177. data/lib/new_relic/agent/instrumentation/grpc/server/rpc_desc_prepend.rb +35 -0
  178. data/lib/new_relic/agent/instrumentation/grpc/server/rpc_server_prepend.rb +26 -0
  179. data/lib/new_relic/agent/instrumentation/grpc_client.rb +23 -0
  180. data/lib/new_relic/agent/instrumentation/grpc_server.rb +25 -0
  181. data/lib/new_relic/agent/instrumentation/httpclient/chain.rb +24 -0
  182. data/lib/new_relic/agent/instrumentation/httpclient/instrumentation.rb +41 -0
  183. data/lib/new_relic/agent/instrumentation/httpclient/prepend.rb +15 -0
  184. data/lib/new_relic/agent/instrumentation/httpclient.rb +10 -32
  185. data/lib/new_relic/agent/instrumentation/httprb/chain.rb +22 -0
  186. data/lib/new_relic/agent/instrumentation/httprb/instrumentation.rb +34 -0
  187. data/lib/new_relic/agent/instrumentation/httprb/prepend.rb +15 -0
  188. data/lib/new_relic/agent/instrumentation/httprb.rb +29 -0
  189. data/lib/new_relic/agent/instrumentation/httpx/chain.rb +20 -0
  190. data/lib/new_relic/agent/instrumentation/httpx/instrumentation.rb +51 -0
  191. data/lib/new_relic/agent/instrumentation/httpx/prepend.rb +15 -0
  192. data/lib/new_relic/agent/instrumentation/httpx.rb +27 -0
  193. data/lib/new_relic/agent/instrumentation/ignore_actions.rb +5 -6
  194. data/lib/new_relic/agent/instrumentation/logger/chain.rb +21 -0
  195. data/lib/new_relic/agent/instrumentation/logger/instrumentation.rb +69 -0
  196. data/lib/new_relic/agent/instrumentation/logger/prepend.rb +13 -0
  197. data/lib/new_relic/agent/instrumentation/logger.rb +26 -0
  198. data/lib/new_relic/agent/instrumentation/memcache/chain.rb +15 -0
  199. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +59 -128
  200. data/lib/new_relic/agent/instrumentation/memcache/helper.rb +59 -0
  201. data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +99 -0
  202. data/lib/new_relic/agent/instrumentation/memcache/prepend.rb +103 -0
  203. data/lib/new_relic/agent/instrumentation/memcache.rb +58 -74
  204. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +15 -14
  205. data/lib/new_relic/agent/instrumentation/middleware_tracing.rb +6 -8
  206. data/lib/new_relic/agent/instrumentation/mongo.rb +6 -137
  207. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +24 -23
  208. data/lib/new_relic/agent/instrumentation/net_http/chain.rb +24 -0
  209. data/lib/new_relic/agent/instrumentation/{net_prepend.rb → net_http/instrumentation.rb} +18 -14
  210. data/lib/new_relic/agent/instrumentation/net_http/prepend.rb +21 -0
  211. data/lib/new_relic/agent/instrumentation/net_http.rb +44 -0
  212. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +56 -10
  213. data/lib/new_relic/agent/instrumentation/padrino/chain.rb +38 -0
  214. data/lib/new_relic/agent/instrumentation/padrino/instrumentation.rb +32 -0
  215. data/lib/new_relic/agent/instrumentation/padrino/prepend.rb +20 -0
  216. data/lib/new_relic/agent/instrumentation/padrino.rb +20 -58
  217. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +6 -6
  218. data/lib/new_relic/agent/instrumentation/queue_time.rb +9 -10
  219. data/lib/new_relic/agent/instrumentation/rack/chain.rb +66 -0
  220. data/lib/new_relic/agent/instrumentation/rack/helpers.rb +33 -0
  221. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +81 -0
  222. data/lib/new_relic/agent/instrumentation/rack/prepend.rb +43 -0
  223. data/lib/new_relic/agent/instrumentation/rack.rb +32 -142
  224. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +25 -56
  225. data/lib/new_relic/agent/instrumentation/rails_middleware.rb +4 -4
  226. data/lib/new_relic/agent/instrumentation/rails_notifications/action_cable.rb +9 -8
  227. data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +18 -5
  228. data/lib/new_relic/agent/instrumentation/rails_notifications/action_view.rb +8 -5
  229. data/lib/new_relic/agent/instrumentation/rails_notifications/custom_events.rb +30 -0
  230. data/lib/new_relic/agent/instrumentation/rake/chain.rb +20 -0
  231. data/lib/new_relic/agent/instrumentation/rake/instrumentation.rb +146 -0
  232. data/lib/new_relic/agent/instrumentation/rake/prepend.rb +14 -0
  233. data/lib/new_relic/agent/instrumentation/rake.rb +17 -158
  234. data/lib/new_relic/agent/instrumentation/redis/chain.rb +45 -0
  235. data/lib/new_relic/agent/instrumentation/redis/constants.rb +17 -0
  236. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +98 -0
  237. data/lib/new_relic/agent/instrumentation/redis/middleware.rb +16 -0
  238. data/lib/new_relic/agent/instrumentation/redis/prepend.rb +29 -0
  239. data/lib/new_relic/agent/instrumentation/redis.rb +19 -108
  240. data/lib/new_relic/agent/instrumentation/resque/chain.rb +21 -0
  241. data/lib/new_relic/agent/instrumentation/resque/helper.rb +19 -0
  242. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +38 -0
  243. data/lib/new_relic/agent/instrumentation/resque/prepend.rb +15 -0
  244. data/lib/new_relic/agent/instrumentation/resque.rb +32 -43
  245. data/lib/new_relic/agent/instrumentation/roda/chain.rb +43 -0
  246. data/lib/new_relic/agent/instrumentation/roda/ignorer.rb +45 -0
  247. data/lib/new_relic/agent/instrumentation/roda/instrumentation.rb +68 -0
  248. data/lib/new_relic/agent/instrumentation/roda/prepend.rb +24 -0
  249. data/lib/new_relic/agent/instrumentation/roda/roda_transaction_namer.rb +29 -0
  250. data/lib/new_relic/agent/instrumentation/roda.rb +36 -0
  251. data/lib/new_relic/agent/instrumentation/sequel.rb +16 -19
  252. data/lib/new_relic/agent/instrumentation/sequel_helper.rb +12 -12
  253. data/lib/new_relic/agent/instrumentation/sidekiq/client.rb +24 -0
  254. data/lib/new_relic/agent/instrumentation/sidekiq/extensions/delayed_class.rb +30 -0
  255. data/lib/new_relic/agent/instrumentation/sidekiq/server.rb +60 -0
  256. data/lib/new_relic/agent/instrumentation/sidekiq.rb +27 -65
  257. data/lib/new_relic/agent/instrumentation/sinatra/chain.rb +55 -0
  258. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +30 -36
  259. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +130 -0
  260. data/lib/new_relic/agent/instrumentation/sinatra/prepend.rb +33 -0
  261. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +4 -6
  262. data/lib/new_relic/agent/instrumentation/sinatra.rb +34 -163
  263. data/lib/new_relic/agent/instrumentation/stripe.rb +28 -0
  264. data/lib/new_relic/agent/instrumentation/stripe_subscriber.rb +77 -0
  265. data/lib/new_relic/agent/instrumentation/thread/chain.rb +24 -0
  266. data/lib/new_relic/agent/instrumentation/thread/instrumentation.rb +24 -0
  267. data/lib/new_relic/agent/instrumentation/thread/prepend.rb +22 -0
  268. data/lib/new_relic/agent/instrumentation/thread.rb +20 -0
  269. data/lib/new_relic/agent/instrumentation/tilt/chain.rb +24 -0
  270. data/lib/new_relic/agent/instrumentation/tilt/instrumentation.rb +46 -0
  271. data/lib/new_relic/agent/instrumentation/tilt/prepend.rb +13 -0
  272. data/lib/new_relic/agent/instrumentation/tilt.rb +25 -0
  273. data/lib/new_relic/agent/instrumentation/typhoeus/chain.rb +22 -0
  274. data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +84 -0
  275. data/lib/new_relic/agent/instrumentation/typhoeus/prepend.rb +14 -0
  276. data/lib/new_relic/agent/instrumentation/typhoeus.rb +12 -91
  277. data/lib/new_relic/agent/instrumentation/view_component/chain.rb +21 -0
  278. data/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb +39 -0
  279. data/lib/new_relic/agent/instrumentation/view_component/prepend.rb +13 -0
  280. data/lib/new_relic/agent/instrumentation/view_component.rb +26 -0
  281. data/lib/new_relic/agent/instrumentation.rb +1 -1
  282. data/lib/new_relic/agent/internal_agent_error.rb +2 -2
  283. data/lib/new_relic/agent/javascript_instrumentor.rb +58 -48
  284. data/lib/new_relic/agent/linking_metadata.rb +44 -0
  285. data/lib/new_relic/agent/local_log_decorator.rb +37 -0
  286. data/lib/new_relic/agent/log_event_aggregator.rb +282 -0
  287. data/lib/new_relic/agent/log_event_attributes.rb +115 -0
  288. data/lib/new_relic/agent/log_once.rb +1 -1
  289. data/lib/new_relic/agent/log_priority.rb +20 -0
  290. data/lib/new_relic/agent/logging.rb +95 -52
  291. data/lib/new_relic/agent/memory_logger.rb +2 -2
  292. data/lib/new_relic/agent/messaging.rb +72 -86
  293. data/lib/new_relic/agent/method_tracer.rb +159 -154
  294. data/lib/new_relic/agent/method_tracer_helpers.rb +109 -11
  295. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +26 -19
  296. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +5 -4
  297. data/lib/new_relic/agent/monitors/inbound_request_monitor.rb +1 -2
  298. data/lib/new_relic/agent/monitors/synthetics_monitor.rb +17 -9
  299. data/lib/new_relic/agent/monitors.rb +6 -7
  300. data/lib/new_relic/agent/new_relic_service/encoders.rb +9 -9
  301. data/lib/new_relic/agent/new_relic_service/json_marshaller.rb +6 -6
  302. data/lib/new_relic/agent/new_relic_service/marshaller.rb +2 -2
  303. data/lib/new_relic/agent/new_relic_service/security_policy_settings.rb +4 -4
  304. data/lib/new_relic/agent/new_relic_service.rb +282 -213
  305. data/lib/new_relic/agent/{noticible_error.rb → noticeable_error.rb} +2 -5
  306. data/lib/new_relic/agent/null_logger.rb +7 -3
  307. data/lib/new_relic/agent/obfuscator.rb +7 -11
  308. data/lib/new_relic/agent/parameter_filtering.rb +29 -15
  309. data/lib/new_relic/agent/payload_metric_mapping.rb +9 -10
  310. data/lib/new_relic/agent/pipe_channel_manager.rb +34 -23
  311. data/lib/new_relic/agent/pipe_service.rb +13 -8
  312. data/lib/new_relic/agent/prepend_supportability.rb +2 -2
  313. data/lib/new_relic/agent/priority_sampled_buffer.rb +9 -12
  314. data/lib/new_relic/agent/rules_engine/replacement_rule.rb +11 -11
  315. data/lib/new_relic/agent/rules_engine/segment_terms_rule.rb +12 -14
  316. data/lib/new_relic/agent/rules_engine.rb +6 -5
  317. data/lib/new_relic/agent/sampler.rb +5 -5
  318. data/lib/new_relic/agent/sampler_collection.rb +4 -5
  319. data/lib/new_relic/agent/samplers/cpu_sampler.rb +8 -7
  320. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +20 -17
  321. data/lib/new_relic/agent/samplers/memory_sampler.rb +32 -21
  322. data/lib/new_relic/agent/samplers/object_sampler.rb +2 -2
  323. data/lib/new_relic/agent/samplers/vm_sampler.rb +21 -19
  324. data/lib/new_relic/agent/span_event_aggregator.rb +15 -15
  325. data/lib/new_relic/agent/span_event_primitive.rb +77 -59
  326. data/lib/new_relic/agent/sql_sampler.rb +20 -21
  327. data/lib/new_relic/agent/stats.rb +79 -42
  328. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +9 -11
  329. data/lib/new_relic/agent/stats_engine/stats_hash.rb +12 -13
  330. data/lib/new_relic/agent/stats_engine.rb +8 -8
  331. data/lib/new_relic/agent/synthetics_event_aggregator.rb +8 -9
  332. data/lib/new_relic/agent/system_info.rb +113 -67
  333. data/lib/new_relic/agent/threading/agent_thread.rb +18 -14
  334. data/lib/new_relic/agent/threading/backtrace_node.rb +12 -13
  335. data/lib/new_relic/agent/threading/backtrace_service.rb +17 -21
  336. data/lib/new_relic/agent/threading/thread_profile.rb +24 -24
  337. data/lib/new_relic/agent/timestamp_sampled_buffer.rb +2 -2
  338. data/lib/new_relic/agent/tracer.rb +101 -96
  339. data/lib/new_relic/agent/transaction/abstract_segment.rb +176 -52
  340. data/lib/new_relic/agent/transaction/datastore_segment.rb +22 -18
  341. data/lib/new_relic/agent/transaction/distributed_tracer.rb +65 -57
  342. data/lib/new_relic/agent/transaction/distributed_tracing.rb +46 -48
  343. data/lib/new_relic/agent/transaction/external_request_segment.rb +52 -39
  344. data/lib/new_relic/agent/transaction/message_broker_segment.rb +31 -37
  345. data/lib/new_relic/agent/transaction/request_attributes.rb +78 -42
  346. data/lib/new_relic/agent/transaction/segment.rb +20 -10
  347. data/lib/new_relic/agent/transaction/slowest_sample_buffer.rb +1 -3
  348. data/lib/new_relic/agent/transaction/synthetics_sample_buffer.rb +2 -2
  349. data/lib/new_relic/agent/transaction/trace.rb +16 -12
  350. data/lib/new_relic/agent/transaction/trace_builder.rb +10 -10
  351. data/lib/new_relic/agent/transaction/trace_context.rb +35 -35
  352. data/lib/new_relic/agent/transaction/trace_node.rb +30 -28
  353. data/lib/new_relic/agent/transaction/tracing.rb +32 -13
  354. data/lib/new_relic/agent/transaction/transaction_sample_buffer.rb +6 -6
  355. data/lib/new_relic/agent/transaction.rb +229 -178
  356. data/lib/new_relic/agent/transaction_error_primitive.rb +43 -25
  357. data/lib/new_relic/agent/transaction_event_aggregator.rb +16 -16
  358. data/lib/new_relic/agent/transaction_event_primitive.rb +51 -33
  359. data/lib/new_relic/agent/transaction_event_recorder.rb +16 -15
  360. data/lib/new_relic/agent/transaction_metrics.rb +10 -9
  361. data/lib/new_relic/agent/transaction_sampler.rb +7 -8
  362. data/lib/new_relic/agent/transaction_time_aggregator.rb +32 -27
  363. data/lib/new_relic/agent/utilization/aws.rb +34 -4
  364. data/lib/new_relic/agent/utilization/azure.rb +6 -6
  365. data/lib/new_relic/agent/utilization/gcp.rb +11 -13
  366. data/lib/new_relic/agent/utilization/pcf.rb +6 -5
  367. data/lib/new_relic/agent/utilization/vendor.rb +45 -32
  368. data/lib/new_relic/agent/utilization_data.rb +7 -5
  369. data/lib/new_relic/agent/vm/c_ruby_vm.rb +99 -0
  370. data/lib/new_relic/agent/vm/jruby_vm.rb +1 -1
  371. data/lib/new_relic/agent/vm/monotonic_gc_profiler.rb +3 -3
  372. data/lib/new_relic/agent/vm/snapshot.rb +5 -5
  373. data/lib/new_relic/agent/vm.rb +3 -3
  374. data/lib/new_relic/agent/worker_loop.rb +10 -12
  375. data/lib/new_relic/agent.rb +204 -111
  376. data/lib/new_relic/base64.rb +25 -0
  377. data/lib/new_relic/cli/command.rb +26 -25
  378. data/lib/new_relic/cli/commands/deployments.rb +100 -49
  379. data/lib/new_relic/cli/commands/install.rb +31 -34
  380. data/lib/new_relic/coerce.rb +15 -13
  381. data/lib/new_relic/collection_helper.rb +50 -48
  382. data/lib/new_relic/constants.rb +13 -12
  383. data/lib/new_relic/control/class_methods.rb +6 -12
  384. data/lib/new_relic/control/frameworks/external.rb +2 -2
  385. data/lib/new_relic/control/frameworks/rails.rb +60 -30
  386. data/lib/new_relic/control/frameworks/rails3.rb +3 -4
  387. data/lib/new_relic/control/frameworks/rails4.rb +1 -1
  388. data/lib/new_relic/control/frameworks/rails_notifications.rb +1 -1
  389. data/lib/new_relic/control/frameworks/roda.rb +20 -0
  390. data/lib/new_relic/control/frameworks/ruby.rb +3 -3
  391. data/lib/new_relic/control/frameworks/sinatra.rb +7 -1
  392. data/lib/new_relic/control/frameworks.rb +1 -1
  393. data/lib/new_relic/control/instance_methods.rb +27 -45
  394. data/lib/new_relic/control/instrumentation.rb +25 -11
  395. data/lib/new_relic/control/private_instance_methods.rb +48 -0
  396. data/lib/new_relic/control/server_methods.rb +3 -4
  397. data/lib/new_relic/control.rb +1 -2
  398. data/lib/new_relic/delayed_job_injection.rb +1 -1
  399. data/lib/new_relic/dependency_detection.rb +141 -32
  400. data/lib/new_relic/environment_report.rb +41 -33
  401. data/lib/new_relic/helper.rb +49 -7
  402. data/lib/new_relic/language_support.rb +39 -6
  403. data/lib/new_relic/latest_changes.rb +10 -9
  404. data/lib/new_relic/local_environment.rb +42 -37
  405. data/lib/new_relic/metric_data.rb +31 -26
  406. data/lib/new_relic/metric_spec.rb +8 -6
  407. data/lib/new_relic/noticed_error.rb +43 -46
  408. data/lib/new_relic/rack/agent_hooks.rb +2 -2
  409. data/lib/new_relic/rack/agent_middleware.rb +3 -17
  410. data/lib/new_relic/rack/browser_monitoring.rb +135 -121
  411. data/lib/new_relic/rack.rb +1 -1
  412. data/lib/new_relic/recipes/capistrano3.rb +4 -62
  413. data/lib/new_relic/recipes/capistrano_legacy.rb +24 -27
  414. data/lib/new_relic/recipes/helpers/send_deployment.rb +70 -0
  415. data/lib/new_relic/recipes.rb +1 -1
  416. data/lib/new_relic/supportability_helper.rb +14 -12
  417. data/lib/new_relic/traced_thread.rb +38 -0
  418. data/lib/new_relic/version.rb +6 -17
  419. data/lib/newrelic_rpm.rb +20 -33
  420. data/lib/sequel/extensions/{newrelic_instrumentation.rb → new_relic_instrumentation.rb} +18 -21
  421. data/lib/sequel/plugins/{newrelic_instrumentation.rb → new_relic_instrumentation.rb} +9 -15
  422. data/lib/tasks/all.rb +3 -3
  423. data/lib/tasks/bump_version.rake +21 -0
  424. data/lib/tasks/config.rake +25 -119
  425. data/lib/tasks/coverage_report.rake +28 -0
  426. data/lib/tasks/helpers/config.html.erb +114 -0
  427. data/lib/tasks/helpers/format.rb +127 -0
  428. data/lib/tasks/helpers/matches.rb +12 -0
  429. data/lib/tasks/helpers/newrelicyml.rb +144 -0
  430. data/lib/tasks/helpers/prompt.rb +24 -0
  431. data/lib/tasks/helpers/version_bump.rb +62 -0
  432. data/lib/tasks/install.rake +8 -4
  433. data/lib/tasks/instrumentation_generator/README.md +63 -0
  434. data/lib/tasks/instrumentation_generator/TODO.md +33 -0
  435. data/lib/tasks/instrumentation_generator/instrumentation.thor +121 -0
  436. data/lib/tasks/instrumentation_generator/templates/Envfile.tt +9 -0
  437. data/lib/tasks/instrumentation_generator/templates/chain.tt +21 -0
  438. data/lib/tasks/instrumentation_generator/templates/chain_method.tt +7 -0
  439. data/lib/tasks/instrumentation_generator/templates/dependency_detection.tt +29 -0
  440. data/lib/tasks/instrumentation_generator/templates/instrumentation.tt +13 -0
  441. data/lib/tasks/instrumentation_generator/templates/instrumentation_method.tt +3 -0
  442. data/lib/tasks/instrumentation_generator/templates/newrelic.yml.tt +19 -0
  443. data/lib/tasks/instrumentation_generator/templates/prepend.tt +13 -0
  444. data/lib/tasks/instrumentation_generator/templates/prepend_method.tt +3 -0
  445. data/lib/tasks/instrumentation_generator/templates/test.tt +15 -0
  446. data/lib/tasks/newrelic.rb +2 -1
  447. data/lib/tasks/newrelicyml.rake +13 -0
  448. data/lib/tasks/tests.rake +83 -17
  449. data/newrelic.yml +790 -4
  450. data/newrelic_rpm.gemspec +52 -43
  451. data/recipes/newrelic.rb +2 -2
  452. data/test/agent_helper.rb +252 -198
  453. metadata +275 -95
  454. data/.gitignore +0 -37
  455. data/.project +0 -23
  456. data/.yardopts +0 -26
  457. data/Guardfile +0 -25
  458. data/ROADMAP.md +0 -24
  459. data/bin/mongrel_rpm +0 -33
  460. data/bin/newrelic_cmd +0 -5
  461. data/cert/cacert.pem +0 -1177
  462. data/config/database.yml +0 -5
  463. data/config.dot +0 -278
  464. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +0 -44
  465. data/lib/new_relic/agent/datastores/mongo/statement_formatter.rb +0 -53
  466. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +0 -75
  467. data/lib/new_relic/agent/instrumentation/authlogic.rb +0 -25
  468. data/lib/new_relic/agent/instrumentation/data_mapper.rb +0 -204
  469. data/lib/new_relic/agent/instrumentation/excon/connection.rb +0 -49
  470. data/lib/new_relic/agent/instrumentation/http.rb +0 -49
  471. data/lib/new_relic/agent/instrumentation/merb/controller.rb +0 -44
  472. data/lib/new_relic/agent/instrumentation/merb/errors.rb +0 -33
  473. data/lib/new_relic/agent/instrumentation/net.rb +0 -70
  474. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +0 -125
  475. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +0 -46
  476. data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +0 -26
  477. data/lib/new_relic/agent/instrumentation/sunspot.rb +0 -33
  478. data/lib/new_relic/agent/range_extensions.rb +0 -47
  479. data/lib/new_relic/agent/supported_versions.rb +0 -275
  480. data/lib/new_relic/agent/vm/mri_vm.rb +0 -81
  481. data/lib/new_relic/control/frameworks/merb.rb +0 -29
  482. data/lib/tasks/config.html.erb +0 -32
  483. data/lib/tasks/multiverse.rake +0 -2
  484. data/lib/tasks/multiverse.rb +0 -104
  485. /data/lib/tasks/{config.text.erb → helpers/config.text.erb} +0 -0
data/test/agent_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
- # encoding: utf-8
2
1
  # This file is distributed under New Relic's license terms.
3
2
  # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
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.
@@ -13,49 +13,28 @@ rescue LoadError
13
13
  # NOP -- Net::HTTP::STATUS_CODES was introduced in Ruby 2.5
14
14
  end
15
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
29
-
30
16
  class ArrayLogDevice
31
- def initialize array=[]
17
+ def initialize(array = [])
32
18
  @array = array
33
19
  end
34
20
  attr_reader :array
35
21
 
36
- def write message
22
+ def write(message)
37
23
  @array << message
38
24
  end
39
25
 
40
26
  def close; end
41
27
  end
42
28
 
43
- def fake_guid length=16
44
- NewRelic::Agent::GuidGenerator.generate_guid length
29
+ def fake_guid(length = 16)
30
+ NewRelic::Agent::GuidGenerator.generate_guid(length)
45
31
  end
46
32
 
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}"
33
+ def assert_between(floor, ceiling, value, message = "expected #{floor} <= #{value} <= #{ceiling}")
55
34
  assert((floor <= value && value <= ceiling), message)
56
35
  end
57
36
 
58
- def assert_in_delta expected, actual, delta
37
+ def assert_in_delta(expected, actual, delta)
59
38
  assert_between((expected - delta), (expected + delta), actual)
60
39
  end
61
40
 
@@ -67,10 +46,10 @@ def reset_error_traces!
67
46
  NewRelic::Agent.instance.error_collector.error_trace_aggregator.reset!
68
47
  end
69
48
 
70
- def assert_has_traced_error error_class
49
+ def assert_has_traced_error(error_class)
71
50
  errors = harvest_error_traces!
72
- assert \
73
- errors.find {|e| e.exception_class_name == error_class.name} != nil, \
51
+
52
+ refute_nil errors.find { |e| e.exception_class_name == error_class.name }, \
74
53
  "Didn't find error of class #{error_class}"
75
54
  end
76
55
 
@@ -102,51 +81,49 @@ def last_error_event
102
81
  harvest_error_events!.last.last
103
82
  end
104
83
 
105
- unless defined? assert_block
106
- def assert_block *msgs
107
- assert yield, *msgs
108
- end
109
- end
110
-
111
84
  unless defined? assert_includes
112
- def assert_includes collection, member, msg=nil
85
+ def assert_includes(collection, member, msg = nil)
113
86
  msg = "Expected #{collection.inspect} to include #{member.inspect}"
114
- assert_block( msg ) { collection.include?(member) }
87
+
88
+ assert_includes collection, member, msg
115
89
  end
116
90
  end
117
91
 
118
92
  unless defined? assert_not_includes
119
- def assert_not_includes collection, member, msg=nil
93
+ def assert_not_includes(collection, member, msg = nil)
120
94
  msg = "Expected #{collection.inspect} not to include #{member.inspect}"
121
- assert !collection.include?(member), msg
95
+
96
+ refute_includes collection, member, msg
122
97
  end
123
98
  end
124
99
 
125
100
  unless defined? assert_empty
126
- def assert_empty collection, msg=nil
127
- assert collection.empty?, msg
101
+ def assert_empty(collection, msg = nil)
102
+ assert_empty collection, msg
128
103
  end
129
104
  end
130
105
 
131
- def assert_equal_unordered left, right
106
+ def assert_equal_unordered(left, right)
132
107
  assert_equal(left.length, right.length, "Lengths don't match. #{left.length} != #{right.length}")
133
108
  left.each { |element| assert_includes(right, element) }
134
109
  end
135
110
 
136
111
  def assert_log_contains(log, message)
137
112
  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
113
+
114
+ assert (lines.any? { |line| line.match(message) }),
115
+ "Could not find message. Log contained: #{lines.join("\n")}"
140
116
  end
141
117
 
142
- def assert_audit_log_contains audit_log_contents, needle
118
+ def assert_audit_log_contains(audit_log_contents, needle)
143
119
  # Original request bodies dumped to the log have symbol keys, but once
144
120
  # they go through a dump/load, they're strings again, so we strip
145
121
  # double-quotes and colons from the log, and the strings we searching for.
146
122
  regex = /[:"]/
147
123
  needle = needle.gsub(regex, '')
148
124
  haystack = audit_log_contents.gsub(regex, '')
149
- assert(haystack.include?(needle), "Expected log to contain '#{needle}'")
125
+
126
+ assert_includes(haystack, needle, "Expected log to contain '#{needle}'")
150
127
  end
151
128
 
152
129
  # Because we don't generate a strictly machine-readable representation of
@@ -157,53 +134,56 @@ end
157
134
  # orderings of the key/value pairs in Hashes that were embedded in the request
158
135
  # body). So, this method traverses an object graph and only makes assertions
159
136
  # about the terminal (non-Array-or-Hash) nodes therein.
160
- def assert_audit_log_contains_object audit_log_contents, o, format = :json
137
+ def assert_audit_log_contains_object(audit_log_contents, o, format = :json)
161
138
  case o
162
139
  when Hash
163
- o.each do |k,v|
140
+ o.each do |k, v|
164
141
  assert_audit_log_contains_object(audit_log_contents, v, format)
165
142
  assert_audit_log_contains_object(audit_log_contents, k, format)
166
143
  end
167
144
  when Array
145
+
168
146
  o.each do |el|
169
147
  assert_audit_log_contains_object(audit_log_contents, el, format)
170
148
  end
171
149
  when NilClass
172
- assert_audit_log_contains(audit_log_contents, format == :json ? "null" : "nil")
150
+
151
+ assert_audit_log_contains(audit_log_contents, format == :json ? 'null' : 'nil')
173
152
  else
174
153
  assert_audit_log_contains(audit_log_contents, o.inspect)
175
154
  end
176
155
  end
177
156
 
178
- def compare_metrics expected, actual
179
- actual.delete_if {|a| a.include?('GC/Transaction/') }
157
+ def compare_metrics(expected, actual)
158
+ actual.delete_if { |a| a.include?('GC/Transaction/') }
159
+
180
160
  assert_equal(expected.to_a.sort, actual.to_a.sort, "extra: #{(actual - expected).to_a.inspect}; missing: #{(expected - actual).to_a.inspect}")
181
161
  end
182
162
 
183
- def metric_spec_from_specish specish
163
+ def metric_spec_from_specish(specish)
184
164
  spec = case specish
185
165
  when String then NewRelic::MetricSpec.new(specish)
186
- when Array then NewRelic::MetricSpec.new(*specish)
166
+ when Array then NewRelic::MetricSpec.new(*specish)
187
167
  end
188
168
  spec
189
169
  end
190
170
 
191
- def _normalize_metric_expectations expectations
171
+ def _normalize_metric_expectations(expectations)
192
172
  case expectations
193
173
  when Array
194
174
  hash = {}
195
175
  # Just assert that the metric is present, nothing about the attributes
196
- expectations.each { |k| hash[k] = { } }
176
+ expectations.each { |k| hash[k] = {} }
197
177
  hash
198
178
  when String
199
- { expectations => {} }
179
+ {expectations => {}}
200
180
  else
201
181
  expectations
202
182
  end
203
183
  end
204
184
 
205
- def dump_stats stats
206
- str = " Call count: #{stats.call_count}\n"
185
+ def dump_stats(stats)
186
+ str = +" Call count: #{stats.call_count}\n"
207
187
  str << " Total call time: #{stats.total_call_time}\n"
208
188
  str << " Total exclusive time: #{stats.total_exclusive_time}\n"
209
189
  str << " Min call time: #{stats.min_call_time}\n"
@@ -215,20 +195,35 @@ def dump_stats stats
215
195
  str
216
196
  end
217
197
 
218
- def assert_stats_has_values stats, expected_spec, expected_attrs
198
+ def assert_stats_has_values(stats, expected_spec, expected_attrs)
219
199
  expected_attrs.each do |attr, expected_value|
220
200
  actual_value = stats.send(attr)
201
+
202
+ msg = "Expected #{attr} for #{expected_spec} to be #{'~' unless attr == :call_count}#{expected_value}, " \
203
+ "got #{actual_value}.\nActual stats:\n#{dump_stats(stats)}"
204
+
221
205
  if attr == :call_count
222
- assert_equal(expected_value, actual_value,
223
- "Expected #{attr} for #{expected_spec} to be #{expected_value}, got #{actual_value}.\nActual stats:\n#{dump_stats(stats)}")
206
+ assert_stats_has_values_with_call_count(expected_value, actual_value, msg)
224
207
  else
225
- assert_in_delta(expected_value, actual_value, 0.0001,
226
- "Expected #{attr} for #{expected_spec} to be ~#{expected_value}, got #{actual_value}.\nActual stats:\n#{dump_stats(stats)}")
208
+ assert_in_delta(expected_value, actual_value, 0.0001, msg)
227
209
  end
228
210
  end
229
211
  end
230
212
 
231
- def assert_metrics_recorded expected
213
+ def assert_stats_has_values_with_call_count(expected_value, actual_value, msg)
214
+ # >, <, >=, <= comparisons
215
+ if expected_value.to_s =~ /([<>]=?)\s*(\d+)/
216
+ operator = Regexp.last_match(1).to_sym
217
+ count = Regexp.last_match(2).to_i
218
+
219
+ assert_operator(actual_value, operator, count, msg)
220
+ # == comparison
221
+ else
222
+ assert_equal(expected_value, actual_value, msg)
223
+ end
224
+ end
225
+
226
+ def assert_metrics_recorded(expected)
232
227
  expected = _normalize_metric_expectations(expected)
233
228
  expected.each do |specish, expected_attrs|
234
229
  expected_spec = metric_spec_from_specish(specish)
@@ -244,6 +239,7 @@ def assert_metrics_recorded expected
244
239
 
245
240
  assert(actual_stats, msg)
246
241
  end
242
+
247
243
  assert_stats_has_values(actual_stats, expected_spec, expected_attrs)
248
244
  end
249
245
  end
@@ -259,8 +255,9 @@ end
259
255
  # the :ignore_filter option. This will allow you to specify a Regex that
260
256
  # allowlists broad swathes of metric territory (e.g. 'Supportability/').
261
257
  #
262
- def assert_metrics_recorded_exclusive expected, options={}
258
+ def assert_metrics_recorded_exclusive(expected, options = {})
263
259
  expected = _normalize_metric_expectations(expected)
260
+
264
261
  assert_metrics_recorded(expected)
265
262
 
266
263
  recorded_metrics = NewRelic::Agent.instance.stats_engine.to_h.keys
@@ -272,14 +269,23 @@ def assert_metrics_recorded_exclusive expected, options={}
272
269
  recorded_metrics.reject! { |m| m.name.match(options[:ignore_filter]) }
273
270
  end
274
271
 
275
- expected_metrics = expected.keys.map { |s| metric_spec_from_specish(s) }
272
+ expected_metrics = expected.keys.map { |s| metric_spec_from_specish(s) }
276
273
 
277
274
  unexpected_metrics = recorded_metrics - expected_metrics
278
- unexpected_metrics.reject! { |m| m.name =~ /GC\/Transaction/ }
275
+ unexpected_metrics.reject! { |m| m.name.include?('GC/Transaction') }
279
276
 
280
277
  assert_equal(0, unexpected_metrics.size, "Found unexpected metrics: #{format_metric_spec_list(unexpected_metrics)}")
281
278
  end
282
279
 
280
+ def assert_newrelic_metadata_present(metadata)
281
+ assert metadata.key?('newrelic')
282
+ refute_nil metadata['newrelic']
283
+ end
284
+
285
+ def assert_distributed_tracing_payload_created_for_transaction(transaction)
286
+ assert transaction.distributed_tracer.instance_variable_get(:@distributed_trace_payload_created)
287
+ end
288
+
283
289
  # The clear_metrics! method prevents metrics from "leaking" between tests by resetting
284
290
  # the @stats_hash instance variable in the current instance of NewRelic::Agent::StatsEngine.
285
291
 
@@ -297,7 +303,7 @@ def clear_metrics!
297
303
  NewRelic::Agent.instance.stats_engine.reset_for_test!
298
304
  end
299
305
 
300
- def assert_metrics_not_recorded not_expected
306
+ def assert_metrics_not_recorded(not_expected)
301
307
  not_expected = _normalize_metric_expectations(not_expected)
302
308
  found_but_not_expected = []
303
309
  not_expected.each do |specish, _|
@@ -306,46 +312,48 @@ def assert_metrics_not_recorded not_expected
306
312
  found_but_not_expected << spec
307
313
  end
308
314
  end
309
- assert_equal([], found_but_not_expected, "Found unexpected metrics: #{format_metric_spec_list(found_but_not_expected)}")
315
+
316
+ assert_empty(found_but_not_expected, "Found unexpected metrics: #{format_metric_spec_list(found_but_not_expected)}")
310
317
  end
311
318
 
312
319
  alias :refute_metrics_recorded :assert_metrics_not_recorded
313
320
 
314
- def assert_no_metrics_match regex
321
+ def assert_no_metrics_match(regex)
315
322
  matching_metrics = []
316
323
  NewRelic::Agent.instance.stats_engine.to_h.keys.map(&:to_s).each do |metric|
317
- matching_metrics << metric if metric.match regex
324
+ matching_metrics << metric if metric.match(regex)
318
325
  end
319
326
 
320
- assert_equal(
321
- [],
327
+ assert_empty(
322
328
  matching_metrics,
323
- "Found unexpected metrics:\n" + matching_metrics.map { |m| " '#{m}'"}.join("\n") + "\n\n"
329
+ "Found unexpected metrics:\n" + matching_metrics.map { |m| " '#{m}'" }.join("\n") + "\n\n"
324
330
  )
325
331
  end
326
332
 
327
333
  alias :refute_metrics_match :assert_no_metrics_match
328
334
 
329
- def format_metric_spec_list specs
335
+ def format_metric_spec_list(specs)
330
336
  spec_strings = specs.map do |spec|
331
337
  "#{spec.name} (#{spec.scope.empty? ? '<unscoped>' : spec.scope})"
332
338
  end
333
339
  "[\n #{spec_strings.join(",\n ")}\n]"
334
340
  end
335
341
 
336
- def assert_truthy expected, msg=nil
342
+ def assert_truthy(expected, msg = nil)
337
343
  msg ||= "Expected #{expected.inspect} to be truthy"
338
- assert !!expected, msg
344
+
345
+ refute !expected, msg
339
346
  end
340
347
 
341
- def assert_falsy expected, msg=nil
348
+ def assert_falsy(expected, msg = nil)
342
349
  msg ||= "Expected #{expected.inspect} to be falsy"
343
- assert !expected, msg
350
+
351
+ refute expected, msg
344
352
  end
345
353
 
346
354
  unless defined? assert_false
347
- def assert_false expected
348
- assert_equal false, expected
355
+ def assert_false(expected)
356
+ refute expected
349
357
  end
350
358
  end
351
359
 
@@ -371,27 +379,26 @@ end
371
379
  # With a transaction name plus category:
372
380
  # in_transaction('foobar', :category => :controller) { ... }
373
381
  #
374
- def in_transaction *args, &blk
375
- opts = (args.last && args.last.is_a?(Hash)) ? args.pop : {}
376
- category = (opts && opts.delete(:category)) || :other
382
+ def in_transaction(*args, &blk)
383
+ opts = args.last&.is_a?(Hash) ? args.pop : {}
384
+ category = (opts&.delete(:category)) || :other
377
385
 
378
386
  # At least one test passes `:transaction_name => nil`, so handle it gently
379
- name = opts.key?(:transaction_name) ? opts.delete(:transaction_name) :
380
- args.first || 'dummy'
387
+ name = opts.key?(:transaction_name) ? opts.delete(:transaction_name) : args.first || 'dummy'
381
388
 
382
389
  state = NewRelic::Agent::Tracer.state
383
390
  txn = nil
384
391
 
385
392
  NewRelic::Agent::Tracer.in_transaction(name: name, category: category, options: opts) do
386
393
  txn = state.current_transaction
387
- yield state.current_transaction
394
+ yield(state.current_transaction)
388
395
  end
389
396
 
390
397
  txn
391
398
  end
392
399
 
393
400
  # Temporarily disables default transformer so tests with invalid inputs can be tried
394
- def with_disabled_defaults_transformer key
401
+ def with_disabled_defaults_transformer(key)
395
402
  begin
396
403
  transformer = NewRelic::Agent::Configuration::DEFAULTS[key][:transform]
397
404
  NewRelic::Agent::Configuration::DEFAULTS[key][:transform] = nil
@@ -402,13 +409,13 @@ def with_disabled_defaults_transformer key
402
409
  end
403
410
 
404
411
  # 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
412
+ # that transaction to work with. The same arguments as provided to in_transaction
406
413
  # may be supplied.
407
- def with_segment *args, &blk
414
+ def with_segment(*args, &blk)
408
415
  segment = nil
409
- txn = in_transaction(*args) do |txn|
410
- segment = txn.current_segment
411
- yield segment, txn
416
+ txn = in_transaction(*args) do |t|
417
+ segment = t.current_segment
418
+ yield(segment, t)
412
419
  end
413
420
  [segment, txn]
414
421
  end
@@ -416,9 +423,10 @@ end
416
423
  # building error attributes on segments are deferred until it's time
417
424
  # to publish/harvest them as spans, so for testing, we'll explicitly
418
425
  # build 'em as appropriate so we can test 'em
419
- def build_deferred_error_attributes segment
426
+ def build_deferred_error_attributes(segment)
420
427
  return unless segment.noticed_error
421
428
  return if segment.noticed_error_attributes.frozen?
429
+
422
430
  segment.noticed_error.build_error_attributes
423
431
  end
424
432
 
@@ -427,34 +435,34 @@ def capture_segment_with_error
427
435
  segment_with_error = nil
428
436
  with_segment do |segment|
429
437
  segment_with_error = segment
430
- raise "oops!"
438
+ raise 'oops!'
431
439
  end
432
440
  rescue Exception => exception
433
- assert segment_with_error, "expected to have a segment_with_error"
434
- build_deferred_error_attributes segment_with_error
441
+ assert segment_with_error, 'expected to have a segment_with_error'
442
+ build_deferred_error_attributes(segment_with_error)
435
443
  return segment_with_error, exception
436
444
  end
437
445
  end
438
446
 
439
- def stub_transaction_guid guid
447
+ def stub_transaction_guid(guid)
440
448
  NewRelic::Agent::Transaction.tl_current.instance_variable_set(:@guid, guid)
441
449
  end
442
450
 
443
451
  # Convenience wrapper around in_transaction that sets the category so that it
444
452
  # looks like we are in a web transaction
445
- def in_web_transaction name='dummy'
453
+ def in_web_transaction(name = 'dummy')
446
454
  in_transaction(name, :category => :controller, :request => stub(:path => '/')) do |txn|
447
- yield txn
455
+ yield(txn)
448
456
  end
449
457
  end
450
458
 
451
- def in_background_transaction name='silly'
459
+ def in_background_transaction(name = 'silly')
452
460
  in_transaction(name, :category => :task) do |txn|
453
- yield txn
461
+ yield(txn)
454
462
  end
455
463
  end
456
464
 
457
- def refute_contains_request_params attributes
465
+ def refute_contains_request_params(attributes)
458
466
  attributes.keys.each do |key|
459
467
  refute_match(/^request\.parameters\./, key.to_s)
460
468
  end
@@ -462,18 +470,19 @@ end
462
470
 
463
471
  def last_transaction_trace
464
472
  return unless last_sample = NewRelic::Agent.agent.transaction_sampler.last_sample
473
+
465
474
  NewRelic::Agent::Transaction::TraceBuilder.build_trace(last_sample)
466
475
  end
467
476
 
468
477
  def last_transaction_trace_request_params
469
478
  agent_attributes = attributes_for(last_transaction_trace, :agent)
470
479
  agent_attributes.inject({}) do |memo, (key, value)|
471
- memo[key] = value if key.to_s.start_with?("request.parameters.")
480
+ memo[key] = value if key.to_s.start_with?('request.parameters.')
472
481
  memo
473
482
  end
474
483
  end
475
484
 
476
- def find_sql_trace metric_name
485
+ def find_sql_trace(metric_name)
477
486
  NewRelic::Agent.agent.sql_sampler.sql_traces.values.detect do |trace|
478
487
  trace.database_metric_name == metric_name
479
488
  end
@@ -483,7 +492,7 @@ def last_sql_trace
483
492
  NewRelic::Agent.agent.sql_sampler.sql_traces.values.last
484
493
  end
485
494
 
486
- def find_last_transaction_node transaction_sample=nil
495
+ def find_last_transaction_node(transaction_sample = nil)
487
496
  if transaction_sample
488
497
  root_node = transaction_sample.root_node
489
498
  else
@@ -491,12 +500,12 @@ def find_last_transaction_node transaction_sample=nil
491
500
  end
492
501
 
493
502
  last_node = nil
494
- root_node.each_node {|s| last_node = s }
503
+ root_node.each_node { |s| last_node = s }
495
504
 
496
505
  return last_node
497
506
  end
498
507
 
499
- def find_node_with_name transaction_sample, name
508
+ def find_node_with_name(transaction_sample, name)
500
509
  transaction_sample.root_node.each_node do |node|
501
510
  if node.metric_name == name
502
511
  return node
@@ -506,9 +515,9 @@ def find_node_with_name transaction_sample, name
506
515
  nil
507
516
  end
508
517
 
509
- def find_node_with_name_matching transaction_sample, regex
518
+ def find_node_with_name_matching(transaction_sample, regex)
510
519
  transaction_sample.root_node.each_node do |node|
511
- if node.metric_name.match regex
520
+ if node.metric_name.match(regex)
512
521
  return node
513
522
  end
514
523
  end
@@ -516,13 +525,13 @@ def find_node_with_name_matching transaction_sample, regex
516
525
  nil
517
526
  end
518
527
 
519
- def find_all_nodes_with_name_matching transaction_sample, regexes
528
+ def find_all_nodes_with_name_matching(transaction_sample, regexes)
520
529
  regexes = [regexes].flatten
521
530
  matching_nodes = []
522
531
 
523
532
  transaction_sample.root_node.each_node do |node|
524
533
  regexes.each do |regex|
525
- if node.metric_name.match regex
534
+ if node.metric_name.match(regex)
526
535
  matching_nodes << node
527
536
  end
528
537
  end
@@ -531,7 +540,7 @@ def find_all_nodes_with_name_matching transaction_sample, regexes
531
540
  matching_nodes
532
541
  end
533
542
 
534
- def with_config config_hash, at_start=true
543
+ def with_config(config_hash, at_start = true)
535
544
  config = NewRelic::Agent::Configuration::DottedHash.new(config_hash, true)
536
545
  NewRelic::Agent.config.add_config_for_testing(config, at_start)
537
546
  NewRelic::Agent.instance.refresh_attribute_filter
@@ -543,20 +552,20 @@ def with_config config_hash, at_start=true
543
552
  end
544
553
  end
545
554
 
546
- def with_server_source config_hash, at_start=true
547
- with_config config_hash, at_start do
555
+ def with_server_source(config_hash, at_start = true)
556
+ with_config(config_hash, at_start) do
548
557
  NewRelic::Agent.config.notify_server_source_added
549
558
  yield
550
559
  end
551
560
  end
552
561
 
553
- def with_config_low_priority config_hash
562
+ def with_config_low_priority(config_hash)
554
563
  with_config(config_hash, false) do
555
564
  yield
556
565
  end
557
566
  end
558
567
 
559
- def with_transaction_renaming_rules rule_specs
568
+ def with_transaction_renaming_rules(rule_specs)
560
569
  original_engine = NewRelic::Agent.agent.instance_variable_get(:@transaction_rules)
561
570
  begin
562
571
  new_engine = NewRelic::Agent::RulesEngine.create_transaction_rules('transaction_name_rules' => rule_specs)
@@ -582,7 +591,7 @@ unless Time.respond_to?(:__original_now)
582
591
  end
583
592
  end
584
593
 
585
- def nr_freeze_time now=Time.now
594
+ def nr_freeze_time(now = Time.now)
586
595
  Time.__frozen_now = now
587
596
  end
588
597
 
@@ -590,11 +599,36 @@ def nr_unfreeze_time
590
599
  Time.__frozen_now = nil
591
600
  end
592
601
 
593
- def advance_time seconds
602
+ def advance_time(seconds)
594
603
  Time.__frozen_now = Time.now + seconds
595
604
  end
596
605
 
597
- def with_constant_defined constant_symbol, implementation=Module.new
606
+ unless Process.respond_to?(:__original_clock_gettime)
607
+ Process.instance_eval do
608
+ class << self
609
+ attr_accessor :__frozen_clock_gettime
610
+ alias_method :__original_clock_gettime, :clock_gettime
611
+
612
+ def clock_gettime(clock_id, unit = :float_second)
613
+ __frozen_clock_gettime || __original_clock_gettime(clock_id, unit)
614
+ end
615
+ end
616
+ end
617
+ end
618
+
619
+ def advance_process_time(seconds, clock_id = Process::CLOCK_REALTIME)
620
+ Process.__frozen_clock_gettime = Process.clock_gettime(clock_id) + seconds
621
+ end
622
+
623
+ def nr_freeze_process_time(now = Process.clock_gettime(Process::CLOCK_REALTIME))
624
+ Process.__frozen_clock_gettime = now
625
+ end
626
+
627
+ def nr_unfreeze_process_time
628
+ Process.__frozen_clock_gettime = nil
629
+ end
630
+
631
+ def with_constant_defined(constant_symbol, implementation = Module.new)
598
632
  const_path = constant_path(constant_symbol.to_s)
599
633
 
600
634
  if const_path
@@ -614,7 +648,7 @@ def with_constant_defined constant_symbol, implementation=Module.new
614
648
  end
615
649
  end
616
650
 
617
- def constant_path name, opts={}
651
+ def constant_path(name, opts = {})
618
652
  allow_partial = opts[:allow_partial]
619
653
  path = [Object]
620
654
  parts = name.gsub(/^::/, '').split('::')
@@ -622,22 +656,24 @@ def constant_path name, opts={}
622
656
  if !path.last.constants.include?(part.to_sym)
623
657
  return allow_partial ? path : nil
624
658
  end
659
+
625
660
  path << path.last.const_get(part)
626
661
  end
627
662
  path
628
663
  end
629
664
 
630
- def get_parent constant_name
665
+ def get_parent(constant_name)
631
666
  parent_name = constant_name.gsub(/::[^:]*$/, '')
632
667
  const_path = constant_path(parent_name)
633
668
  const_path ? const_path[-1] : nil
634
669
  end
635
670
 
636
- def undefine_constant constant_symbol
671
+ def undefine_constant(constant_symbol)
637
672
  const_str = constant_symbol.to_s
638
673
  parent = get_parent(const_str)
639
674
  const_name = const_str.gsub(/.*::/, '')
640
- return yield unless parent && parent.constants.include?(const_name.to_sym)
675
+ return yield unless parent&.constants&.include?(const_name.to_sym)
676
+
641
677
  removed_constant = parent.send(:remove_const, const_name)
642
678
  yield
643
679
  ensure
@@ -648,7 +684,7 @@ def with_debug_logging
648
684
  orig_logger = NewRelic::Agent.logger
649
685
  $stderr.puts '', '---', ''
650
686
  NewRelic::Agent.logger =
651
- NewRelic::Agent::AgentLogger.new('', Logger.new($stderr) )
687
+ NewRelic::Agent::AgentLogger.new('', Logger.new($stderr))
652
688
 
653
689
  with_config(:log_level => 'debug') do
654
690
  yield
@@ -657,18 +693,18 @@ ensure
657
693
  NewRelic::Agent.logger = orig_logger
658
694
  end
659
695
 
660
- def create_agent_command args={}
661
- NewRelic::Agent::Commands::AgentCommand.new([-1, { "name" => "command_name", "arguments" => args}])
696
+ def create_agent_command(args = {})
697
+ NewRelic::Agent::Commands::AgentCommand.new([-1, {'name' => 'command_name', 'arguments' => args}])
662
698
  end
663
699
 
664
- def wait_for_backtrace_service_poll opts={}
700
+ def wait_for_backtrace_service_poll(opts = {})
665
701
  defaults = {
666
702
  :timeout => 10.0,
667
- :service => NewRelic::Agent.agent.agent_command_router.backtrace_service,
703
+ :service => NewRelic::Agent.agent.instance_variable_get(:@agent_command_router).backtrace_service,
668
704
  :iterations => 1
669
705
  }
670
706
  opts = defaults.merge(opts)
671
- deadline = Time.now + opts[:timeout]
707
+ deadline = Process.clock_gettime(Process::CLOCK_REALTIME) + opts[:timeout]
672
708
 
673
709
  service = opts[:service]
674
710
  worker_loop = service.worker_loop
@@ -676,25 +712,25 @@ def wait_for_backtrace_service_poll opts={}
676
712
 
677
713
  until worker_loop.iterations > opts[:iterations]
678
714
  sleep(0.01)
679
- if Time.now > deadline
715
+ if Process.clock_gettime(Process::CLOCK_REALTIME) > deadline
680
716
  raise "Timed out waiting #{opts[:timeout]} s for backtrace service poll\n" +
681
- "Worker loop ran for #{opts[:service].worker_loop.iterations} iterations\n\n" +
682
- Thread.list.map { |t|
683
- "#{t.to_s}: newrelic_label: #{t[:newrelic_label].inspect}\n\n" +
684
- (t.backtrace || []).join("\n\t")
685
- }.join("\n\n")
717
+ "Worker loop ran for #{opts[:service].worker_loop.iterations} iterations\n\n" +
718
+ Thread.list.map { |t|
719
+ "#{t.to_s}: newrelic_label: #{t[:newrelic_label].inspect}\n\n" +
720
+ (t.backtrace || []).join("\n\t")
721
+ }.join("\n\n")
686
722
  end
687
723
  end
688
724
  end
689
725
 
690
- def with_array_logger level=:info
726
+ def with_array_logger(level = :info)
691
727
  orig_logger = NewRelic::Agent.logger
692
- config = { :log_level => level }
728
+ config = {:log_level => level}
693
729
  logdev = ArrayLogDevice.new
694
730
  override_logger = Logger.new(logdev)
695
731
 
696
732
  with_config(config) do
697
- NewRelic::Agent.logger = NewRelic::Agent::AgentLogger.new("", override_logger)
733
+ NewRelic::Agent.logger = NewRelic::Agent::AgentLogger.new('', override_logger)
698
734
  yield
699
735
  end
700
736
 
@@ -724,7 +760,7 @@ class EnvUpdater
724
760
 
725
761
  # Will attempt the given block up to MAX_RETRIES before
726
762
  # surfacing the exception down the chain.
727
- def with_retry retry_limit=MAX_RETRIES
763
+ def with_retry(retry_limit = MAX_RETRIES)
728
764
  retries ||= 0
729
765
  sleep(retries)
730
766
  yield
@@ -733,19 +769,19 @@ class EnvUpdater
733
769
  end
734
770
 
735
771
  # Locks and updates the ENV
736
- def safe_update env
772
+ def safe_update(env)
737
773
  with_retry do
738
774
  @mutex.synchronize do
739
- env.each{ |key, val| ENV[key] = val.to_s }
775
+ env.each { |key, val| ENV[key] = val.to_s }
740
776
  end
741
777
  end
742
778
  end
743
779
 
744
780
  # Locks and restores the ENV
745
- def safe_restore old_env
781
+ def safe_restore(old_env)
746
782
  with_retry do
747
783
  @mutex.synchronize do
748
- old_env.each{ |key, val| val ? ENV[key] = val : ENV.delete(key) }
784
+ old_env.each { |key, val| val ? ENV[key] = val : ENV.delete(key) }
749
785
  end
750
786
  end
751
787
  end
@@ -755,19 +791,19 @@ class EnvUpdater
755
791
  @@instance ||= EnvUpdater.new
756
792
  end
757
793
 
758
- def self.safe_update env
759
- instance.safe_update env
794
+ def self.safe_update(env)
795
+ instance.safe_update(env)
760
796
  end
761
797
 
762
- def self.safe_restore old_env
763
- instance.safe_restore old_env
798
+ def self.safe_restore(old_env)
799
+ instance.safe_restore(old_env)
764
800
  end
765
801
 
766
802
  # Effectively saves current ENV settings for given env's key/values,
767
803
  # runs given block, then restores ENV to original state before returning.
768
- def self.inject env, &block
804
+ def self.inject(env, &block)
769
805
  old_env = {}
770
- env.each{ |key, val| old_env[key] = ENV[key] }
806
+ env.each { |key, val| old_env[key] = ENV[key] }
771
807
  begin
772
808
  safe_update(env)
773
809
  yield
@@ -782,11 +818,11 @@ end
782
818
 
783
819
  # Changes ENV settings to given and runs given block and restores ENV
784
820
  # to original values before returning.
785
- def with_environment env, &block
821
+ def with_environment(env, &block)
786
822
  EnvUpdater.inject(env) { yield }
787
823
  end
788
824
 
789
- def with_argv argv
825
+ def with_argv(argv)
790
826
  old_argv = ARGV.dup
791
827
  ARGV.clear
792
828
  ARGV.concat(argv)
@@ -799,7 +835,7 @@ def with_argv argv
799
835
  end
800
836
  end
801
837
 
802
- def with_ignore_error_filter filter, &blk
838
+ def with_ignore_error_filter(filter, &blk)
803
839
  original_filter = NewRelic::Agent.ignore_error_filter
804
840
  NewRelic::Agent.ignore_error_filter(&filter)
805
841
 
@@ -808,15 +844,15 @@ ensure
808
844
  NewRelic::Agent::ErrorCollector.ignore_error_filter = original_filter
809
845
  end
810
846
 
811
- def json_dump_and_encode object
812
- Base64.encode64(::JSON.dump(object))
847
+ def json_dump_and_encode(object)
848
+ NewRelic::Base64.encode64(JSON.dump(object))
813
849
  end
814
850
 
815
851
  def get_last_analytics_event
816
852
  NewRelic::Agent.agent.transaction_event_aggregator.harvest![1].last
817
853
  end
818
854
 
819
- def swap_instance_method target, method_name, new_method_implementation, &blk
855
+ def swap_instance_method(target, method_name, new_method_implementation, &blk)
820
856
  old_method_implementation = target.instance_method(method_name)
821
857
  target.send(:define_method, method_name, new_method_implementation)
822
858
  yield
@@ -831,22 +867,22 @@ def cross_agent_tests_dir
831
867
  File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'cross_agent_tests'))
832
868
  end
833
869
 
834
- def load_cross_agent_test name
870
+ def load_cross_agent_test(name)
835
871
  test_file_path = File.join(cross_agent_tests_dir, "#{name}.json")
836
872
  data = File.read(test_file_path)
837
873
  data.gsub!('callCount', 'call_count')
838
- data = ::JSON.load(data)
839
- data.each { |testcase| testcase['testname'].gsub! ' ', '_' if String === testcase['testname'] }
874
+ data = JSON.parse(data)
875
+ data.each { |testcase| testcase['testname'].tr!(' ', '_') if String === testcase['testname'] }
840
876
  data
841
877
  end
842
878
 
843
- def each_cross_agent_test options
844
- options = {:dir => nil, :pattern => "*"}.update(options)
845
- path = File.join [cross_agent_tests_dir, options[:dir], options[:pattern]].compact
846
- Dir.glob(path).each { |file| yield file}
879
+ def each_cross_agent_test(options)
880
+ options = {:dir => nil, :pattern => '*'}.update(options)
881
+ path = File.join([cross_agent_tests_dir, options[:dir], options[:pattern]].compact)
882
+ Dir.glob(path).each { |file| yield(file) }
847
883
  end
848
884
 
849
- def assert_event_attributes event, test_name, expected_attributes, non_expected_attributes
885
+ def assert_event_attributes(event, test_name, expected_attributes, non_expected_attributes)
850
886
  incorrect_attributes = []
851
887
 
852
888
  event_attrs = event[0]
@@ -856,7 +892,7 @@ def assert_event_attributes event, test_name, expected_attributes, non_expected_
856
892
  incorrect_attributes << name unless actual_value == expected_value
857
893
  end
858
894
 
859
- msg = "Found missing or incorrect attribute values in #{test_name}:\n"
895
+ msg = +"Found missing or incorrect attribute values in #{test_name}:\n"
860
896
 
861
897
  incorrect_attributes.each do |name|
862
898
  msg << " #{name}: expected = #{expected_attributes[name].inspect}, actual = #{event_attrs[name].inspect}\n"
@@ -867,19 +903,20 @@ def assert_event_attributes event, test_name, expected_attributes, non_expected_
867
903
  event_attrs.each do |name, actual_value|
868
904
  msg << " #{name}: #{actual_value.inspect}\n"
869
905
  end
870
- assert(incorrect_attributes.empty?, msg)
906
+
907
+ assert_empty(incorrect_attributes, msg)
871
908
 
872
909
  non_expected_attributes.each do |name|
873
910
  refute event_attrs[name], "Found value '#{event_attrs[name]}' for attribute '#{name}', but expected nothing in #{test_name}"
874
911
  end
875
912
  end
876
913
 
877
- def attributes_for sample, type
914
+ def attributes_for(sample, type)
878
915
  sample.attributes.instance_variable_get("@#{type}_attributes")
879
916
  end
880
917
 
881
918
  def uncache_trusted_account_key
882
- NewRelic::Agent::Transaction::TraceContext::AccountHelpers.instance_variable_set :@trace_state_entry_key, nil
919
+ NewRelic::Agent::Transaction::TraceContext::AccountHelpers.instance_variable_set(:@trace_state_entry_key, nil)
883
920
  end
884
921
 
885
922
  def reset_buffers_and_caches
@@ -887,17 +924,17 @@ def reset_buffers_and_caches
887
924
  uncache_trusted_account_key
888
925
  end
889
926
 
890
- def message_for_status_code code
927
+ def message_for_status_code(code)
891
928
  # Net::HTTP::STATUS_CODES was introduced in Ruby 2.5
892
929
  if defined?(Net::HTTP::STATUS_CODES)
893
930
  return Net::HTTP::STATUS_CODES[code]
894
931
  end
895
932
 
896
933
  case code
897
- when 200 then "OK"
898
- when 404 then "Not Found"
899
- when 403 then "Forbidden"
900
- else "Unknown"
934
+ when 200 then 'OK'
935
+ when 404 then 'Not Found'
936
+ when 403 then 'Forbidden'
937
+ else 'Unknown'
901
938
  end
902
939
  end
903
940
 
@@ -905,18 +942,19 @@ end
905
942
  # http status code associated with it.
906
943
  # a "status_code" may be passed in the headers to alter the HTTP Status Code
907
944
  # 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
945
+ def mock_http_response(headers, wrap_it = true)
946
+ status_code = (headers.delete('status_code') || 200).to_i
910
947
  net_http_resp = Net::HTTPResponse.new(1.0, status_code, message_for_status_code(status_code))
911
948
  headers.each do |key, value|
912
- net_http_resp.add_field key.to_s, value
949
+ net_http_resp.add_field(key.to_s, value)
913
950
  end
914
951
  return net_http_resp unless wrap_it
952
+
915
953
  NewRelic::Agent::HTTPClients::NetHTTPResponse.new(net_http_resp)
916
954
  end
917
955
 
918
956
  # +expected+ can be a string or regular expression
919
- def assert_match_or_equal expected, value
957
+ def assert_match_or_equal(expected, value)
920
958
  if expected.is_a?(Regexp)
921
959
  assert_match expected, value
922
960
  else
@@ -926,9 +964,10 @@ end
926
964
 
927
965
  # selects the last segment with a noticed_error and checks
928
966
  # 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"
967
+ def assert_segment_noticed_error(txn, segment_name, error_classes, error_message)
968
+ error_segment = txn.segments.reverse.detect { |s| s.noticed_error }
969
+
970
+ assert error_segment, 'Expected at least one segment with a noticed_error'
932
971
 
933
972
  assert_match_or_equal segment_name, error_segment.name
934
973
 
@@ -938,48 +977,63 @@ def assert_segment_noticed_error txn, segment_name, error_classes, error_message
938
977
  assert_match_or_equal error_message, noticed_error.message
939
978
  end
940
979
 
941
- def assert_transaction_noticed_error txn, error_classes
942
- refute_empty txn.exceptions, "Expected transaction to notice the error"
980
+ def assert_transaction_noticed_error(txn, error_classes)
981
+ refute_empty txn.exceptions, 'Expected transaction to notice the error'
943
982
  assert_match_or_equal error_classes, txn.exceptions.keys.first.class.name
944
983
  end
945
984
 
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"
985
+ def refute_transaction_noticed_error(txn, error_class)
986
+ error_segment = txn.segments.reverse.detect { |s| s.noticed_error }
987
+
988
+ assert error_segment, 'Expected at least one segment with a noticed_error'
989
+ assert_empty txn.exceptions, 'Expected transaction to NOT notice any segment errors'
950
990
  end
951
991
 
952
- def refute_raises *exp
992
+ def refute_raises(*exp)
953
993
  msg = "#{exp.pop}.\n" if String === exp.last
954
994
 
955
995
  begin
956
996
  yield
957
997
  rescue MiniTest::Skip => e
958
998
  puts "SKIP REPORTS: #{e.inspect}"
959
- return e if exp.include? MiniTest::Skip
999
+ return e if exp.include?(MiniTest::Skip)
1000
+
960
1001
  raise e
961
1002
  rescue Exception => e
962
1003
  puts "EXCEPTION RAISED: #{e.inspect}\n#{e.backtrace}"
963
1004
  exp = exp.first if exp.size == 1
964
- flunk msg || "unexpected exception raised: #{e}"
1005
+
1006
+ flunk(msg || "unexpected exception raised: #{e}")
965
1007
  end
966
1008
  end
967
1009
 
968
- def assert_implements instance, method, *args
1010
+ def assert_implements(instance, method, *args)
969
1011
  fail_message = "expected #{instance.class}##{method} method to be implemented"
970
1012
  refute_raises NotImplementedError, fail_message do
971
1013
  instance.send(method, *args)
972
1014
  end
973
1015
  end
974
1016
 
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?)
1017
+ def defer_testing_to_min_supported_rails(test_file, min_rails_version, supports_jruby = true)
1018
+ if defined?(Rails) &&
1019
+ defined?(Rails::VERSION::STRING) &&
1020
+ (Rails::VERSION::STRING.to_f >= min_rails_version) &&
1021
+ (supports_jruby || !NewRelic::LanguageSupport.jruby?)
980
1022
 
981
1023
  yield
982
1024
  else
983
- puts "Skipping tests in #{test_file} because Rails >= #{min_rails_version} is unavailable"
1025
+ puts "Skipping tests in #{File.basename(test_file)} because Rails >= #{min_rails_version} is unavailable" if ENV['VERBOSE_TEST_OUTPUT']
984
1026
  end
985
1027
  end
1028
+
1029
+ def first_call_for(subject)
1030
+ items = $collector.calls_for(subject)
1031
+
1032
+ if defined?(JRUBY_VERSION)
1033
+ refute_predicate items.size, :zero?, "Expected at least one call for '#{subject}'"
1034
+ else
1035
+ assert_equal 1, items.size, "Expected exactly one call for '#{subject}'"
1036
+ end
1037
+
1038
+ items.first
1039
+ end