newrelic_rpm 6.8.0.360 → 8.13.1

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