newrelic_rpm 8.7.0 → 8.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (398) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/.rubocop.yml +68 -189
  4. data/.rubocop_todo.yml +52 -0
  5. data/.simplecov +15 -0
  6. data/.snyk +11 -0
  7. data/.yardopts +1 -0
  8. data/Brewfile +1 -0
  9. data/CHANGELOG.md +181 -23
  10. data/CONTRIBUTING.md +1 -1
  11. data/Gemfile +0 -1
  12. data/Guardfile +1 -0
  13. data/README.md +1 -1
  14. data/Rakefile +13 -20
  15. data/Thorfile +5 -0
  16. data/bin/newrelic +2 -1
  17. data/bin/newrelic_cmd +1 -0
  18. data/bin/nrdebug +66 -45
  19. data/config.dot +5 -5
  20. data/init.rb +4 -4
  21. data/install.rb +2 -2
  22. data/lib/new_relic/agent/adaptive_sampler.rb +4 -4
  23. data/lib/new_relic/agent/agent/shutdown.rb +34 -0
  24. data/lib/new_relic/agent/agent/special_startup.rb +70 -0
  25. data/lib/new_relic/agent/agent/start_worker_thread.rb +163 -0
  26. data/lib/new_relic/agent/agent/startup.rb +196 -0
  27. data/lib/new_relic/agent/agent.rb +134 -534
  28. data/lib/new_relic/agent/agent_logger.rb +8 -10
  29. data/lib/new_relic/agent/attribute_filter.rb +40 -21
  30. data/lib/new_relic/agent/attribute_processing.rb +7 -7
  31. data/lib/new_relic/agent/attributes.rb +2 -2
  32. data/lib/new_relic/agent/audit_logger.rb +3 -3
  33. data/lib/new_relic/agent/autostart.rb +2 -2
  34. data/lib/new_relic/agent/chained_call.rb +1 -1
  35. data/lib/new_relic/agent/commands/agent_command.rb +1 -1
  36. data/lib/new_relic/agent/commands/agent_command_router.rb +8 -8
  37. data/lib/new_relic/agent/commands/thread_profiler_session.rb +1 -1
  38. data/lib/new_relic/agent/configuration/default_source.rb +105 -41
  39. data/lib/new_relic/agent/configuration/dotted_hash.rb +2 -2
  40. data/lib/new_relic/agent/configuration/environment_source.rb +5 -5
  41. data/lib/new_relic/agent/configuration/event_harvest_config.rb +1 -1
  42. data/lib/new_relic/agent/configuration/high_security_source.rb +1 -1
  43. data/lib/new_relic/agent/configuration/manager.rb +5 -5
  44. data/lib/new_relic/agent/configuration/manual_source.rb +1 -1
  45. data/lib/new_relic/agent/configuration/mask_defaults.rb +3 -3
  46. data/lib/new_relic/agent/configuration/security_policy_source.rb +7 -4
  47. data/lib/new_relic/agent/configuration/server_source.rb +7 -7
  48. data/lib/new_relic/agent/configuration/yaml_source.rb +6 -4
  49. data/lib/new_relic/agent/configuration.rb +1 -1
  50. data/lib/new_relic/agent/connect/request_builder.rb +3 -3
  51. data/lib/new_relic/agent/connect/response_handler.rb +5 -5
  52. data/lib/new_relic/agent/custom_event_aggregator.rb +5 -5
  53. data/lib/new_relic/agent/database/explain_plan_helpers.rb +1 -1
  54. data/lib/new_relic/agent/database/obfuscation_helpers.rb +1 -1
  55. data/lib/new_relic/agent/database/obfuscator.rb +2 -2
  56. data/lib/new_relic/agent/database/postgres_explain_obfuscator.rb +1 -1
  57. data/lib/new_relic/agent/database.rb +21 -21
  58. data/lib/new_relic/agent/database_adapter.rb +1 -1
  59. data/lib/new_relic/agent/datastores/metric_helper.rb +12 -13
  60. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +4 -3
  61. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +2 -4
  62. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +2 -2
  63. data/lib/new_relic/agent/datastores/mongo.rb +1 -1
  64. data/lib/new_relic/agent/datastores/redis.rb +3 -3
  65. data/lib/new_relic/agent/datastores.rb +4 -4
  66. data/lib/new_relic/agent/deprecator.rb +1 -1
  67. data/lib/new_relic/agent/distributed_tracing/cross_app_payload.rb +1 -1
  68. data/lib/new_relic/agent/distributed_tracing/cross_app_tracing.rb +16 -17
  69. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +9 -10
  70. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +18 -19
  71. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +8 -10
  72. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +2 -3
  73. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +40 -40
  74. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +16 -15
  75. data/lib/new_relic/agent/distributed_tracing.rb +17 -18
  76. data/lib/new_relic/agent/encoding_normalizer.rb +1 -1
  77. data/lib/new_relic/agent/error_collector.rb +6 -5
  78. data/lib/new_relic/agent/error_event_aggregator.rb +3 -4
  79. data/lib/new_relic/agent/error_filter.rb +5 -5
  80. data/lib/new_relic/agent/error_trace_aggregator.rb +3 -3
  81. data/lib/new_relic/agent/event_aggregator.rb +20 -20
  82. data/lib/new_relic/agent/event_buffer.rb +1 -1
  83. data/lib/new_relic/agent/event_listener.rb +1 -1
  84. data/lib/new_relic/agent/event_loop.rb +9 -9
  85. data/lib/new_relic/agent/external.rb +10 -10
  86. data/lib/new_relic/agent/guid_generator.rb +2 -2
  87. data/lib/new_relic/agent/harvester.rb +1 -1
  88. data/lib/new_relic/agent/heap.rb +5 -5
  89. data/lib/new_relic/agent/hostname.rb +4 -3
  90. data/lib/new_relic/agent/http_clients/abstract.rb +1 -2
  91. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +5 -6
  92. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +4 -5
  93. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +2 -3
  94. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +1 -2
  95. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +0 -1
  96. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +1 -2
  97. data/lib/new_relic/agent/http_clients/uri_util.rb +1 -2
  98. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +6 -5
  99. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +31 -23
  100. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +7 -6
  101. data/lib/new_relic/agent/instrumentation/active_job.rb +11 -4
  102. data/lib/new_relic/agent/instrumentation/active_merchant.rb +4 -3
  103. data/lib/new_relic/agent/instrumentation/active_record.rb +9 -9
  104. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +37 -21
  105. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +3 -3
  106. data/lib/new_relic/agent/instrumentation/active_record_prepend.rb +1 -1
  107. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +21 -11
  108. data/lib/new_relic/agent/instrumentation/active_storage.rb +2 -2
  109. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +16 -15
  110. data/lib/new_relic/agent/instrumentation/active_support_logger/chain.rb +1 -1
  111. data/lib/new_relic/agent/instrumentation/active_support_logger/instrumentation.rb +1 -1
  112. data/lib/new_relic/agent/instrumentation/active_support_logger/prepend.rb +1 -1
  113. data/lib/new_relic/agent/instrumentation/active_support_logger.rb +2 -2
  114. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +2 -2
  115. data/lib/new_relic/agent/instrumentation/authlogic.rb +2 -2
  116. data/lib/new_relic/agent/instrumentation/bunny/chain.rb +10 -10
  117. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +10 -11
  118. data/lib/new_relic/agent/instrumentation/bunny/prepend.rb +4 -4
  119. data/lib/new_relic/agent/instrumentation/bunny.rb +1 -2
  120. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +25 -17
  121. data/lib/new_relic/agent/instrumentation/curb/chain.rb +21 -22
  122. data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +19 -16
  123. data/lib/new_relic/agent/instrumentation/curb/prepend.rb +2 -3
  124. data/lib/new_relic/agent/instrumentation/curb.rb +2 -2
  125. data/lib/new_relic/agent/instrumentation/data_mapper.rb +44 -44
  126. data/lib/new_relic/agent/instrumentation/delayed_job/chain.rb +1 -2
  127. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +2 -3
  128. data/lib/new_relic/agent/instrumentation/delayed_job/prepend.rb +1 -2
  129. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +3 -3
  130. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +3 -3
  131. data/lib/new_relic/agent/instrumentation/excon.rb +2 -2
  132. data/lib/new_relic/agent/instrumentation/grape/chain.rb +6 -6
  133. data/lib/new_relic/agent/instrumentation/grape/instrumentation.rb +5 -5
  134. data/lib/new_relic/agent/instrumentation/grape/prepend.rb +4 -4
  135. data/lib/new_relic/agent/instrumentation/grape.rb +1 -1
  136. data/lib/new_relic/agent/instrumentation/grpc/client/chain.rb +97 -0
  137. data/lib/new_relic/agent/instrumentation/grpc/client/instrumentation.rb +89 -0
  138. data/lib/new_relic/agent/instrumentation/grpc/client/prepend.rb +111 -0
  139. data/lib/new_relic/agent/instrumentation/grpc/client/request_wrapper.rb +30 -0
  140. data/lib/new_relic/agent/instrumentation/grpc/helper.rb +32 -0
  141. data/lib/new_relic/agent/instrumentation/grpc/server/chain.rb +69 -0
  142. data/lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb +134 -0
  143. data/lib/new_relic/agent/instrumentation/grpc/server/rpc_desc_prepend.rb +35 -0
  144. data/lib/new_relic/agent/instrumentation/grpc/server/rpc_server_prepend.rb +26 -0
  145. data/lib/new_relic/agent/instrumentation/grpc_client.rb +23 -0
  146. data/lib/new_relic/agent/instrumentation/grpc_server.rb +25 -0
  147. data/lib/new_relic/agent/instrumentation/httpclient/chain.rb +1 -1
  148. data/lib/new_relic/agent/instrumentation/httpclient/instrumentation.rb +6 -6
  149. data/lib/new_relic/agent/instrumentation/httpclient/prepend.rb +1 -1
  150. data/lib/new_relic/agent/instrumentation/httpclient.rb +2 -2
  151. data/lib/new_relic/agent/instrumentation/httprb/chain.rb +1 -1
  152. data/lib/new_relic/agent/instrumentation/httprb/instrumentation.rb +4 -4
  153. data/lib/new_relic/agent/instrumentation/httprb/prepend.rb +1 -1
  154. data/lib/new_relic/agent/instrumentation/httprb.rb +2 -2
  155. data/lib/new_relic/agent/instrumentation/ignore_actions.rb +2 -2
  156. data/lib/new_relic/agent/instrumentation/logger/chain.rb +2 -2
  157. data/lib/new_relic/agent/instrumentation/logger/instrumentation.rb +5 -1
  158. data/lib/new_relic/agent/instrumentation/logger/prepend.rb +1 -1
  159. data/lib/new_relic/agent/instrumentation/logger.rb +2 -2
  160. data/lib/new_relic/agent/instrumentation/memcache/chain.rb +2 -3
  161. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +24 -17
  162. data/lib/new_relic/agent/instrumentation/memcache/helper.rb +8 -9
  163. data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +17 -15
  164. data/lib/new_relic/agent/instrumentation/memcache/prepend.rb +21 -15
  165. data/lib/new_relic/agent/instrumentation/memcache.rb +5 -6
  166. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +2 -2
  167. data/lib/new_relic/agent/instrumentation/middleware_tracing.rb +0 -1
  168. data/lib/new_relic/agent/instrumentation/mongo.rb +2 -2
  169. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +15 -14
  170. data/lib/new_relic/agent/instrumentation/net_http/chain.rb +1 -1
  171. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +5 -5
  172. data/lib/new_relic/agent/instrumentation/net_http/prepend.rb +1 -1
  173. data/lib/new_relic/agent/instrumentation/net_http.rb +2 -2
  174. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +6 -6
  175. data/lib/new_relic/agent/instrumentation/padrino/chain.rb +1 -1
  176. data/lib/new_relic/agent/instrumentation/padrino/instrumentation.rb +1 -1
  177. data/lib/new_relic/agent/instrumentation/padrino/prepend.rb +1 -1
  178. data/lib/new_relic/agent/instrumentation/padrino.rb +2 -2
  179. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +3 -3
  180. data/lib/new_relic/agent/instrumentation/queue_time.rb +1 -2
  181. data/lib/new_relic/agent/instrumentation/rack/chain.rb +12 -12
  182. data/lib/new_relic/agent/instrumentation/rack/helpers.rb +1 -1
  183. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +7 -7
  184. data/lib/new_relic/agent/instrumentation/rack/prepend.rb +4 -4
  185. data/lib/new_relic/agent/instrumentation/rack.rb +1 -1
  186. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +6 -6
  187. data/lib/new_relic/agent/instrumentation/rails_middleware.rb +1 -1
  188. data/lib/new_relic/agent/instrumentation/rails_notifications/action_cable.rb +3 -2
  189. data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +3 -2
  190. data/lib/new_relic/agent/instrumentation/rails_notifications/action_view.rb +3 -2
  191. data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +3 -3
  192. data/lib/new_relic/agent/instrumentation/rake/chain.rb +2 -2
  193. data/lib/new_relic/agent/instrumentation/rake/instrumentation.rb +6 -2
  194. data/lib/new_relic/agent/instrumentation/rake/prepend.rb +1 -1
  195. data/lib/new_relic/agent/instrumentation/rake.rb +3 -3
  196. data/lib/new_relic/agent/instrumentation/redis/chain.rb +3 -4
  197. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +5 -6
  198. data/lib/new_relic/agent/instrumentation/redis/prepend.rb +3 -4
  199. data/lib/new_relic/agent/instrumentation/redis.rb +2 -2
  200. data/lib/new_relic/agent/instrumentation/resque/chain.rb +2 -2
  201. data/lib/new_relic/agent/instrumentation/resque/helper.rb +1 -1
  202. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +0 -1
  203. data/lib/new_relic/agent/instrumentation/resque/prepend.rb +1 -1
  204. data/lib/new_relic/agent/instrumentation/resque.rb +3 -3
  205. data/lib/new_relic/agent/instrumentation/sequel.rb +8 -8
  206. data/lib/new_relic/agent/instrumentation/sequel_helper.rb +1 -1
  207. data/lib/new_relic/agent/instrumentation/sidekiq.rb +6 -5
  208. data/lib/new_relic/agent/instrumentation/sinatra/chain.rb +1 -1
  209. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +2 -2
  210. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +1 -1
  211. data/lib/new_relic/agent/instrumentation/sinatra/prepend.rb +1 -1
  212. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +2 -2
  213. data/lib/new_relic/agent/instrumentation/sinatra.rb +2 -2
  214. data/lib/new_relic/agent/instrumentation/sunspot.rb +4 -4
  215. data/lib/new_relic/agent/instrumentation/thread/chain.rb +2 -2
  216. data/lib/new_relic/agent/instrumentation/thread/instrumentation.rb +1 -1
  217. data/lib/new_relic/agent/instrumentation/thread/prepend.rb +1 -1
  218. data/lib/new_relic/agent/instrumentation/thread.rb +2 -2
  219. data/lib/new_relic/agent/instrumentation/tilt/chain.rb +1 -1
  220. data/lib/new_relic/agent/instrumentation/tilt/instrumentation.rb +1 -1
  221. data/lib/new_relic/agent/instrumentation/tilt/prepend.rb +1 -1
  222. data/lib/new_relic/agent/instrumentation/tilt.rb +1 -1
  223. data/lib/new_relic/agent/instrumentation/typhoeus/chain.rb +1 -1
  224. data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +8 -8
  225. data/lib/new_relic/agent/instrumentation/typhoeus/prepend.rb +1 -1
  226. data/lib/new_relic/agent/instrumentation/typhoeus.rb +2 -2
  227. data/lib/new_relic/agent/instrumentation.rb +1 -1
  228. data/lib/new_relic/agent/internal_agent_error.rb +1 -1
  229. data/lib/new_relic/agent/javascript_instrumentor.rb +8 -8
  230. data/lib/new_relic/agent/linking_metadata.rb +2 -3
  231. data/lib/new_relic/agent/local_log_decorator.rb +0 -1
  232. data/lib/new_relic/agent/log_event_aggregator.rb +7 -7
  233. data/lib/new_relic/agent/log_once.rb +1 -1
  234. data/lib/new_relic/agent/log_priority.rb +1 -1
  235. data/lib/new_relic/agent/logging.rb +13 -13
  236. data/lib/new_relic/agent/memory_logger.rb +1 -1
  237. data/lib/new_relic/agent/messaging.rb +19 -20
  238. data/lib/new_relic/agent/method_tracer.rb +15 -10
  239. data/lib/new_relic/agent/method_tracer_helpers.rb +83 -1
  240. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +4 -4
  241. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +3 -3
  242. data/lib/new_relic/agent/monitors/inbound_request_monitor.rb +1 -1
  243. data/lib/new_relic/agent/monitors/synthetics_monitor.rb +2 -2
  244. data/lib/new_relic/agent/monitors.rb +5 -5
  245. data/lib/new_relic/agent/new_relic_service/encoders.rb +2 -2
  246. data/lib/new_relic/agent/new_relic_service/json_marshaller.rb +4 -4
  247. data/lib/new_relic/agent/new_relic_service/marshaller.rb +1 -1
  248. data/lib/new_relic/agent/new_relic_service/security_policy_settings.rb +1 -1
  249. data/lib/new_relic/agent/new_relic_service.rb +214 -166
  250. data/lib/new_relic/agent/{noticible_error.rb → noticeable_error.rb} +2 -3
  251. data/lib/new_relic/agent/null_logger.rb +1 -1
  252. data/lib/new_relic/agent/obfuscator.rb +3 -3
  253. data/lib/new_relic/agent/parameter_filtering.rb +1 -1
  254. data/lib/new_relic/agent/payload_metric_mapping.rb +1 -1
  255. data/lib/new_relic/agent/pipe_channel_manager.rb +10 -5
  256. data/lib/new_relic/agent/pipe_service.rb +2 -2
  257. data/lib/new_relic/agent/prepend_supportability.rb +2 -2
  258. data/lib/new_relic/agent/priority_sampled_buffer.rb +4 -4
  259. data/lib/new_relic/agent/range_extensions.rb +8 -28
  260. data/lib/new_relic/agent/rules_engine/replacement_rule.rb +2 -2
  261. data/lib/new_relic/agent/rules_engine/segment_terms_rule.rb +1 -1
  262. data/lib/new_relic/agent/rules_engine.rb +2 -2
  263. data/lib/new_relic/agent/sampler.rb +1 -1
  264. data/lib/new_relic/agent/sampler_collection.rb +1 -1
  265. data/lib/new_relic/agent/samplers/cpu_sampler.rb +1 -1
  266. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +2 -2
  267. data/lib/new_relic/agent/samplers/memory_sampler.rb +10 -10
  268. data/lib/new_relic/agent/samplers/object_sampler.rb +1 -1
  269. data/lib/new_relic/agent/samplers/vm_sampler.rb +3 -1
  270. data/lib/new_relic/agent/span_event_aggregator.rb +2 -2
  271. data/lib/new_relic/agent/span_event_primitive.rb +24 -19
  272. data/lib/new_relic/agent/sql_sampler.rb +8 -8
  273. data/lib/new_relic/agent/stats.rb +15 -4
  274. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -2
  275. data/lib/new_relic/agent/stats_engine/stats_hash.rb +4 -4
  276. data/lib/new_relic/agent/stats_engine.rb +1 -1
  277. data/lib/new_relic/agent/synthetics_event_aggregator.rb +6 -6
  278. data/lib/new_relic/agent/system_info.rb +68 -53
  279. data/lib/new_relic/agent/threading/agent_thread.rb +13 -9
  280. data/lib/new_relic/agent/threading/backtrace_node.rb +6 -4
  281. data/lib/new_relic/agent/threading/backtrace_service.rb +1 -1
  282. data/lib/new_relic/agent/threading/thread_profile.rb +4 -4
  283. data/lib/new_relic/agent/timestamp_sampled_buffer.rb +1 -1
  284. data/lib/new_relic/agent/tracer.rb +19 -17
  285. data/lib/new_relic/agent/transaction/abstract_segment.rb +93 -34
  286. data/lib/new_relic/agent/transaction/datastore_segment.rb +12 -12
  287. data/lib/new_relic/agent/transaction/distributed_tracer.rb +52 -48
  288. data/lib/new_relic/agent/transaction/distributed_tracing.rb +21 -22
  289. data/lib/new_relic/agent/transaction/external_request_segment.rb +37 -29
  290. data/lib/new_relic/agent/transaction/message_broker_segment.rb +6 -6
  291. data/lib/new_relic/agent/transaction/request_attributes.rb +29 -29
  292. data/lib/new_relic/agent/transaction/segment.rb +8 -8
  293. data/lib/new_relic/agent/transaction/slowest_sample_buffer.rb +1 -1
  294. data/lib/new_relic/agent/transaction/synthetics_sample_buffer.rb +1 -1
  295. data/lib/new_relic/agent/transaction/trace.rb +3 -3
  296. data/lib/new_relic/agent/transaction/trace_builder.rb +10 -10
  297. data/lib/new_relic/agent/transaction/trace_context.rb +22 -20
  298. data/lib/new_relic/agent/transaction/trace_node.rb +9 -9
  299. data/lib/new_relic/agent/transaction/tracing.rb +7 -7
  300. data/lib/new_relic/agent/transaction/transaction_sample_buffer.rb +1 -2
  301. data/lib/new_relic/agent/transaction.rb +35 -31
  302. data/lib/new_relic/agent/transaction_error_primitive.rb +9 -9
  303. data/lib/new_relic/agent/transaction_event_aggregator.rb +5 -5
  304. data/lib/new_relic/agent/transaction_event_primitive.rb +3 -4
  305. data/lib/new_relic/agent/transaction_event_recorder.rb +15 -14
  306. data/lib/new_relic/agent/transaction_metrics.rb +4 -4
  307. data/lib/new_relic/agent/transaction_sampler.rb +5 -5
  308. data/lib/new_relic/agent/transaction_time_aggregator.rb +14 -14
  309. data/lib/new_relic/agent/utilization/aws.rb +4 -4
  310. data/lib/new_relic/agent/utilization/azure.rb +2 -2
  311. data/lib/new_relic/agent/utilization/gcp.rb +7 -7
  312. data/lib/new_relic/agent/utilization/pcf.rb +4 -4
  313. data/lib/new_relic/agent/utilization/vendor.rb +28 -28
  314. data/lib/new_relic/agent/utilization_data.rb +1 -1
  315. data/lib/new_relic/agent/vm/jruby_vm.rb +1 -1
  316. data/lib/new_relic/agent/vm/monotonic_gc_profiler.rb +1 -1
  317. data/lib/new_relic/agent/vm/mri_vm.rb +36 -23
  318. data/lib/new_relic/agent/vm/snapshot.rb +2 -2
  319. data/lib/new_relic/agent/vm.rb +1 -1
  320. data/lib/new_relic/agent/worker_loop.rb +2 -2
  321. data/lib/new_relic/agent.rb +27 -27
  322. data/lib/new_relic/cli/command.rb +6 -6
  323. data/lib/new_relic/cli/commands/deployments.rb +81 -29
  324. data/lib/new_relic/cli/commands/install.rb +6 -6
  325. data/lib/new_relic/coerce.rb +5 -6
  326. data/lib/new_relic/collection_helper.rb +4 -4
  327. data/lib/new_relic/constants.rb +0 -1
  328. data/lib/new_relic/control/class_methods.rb +1 -1
  329. data/lib/new_relic/control/frameworks/external.rb +1 -1
  330. data/lib/new_relic/control/frameworks/rails.rb +3 -3
  331. data/lib/new_relic/control/frameworks/rails3.rb +1 -1
  332. data/lib/new_relic/control/frameworks/rails4.rb +1 -1
  333. data/lib/new_relic/control/frameworks/rails_notifications.rb +1 -1
  334. data/lib/new_relic/control/frameworks/ruby.rb +1 -1
  335. data/lib/new_relic/control/frameworks/sinatra.rb +1 -1
  336. data/lib/new_relic/control/frameworks.rb +1 -1
  337. data/lib/new_relic/control/instance_methods.rb +20 -42
  338. data/lib/new_relic/control/instrumentation.rb +7 -7
  339. data/lib/new_relic/control/private_instance_methods.rb +48 -0
  340. data/lib/new_relic/control/server_methods.rb +2 -2
  341. data/lib/new_relic/control.rb +1 -1
  342. data/lib/new_relic/delayed_job_injection.rb +1 -1
  343. data/lib/new_relic/dependency_detection.rb +23 -22
  344. data/lib/new_relic/environment_report.rb +24 -14
  345. data/lib/new_relic/helper.rb +5 -5
  346. data/lib/new_relic/language_support.rb +8 -3
  347. data/lib/new_relic/latest_changes.rb +4 -4
  348. data/lib/new_relic/local_environment.rb +9 -11
  349. data/lib/new_relic/metric_data.rb +30 -24
  350. data/lib/new_relic/metric_spec.rb +3 -3
  351. data/lib/new_relic/noticed_error.rb +3 -6
  352. data/lib/new_relic/rack/agent_hooks.rb +1 -1
  353. data/lib/new_relic/rack/agent_middleware.rb +1 -1
  354. data/lib/new_relic/rack/browser_monitoring.rb +132 -123
  355. data/lib/new_relic/rack.rb +1 -1
  356. data/lib/new_relic/recipes/capistrano3.rb +3 -59
  357. data/lib/new_relic/recipes/capistrano_legacy.rb +13 -13
  358. data/lib/new_relic/recipes/helpers/send_deployment.rb +69 -0
  359. data/lib/new_relic/recipes.rb +1 -1
  360. data/lib/new_relic/supportability_helper.rb +6 -6
  361. data/lib/new_relic/traced_thread.rb +5 -7
  362. data/lib/new_relic/version.rb +2 -2
  363. data/lib/newrelic_rpm.rb +1 -1
  364. data/lib/sequel/extensions/newrelic_instrumentation.rb +8 -8
  365. data/lib/sequel/plugins/newrelic_instrumentation.rb +1 -1
  366. data/lib/tasks/all.rb +1 -1
  367. data/lib/tasks/config.rake +8 -112
  368. data/lib/tasks/coverage_report.rake +28 -0
  369. data/lib/tasks/{config.html.erb → helpers/config.html.erb} +0 -0
  370. data/lib/tasks/{config.text.erb → helpers/config.text.erb} +0 -0
  371. data/lib/tasks/helpers/format.rb +120 -0
  372. data/lib/tasks/helpers/matches.rb +12 -0
  373. data/lib/tasks/helpers/prompt.rb +24 -0
  374. data/lib/tasks/helpers/removers.rb +33 -0
  375. data/lib/tasks/install.rake +4 -0
  376. data/lib/tasks/instrumentation_generator/README.md +63 -0
  377. data/lib/tasks/instrumentation_generator/TODO.md +33 -0
  378. data/lib/tasks/instrumentation_generator/instrumentation.thor +102 -0
  379. data/lib/tasks/instrumentation_generator/templates/Envfile.tt +9 -0
  380. data/lib/tasks/instrumentation_generator/templates/chain.tt +21 -0
  381. data/lib/tasks/instrumentation_generator/templates/chain_method.tt +7 -0
  382. data/lib/tasks/instrumentation_generator/templates/dependency_detection.tt +29 -0
  383. data/lib/tasks/instrumentation_generator/templates/instrumentation.tt +12 -0
  384. data/lib/tasks/instrumentation_generator/templates/instrumentation_method.tt +3 -0
  385. data/lib/tasks/instrumentation_generator/templates/newrelic.yml.tt +19 -0
  386. data/lib/tasks/instrumentation_generator/templates/prepend.tt +13 -0
  387. data/lib/tasks/instrumentation_generator/templates/prepend_method.tt +3 -0
  388. data/lib/tasks/instrumentation_generator/templates/test.tt +15 -0
  389. data/lib/tasks/multiverse.rake +4 -0
  390. data/lib/tasks/multiverse.rb +5 -30
  391. data/lib/tasks/newrelic.rb +1 -1
  392. data/lib/tasks/tests.rake +7 -8
  393. data/newrelic.yml +514 -483
  394. data/newrelic_rpm.gemspec +16 -21
  395. data/recipes/newrelic.rb +1 -1
  396. data/test/agent_helper.rb +125 -147
  397. metadata +82 -90
  398. data/bin/mongrel_rpm +0 -32
@@ -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
  require 'socket'
6
6
  require 'net/https'
@@ -37,6 +37,11 @@ require 'new_relic/agent/adaptive_sampler'
37
37
  require 'new_relic/agent/connect/request_builder'
38
38
  require 'new_relic/agent/connect/response_handler'
39
39
 
40
+ require 'new_relic/agent/agent/start_worker_thread'
41
+ require 'new_relic/agent/agent/startup'
42
+ require 'new_relic/agent/agent/special_startup'
43
+ require 'new_relic/agent/agent/shutdown'
44
+
40
45
  module NewRelic
41
46
  module Agent
42
47
  # The Agent is a singleton that is instantiated when the plugin is
@@ -48,44 +53,56 @@ module NewRelic
48
53
  ::NewRelic::Agent.config
49
54
  end
50
55
 
56
+ include NewRelic::Agent::StartWorkerThread
57
+ include NewRelic::Agent::SpecialStartup
58
+ include NewRelic::Agent::Startup
59
+ include NewRelic::Agent::Shutdown
60
+
51
61
  def initialize
62
+ init_basics
63
+ init_components
64
+ init_event_handlers
65
+ setup_attribute_filter
66
+ end
67
+
68
+ private
69
+
70
+ def init_basics
52
71
  @started = false
53
72
  @event_loop = nil
54
73
  @worker_thread = nil
74
+ @connect_state = :pending
75
+ @connect_attempts = 0
76
+ @waited_on_connect = nil
77
+ @connected_pid = nil
78
+ @wait_on_connect_mutex = Mutex.new
79
+ @after_fork_lock = Mutex.new
80
+ @wait_on_connect_condition = ConditionVariable.new
81
+ end
55
82
 
83
+ def init_components
56
84
  @service = NewRelicService.new
57
-
58
85
  @events = EventListener.new
59
86
  @stats_engine = StatsEngine.new
60
87
  @transaction_sampler = TransactionSampler.new
61
88
  @sql_sampler = SqlSampler.new
62
- @agent_command_router = Commands::AgentCommandRouter.new @events
63
- @monitors = Monitors.new @events
64
- @error_collector = ErrorCollector.new @events
65
89
  @transaction_rules = RulesEngine.new
66
- @harvest_samplers = SamplerCollection.new @events
67
90
  @monotonic_gc_profiler = VM::MonotonicGCProfiler.new
68
- @javascript_instrumentor = JavascriptInstrumentor.new @events
69
91
  @adaptive_sampler = AdaptiveSampler.new(Agent.config[:sampling_target],
70
92
  Agent.config[:sampling_target_period_in_seconds])
93
+ end
71
94
 
72
- @harvester = Harvester.new @events
73
- @after_fork_lock = Mutex.new
74
-
75
- @transaction_event_recorder = TransactionEventRecorder.new @events
76
- @custom_event_aggregator = CustomEventAggregator.new @events
77
- @span_event_aggregator = SpanEventAggregator.new @events
78
- @log_event_aggregator = LogEventAggregator.new @events
79
-
80
- @connect_state = :pending
81
- @connect_attempts = 0
82
- @waited_on_connect = nil
83
- @connected_pid = nil
84
-
85
- @wait_on_connect_mutex = Mutex.new
86
- @wait_on_connect_condition = ConditionVariable.new
87
-
88
- setup_attribute_filter
95
+ def init_event_handlers
96
+ @agent_command_router = Commands::AgentCommandRouter.new(@events)
97
+ @monitors = Monitors.new(@events)
98
+ @error_collector = ErrorCollector.new(@events)
99
+ @harvest_samplers = SamplerCollection.new(@events)
100
+ @javascript_instrumentor = JavaScriptInstrumentor.new(@events)
101
+ @harvester = Harvester.new(@events)
102
+ @transaction_event_recorder = TransactionEventRecorder.new(@events)
103
+ @custom_event_aggregator = CustomEventAggregator.new(@events)
104
+ @span_event_aggregator = SpanEventAggregator.new(@events)
105
+ @log_event_aggregator = LogEventAggregator.new(@events)
89
106
  end
90
107
 
91
108
  def setup_attribute_filter
@@ -96,6 +113,8 @@ module NewRelic
96
113
  end
97
114
  end
98
115
 
116
+ public
117
+
99
118
  def refresh_attribute_filter
100
119
  @attribute_filter = AttributeFilter.new(Agent.config)
101
120
  end
@@ -143,7 +162,7 @@ module NewRelic
143
162
  # collector on connect. The former are applied during txns,
144
163
  # the latter during harvest.
145
164
  attr_accessor :transaction_rules
146
- # Responsbile for restarting the harvest thread
165
+ # Responsible for restarting the harvest thread
147
166
  attr_reader :harvester
148
167
  # GC::Profiler.total_time is not monotonic so we wrap it.
149
168
  attr_reader :monotonic_gc_profiler
@@ -171,7 +190,7 @@ module NewRelic
171
190
  # It assumes the parent process initialized the agent, but does
172
191
  # not assume the agent started.
173
192
  #
174
- # The call is idempotent, but not re-entrant.
193
+ # The call is idempotent, but not reentrant.
175
194
  #
176
195
  # * It clears any metrics carried over from the parent process
177
196
  # * Restarts the sampler thread if necessary
@@ -187,28 +206,34 @@ module NewRelic
187
206
  # connection, this tells me to only try it once so this method returns
188
207
  # quickly if there is some kind of latency with the server.
189
208
  def after_fork(options = {})
209
+ return unless needs_after_fork_work?
210
+
211
+ ::NewRelic::Agent.logger.debug("Starting the worker thread in #{Process.pid} (parent #{Process.ppid}) after forking.")
212
+
213
+ channel_id = options[:report_to_channel]
214
+ install_pipe_service(channel_id) if channel_id
215
+
216
+ # Clear out locks and stats left over from parent process
217
+ reset_objects_with_locks
218
+ drop_buffered_data
219
+
220
+ setup_and_start_agent(options)
221
+ end
222
+
223
+ def needs_after_fork_work?
190
224
  needs_restart = false
191
225
  @after_fork_lock.synchronize do
192
226
  needs_restart = @harvester.needs_restart?
193
227
  @harvester.mark_started
194
228
  end
195
229
 
196
- return if !needs_restart ||
230
+ return false if !needs_restart ||
197
231
  !Agent.config[:agent_enabled] ||
198
232
  !Agent.config[:monitor_mode] ||
199
233
  disconnected? ||
200
234
  !control.security_settings_valid?
201
235
 
202
- ::NewRelic::Agent.logger.debug "Starting the worker thread in #{Process.pid} (parent #{Process.ppid}) after forking."
203
-
204
- channel_id = options[:report_to_channel]
205
- install_pipe_service(channel_id) if channel_id
206
-
207
- # Clear out locks and stats left over from parent process
208
- reset_objects_with_locks
209
- drop_buffered_data
210
-
211
- setup_and_start_agent(options)
236
+ true
212
237
  end
213
238
 
214
239
  def install_pipe_service(channel_id)
@@ -222,48 +247,11 @@ module NewRelic
222
247
  end
223
248
  end
224
249
 
225
- # True if we have initialized and completed 'start'
226
- def started?
227
- @started
228
- end
229
-
230
- # Attempt a graceful shutdown of the agent, flushing any remaining
231
- # data.
232
- def shutdown
233
- return unless started?
234
- ::NewRelic::Agent.logger.info "Starting Agent shutdown"
235
-
236
- stop_event_loop
237
- trap_signals_for_litespeed
238
- untraced_graceful_disconnect
239
- revert_to_default_configuration
240
-
241
- @started = nil
242
- Control.reset
243
- end
244
-
245
250
  def revert_to_default_configuration
246
251
  Agent.config.remove_config_type(:manual)
247
252
  Agent.config.remove_config_type(:server)
248
253
  end
249
254
 
250
- # If the @worker_thread encounters an error during the attempt to connect to the collector
251
- # then the connect attempts enter an exponential backoff retry loop. To avoid potential
252
- # race conditions with shutting down while also attempting to reconnect, we join the
253
- # @worker_thread with a timeout threshold. This allows potentially connecting and flushing
254
- # pending data to the server, but without waiting indefinitely for a reconnect to succeed.
255
- # The use-case where this typically arises is in cronjob scheduled rake tasks where there's
256
- # also some network stability/latency issues happening.
257
- def stop_event_loop
258
- @event_loop.stop if @event_loop
259
- # Wait the end of the event loop thread.
260
- if @worker_thread
261
- unless @worker_thread.join(3)
262
- ::NewRelic::Agent.logger.debug "Event loop thread did not stop within 3 seconds"
263
- end
264
- end
265
- end
266
-
267
255
  def trap_signals_for_litespeed
268
256
  # if litespeed, then ignore all future SIGUSR1 - it's
269
257
  # litespeed trying to shut us down
@@ -273,16 +261,6 @@ module NewRelic
273
261
  end
274
262
  end
275
263
 
276
- def untraced_graceful_disconnect
277
- begin
278
- NewRelic::Agent.disable_all_tracing do
279
- graceful_disconnect
280
- end
281
- rescue => e
282
- ::NewRelic::Agent.logger.error e
283
- end
284
- end
285
-
286
264
  # Sets a thread local variable as to whether we should or
287
265
  # should not record sql in the current thread. Returns the
288
266
  # previous value, if there is one
@@ -307,255 +285,6 @@ module NewRelic
307
285
  Tracer.state.pop_traced
308
286
  end
309
287
 
310
- # Herein lies the corpse of the former 'start' method. May
311
- # its unmatched flog score rest in pieces.
312
- module Start
313
- # Check whether we have already started, which is an error condition
314
- def already_started?
315
- if started?
316
- ::NewRelic::Agent.logger.error("Agent Started Already!")
317
- true
318
- end
319
- end
320
-
321
- # The agent is disabled when it is not force enabled by the
322
- # 'agent_enabled' option (e.g. in a manual start), or
323
- # enabled normally through the configuration file
324
- def disabled?
325
- !Agent.config[:agent_enabled]
326
- end
327
-
328
- # Log startup information that we almost always want to know
329
- def log_startup
330
- log_environment
331
- log_dispatcher
332
- log_app_name
333
- end
334
-
335
- # Log the environment the app thinks it's running in.
336
- # Useful in debugging, as this is the key for config YAML lookups.
337
- def log_environment
338
- ::NewRelic::Agent.logger.info "Environment: #{NewRelic::Control.instance.env}"
339
- end
340
-
341
- # Logs the dispatcher to the log file to assist with
342
- # debugging. When no debugger is present, logs this fact to
343
- # assist with proper dispatcher detection
344
- def log_dispatcher
345
- dispatcher_name = Agent.config[:dispatcher].to_s
346
-
347
- if dispatcher_name.empty?
348
- ::NewRelic::Agent.logger.info 'No known dispatcher detected.'
349
- else
350
- ::NewRelic::Agent.logger.info "Dispatcher: #{dispatcher_name}"
351
- end
352
- end
353
-
354
- def log_app_name
355
- ::NewRelic::Agent.logger.info "Application: #{Agent.config[:app_name].join(", ")}"
356
- end
357
-
358
- def log_ignore_url_regexes
359
- regexes = NewRelic::Agent.config[:'rules.ignore_url_regexes']
360
-
361
- unless regexes.empty?
362
- ::NewRelic::Agent.logger.info "Ignoring URLs that match the following regexes: #{regexes.map(&:inspect).join(", ")}."
363
- end
364
- end
365
-
366
- # Logs the configured application names
367
- def app_name_configured?
368
- names = Agent.config[:app_name]
369
- return names.respond_to?(:any?) && names.any?
370
- end
371
-
372
- # Connecting in the foreground blocks further startup of the
373
- # agent until we have a connection - useful in cases where
374
- # you're trying to log a very-short-running process and want
375
- # to get statistics from before a server connection
376
- # (typically 20 seconds) exists
377
- def connect_in_foreground
378
- NewRelic::Agent.disable_all_tracing { connect(:keep_retrying => false) }
379
- end
380
-
381
- # This matters when the following three criteria are met:
382
- #
383
- # 1. A Sinatra 'classic' application is being run
384
- # 2. The app is being run by executing the main file directly, rather
385
- # than via a config.ru file.
386
- # 3. newrelic_rpm is required *after* sinatra
387
- #
388
- # In this case, the entire application runs from an at_exit handler in
389
- # Sinatra, and if we were to install ours, it would be executed before
390
- # the one in Sinatra, meaning that we'd shutdown the agent too early
391
- # and never collect any data.
392
- def sinatra_classic_app?
393
- (
394
- defined?(Sinatra::Application) &&
395
- Sinatra::Application.respond_to?(:run) &&
396
- Sinatra::Application.run?
397
- )
398
- end
399
-
400
- def should_install_exit_handler?
401
- return false unless Agent.config[:send_data_on_exit]
402
- !sinatra_classic_app? || Agent.config[:force_install_exit_handler]
403
- end
404
-
405
- def install_exit_handler
406
- return unless should_install_exit_handler?
407
- NewRelic::Agent.logger.debug("Installing at_exit handler")
408
- at_exit { shutdown }
409
- end
410
-
411
- # Classy logging of the agent version and the current pid,
412
- # so we can disambiguate processes in the log file and make
413
- # sure they're running a reasonable version
414
- def log_version_and_pid
415
- ::NewRelic::Agent.logger.debug "New Relic Ruby Agent #{NewRelic::VERSION::STRING} Initialized: pid = #{$$}"
416
- end
417
-
418
- # Warn the user if they have configured their agent not to
419
- # send data, that way we can see this clearly in the log file
420
- def monitoring?
421
- if Agent.config[:monitor_mode]
422
- true
423
- else
424
- ::NewRelic::Agent.logger.warn('Agent configured not to send data in this environment.')
425
- false
426
- end
427
- end
428
-
429
- # Tell the user when the license key is missing so they can
430
- # fix it by adding it to the file
431
- def has_license_key?
432
- if Agent.config[:license_key] && Agent.config[:license_key].length > 0
433
- true
434
- else
435
- ::NewRelic::Agent.logger.warn("No license key found. " +
436
- "This often means your newrelic.yml file was not found, or it lacks a section for the running environment, '#{NewRelic::Control.instance.env}'. You may also want to try linting your newrelic.yml to ensure it is valid YML.")
437
- false
438
- end
439
- end
440
-
441
- # A correct license key exists and is of the proper length
442
- def has_correct_license_key?
443
- has_license_key? && correct_license_length
444
- end
445
-
446
- # A license key is an arbitrary 40 character string,
447
- # usually looks something like a SHA1 hash
448
- def correct_license_length
449
- key = Agent.config[:license_key]
450
-
451
- if key.length == 40
452
- true
453
- else
454
- ::NewRelic::Agent.logger.error("Invalid license key: #{key}")
455
- false
456
- end
457
- end
458
-
459
- # If we're using a dispatcher that forks before serving
460
- # requests, we need to wait until the children are forked
461
- # before connecting, otherwise the parent process sends useless data
462
- def using_forking_dispatcher?
463
- # TODO: MAJOR VERSION - remove :rainbows
464
- if [:puma, :passenger, :rainbows, :unicorn].include? Agent.config[:dispatcher]
465
- ::NewRelic::Agent.logger.info "Deferring startup of agent reporting thread because #{Agent.config[:dispatcher]} may fork."
466
- true
467
- else
468
- false
469
- end
470
- end
471
-
472
- # Return true if we're using resque and it hasn't had a chance to (potentially)
473
- # daemonize itself. This avoids hanging when there's a Thread started
474
- # before Resque calls Process.daemon (Jira RUBY-857)
475
- def defer_for_resque?
476
- NewRelic::Agent.config[:dispatcher] == :resque &&
477
- NewRelic::Agent::Instrumentation::Resque::Helper.resque_fork_per_job? &&
478
- !PipeChannelManager.listener.started?
479
- end
480
-
481
- def in_resque_child_process?
482
- defined?(@service) && @service.is_a?(PipeService)
483
- end
484
-
485
- # Sanity-check the agent configuration and start the agent,
486
- # setting up the worker thread and the exit handler to shut
487
- # down the agent
488
- def check_config_and_start_agent
489
- return unless monitoring? && has_correct_license_key?
490
- return if using_forking_dispatcher?
491
- setup_and_start_agent
492
- end
493
-
494
- # This is the shared method between the main agent startup and the
495
- # after_fork call restarting the thread in deferred dispatchers.
496
- #
497
- # Treatment of @started and env report is important to get right.
498
- def setup_and_start_agent(options = {})
499
- @started = true
500
- @harvester.mark_started
501
-
502
- unless in_resque_child_process?
503
- install_exit_handler
504
- environment_for_connect
505
- @harvest_samplers.load_samplers unless Agent.config[:disable_samplers]
506
- end
507
-
508
- connect_in_foreground if Agent.config[:sync_startup]
509
- start_worker_thread(options)
510
- end
511
- end
512
-
513
- include Start
514
-
515
- def defer_for_delayed_job?
516
- NewRelic::Agent.config[:dispatcher] == :delayed_job &&
517
- !NewRelic::DelayedJobInjection.worker_name
518
- end
519
-
520
- # Check to see if the agent should start, returning +true+ if it should.
521
- def agent_should_start?
522
- return false if already_started? || disabled?
523
-
524
- if defer_for_delayed_job?
525
- ::NewRelic::Agent.logger.debug "Deferring startup for DelayedJob"
526
- return false
527
- end
528
-
529
- if defer_for_resque?
530
- ::NewRelic::Agent.logger.debug "Deferring startup for Resque in case it daemonizes"
531
- return false
532
- end
533
-
534
- unless app_name_configured?
535
- NewRelic::Agent.logger.error "No application name configured.",
536
- "The Agent cannot start without at least one. Please check your ",
537
- "newrelic.yml and ensure that it is valid and has at least one ",
538
- "value set for app_name in the #{NewRelic::Control.instance.env} ",
539
- "environment."
540
- return false
541
- end
542
-
543
- return true
544
- end
545
-
546
- # Logs a bunch of data and starts the agent, if needed
547
- def start
548
- return unless agent_should_start?
549
-
550
- log_startup
551
- check_config_and_start_agent
552
- log_version_and_pid
553
-
554
- events.subscribe(:initial_configuration_complete) do
555
- log_ignore_url_regexes
556
- end
557
- end
558
-
559
288
  # Clear out the metric data, errors, and transaction traces, etc.
560
289
  def drop_buffered_data
561
290
  @stats_engine.reset!
@@ -581,160 +310,12 @@ module NewRelic
581
310
 
582
311
  def flush_pipe_data
583
312
  if connected? && @service.is_a?(PipeService)
584
- transmit_data
585
- transmit_analytic_event_data
586
- transmit_custom_event_data
587
- transmit_error_event_data
588
- transmit_span_event_data
589
- transmit_log_event_data
313
+ transmit_data_types
590
314
  end
591
315
  end
592
316
 
593
317
  private
594
318
 
595
- # All of this module used to be contained in the
596
- # start_worker_thread method - this is an artifact of
597
- # refactoring and can be moved, renamed, etc at will
598
- module StartWorkerThread
599
- def create_event_loop
600
- EventLoop.new
601
- end
602
-
603
- LOG_ONCE_KEYS_RESET_PERIOD = 60.0
604
-
605
- # Certain event types may sometimes need to be on the same interval as metrics,
606
- # so we will check config assigned in EventHarvestConfig to determine the interval
607
- # on which to report them
608
- def interval_for event_type
609
- interval = Agent.config[:"event_report_period.#{event_type}"]
610
- :"#{interval}_second_harvest"
611
- end
612
-
613
- TRANSACTION_EVENT_DATA = "transaction_event_data".freeze
614
- CUSTOM_EVENT_DATA = "custom_event_data".freeze
615
- ERROR_EVENT_DATA = "error_event_data".freeze
616
- SPAN_EVENT_DATA = "span_event_data".freeze
617
- LOG_EVENT_DATA = "log_event_data".freeze
618
-
619
- def create_and_run_event_loop
620
- data_harvest = :"#{Agent.config[:data_report_period]}_second_harvest"
621
- event_harvest = :"#{Agent.config[:event_report_period]}_second_harvest"
622
-
623
- @event_loop = create_event_loop
624
- @event_loop.on(data_harvest) do
625
- transmit_data
626
- end
627
-
628
- @event_loop.on(interval_for TRANSACTION_EVENT_DATA) do
629
- transmit_analytic_event_data
630
- end
631
- @event_loop.on(interval_for CUSTOM_EVENT_DATA) do
632
- transmit_custom_event_data
633
- end
634
- @event_loop.on(interval_for ERROR_EVENT_DATA) do
635
- transmit_error_event_data
636
- end
637
- @event_loop.on(interval_for SPAN_EVENT_DATA) do
638
- transmit_span_event_data
639
- end
640
- @event_loop.on(interval_for LOG_EVENT_DATA) do
641
- transmit_log_event_data
642
- end
643
-
644
- @event_loop.on(:reset_log_once_keys) do
645
- ::NewRelic::Agent.logger.clear_already_logged
646
- end
647
- @event_loop.fire_every(Agent.config[:data_report_period], data_harvest)
648
- @event_loop.fire_every(Agent.config[:event_report_period], event_harvest)
649
- @event_loop.fire_every(LOG_ONCE_KEYS_RESET_PERIOD, :reset_log_once_keys)
650
-
651
- @event_loop.run
652
- end
653
-
654
- # Handles the case where the server tells us to restart -
655
- # this clears the data, clears connection attempts, and
656
- # waits a while to reconnect.
657
- def handle_force_restart(error)
658
- ::NewRelic::Agent.logger.debug error.message
659
- drop_buffered_data
660
- @service.force_restart if @service
661
- @connect_state = :pending
662
- sleep 30
663
- end
664
-
665
- # when a disconnect is requested, stop the current thread, which
666
- # is the worker thread that gathers data and talks to the
667
- # server.
668
- def handle_force_disconnect(error)
669
- ::NewRelic::Agent.logger.warn "Agent received a ForceDisconnectException from the server, disconnecting. (#{error.message})"
670
- disconnect
671
- end
672
-
673
- # Handles an unknown error in the worker thread by logging
674
- # it and disconnecting the agent, since we are now in an
675
- # unknown state.
676
- def handle_other_error(error)
677
- ::NewRelic::Agent.logger.error "Unhandled error in worker thread, disconnecting."
678
- # These errors are fatal (that is, they will prevent the agent from
679
- # reporting entirely), so we really want backtraces when they happen
680
- ::NewRelic::Agent.logger.log_exception(:error, error)
681
- disconnect
682
- end
683
-
684
- # a wrapper method to handle all the errors that can happen
685
- # in the connection and worker thread system. This
686
- # guarantees a no-throw from the background thread.
687
- def catch_errors
688
- yield
689
- rescue NewRelic::Agent::ForceRestartException => e
690
- handle_force_restart(e)
691
- retry
692
- rescue NewRelic::Agent::ForceDisconnectException => e
693
- handle_force_disconnect(e)
694
- rescue => e
695
- handle_other_error(e)
696
- end
697
-
698
- # This is the method that is run in a new thread in order to
699
- # background the harvesting and sending of data during the
700
- # normal operation of the agent.
701
- #
702
- # Takes connection options that determine how we should
703
- # connect to the server, and loops endlessly - typically we
704
- # never return from this method unless we're shutting down
705
- # the agent
706
- def deferred_work!(connection_options)
707
- catch_errors do
708
- NewRelic::Agent.disable_all_tracing do
709
- connect(connection_options)
710
- if connected?
711
- create_and_run_event_loop
712
- # never reaches here unless there is a problem or
713
- # the agent is exiting
714
- else
715
- ::NewRelic::Agent.logger.debug "No connection. Worker thread ending."
716
- end
717
- end
718
- end
719
- end
720
- end
721
- include StartWorkerThread
722
-
723
- # Try to launch the worker thread and connect to the server.
724
- #
725
- # See #connect for a description of connection_options.
726
- def start_worker_thread(connection_options = {})
727
- if disable = NewRelic::Agent.config[:disable_harvest_thread]
728
- NewRelic::Agent.logger.info "Not starting Ruby Agent worker thread because :disable_harvest_thread is #{disable}"
729
- return
730
- end
731
-
732
- ::NewRelic::Agent.logger.debug "Creating Ruby Agent worker thread."
733
- @worker_thread = Threading::AgentThread.create('Worker Loop') do
734
- deferred_work!(connection_options)
735
- end
736
- end
737
-
738
319
  # A shorthand for NewRelic::Control.instance
739
320
  def control
740
321
  NewRelic::Control.instance
@@ -785,7 +366,7 @@ module NewRelic
785
366
  # to tell the user what happened, since this is not an error
786
367
  # we can handle gracefully.
787
368
  def log_error(error)
788
- ::NewRelic::Agent.logger.error "Error establishing connection with New Relic Service at #{control.server}:", error
369
+ ::NewRelic::Agent.logger.error("Error establishing connection with New Relic Service at #{control.server}:", error)
789
370
  end
790
371
 
791
372
  # When the server sends us an error with the license key, we
@@ -828,26 +409,27 @@ module NewRelic
828
409
  # connects, then configures the agent using the response from
829
410
  # the connect service
830
411
  def connect_to_server
831
- request_builder = ::NewRelic::Agent::Connect::RequestBuilder.new \
412
+ request_builder = ::NewRelic::Agent::Connect::RequestBuilder.new( \
832
413
  @service,
833
414
  Agent.config,
834
415
  event_harvest_config,
835
416
  environment_for_connect
836
- connect_response = @service.connect request_builder.connect_payload
417
+ )
418
+ connect_response = @service.connect(request_builder.connect_payload)
837
419
 
838
420
  response_handler = ::NewRelic::Agent::Connect::ResponseHandler.new(self, Agent.config)
839
421
  response_handler.configure_agent(connect_response)
840
422
 
841
- log_connection connect_response if connect_response
423
+ log_connection(connect_response) if connect_response
842
424
  connect_response
843
425
  end
844
426
 
845
427
  # Logs when we connect to the server, for debugging purposes
846
428
  # - makes sure we know if an agent has not connected
847
429
  def log_connection(config_data)
848
- ::NewRelic::Agent.logger.debug "Connected to NewRelic Service at #{@service.collector.name}"
849
- ::NewRelic::Agent.logger.debug "Agent Run = #{@service.agent_id}."
850
- ::NewRelic::Agent.logger.debug "Connection data = #{config_data.inspect}"
430
+ ::NewRelic::Agent.logger.debug("Connected to NewRelic Service at #{@service.collector.name}")
431
+ ::NewRelic::Agent.logger.debug("Agent Run = #{@service.agent_id}.")
432
+ ::NewRelic::Agent.logger.debug("Connection data = #{config_data.inspect}")
851
433
  if config_data['messages'] && config_data['messages'].any?
852
434
  log_collector_messages(config_data['messages'])
853
435
  end
@@ -911,7 +493,7 @@ module NewRelic
911
493
 
912
494
  def merge_data_for_endpoint(endpoint, data)
913
495
  if data && !data.empty?
914
- container = container_for_endpoint endpoint
496
+ container = container_for_endpoint(endpoint)
915
497
  if container.respond_to?(:has_metadata?) && container.has_metadata?
916
498
  container_for_endpoint(endpoint).merge!(data, false)
917
499
  else
@@ -943,15 +525,10 @@ module NewRelic
943
525
  # forking off from a parent process.
944
526
  #
945
527
  def connect(options = {})
946
- defaults = {
947
- :keep_retrying => Agent.config[:keep_retrying],
948
- :force_reconnect => Agent.config[:force_reconnect]
949
- }
950
- opts = defaults.merge(options)
951
-
528
+ opts = connect_options(options)
952
529
  return unless should_connect?(opts[:force_reconnect])
953
530
 
954
- ::NewRelic::Agent.logger.debug "Connecting Process to New Relic: #$0"
531
+ ::NewRelic::Agent.logger.debug("Connecting Process to New Relic: #$0")
955
532
  connect_to_server
956
533
  @connected_pid = $$
957
534
  @connect_state = :connected
@@ -963,21 +540,32 @@ module NewRelic
963
540
  rescue NewRelic::Agent::UnrecoverableAgentException => e
964
541
  handle_unrecoverable_agent_error(e)
965
542
  rescue StandardError, Timeout::Error, NewRelic::Agent::ServerConnectionException => e
543
+ retry if retry_from_error?(e, opts)
544
+ rescue Exception => e
545
+ ::NewRelic::Agent.logger.error("Exception of unexpected type during Agent#connect():", e)
546
+
547
+ raise
548
+ end
549
+
550
+ def connect_options(options)
551
+ {
552
+ keep_retrying: Agent.config[:keep_retrying],
553
+ force_reconnect: Agent.config[:force_reconnect]
554
+ }.merge(options)
555
+ end
556
+
557
+ def retry_from_error?(e, opts)
966
558
  # Allow a killed (aborting) thread to continue exiting during shutdown.
967
559
  # See: https://github.com/newrelic/newrelic-ruby-agent/issues/340
968
560
  raise if Thread.current.status == 'aborting'
969
561
 
970
562
  log_error(e)
971
- if opts[:keep_retrying]
972
- note_connect_failure
973
- ::NewRelic::Agent.logger.info "Will re-attempt in #{connect_retry_period} seconds"
974
- sleep connect_retry_period
975
- retry
976
- end
977
- rescue Exception => e
978
- ::NewRelic::Agent.logger.error "Exception of unexpected type during Agent#connect():", e
563
+ return false unless opts[:keep_retrying]
979
564
 
980
- raise
565
+ note_connect_failure
566
+ ::NewRelic::Agent.logger.info("Will re-attempt in #{connect_retry_period} seconds")
567
+ sleep(connect_retry_period)
568
+ true
981
569
  end
982
570
 
983
571
  # Delegates to the control class to determine the root
@@ -1005,14 +593,14 @@ module NewRelic
1005
593
  #
1006
594
  def harvest_and_send_from_container(container, endpoint)
1007
595
  payload = harvest_from_container(container, endpoint)
1008
- sample_count = harvest_size container, payload
596
+ sample_count = harvest_size(container, payload)
1009
597
  if sample_count > 0
1010
598
  NewRelic::Agent.logger.debug("Sending #{sample_count} items to #{endpoint}")
1011
599
  send_data_to_endpoint(endpoint, payload, container)
1012
600
  end
1013
601
  end
1014
602
 
1015
- def harvest_size container, items
603
+ def harvest_size(container, items)
1016
604
  if container.respond_to?(:has_metadata?) && container.has_metadata? && !items.empty?
1017
605
  items.last.size
1018
606
  else
@@ -1086,7 +674,7 @@ module NewRelic
1086
674
  end
1087
675
 
1088
676
  def harvest_and_send_error_event_data
1089
- harvest_and_send_from_container @error_collector.error_event_aggregator, :error_event_data
677
+ harvest_and_send_from_container(@error_collector.error_event_aggregator, :error_event_data)
1090
678
  end
1091
679
 
1092
680
  def harvest_and_send_span_event_data
@@ -1146,7 +734,7 @@ module NewRelic
1146
734
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
1147
735
 
1148
736
  msg = "Sending #{supportability_name} data to New Relic Service"
1149
- ::NewRelic::Agent.logger.debug msg
737
+ ::NewRelic::Agent.logger.debug(msg)
1150
738
 
1151
739
  @service.session do # use http keep-alive
1152
740
  self.send(harvest_method)
@@ -1158,17 +746,11 @@ module NewRelic
1158
746
 
1159
747
  def transmit_data
1160
748
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
1161
- ::NewRelic::Agent.logger.debug "Sending data to New Relic Service"
749
+ ::NewRelic::Agent.logger.debug("Sending data to New Relic Service")
1162
750
 
1163
751
  @events.notify(:before_harvest)
1164
752
  @service.session do # use http keep-alive
1165
- harvest_and_send_errors
1166
- harvest_and_send_error_event_data
1167
- harvest_and_send_transaction_traces
1168
- harvest_and_send_slowest_sql
1169
- harvest_and_send_timeslice_data
1170
- harvest_and_send_span_event_data
1171
- harvest_and_send_log_event_data
753
+ harvest_and_send_data_types
1172
754
 
1173
755
  check_for_and_handle_agent_commands
1174
756
  harvest_and_send_for_agent_commands
@@ -1192,29 +774,47 @@ module NewRelic
1192
774
  @service.request_timeout = 10
1193
775
 
1194
776
  @events.notify(:before_shutdown)
1195
- transmit_data
1196
- transmit_analytic_event_data
1197
- transmit_custom_event_data
1198
- transmit_error_event_data
1199
- transmit_span_event_data
1200
- transmit_log_event_data
1201
-
1202
- if @connected_pid == $$ && !@service.kind_of?(NewRelic::Agent::NewRelicService)
1203
- ::NewRelic::Agent.logger.debug "Sending New Relic service agent run shutdown message"
1204
- @service.shutdown
1205
- else
1206
- ::NewRelic::Agent.logger.debug "This agent connected from parent process #{@connected_pid}--not sending shutdown"
1207
- end
1208
- ::NewRelic::Agent.logger.debug "Graceful disconnect complete"
777
+ transmit_data_types
778
+ shutdown_service
779
+
780
+ ::NewRelic::Agent.logger.debug("Graceful disconnect complete")
1209
781
  rescue Timeout::Error, StandardError => e
1210
- ::NewRelic::Agent.logger.debug "Error when disconnecting #{e.class.name}: #{e.message}"
782
+ ::NewRelic::Agent.logger.debug("Error when disconnecting #{e.class.name}: #{e.message}")
1211
783
  end
1212
784
  else
1213
- ::NewRelic::Agent.logger.debug "Bypassing graceful disconnect - agent not connected"
785
+ ::NewRelic::Agent.logger.debug("Bypassing graceful disconnect - agent not connected")
1214
786
  end
1215
787
  end
1216
788
  end
1217
789
 
790
+ def shutdown_service
791
+ if @connected_pid == $$ && !@service.kind_of?(NewRelic::Agent::NewRelicService)
792
+ ::NewRelic::Agent.logger.debug("Sending New Relic service agent run shutdown message")
793
+ @service.shutdown
794
+ else
795
+ ::NewRelic::Agent.logger.debug("This agent connected from parent process #{@connected_pid}--not sending shutdown")
796
+ end
797
+ end
798
+
799
+ def transmit_data_types
800
+ transmit_data
801
+ transmit_analytic_event_data
802
+ transmit_custom_event_data
803
+ transmit_error_event_data
804
+ transmit_span_event_data
805
+ transmit_log_event_data
806
+ end
807
+
808
+ def harvest_and_send_data_types
809
+ harvest_and_send_errors
810
+ harvest_and_send_error_event_data
811
+ harvest_and_send_transaction_traces
812
+ harvest_and_send_slowest_sql
813
+ harvest_and_send_timeslice_data
814
+ harvest_and_send_span_event_data
815
+ harvest_and_send_log_event_data
816
+ end
817
+
1218
818
  extend ClassMethods
1219
819
  include InstanceMethods
1220
820
  end