newrelic_rpm 5.7.0.350 → 9.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (476) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +15 -1
  3. data/.rubocop.yml +1919 -0
  4. data/.rubocop_todo.yml +100 -0
  5. data/.simplecov +15 -0
  6. data/.snyk +11 -0
  7. data/.yardopts +2 -0
  8. data/Brewfile +12 -0
  9. data/CHANGELOG.md +4056 -2339
  10. data/CONTRIBUTING.md +132 -19
  11. data/DOCKER.md +167 -0
  12. data/Dockerfile +10 -0
  13. data/Gemfile +5 -2
  14. data/Guardfile +22 -4
  15. data/LICENSE +202 -38
  16. data/README.md +87 -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.rb +125 -969
  30. data/lib/new_relic/agent/agent_helpers/connect.rb +227 -0
  31. data/lib/new_relic/agent/agent_helpers/harvest.rb +153 -0
  32. data/lib/new_relic/agent/agent_helpers/shutdown.rb +72 -0
  33. data/lib/new_relic/agent/agent_helpers/special_startup.rb +74 -0
  34. data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +167 -0
  35. data/lib/new_relic/agent/agent_helpers/startup.rb +202 -0
  36. data/lib/new_relic/agent/agent_helpers/transmit.rb +76 -0
  37. data/lib/new_relic/agent/agent_logger.rb +26 -18
  38. data/lib/new_relic/agent/attribute_filter.rb +69 -52
  39. data/lib/new_relic/agent/attribute_processing.rb +8 -8
  40. data/lib/new_relic/agent/attributes.rb +153 -0
  41. data/lib/new_relic/agent/audit_logger.rb +19 -4
  42. data/lib/new_relic/agent/autostart.rb +34 -28
  43. data/lib/new_relic/agent/chained_call.rb +2 -2
  44. data/lib/new_relic/agent/commands/agent_command.rb +4 -4
  45. data/lib/new_relic/agent/commands/agent_command_router.rb +15 -33
  46. data/lib/new_relic/agent/commands/thread_profiler_session.rb +13 -11
  47. data/lib/new_relic/agent/configuration/default_source.rb +1480 -1053
  48. data/lib/new_relic/agent/configuration/dotted_hash.rb +7 -6
  49. data/lib/new_relic/agent/configuration/environment_source.rb +15 -11
  50. data/lib/new_relic/agent/configuration/event_harvest_config.rb +68 -0
  51. data/lib/new_relic/agent/configuration/high_security_source.rb +9 -9
  52. data/lib/new_relic/agent/configuration/manager.rb +96 -79
  53. data/lib/new_relic/agent/configuration/manual_source.rb +2 -2
  54. data/lib/new_relic/agent/configuration/mask_defaults.rb +4 -4
  55. data/lib/new_relic/agent/configuration/security_policy_source.rb +83 -86
  56. data/lib/new_relic/agent/configuration/server_source.rb +49 -12
  57. data/lib/new_relic/agent/configuration/yaml_source.rb +42 -13
  58. data/lib/new_relic/agent/configuration.rb +2 -2
  59. data/lib/new_relic/agent/connect/request_builder.rb +61 -0
  60. data/lib/new_relic/agent/connect/response_handler.rb +58 -0
  61. data/lib/new_relic/agent/custom_event_aggregator.rb +15 -15
  62. data/lib/new_relic/agent/database/explain_plan_helpers.rb +5 -6
  63. data/lib/new_relic/agent/database/obfuscation_helpers.rb +16 -15
  64. data/lib/new_relic/agent/database/obfuscator.rb +3 -3
  65. data/lib/new_relic/agent/database/postgres_explain_obfuscator.rb +4 -4
  66. data/lib/new_relic/agent/database.rb +44 -53
  67. data/lib/new_relic/agent/database_adapter.rb +35 -0
  68. data/lib/new_relic/agent/datastores/metric_helper.rb +18 -20
  69. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +9 -8
  70. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +7 -11
  71. data/lib/new_relic/agent/datastores/mongo.rb +7 -12
  72. data/lib/new_relic/agent/datastores/nosql_obfuscator.rb +41 -0
  73. data/lib/new_relic/agent/datastores/redis.rb +6 -12
  74. data/lib/new_relic/agent/datastores.rb +19 -23
  75. data/lib/new_relic/agent/deprecator.rb +2 -2
  76. data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +13 -12
  77. data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +87 -66
  78. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +84 -0
  79. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  80. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +163 -0
  81. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +38 -0
  82. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +245 -0
  83. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +127 -0
  84. data/lib/new_relic/agent/distributed_tracing.rb +113 -32
  85. data/lib/new_relic/agent/encoding_normalizer.rb +5 -3
  86. data/lib/new_relic/agent/error_collector.rb +99 -63
  87. data/lib/new_relic/agent/error_event_aggregator.rb +10 -8
  88. data/lib/new_relic/agent/error_filter.rb +174 -0
  89. data/lib/new_relic/agent/error_trace_aggregator.rb +6 -4
  90. data/lib/new_relic/agent/event_aggregator.rb +43 -48
  91. data/lib/new_relic/agent/event_buffer.rb +8 -9
  92. data/lib/new_relic/agent/event_listener.rb +2 -3
  93. data/lib/new_relic/agent/event_loop.rb +27 -25
  94. data/lib/new_relic/agent/external.rb +20 -51
  95. data/lib/new_relic/agent/guid_generator.rb +30 -0
  96. data/lib/new_relic/agent/harvester.rb +5 -6
  97. data/lib/new_relic/agent/heap.rb +8 -10
  98. data/lib/new_relic/agent/hostname.rb +26 -5
  99. data/lib/new_relic/agent/http_clients/abstract.rb +81 -0
  100. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +26 -26
  101. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +31 -17
  102. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +18 -23
  103. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +12 -15
  104. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +24 -8
  105. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +9 -12
  106. data/lib/new_relic/agent/http_clients/uri_util.rb +12 -13
  107. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +22 -52
  108. data/lib/new_relic/agent/instrumentation/action_controller_other_subscriber.rb +39 -0
  109. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +59 -72
  110. data/lib/new_relic/agent/instrumentation/action_dispatch.rb +31 -0
  111. data/lib/new_relic/agent/instrumentation/action_dispatch_subscriber.rb +64 -0
  112. data/lib/new_relic/agent/instrumentation/action_mailbox.rb +30 -0
  113. data/lib/new_relic/agent/instrumentation/action_mailbox_subscriber.rb +33 -0
  114. data/lib/new_relic/agent/instrumentation/action_mailer.rb +30 -0
  115. data/lib/new_relic/agent/instrumentation/action_mailer_subscriber.rb +85 -0
  116. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +86 -62
  117. data/lib/new_relic/agent/instrumentation/active_job.rb +38 -19
  118. data/lib/new_relic/agent/instrumentation/active_job_subscriber.rb +41 -0
  119. data/lib/new_relic/agent/instrumentation/active_merchant.rb +21 -7
  120. data/lib/new_relic/agent/instrumentation/active_record.rb +95 -46
  121. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +82 -61
  122. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +155 -0
  123. data/lib/new_relic/agent/instrumentation/active_record_prepend.rb +36 -12
  124. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +69 -64
  125. data/lib/new_relic/agent/instrumentation/active_storage.rb +8 -4
  126. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +11 -32
  127. data/lib/new_relic/agent/instrumentation/active_support.rb +27 -0
  128. data/lib/new_relic/agent/instrumentation/active_support_logger/chain.rb +23 -0
  129. data/lib/new_relic/agent/instrumentation/active_support_logger/instrumentation.rb +20 -0
  130. data/lib/new_relic/agent/instrumentation/active_support_logger/prepend.rb +12 -0
  131. data/lib/new_relic/agent/instrumentation/active_support_logger.rb +24 -0
  132. data/lib/new_relic/agent/instrumentation/active_support_subscriber.rb +41 -0
  133. data/lib/new_relic/agent/instrumentation/bunny/chain.rb +45 -0
  134. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +150 -0
  135. data/lib/new_relic/agent/instrumentation/bunny/prepend.rb +35 -0
  136. data/lib/new_relic/agent/instrumentation/bunny.rb +14 -134
  137. data/lib/new_relic/agent/instrumentation/concurrent_ruby/chain.rb +36 -0
  138. data/lib/new_relic/agent/instrumentation/concurrent_ruby/instrumentation.rb +21 -0
  139. data/lib/new_relic/agent/instrumentation/concurrent_ruby/prepend.rb +27 -0
  140. data/lib/new_relic/agent/instrumentation/concurrent_ruby.rb +31 -0
  141. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +77 -61
  142. data/lib/new_relic/agent/instrumentation/curb/chain.rb +91 -0
  143. data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +221 -0
  144. data/lib/new_relic/agent/instrumentation/curb/prepend.rb +61 -0
  145. data/lib/new_relic/agent/instrumentation/curb.rb +15 -187
  146. data/lib/new_relic/agent/instrumentation/custom_events.rb +12 -0
  147. data/lib/new_relic/agent/instrumentation/custom_events_subscriber.rb +37 -0
  148. data/lib/new_relic/agent/instrumentation/delayed_job/chain.rb +35 -0
  149. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +48 -0
  150. data/lib/new_relic/agent/instrumentation/delayed_job/prepend.rb +33 -0
  151. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +30 -52
  152. data/lib/new_relic/agent/instrumentation/elasticsearch/chain.rb +29 -0
  153. data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +66 -0
  154. data/lib/new_relic/agent/instrumentation/elasticsearch/prepend.rb +13 -0
  155. data/lib/new_relic/agent/instrumentation/elasticsearch.rb +31 -0
  156. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +8 -7
  157. data/lib/new_relic/agent/instrumentation/excon.rb +29 -31
  158. data/lib/new_relic/agent/instrumentation/fiber/chain.rb +20 -0
  159. data/lib/new_relic/agent/instrumentation/fiber/instrumentation.rb +24 -0
  160. data/lib/new_relic/agent/instrumentation/fiber/prepend.rb +18 -0
  161. data/lib/new_relic/agent/instrumentation/fiber.rb +25 -0
  162. data/lib/new_relic/agent/instrumentation/grape/chain.rb +24 -0
  163. data/lib/new_relic/agent/instrumentation/grape/instrumentation.rb +100 -0
  164. data/lib/new_relic/agent/instrumentation/grape/prepend.rb +17 -0
  165. data/lib/new_relic/agent/instrumentation/grape.rb +16 -121
  166. data/lib/new_relic/agent/instrumentation/grpc/client/chain.rb +97 -0
  167. data/lib/new_relic/agent/instrumentation/grpc/client/instrumentation.rb +89 -0
  168. data/lib/new_relic/agent/instrumentation/grpc/client/prepend.rb +111 -0
  169. data/lib/new_relic/agent/instrumentation/grpc/client/request_wrapper.rb +30 -0
  170. data/lib/new_relic/agent/instrumentation/grpc/helper.rb +32 -0
  171. data/lib/new_relic/agent/instrumentation/grpc/server/chain.rb +69 -0
  172. data/lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb +134 -0
  173. data/lib/new_relic/agent/instrumentation/grpc/server/rpc_desc_prepend.rb +35 -0
  174. data/lib/new_relic/agent/instrumentation/grpc/server/rpc_server_prepend.rb +26 -0
  175. data/lib/new_relic/agent/instrumentation/grpc_client.rb +23 -0
  176. data/lib/new_relic/agent/instrumentation/grpc_server.rb +25 -0
  177. data/lib/new_relic/agent/instrumentation/httpclient/chain.rb +24 -0
  178. data/lib/new_relic/agent/instrumentation/httpclient/instrumentation.rb +37 -0
  179. data/lib/new_relic/agent/instrumentation/httpclient/prepend.rb +15 -0
  180. data/lib/new_relic/agent/instrumentation/httpclient.rb +12 -32
  181. data/lib/new_relic/agent/instrumentation/httprb/chain.rb +22 -0
  182. data/lib/new_relic/agent/instrumentation/httprb/instrumentation.rb +30 -0
  183. data/lib/new_relic/agent/instrumentation/httprb/prepend.rb +15 -0
  184. data/lib/new_relic/agent/instrumentation/httprb.rb +29 -0
  185. data/lib/new_relic/agent/instrumentation/ignore_actions.rb +5 -6
  186. data/lib/new_relic/agent/instrumentation/logger/chain.rb +21 -0
  187. data/lib/new_relic/agent/instrumentation/logger/instrumentation.rb +66 -0
  188. data/lib/new_relic/agent/instrumentation/logger/prepend.rb +13 -0
  189. data/lib/new_relic/agent/instrumentation/logger.rb +26 -0
  190. data/lib/new_relic/agent/instrumentation/memcache/chain.rb +15 -0
  191. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +58 -125
  192. data/lib/new_relic/agent/instrumentation/memcache/helper.rb +59 -0
  193. data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +90 -0
  194. data/lib/new_relic/agent/instrumentation/memcache/prepend.rb +101 -0
  195. data/lib/new_relic/agent/instrumentation/memcache.rb +57 -71
  196. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +15 -14
  197. data/lib/new_relic/agent/instrumentation/middleware_tracing.rb +21 -14
  198. data/lib/new_relic/agent/instrumentation/mongo.rb +7 -132
  199. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +49 -13
  200. data/lib/new_relic/agent/instrumentation/net_http/chain.rb +24 -0
  201. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +40 -0
  202. data/lib/new_relic/agent/instrumentation/net_http/prepend.rb +21 -0
  203. data/lib/new_relic/agent/instrumentation/net_http.rb +44 -0
  204. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +142 -0
  205. data/lib/new_relic/agent/instrumentation/padrino/chain.rb +38 -0
  206. data/lib/new_relic/agent/instrumentation/padrino/instrumentation.rb +28 -0
  207. data/lib/new_relic/agent/instrumentation/padrino/prepend.rb +20 -0
  208. data/lib/new_relic/agent/instrumentation/padrino.rb +22 -58
  209. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +7 -7
  210. data/lib/new_relic/agent/instrumentation/queue_time.rb +9 -10
  211. data/lib/new_relic/agent/instrumentation/rack/chain.rb +66 -0
  212. data/lib/new_relic/agent/instrumentation/rack/helpers.rb +33 -0
  213. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +75 -0
  214. data/lib/new_relic/agent/instrumentation/rack/prepend.rb +43 -0
  215. data/lib/new_relic/agent/instrumentation/rack.rb +33 -141
  216. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +19 -55
  217. data/lib/new_relic/agent/instrumentation/rails_middleware.rb +5 -5
  218. data/lib/new_relic/agent/instrumentation/rails_notifications/action_cable.rb +36 -0
  219. data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +45 -0
  220. data/lib/new_relic/agent/instrumentation/rails_notifications/action_view.rb +30 -0
  221. data/lib/new_relic/agent/instrumentation/rails_notifications/custom_events.rb +30 -0
  222. data/lib/new_relic/agent/instrumentation/rake/chain.rb +20 -0
  223. data/lib/new_relic/agent/instrumentation/rake/instrumentation.rb +142 -0
  224. data/lib/new_relic/agent/instrumentation/rake/prepend.rb +14 -0
  225. data/lib/new_relic/agent/instrumentation/rake.rb +18 -159
  226. data/lib/new_relic/agent/instrumentation/redis/chain.rb +45 -0
  227. data/lib/new_relic/agent/instrumentation/redis/constants.rb +17 -0
  228. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +93 -0
  229. data/lib/new_relic/agent/instrumentation/redis/middleware.rb +16 -0
  230. data/lib/new_relic/agent/instrumentation/redis/prepend.rb +29 -0
  231. data/lib/new_relic/agent/instrumentation/redis.rb +20 -103
  232. data/lib/new_relic/agent/instrumentation/resque/chain.rb +21 -0
  233. data/lib/new_relic/agent/instrumentation/resque/helper.rb +19 -0
  234. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +34 -0
  235. data/lib/new_relic/agent/instrumentation/resque/prepend.rb +15 -0
  236. data/lib/new_relic/agent/instrumentation/resque.rb +33 -41
  237. data/lib/new_relic/agent/instrumentation/sequel.rb +17 -20
  238. data/lib/new_relic/agent/instrumentation/sequel_helper.rb +3 -3
  239. data/lib/new_relic/agent/instrumentation/sidekiq/client.rb +20 -0
  240. data/lib/new_relic/agent/instrumentation/sidekiq/extensions/delayed_class.rb +30 -0
  241. data/lib/new_relic/agent/instrumentation/sidekiq/server.rb +37 -0
  242. data/lib/new_relic/agent/instrumentation/sidekiq.rb +29 -46
  243. data/lib/new_relic/agent/instrumentation/sinatra/chain.rb +55 -0
  244. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +31 -37
  245. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +125 -0
  246. data/lib/new_relic/agent/instrumentation/sinatra/prepend.rb +33 -0
  247. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +3 -3
  248. data/lib/new_relic/agent/instrumentation/sinatra.rb +35 -165
  249. data/lib/new_relic/agent/instrumentation/thread/chain.rb +24 -0
  250. data/lib/new_relic/agent/instrumentation/thread/instrumentation.rb +28 -0
  251. data/lib/new_relic/agent/instrumentation/thread/prepend.rb +22 -0
  252. data/lib/new_relic/agent/instrumentation/thread.rb +20 -0
  253. data/lib/new_relic/agent/instrumentation/tilt/chain.rb +24 -0
  254. data/lib/new_relic/agent/instrumentation/tilt/instrumentation.rb +41 -0
  255. data/lib/new_relic/agent/instrumentation/tilt/prepend.rb +13 -0
  256. data/lib/new_relic/agent/instrumentation/tilt.rb +25 -0
  257. data/lib/new_relic/agent/instrumentation/typhoeus/chain.rb +22 -0
  258. data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +80 -0
  259. data/lib/new_relic/agent/instrumentation/typhoeus/prepend.rb +14 -0
  260. data/lib/new_relic/agent/instrumentation/typhoeus.rb +14 -76
  261. data/lib/new_relic/agent/instrumentation.rb +2 -2
  262. data/lib/new_relic/agent/internal_agent_error.rb +3 -3
  263. data/lib/new_relic/agent/javascript_instrumentor.rb +51 -45
  264. data/lib/new_relic/agent/linking_metadata.rb +44 -0
  265. data/lib/new_relic/agent/local_log_decorator.rb +37 -0
  266. data/lib/new_relic/agent/log_event_aggregator.rb +235 -0
  267. data/lib/new_relic/agent/log_once.rb +2 -2
  268. data/lib/new_relic/agent/log_priority.rb +20 -0
  269. data/lib/new_relic/agent/logging.rb +142 -0
  270. data/lib/new_relic/agent/memory_logger.rb +3 -3
  271. data/lib/new_relic/agent/messaging.rb +81 -164
  272. data/lib/new_relic/agent/method_tracer.rb +152 -145
  273. data/lib/new_relic/agent/method_tracer_helpers.rb +90 -13
  274. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +117 -0
  275. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +28 -0
  276. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +5 -6
  277. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +9 -15
  278. data/lib/new_relic/agent/monitors.rb +26 -0
  279. data/lib/new_relic/agent/new_relic_service/encoders.rb +7 -7
  280. data/lib/new_relic/agent/new_relic_service/json_marshaller.rb +6 -7
  281. data/lib/new_relic/agent/new_relic_service/marshaller.rb +8 -29
  282. data/lib/new_relic/agent/new_relic_service/security_policy_settings.rb +5 -5
  283. data/lib/new_relic/agent/new_relic_service.rb +282 -166
  284. data/lib/new_relic/agent/noticeable_error.rb +19 -0
  285. data/lib/new_relic/agent/null_logger.rb +8 -4
  286. data/lib/new_relic/agent/obfuscator.rb +9 -11
  287. data/lib/new_relic/agent/parameter_filtering.rb +35 -8
  288. data/lib/new_relic/agent/payload_metric_mapping.rb +10 -11
  289. data/lib/new_relic/agent/pipe_channel_manager.rb +28 -18
  290. data/lib/new_relic/agent/pipe_service.rb +9 -6
  291. data/lib/new_relic/agent/prepend_supportability.rb +3 -3
  292. data/lib/new_relic/agent/priority_sampled_buffer.rb +16 -14
  293. data/lib/new_relic/agent/range_extensions.rb +9 -29
  294. data/lib/new_relic/agent/rules_engine/replacement_rule.rb +12 -12
  295. data/lib/new_relic/agent/rules_engine/segment_terms_rule.rb +13 -14
  296. data/lib/new_relic/agent/rules_engine.rb +6 -5
  297. data/lib/new_relic/agent/sampler.rb +4 -5
  298. data/lib/new_relic/agent/sampler_collection.rb +4 -5
  299. data/lib/new_relic/agent/samplers/cpu_sampler.rb +4 -3
  300. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +14 -11
  301. data/lib/new_relic/agent/samplers/memory_sampler.rb +26 -15
  302. data/lib/new_relic/agent/samplers/object_sampler.rb +2 -2
  303. data/lib/new_relic/agent/samplers/vm_sampler.rb +22 -20
  304. data/lib/new_relic/agent/span_event_aggregator.rb +14 -16
  305. data/lib/new_relic/agent/span_event_primitive.rb +118 -58
  306. data/lib/new_relic/agent/sql_sampler.rb +25 -25
  307. data/lib/new_relic/agent/stats.rb +79 -42
  308. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +11 -13
  309. data/lib/new_relic/agent/stats_engine/stats_hash.rb +13 -14
  310. data/lib/new_relic/agent/stats_engine.rb +11 -11
  311. data/lib/new_relic/agent/synthetics_event_aggregator.rb +8 -9
  312. data/lib/new_relic/agent/system_info.rb +100 -66
  313. data/lib/new_relic/agent/threading/agent_thread.rb +20 -16
  314. data/lib/new_relic/agent/threading/backtrace_node.rb +13 -14
  315. data/lib/new_relic/agent/threading/backtrace_service.rb +18 -18
  316. data/lib/new_relic/agent/threading/thread_profile.rb +31 -45
  317. data/lib/new_relic/agent/timestamp_sampled_buffer.rb +2 -2
  318. data/lib/new_relic/agent/tracer.rb +513 -0
  319. data/lib/new_relic/agent/transaction/abstract_segment.rb +131 -41
  320. data/lib/new_relic/agent/transaction/datastore_segment.rb +22 -18
  321. data/lib/new_relic/agent/transaction/distributed_tracer.rb +184 -0
  322. data/lib/new_relic/agent/transaction/distributed_tracing.rb +72 -163
  323. data/lib/new_relic/agent/transaction/external_request_segment.rb +66 -63
  324. data/lib/new_relic/agent/transaction/message_broker_segment.rb +34 -46
  325. data/lib/new_relic/agent/transaction/request_attributes.rb +36 -36
  326. data/lib/new_relic/agent/transaction/segment.rb +46 -10
  327. data/lib/new_relic/agent/transaction/slowest_sample_buffer.rb +2 -4
  328. data/lib/new_relic/agent/transaction/synthetics_sample_buffer.rb +2 -2
  329. data/lib/new_relic/agent/transaction/trace.rb +21 -24
  330. data/lib/new_relic/agent/transaction/trace_builder.rb +11 -12
  331. data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
  332. data/lib/new_relic/agent/transaction/trace_node.rb +31 -28
  333. data/lib/new_relic/agent/transaction/tracing.rb +15 -111
  334. data/lib/new_relic/agent/transaction/transaction_sample_buffer.rb +6 -6
  335. data/lib/new_relic/agent/transaction.rb +252 -259
  336. data/lib/new_relic/agent/transaction_error_primitive.rb +34 -37
  337. data/lib/new_relic/agent/transaction_event_aggregator.rb +13 -13
  338. data/lib/new_relic/agent/transaction_event_primitive.rb +44 -56
  339. data/lib/new_relic/agent/transaction_event_recorder.rb +17 -16
  340. data/lib/new_relic/agent/transaction_metrics.rb +11 -10
  341. data/lib/new_relic/agent/transaction_sampler.rb +7 -12
  342. data/lib/new_relic/agent/transaction_time_aggregator.rb +41 -26
  343. data/lib/new_relic/agent/utilization/aws.rb +34 -4
  344. data/lib/new_relic/agent/utilization/azure.rb +4 -4
  345. data/lib/new_relic/agent/utilization/gcp.rb +8 -8
  346. data/lib/new_relic/agent/utilization/pcf.rb +6 -5
  347. data/lib/new_relic/agent/utilization/vendor.rb +44 -29
  348. data/lib/new_relic/agent/utilization_data.rb +43 -6
  349. data/lib/new_relic/agent/vm/jruby_vm.rb +2 -2
  350. data/lib/new_relic/agent/vm/monotonic_gc_profiler.rb +3 -3
  351. data/lib/new_relic/agent/vm/mri_vm.rb +46 -19
  352. data/lib/new_relic/agent/vm/snapshot.rb +6 -6
  353. data/lib/new_relic/agent/vm.rb +2 -2
  354. data/lib/new_relic/agent/worker_loop.rb +11 -13
  355. data/lib/new_relic/agent.rb +151 -79
  356. data/lib/new_relic/cli/command.rb +21 -23
  357. data/lib/new_relic/cli/commands/deployments.rb +94 -45
  358. data/lib/new_relic/cli/commands/install.rb +24 -26
  359. data/lib/new_relic/coerce.rb +42 -15
  360. data/lib/new_relic/collection_helper.rb +51 -49
  361. data/lib/new_relic/constants.rb +39 -0
  362. data/lib/new_relic/control/class_methods.rb +11 -5
  363. data/lib/new_relic/control/frameworks/external.rb +3 -3
  364. data/lib/new_relic/control/frameworks/rails.rb +24 -18
  365. data/lib/new_relic/control/frameworks/rails3.rb +4 -5
  366. data/lib/new_relic/control/frameworks/rails4.rb +2 -2
  367. data/lib/new_relic/control/frameworks/rails_notifications.rb +14 -0
  368. data/lib/new_relic/control/frameworks/ruby.rb +4 -4
  369. data/lib/new_relic/control/frameworks/sinatra.rb +8 -2
  370. data/lib/new_relic/control/frameworks.rb +2 -2
  371. data/lib/new_relic/control/instance_methods.rb +33 -42
  372. data/lib/new_relic/control/instrumentation.rb +40 -12
  373. data/lib/new_relic/control/private_instance_methods.rb +48 -0
  374. data/lib/new_relic/control/server_methods.rb +4 -5
  375. data/lib/new_relic/control.rb +2 -3
  376. data/lib/new_relic/delayed_job_injection.rb +2 -2
  377. data/lib/new_relic/dependency_detection.rb +129 -18
  378. data/lib/new_relic/environment_report.rb +41 -35
  379. data/lib/new_relic/helper.rb +49 -8
  380. data/lib/new_relic/language_support.rb +30 -6
  381. data/lib/new_relic/latest_changes.rb +9 -8
  382. data/lib/new_relic/local_environment.rb +23 -27
  383. data/lib/new_relic/metric_data.rb +32 -27
  384. data/lib/new_relic/metric_spec.rb +9 -7
  385. data/lib/new_relic/noticed_error.rb +46 -33
  386. data/lib/new_relic/rack/agent_hooks.rb +2 -2
  387. data/lib/new_relic/rack/agent_middleware.rb +7 -5
  388. data/lib/new_relic/rack/browser_monitoring.rb +134 -117
  389. data/lib/new_relic/rack.rb +2 -2
  390. data/lib/new_relic/recipes/capistrano3.rb +4 -62
  391. data/lib/new_relic/recipes/capistrano_legacy.rb +24 -27
  392. data/lib/new_relic/recipes/helpers/send_deployment.rb +70 -0
  393. data/lib/new_relic/recipes.rb +2 -2
  394. data/lib/new_relic/supportability_helper.rb +21 -7
  395. data/lib/new_relic/traced_thread.rb +39 -0
  396. data/lib/new_relic/version.rb +7 -18
  397. data/lib/newrelic_rpm.rb +20 -33
  398. data/lib/sequel/extensions/{newrelic_instrumentation.rb → new_relic_instrumentation.rb} +16 -19
  399. data/lib/sequel/plugins/{newrelic_instrumentation.rb → new_relic_instrumentation.rb} +9 -15
  400. data/lib/tasks/all.rb +4 -4
  401. data/lib/tasks/config.rake +22 -118
  402. data/lib/tasks/coverage_report.rake +28 -0
  403. data/lib/tasks/helpers/config.html.erb +21 -0
  404. data/lib/tasks/helpers/format.rb +123 -0
  405. data/lib/tasks/helpers/matches.rb +12 -0
  406. data/lib/tasks/helpers/prompt.rb +24 -0
  407. data/lib/tasks/helpers/removers.rb +33 -0
  408. data/lib/tasks/install.rake +4 -0
  409. data/lib/tasks/instrumentation_generator/README.md +63 -0
  410. data/lib/tasks/instrumentation_generator/TODO.md +33 -0
  411. data/lib/tasks/instrumentation_generator/instrumentation.thor +121 -0
  412. data/lib/tasks/instrumentation_generator/templates/Envfile.tt +9 -0
  413. data/lib/tasks/instrumentation_generator/templates/chain.tt +22 -0
  414. data/lib/tasks/instrumentation_generator/templates/chain_method.tt +8 -0
  415. data/lib/tasks/instrumentation_generator/templates/dependency_detection.tt +29 -0
  416. data/lib/tasks/instrumentation_generator/templates/instrumentation.tt +13 -0
  417. data/lib/tasks/instrumentation_generator/templates/instrumentation_method.tt +3 -0
  418. data/lib/tasks/instrumentation_generator/templates/newrelic.yml.tt +19 -0
  419. data/lib/tasks/instrumentation_generator/templates/prepend.tt +13 -0
  420. data/lib/tasks/instrumentation_generator/templates/prepend_method.tt +3 -0
  421. data/lib/tasks/instrumentation_generator/templates/test.tt +15 -0
  422. data/lib/tasks/multiverse.rake +4 -0
  423. data/lib/tasks/multiverse.rb +12 -5
  424. data/lib/tasks/newrelic.rb +2 -2
  425. data/lib/tasks/tests.rake +14 -14
  426. data/newrelic.yml +672 -3
  427. data/newrelic_rpm.gemspec +40 -31
  428. data/recipes/newrelic.rb +3 -3
  429. data/test/agent_helper.rb +419 -98
  430. metadata +238 -127
  431. data/.travis.yml +0 -228
  432. data/bin/mongrel_rpm +0 -33
  433. data/cert/cacert.pem +0 -1177
  434. data/lib/new_relic/agent/commands/xray_session.rb +0 -55
  435. data/lib/new_relic/agent/commands/xray_session_collection.rb +0 -161
  436. data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
  437. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +0 -44
  438. data/lib/new_relic/agent/datastores/mongo/statement_formatter.rb +0 -53
  439. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -41
  440. data/lib/new_relic/agent/distributed_trace_payload.rb +0 -246
  441. data/lib/new_relic/agent/http_clients/abstract_request.rb +0 -31
  442. data/lib/new_relic/agent/instrumentation/active_record_4.rb +0 -42
  443. data/lib/new_relic/agent/instrumentation/active_record_5.rb +0 -41
  444. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +0 -74
  445. data/lib/new_relic/agent/instrumentation/authlogic.rb +0 -25
  446. data/lib/new_relic/agent/instrumentation/data_mapper.rb +0 -202
  447. data/lib/new_relic/agent/instrumentation/evented_subscriber.rb +0 -104
  448. data/lib/new_relic/agent/instrumentation/excon/connection.rb +0 -46
  449. data/lib/new_relic/agent/instrumentation/http.rb +0 -46
  450. data/lib/new_relic/agent/instrumentation/merb/controller.rb +0 -44
  451. data/lib/new_relic/agent/instrumentation/merb/errors.rb +0 -33
  452. data/lib/new_relic/agent/instrumentation/net.rb +0 -50
  453. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +0 -125
  454. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +0 -46
  455. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +0 -32
  456. data/lib/new_relic/agent/instrumentation/rails4/action_view.rb +0 -27
  457. data/lib/new_relic/agent/instrumentation/rails5/action_cable.rb +0 -36
  458. data/lib/new_relic/agent/instrumentation/rails5/action_controller.rb +0 -33
  459. data/lib/new_relic/agent/instrumentation/rails5/action_view.rb +0 -27
  460. data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +0 -26
  461. data/lib/new_relic/agent/instrumentation/sunspot.rb +0 -33
  462. data/lib/new_relic/agent/supported_versions.rb +0 -275
  463. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  464. data/lib/new_relic/agent/transaction/xray_sample_buffer.rb +0 -64
  465. data/lib/new_relic/agent/transaction_state.rb +0 -186
  466. data/lib/new_relic/build.rb +0 -2
  467. data/lib/new_relic/control/frameworks/merb.rb +0 -29
  468. data/lib/new_relic/control/frameworks/rails5.rb +0 -14
  469. data/lib/new_relic/metrics.rb +0 -13
  470. data/lib/tasks/config.html.erb +0 -32
  471. data/lib/tasks/versions.html.erb +0 -28
  472. data/lib/tasks/versions.postface.html +0 -8
  473. data/lib/tasks/versions.preface.html +0 -9
  474. data/lib/tasks/versions.rake +0 -65
  475. data/lib/tasks/versions.txt.erb +0 -14
  476. /data/lib/tasks/{config.text.erb → helpers/config.text.erb} +0 -0
@@ -1,6 +1,6 @@
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
  require 'socket'
6
6
  require 'net/https'
@@ -8,6 +8,9 @@ require 'net/http'
8
8
  require 'logger'
9
9
  require 'zlib'
10
10
  require 'stringio'
11
+ require 'new_relic/constants'
12
+ require 'new_relic/traced_thread'
13
+ require 'new_relic/coerce'
11
14
  require 'new_relic/agent/autostart'
12
15
  require 'new_relic/agent/harvester'
13
16
  require 'new_relic/agent/hostname'
@@ -15,14 +18,15 @@ require 'new_relic/agent/new_relic_service'
15
18
  require 'new_relic/agent/pipe_service'
16
19
  require 'new_relic/agent/configuration/manager'
17
20
  require 'new_relic/agent/database'
21
+ require 'new_relic/agent/instrumentation/resque/helper'
18
22
  require 'new_relic/agent/commands/agent_command_router'
19
23
  require 'new_relic/agent/event_listener'
20
- require 'new_relic/agent/cross_app_monitor'
21
- require 'new_relic/agent/distributed_trace_monitor'
22
- require 'new_relic/agent/synthetics_monitor'
24
+ require 'new_relic/agent/distributed_tracing'
25
+ require 'new_relic/agent/monitors'
23
26
  require 'new_relic/agent/transaction_event_recorder'
24
27
  require 'new_relic/agent/custom_event_aggregator'
25
28
  require 'new_relic/agent/span_event_aggregator'
29
+ require 'new_relic/agent/log_event_aggregator'
26
30
  require 'new_relic/agent/sampler_collection'
27
31
  require 'new_relic/agent/javascript_instrumentor'
28
32
  require 'new_relic/agent/vm/monotonic_gc_profiler'
@@ -30,10 +34,19 @@ require 'new_relic/agent/utilization_data'
30
34
  require 'new_relic/environment_report'
31
35
  require 'new_relic/agent/attribute_filter'
32
36
  require 'new_relic/agent/adaptive_sampler'
37
+ require 'new_relic/agent/connect/request_builder'
38
+ require 'new_relic/agent/connect/response_handler'
39
+
40
+ require 'new_relic/agent/agent_helpers/connect'
41
+ require 'new_relic/agent/agent_helpers/harvest'
42
+ require 'new_relic/agent/agent_helpers/start_worker_thread'
43
+ require 'new_relic/agent/agent_helpers/startup'
44
+ require 'new_relic/agent/agent_helpers/special_startup'
45
+ require 'new_relic/agent/agent_helpers/shutdown'
46
+ require 'new_relic/agent/agent_helpers/transmit'
33
47
 
34
48
  module NewRelic
35
49
  module Agent
36
-
37
50
  # The Agent is a singleton that is instantiated when the plugin is
38
51
  # activated. It collects performance data from ruby applications
39
52
  # in realtime as the application runs, and periodically sends that
@@ -43,57 +56,73 @@ module NewRelic
43
56
  ::NewRelic::Agent.config
44
57
  end
45
58
 
46
- def initialize
47
- @started = false
48
- @event_loop = nil
49
-
50
- @service = NewRelicService.new
51
-
52
- @events = NewRelic::Agent::EventListener.new
53
- @stats_engine = NewRelic::Agent::StatsEngine.new
54
- @transaction_sampler = NewRelic::Agent::TransactionSampler.new
55
- @sql_sampler = NewRelic::Agent::SqlSampler.new
56
- @agent_command_router = NewRelic::Agent::Commands::AgentCommandRouter.new(@events)
57
- @cross_app_monitor = NewRelic::Agent::CrossAppMonitor.new(@events)
58
- @distributed_trace_monitor = NewRelic::Agent::DistributedTraceMonitor.new(@events)
59
- @synthetics_monitor = NewRelic::Agent::SyntheticsMonitor.new(@events)
60
- @error_collector = NewRelic::Agent::ErrorCollector.new
61
- @transaction_rules = NewRelic::Agent::RulesEngine.new
62
- @harvest_samplers = NewRelic::Agent::SamplerCollection.new(@events)
63
- @monotonic_gc_profiler = NewRelic::Agent::VM::MonotonicGCProfiler.new
64
- @javascript_instrumentor = NewRelic::Agent::JavascriptInstrumentor.new(@events)
65
- @adaptive_sampler = NewRelic::Agent::AdaptiveSampler.new(self.class.config[:sampling_target],
66
- self.class.config[:sampling_target_period_in_seconds])
67
-
68
- @harvester = NewRelic::Agent::Harvester.new(@events)
69
- @after_fork_lock = Mutex.new
59
+ include NewRelic::Agent::AgentHelpers::Connect
60
+ include NewRelic::Agent::AgentHelpers::Harvest
61
+ include NewRelic::Agent::AgentHelpers::StartWorkerThread
62
+ include NewRelic::Agent::AgentHelpers::SpecialStartup
63
+ include NewRelic::Agent::AgentHelpers::Startup
64
+ include NewRelic::Agent::AgentHelpers::Shutdown
65
+ include NewRelic::Agent::AgentHelpers::Transmit
70
66
 
71
- @transaction_event_recorder = NewRelic::Agent::TransactionEventRecorder.new
72
- @custom_event_aggregator = NewRelic::Agent::CustomEventAggregator.new
73
- @span_event_aggregator = NewRelic::Agent::SpanEventAggregator.new
67
+ def initialize
68
+ init_basics
69
+ init_components
70
+ init_event_handlers
71
+ setup_attribute_filter
72
+ end
74
73
 
75
- @connect_state = :pending
76
- @connect_attempts = 0
77
- @environment_report = nil
78
- @waited_on_connect = nil
79
- @connected_pid = nil
74
+ private
80
75
 
76
+ def init_basics
77
+ @started = false
78
+ @event_loop = nil
79
+ @worker_thread = nil
80
+ @connect_state = :pending
81
+ @connect_attempts = 0
82
+ @waited_on_connect = nil
83
+ @connected_pid = nil
81
84
  @wait_on_connect_mutex = Mutex.new
85
+ @after_fork_lock = Mutex.new
82
86
  @wait_on_connect_condition = ConditionVariable.new
87
+ end
83
88
 
84
- setup_attribute_filter
89
+ def init_components
90
+ @service = NewRelicService.new
91
+ @events = EventListener.new
92
+ @stats_engine = StatsEngine.new
93
+ @transaction_sampler = TransactionSampler.new
94
+ @sql_sampler = SqlSampler.new
95
+ @transaction_rules = RulesEngine.new
96
+ @monotonic_gc_profiler = VM::MonotonicGCProfiler.new
97
+ @adaptive_sampler = AdaptiveSampler.new(Agent.config[:sampling_target],
98
+ Agent.config[:sampling_target_period_in_seconds])
99
+ end
100
+
101
+ def init_event_handlers
102
+ @agent_command_router = Commands::AgentCommandRouter.new(@events)
103
+ @monitors = Monitors.new(@events)
104
+ @error_collector = ErrorCollector.new(@events)
105
+ @harvest_samplers = SamplerCollection.new(@events)
106
+ @javascript_instrumentor = JavaScriptInstrumentor.new(@events)
107
+ @harvester = Harvester.new(@events)
108
+ @transaction_event_recorder = TransactionEventRecorder.new(@events)
109
+ @custom_event_aggregator = CustomEventAggregator.new(@events)
110
+ @span_event_aggregator = SpanEventAggregator.new(@events)
111
+ @log_event_aggregator = LogEventAggregator.new(@events)
85
112
  end
86
113
 
87
114
  def setup_attribute_filter
88
115
  refresh_attribute_filter
89
116
 
90
- @events.subscribe(:finished_configuring) do
117
+ @events.subscribe(:initial_configuration_complete) do
91
118
  refresh_attribute_filter
92
119
  end
93
120
  end
94
121
 
122
+ public
123
+
95
124
  def refresh_attribute_filter
96
- @attribute_filter = NewRelic::Agent::AttributeFilter.new(NewRelic::Agent.config)
125
+ @attribute_filter = AttributeFilter.new(Agent.config)
97
126
  end
98
127
 
99
128
  # contains all the class-level methods for NewRelic::Agent::Agent
@@ -108,7 +137,6 @@ module NewRelic
108
137
  # Holds all the methods defined on NewRelic::Agent::Agent
109
138
  # instances
110
139
  module InstanceMethods
111
-
112
140
  # the statistics engine that holds all the timeslice data
113
141
  attr_reader :stats_engine
114
142
  # the transaction sampler that handles recording transactions
@@ -126,26 +154,31 @@ module NewRelic
126
154
  # cross application tracing ids and encoding
127
155
  attr_reader :cross_process_id
128
156
  attr_reader :cross_app_encoding_bytes
129
- attr_reader :cross_app_monitor
130
157
  # service for communicating with collector
131
158
  attr_accessor :service
132
159
  # Global events dispatcher. This will provides our primary mechanism
133
160
  # for agent-wide events, such as finishing configuration, error notification
134
161
  # and request before/after from Rack.
135
162
  attr_reader :events
163
+
164
+ # listens and responds to events that need to process headers
165
+ # for synthetics and distributed tracing
166
+ attr_reader :monitors
136
167
  # Transaction and metric renaming rules as provided by the
137
168
  # collector on connect. The former are applied during txns,
138
169
  # the latter during harvest.
139
- attr_reader :transaction_rules
140
- # Responsbile for restarting the harvest thread
170
+ attr_accessor :transaction_rules
171
+ # Responsible for restarting the harvest thread
141
172
  attr_reader :harvester
142
173
  # GC::Profiler.total_time is not monotonic so we wrap it.
143
174
  attr_reader :monotonic_gc_profiler
144
175
  attr_reader :custom_event_aggregator
145
176
  attr_reader :span_event_aggregator
177
+ attr_reader :log_event_aggregator
146
178
  attr_reader :transaction_event_recorder
147
179
  attr_reader :attribute_filter
148
180
  attr_reader :adaptive_sampler
181
+ attr_reader :environment_report
149
182
 
150
183
  def transaction_event_aggregator
151
184
  @transaction_event_recorder.transaction_event_aggregator
@@ -155,11 +188,15 @@ module NewRelic
155
188
  @transaction_event_recorder.synthetics_event_aggregator
156
189
  end
157
190
 
191
+ def agent_id=(agent_id)
192
+ @service.agent_id = agent_id
193
+ end
194
+
158
195
  # This method should be called in a forked process after a fork.
159
196
  # It assumes the parent process initialized the agent, but does
160
197
  # not assume the agent started.
161
198
  #
162
- # The call is idempotent, but not re-entrant.
199
+ # The call is idempotent, but not reentrant.
163
200
  #
164
201
  # * It clears any metrics carried over from the parent process
165
202
  # * Restarts the sampler thread if necessary
@@ -174,69 +211,51 @@ module NewRelic
174
211
  # * <tt>:keep_retrying => false</tt> if we try to initiate a new
175
212
  # connection, this tells me to only try it once so this method returns
176
213
  # quickly if there is some kind of latency with the server.
177
- def after_fork(options={})
214
+ def after_fork(options = {})
215
+ return unless needs_after_fork_work?
216
+
217
+ ::NewRelic::Agent.logger.debug("Starting the worker thread in #{Process.pid} (parent #{Process.ppid}) after forking.")
218
+
219
+ channel_id = options[:report_to_channel]
220
+ install_pipe_service(channel_id) if channel_id
221
+
222
+ # Clear out locks and stats left over from parent process
223
+ reset_objects_with_locks
224
+ drop_buffered_data
225
+
226
+ setup_and_start_agent(options)
227
+ end
228
+
229
+ def needs_after_fork_work?
178
230
  needs_restart = false
179
231
  @after_fork_lock.synchronize do
180
232
  needs_restart = @harvester.needs_restart?
181
233
  @harvester.mark_started
182
234
  end
183
235
 
184
- return if !needs_restart ||
236
+ return false if !needs_restart ||
185
237
  !Agent.config[:agent_enabled] ||
186
238
  !Agent.config[:monitor_mode] ||
187
239
  disconnected? ||
188
240
  !control.security_settings_valid?
189
241
 
190
- ::NewRelic::Agent.logger.debug "Starting the worker thread in #{Process.pid} (parent #{Process.ppid}) after forking."
191
-
192
- channel_id = options[:report_to_channel]
193
- install_pipe_service(channel_id) if channel_id
194
-
195
- # Clear out locks and stats left over from parent process
196
- reset_objects_with_locks
197
- drop_buffered_data
198
-
199
- setup_and_start_agent(options)
242
+ true
200
243
  end
201
244
 
202
245
  def install_pipe_service(channel_id)
203
- @service = NewRelic::Agent::PipeService.new(channel_id)
246
+ @service = PipeService.new(channel_id)
204
247
  if connected?
205
248
  @connected_pid = Process.pid
206
249
  else
207
250
  ::NewRelic::Agent.logger.debug("Child process #{Process.pid} not reporting to non-connected parent (process #{Process.ppid}).")
208
- @service.shutdown(Time.now)
251
+ @service.shutdown
209
252
  disconnect
210
253
  end
211
254
  end
212
255
 
213
- # True if we have initialized and completed 'start'
214
- def started?
215
- @started
216
- end
217
-
218
- # Attempt a graceful shutdown of the agent, flushing any remaining
219
- # data.
220
- def shutdown
221
- return unless started?
222
- ::NewRelic::Agent.logger.info "Starting Agent shutdown"
223
-
224
- stop_event_loop
225
- trap_signals_for_litespeed
226
- untraced_graceful_disconnect
227
- revert_to_default_configuration
228
-
229
- @started = nil
230
- Control.reset
231
- end
232
-
233
256
  def revert_to_default_configuration
234
- NewRelic::Agent.config.remove_config_type(:manual)
235
- NewRelic::Agent.config.remove_config_type(:server)
236
- end
237
-
238
- def stop_event_loop
239
- @event_loop.stop if @event_loop
257
+ Agent.config.remove_config_type(:manual)
258
+ Agent.config.remove_config_type(:server)
240
259
  end
241
260
 
242
261
  def trap_signals_for_litespeed
@@ -248,21 +267,11 @@ module NewRelic
248
267
  end
249
268
  end
250
269
 
251
- def untraced_graceful_disconnect
252
- begin
253
- NewRelic::Agent.disable_all_tracing do
254
- graceful_disconnect
255
- end
256
- rescue => e
257
- ::NewRelic::Agent.logger.error e
258
- end
259
- end
260
-
261
270
  # Sets a thread local variable as to whether we should or
262
271
  # should not record sql in the current thread. Returns the
263
272
  # previous value, if there is one
264
- def set_record_sql(should_record) #THREAD_LOCAL_ACCESS
265
- state = TransactionState.tl_get
273
+ def set_record_sql(should_record) # THREAD_LOCAL_ACCESS
274
+ state = Tracer.state
266
275
  prev = state.record_sql
267
276
  state.record_sql = should_record
268
277
  prev.nil? || prev
@@ -272,264 +281,14 @@ module NewRelic
272
281
  # thread. This uses a stack which allows us to disable tracing
273
282
  # children of a transaction without affecting the tracing of
274
283
  # the whole transaction
275
- def push_trace_execution_flag(should_trace=false) #THREAD_LOCAL_ACCESS
276
- TransactionState.tl_get.push_traced(should_trace)
284
+ def push_trace_execution_flag(should_trace = false) # THREAD_LOCAL_ACCESS
285
+ Tracer.state.push_traced(should_trace)
277
286
  end
278
287
 
279
288
  # Pop the current trace execution status. Restore trace execution status
280
289
  # to what it was before we pushed the current flag.
281
- def pop_trace_execution_flag #THREAD_LOCAL_ACCESS
282
- TransactionState.tl_get.pop_traced
283
- end
284
-
285
- # Herein lies the corpse of the former 'start' method. May
286
- # its unmatched flog score rest in pieces.
287
- module Start
288
- # Check whether we have already started, which is an error condition
289
- def already_started?
290
- if started?
291
- ::NewRelic::Agent.logger.error("Agent Started Already!")
292
- true
293
- end
294
- end
295
-
296
- # The agent is disabled when it is not force enabled by the
297
- # 'agent_enabled' option (e.g. in a manual start), or
298
- # enabled normally through the configuration file
299
- def disabled?
300
- !Agent.config[:agent_enabled]
301
- end
302
-
303
- # Log startup information that we almost always want to know
304
- def log_startup
305
- log_environment
306
- log_dispatcher
307
- log_app_name
308
- end
309
-
310
- # Log the environment the app thinks it's running in.
311
- # Useful in debugging, as this is the key for config YAML lookups.
312
- def log_environment
313
- ::NewRelic::Agent.logger.info "Environment: #{NewRelic::Control.instance.env}"
314
- end
315
-
316
- # Logs the dispatcher to the log file to assist with
317
- # debugging. When no debugger is present, logs this fact to
318
- # assist with proper dispatcher detection
319
- def log_dispatcher
320
- dispatcher_name = Agent.config[:dispatcher].to_s
321
-
322
- if dispatcher_name.empty?
323
- ::NewRelic::Agent.logger.info 'No known dispatcher detected.'
324
- else
325
- ::NewRelic::Agent.logger.info "Dispatcher: #{dispatcher_name}"
326
- end
327
- end
328
-
329
- def log_app_name
330
- ::NewRelic::Agent.logger.info "Application: #{Agent.config.app_names.join(", ")}"
331
- end
332
-
333
- def log_ignore_url_regexes
334
- regexes = NewRelic::Agent.config[:'rules.ignore_url_regexes']
335
-
336
- unless regexes.empty?
337
- ::NewRelic::Agent.logger.info "Ignoring URLs that match the following regexes: #{regexes.map(&:inspect).join(", ")}."
338
- end
339
- end
340
-
341
- # Logs the configured application names
342
- def app_name_configured?
343
- names = Agent.config.app_names
344
- return names.respond_to?(:any?) && names.any?
345
- end
346
-
347
- # Connecting in the foreground blocks further startup of the
348
- # agent until we have a connection - useful in cases where
349
- # you're trying to log a very-short-running process and want
350
- # to get statistics from before a server connection
351
- # (typically 20 seconds) exists
352
- def connect_in_foreground
353
- NewRelic::Agent.disable_all_tracing { connect(:keep_retrying => false) }
354
- end
355
-
356
- # This matters when the following three criteria are met:
357
- #
358
- # 1. A Sinatra 'classic' application is being run
359
- # 2. The app is being run by executing the main file directly, rather
360
- # than via a config.ru file.
361
- # 3. newrelic_rpm is required *after* sinatra
362
- #
363
- # In this case, the entire application runs from an at_exit handler in
364
- # Sinatra, and if we were to install ours, it would be executed before
365
- # the one in Sinatra, meaning that we'd shutdown the agent too early
366
- # and never collect any data.
367
- def sinatra_classic_app?
368
- (
369
- defined?(Sinatra::Application) &&
370
- Sinatra::Application.respond_to?(:run) &&
371
- Sinatra::Application.run?
372
- )
373
- end
374
-
375
- def should_install_exit_handler?
376
- (
377
- Agent.config[:send_data_on_exit] &&
378
- !sinatra_classic_app?
379
- )
380
- end
381
-
382
- def install_exit_handler
383
- return unless should_install_exit_handler?
384
- NewRelic::Agent.logger.debug("Installing at_exit handler")
385
- at_exit { shutdown }
386
- end
387
-
388
- # Classy logging of the agent version and the current pid,
389
- # so we can disambiguate processes in the log file and make
390
- # sure they're running a reasonable version
391
- def log_version_and_pid
392
- ::NewRelic::Agent.logger.debug "New Relic Ruby Agent #{NewRelic::VERSION::STRING} Initialized: pid = #{$$}"
393
- end
394
-
395
- # Warn the user if they have configured their agent not to
396
- # send data, that way we can see this clearly in the log file
397
- def monitoring?
398
- if Agent.config[:monitor_mode]
399
- true
400
- else
401
- ::NewRelic::Agent.logger.warn('Agent configured not to send data in this environment.')
402
- false
403
- end
404
- end
405
-
406
- # Tell the user when the license key is missing so they can
407
- # fix it by adding it to the file
408
- def has_license_key?
409
- if Agent.config[:license_key] && Agent.config[:license_key].length > 0
410
- true
411
- else
412
- ::NewRelic::Agent.logger.warn("No license key found. " +
413
- "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.")
414
- false
415
- end
416
- end
417
-
418
- # A correct license key exists and is of the proper length
419
- def has_correct_license_key?
420
- has_license_key? && correct_license_length
421
- end
422
-
423
- # A license key is an arbitrary 40 character string,
424
- # usually looks something like a SHA1 hash
425
- def correct_license_length
426
- key = Agent.config[:license_key]
427
-
428
- if key.length == 40
429
- true
430
- else
431
- ::NewRelic::Agent.logger.error("Invalid license key: #{key}")
432
- false
433
- end
434
- end
435
-
436
- # If we're using a dispatcher that forks before serving
437
- # requests, we need to wait until the children are forked
438
- # before connecting, otherwise the parent process sends useless data
439
- def using_forking_dispatcher?
440
- if [:puma, :passenger, :rainbows, :unicorn].include? Agent.config[:dispatcher]
441
- ::NewRelic::Agent.logger.info "Deferring startup of agent reporting thread because #{Agent.config[:dispatcher]} may fork."
442
- true
443
- else
444
- false
445
- end
446
- end
447
-
448
- # Return true if we're using resque and it hasn't had a chance to (potentially)
449
- # daemonize itself. This avoids hanging when there's a Thread started
450
- # before Resque calls Process.daemon (Jira RUBY-857)
451
- def defer_for_resque?
452
- NewRelic::Agent.config[:dispatcher] == :resque &&
453
- NewRelic::LanguageSupport.can_fork? &&
454
- !NewRelic::Agent::PipeChannelManager.listener.started?
455
- end
456
-
457
- def in_resque_child_process?
458
- defined?(@service) && @service.is_a?(NewRelic::Agent::PipeService)
459
- end
460
-
461
- # Sanity-check the agent configuration and start the agent,
462
- # setting up the worker thread and the exit handler to shut
463
- # down the agent
464
- def check_config_and_start_agent
465
- return unless monitoring? && has_correct_license_key?
466
- return if using_forking_dispatcher?
467
- setup_and_start_agent
468
- end
469
-
470
- # This is the shared method between the main agent startup and the
471
- # after_fork call restarting the thread in deferred dispatchers.
472
- #
473
- # Treatment of @started and env report is important to get right.
474
- def setup_and_start_agent(options={})
475
- @started = true
476
- @harvester.mark_started
477
-
478
- unless in_resque_child_process?
479
- generate_environment_report
480
- install_exit_handler
481
- @harvest_samplers.load_samplers unless Agent.config[:disable_samplers]
482
- end
483
-
484
- connect_in_foreground if Agent.config[:sync_startup]
485
- start_worker_thread(options)
486
- end
487
- end
488
-
489
- include Start
490
-
491
- def defer_for_delayed_job?
492
- NewRelic::Agent.config[:dispatcher] == :delayed_job &&
493
- !NewRelic::DelayedJobInjection.worker_name
494
- end
495
-
496
- # Check to see if the agent should start, returning +true+ if it should.
497
- def agent_should_start?
498
- return false if already_started? || disabled?
499
-
500
- if defer_for_delayed_job?
501
- ::NewRelic::Agent.logger.debug "Deferring startup for DelayedJob"
502
- return false
503
- end
504
-
505
- if defer_for_resque?
506
- ::NewRelic::Agent.logger.debug "Deferring startup for Resque in case it daemonizes"
507
- return false
508
- end
509
-
510
- unless app_name_configured?
511
- NewRelic::Agent.logger.error "No application name configured.",
512
- "The Agent cannot start without at least one. Please check your ",
513
- "newrelic.yml and ensure that it is valid and has at least one ",
514
- "value set for app_name in the #{NewRelic::Control.instance.env} ",
515
- "environment."
516
- return false
517
- end
518
-
519
- return true
520
- end
521
-
522
- # Logs a bunch of data and starts the agent, if needed
523
- def start
524
- return unless agent_should_start?
525
-
526
- log_startup
527
- check_config_and_start_agent
528
- log_version_and_pid
529
-
530
- events.subscribe(:finished_configuring) do
531
- log_ignore_url_regexes
532
- end
290
+ def pop_trace_execution_flag # THREAD_LOCAL_ACCESS
291
+ Tracer.state.pop_traced
533
292
  end
534
293
 
535
294
  # Clear out the metric data, errors, and transaction traces, etc.
@@ -540,10 +299,11 @@ module NewRelic
540
299
  @transaction_event_recorder.drop_buffered_data
541
300
  @custom_event_aggregator.reset!
542
301
  @span_event_aggregator.reset!
302
+ @log_event_aggregator.reset!
543
303
  @sql_sampler.reset!
544
304
 
545
305
  if Agent.config[:clear_transaction_state_after_fork]
546
- TransactionState.tl_clear
306
+ Tracer.clear_state
547
307
  end
548
308
  end
549
309
 
@@ -551,389 +311,39 @@ module NewRelic
551
311
  # This is necessary for cases where we're in a forked child and Ruby
552
312
  # might be holding locks for background thread that aren't there anymore.
553
313
  def reset_objects_with_locks
554
- @stats_engine = NewRelic::Agent::StatsEngine.new
314
+ @stats_engine = StatsEngine.new
555
315
  end
556
316
 
557
317
  def flush_pipe_data
558
- if connected? && @service.is_a?(::NewRelic::Agent::PipeService)
559
- transmit_data
560
- transmit_event_data
318
+ if connected? && @service.is_a?(PipeService)
319
+ transmit_data_types
561
320
  end
562
321
  end
563
322
 
564
323
  private
565
324
 
566
- # All of this module used to be contained in the
567
- # start_worker_thread method - this is an artifact of
568
- # refactoring and can be moved, renamed, etc at will
569
- module StartWorkerThread
570
- def create_event_loop
571
- EventLoop.new
572
- end
573
-
574
- # Never allow any data type to be reported more frequently than once
575
- # per second.
576
- MIN_ALLOWED_REPORT_PERIOD = 1.0
577
-
578
- def report_period_for(method)
579
- config_key = "data_report_periods.#{method}".to_sym
580
- period = Agent.config[config_key]
581
- if !period
582
- period = Agent.config[:data_report_period]
583
- ::NewRelic::Agent.logger.warn("Could not find configured period for #{method}, falling back to data_report_period (#{period} s)")
584
- end
585
- if period < MIN_ALLOWED_REPORT_PERIOD
586
- ::NewRelic::Agent.logger.warn("Configured #{config_key} was #{period}, but minimum allowed is #{MIN_ALLOWED_REPORT_PERIOD}, using #{MIN_ALLOWED_REPORT_PERIOD}.")
587
- period = MIN_ALLOWED_REPORT_PERIOD
588
- end
589
- period
590
- end
591
-
592
- LOG_ONCE_KEYS_RESET_PERIOD = 60.0
593
-
594
- def create_and_run_event_loop
595
- @event_loop = create_event_loop
596
- @event_loop.on(:report_data) do
597
- transmit_data
598
- end
599
- @event_loop.on(:report_event_data) do
600
- transmit_event_data
601
- end
602
- @event_loop.on(:reset_log_once_keys) do
603
- ::NewRelic::Agent.logger.clear_already_logged
604
- end
605
- @event_loop.fire_every(Agent.config[:data_report_period], :report_data)
606
- @event_loop.fire_every(report_period_for(:analytic_event_data), :report_event_data)
607
- @event_loop.fire_every(LOG_ONCE_KEYS_RESET_PERIOD, :reset_log_once_keys)
608
-
609
- @event_loop.run
610
- end
611
-
612
- # Handles the case where the server tells us to restart -
613
- # this clears the data, clears connection attempts, and
614
- # waits a while to reconnect.
615
- def handle_force_restart(error)
616
- ::NewRelic::Agent.logger.debug error.message
617
- drop_buffered_data
618
- @service.force_restart if @service
619
- @connect_state = :pending
620
- sleep 30
621
- end
622
-
623
- # when a disconnect is requested, stop the current thread, which
624
- # is the worker thread that gathers data and talks to the
625
- # server.
626
- def handle_force_disconnect(error)
627
- ::NewRelic::Agent.logger.warn "New Relic forced this agent to disconnect (#{error.message})"
628
- disconnect
629
- end
630
-
631
- # Handles an unknown error in the worker thread by logging
632
- # it and disconnecting the agent, since we are now in an
633
- # unknown state.
634
- def handle_other_error(error)
635
- ::NewRelic::Agent.logger.error "Unhandled error in worker thread, disconnecting this agent process:"
636
- # These errors are fatal (that is, they will prevent the agent from
637
- # reporting entirely), so we really want backtraces when they happen
638
- ::NewRelic::Agent.logger.log_exception(:error, error)
639
- disconnect
640
- end
641
-
642
- # a wrapper method to handle all the errors that can happen
643
- # in the connection and worker thread system. This
644
- # guarantees a no-throw from the background thread.
645
- def catch_errors
646
- yield
647
- rescue NewRelic::Agent::ForceRestartException => e
648
- handle_force_restart(e)
649
- retry
650
- rescue NewRelic::Agent::ForceDisconnectException => e
651
- handle_force_disconnect(e)
652
- rescue => e
653
- handle_other_error(e)
654
- end
655
-
656
- # This is the method that is run in a new thread in order to
657
- # background the harvesting and sending of data during the
658
- # normal operation of the agent.
659
- #
660
- # Takes connection options that determine how we should
661
- # connect to the server, and loops endlessly - typically we
662
- # never return from this method unless we're shutting down
663
- # the agent
664
- def deferred_work!(connection_options)
665
- catch_errors do
666
- NewRelic::Agent.disable_all_tracing do
667
- connect(connection_options)
668
- if connected?
669
- create_and_run_event_loop
670
- # never reaches here unless there is a problem or
671
- # the agent is exiting
672
- else
673
- ::NewRelic::Agent.logger.debug "No connection. Worker thread ending."
674
- end
675
- end
676
- end
677
- end
678
- end
679
- include StartWorkerThread
680
-
681
- # Try to launch the worker thread and connect to the server.
682
- #
683
- # See #connect for a description of connection_options.
684
- def start_worker_thread(connection_options = {})
685
- if disable = NewRelic::Agent.config[:disable_harvest_thread]
686
- NewRelic::Agent.logger.info "Not starting Ruby Agent worker thread because :disable_harvest_thread is #{disable}"
687
- return
688
- end
689
-
690
- ::NewRelic::Agent.logger.debug "Creating Ruby Agent worker thread."
691
- @worker_thread = NewRelic::Agent::Threading::AgentThread.create('Worker Loop') do
692
- deferred_work!(connection_options)
693
- end
694
- end
695
-
696
325
  # A shorthand for NewRelic::Control.instance
697
326
  def control
698
327
  NewRelic::Control.instance
699
328
  end
700
329
 
701
- # This module is an artifact of a refactoring of the connect
702
- # method - all of its methods are used in that context, so it
703
- # can be refactored at will. It should be fully tested
704
- module Connect
705
- # number of attempts we've made to contact the server
706
- attr_accessor :connect_attempts
707
-
708
- # Disconnect just sets the connect state to disconnected, preventing
709
- # further retries.
710
- def disconnect
711
- @connect_state = :disconnected
712
- true
713
- end
714
-
715
- def connected?
716
- @connect_state == :connected
717
- end
718
-
719
- def disconnected?
720
- @connect_state == :disconnected
721
- end
722
-
723
- # Don't connect if we're already connected, or if we tried to connect
724
- # and were rejected with prejudice because of a license issue, unless
725
- # we're forced to by force_reconnect.
726
- def should_connect?(force=false)
727
- force || (!connected? && !disconnected?)
728
- end
729
-
730
- # Per the spec at
731
- # /agents/agent-specs/Collector-Response-Handling.md, retry
732
- # connections after a specific backoff sequence to prevent
733
- # hammering the server.
734
- CONNECT_RETRY_PERIODS = [15, 15, 30, 60, 120, 300]
735
-
736
- def connect_retry_period
737
- CONNECT_RETRY_PERIODS[connect_attempts] || 300
738
- end
739
-
740
- def note_connect_failure
741
- self.connect_attempts += 1
742
- end
743
-
744
- # When we have a problem connecting to the server, we need
745
- # to tell the user what happened, since this is not an error
746
- # we can handle gracefully.
747
- def log_error(error)
748
- ::NewRelic::Agent.logger.error "Error establishing connection with New Relic Service at #{control.server}:", error
749
- end
750
-
751
- # When the server sends us an error with the license key, we
752
- # want to tell the user that something went wrong, and let
753
- # them know where to go to get a valid license key
754
- #
755
- # After this runs, it disconnects the agent so that it will
756
- # no longer try to connect to the server, saving the
757
- # application and the server load
758
- def handle_license_error(error)
759
- ::NewRelic::Agent.logger.error( \
760
- error.message, \
761
- "Visit NewRelic.com to obtain a valid license key, or to upgrade your account.")
762
- disconnect
763
- end
764
-
765
- def handle_unrecoverable_agent_error(error)
766
- ::NewRelic::Agent.logger.error(error.message)
767
- disconnect
768
- shutdown
769
- end
770
-
771
- def generate_environment_report
772
- @environment_report = environment_for_connect
773
- end
774
-
775
- # Checks whether we should send environment info, and if so,
776
- # returns the snapshot from the local environment.
777
- # Generating the EnvironmentReport has the potential to trigger
778
- # require calls in Rails environments, so this method should only
779
- # be called synchronously from on the main thread.
780
- def environment_for_connect
781
- Agent.config[:send_environment_info] ? Array(EnvironmentReport.new) : []
782
- end
783
-
784
- # We've seen objects in the environment report (Rails.env in
785
- # particular) that can't seralize to JSON. Cope with that here and
786
- # clear out so downstream code doesn't have to check again.
787
- def sanitize_environment_report
788
- if !@service.valid_to_marshal?(@environment_report)
789
- @environment_report = []
790
- end
791
- end
792
-
793
- # Initializes the hash of settings that we send to the
794
- # server. Returns a literal hash containing the options
795
- def connect_settings
796
- sanitize_environment_report
797
-
798
- {
799
- :pid => $$,
800
- :host => local_host,
801
- :display_host => Agent.config[:'process_host.display_name'],
802
- :app_name => Agent.config.app_names,
803
- :language => 'ruby',
804
- :labels => Agent.config.parsed_labels,
805
- :agent_version => NewRelic::VERSION::STRING,
806
- :environment => @environment_report,
807
- :settings => Agent.config.to_collector_hash,
808
- :high_security => Agent.config[:high_security],
809
- :utilization => UtilizationData.new.to_collector_hash,
810
- :identifier => "ruby:#{local_host}:#{Agent.config.app_names.sort.join(',')}"
811
- }
812
- end
813
-
814
- # Returns connect data passed back from the server
815
- def connect_to_server
816
- @service.connect(connect_settings)
817
- end
818
-
819
- # apdex_f is always 4 times the apdex_t
820
- def apdex_f
821
- (4 * Agent.config[:apdex_t]).to_f
822
- end
823
-
824
- # Sets the collector host and connects to the server, then
825
- # invokes the final configuration with the returned data
826
- def query_server_for_configuration
827
- finish_setup(connect_to_server)
828
- end
829
-
830
- # Takes a hash of configuration data returned from the
831
- # server and uses it to set local variables and to
832
- # initialize various parts of the agent that are configured
833
- # separately.
834
- #
835
- # Can accommodate most arbitrary data - anything extra is
836
- # ignored unless we say to do something with it here.
837
- def finish_setup(config_data)
838
- return if config_data == nil
839
-
840
- @service.agent_id = config_data['agent_run_id']
841
-
842
- security_policies = config_data.delete('security_policies')
843
-
844
- add_server_side_config(config_data)
845
- add_security_policy_config(security_policies) if security_policies
846
-
847
- log_connection!(config_data)
848
- @transaction_rules = RulesEngine.create_transaction_rules(config_data)
849
- @stats_engine.metric_rules = RulesEngine.create_metric_rules(config_data)
850
-
851
- # If you're adding something else here to respond to the server-side config,
852
- # use Agent.instance.events.subscribe(:finished_configuring) callback instead!
853
- end
854
-
855
- def add_server_side_config(config_data)
856
- if config_data['agent_config']
857
- ::NewRelic::Agent.logger.debug "Using config from server"
858
- end
859
-
860
- ::NewRelic::Agent.logger.debug "Server provided config: #{config_data.inspect}"
861
- server_config = NewRelic::Agent::Configuration::ServerSource.new(config_data, Agent.config)
862
- Agent.config.replace_or_add_config(server_config)
863
- end
864
-
865
- def add_security_policy_config(security_policies)
866
- ::NewRelic::Agent.logger.info 'Installing security policies'
867
- security_policy_source = NewRelic::Agent::Configuration::SecurityPolicySource.new(security_policies)
868
- Agent.config.replace_or_add_config(security_policy_source)
869
- # drop data collected before applying security policies
870
- drop_buffered_data
871
- end
872
-
873
- class WaitOnConnectTimeout < StandardError
874
- end
875
-
876
- # Used for testing to let us know we've actually started to wait
877
- def waited_on_connect?
878
- @waited_on_connect
879
- end
880
-
881
- def signal_connected
882
- @wait_on_connect_mutex.synchronize do
883
- @wait_on_connect_condition.signal
884
- end
885
- end
886
-
887
- def wait_on_connect(timeout)
888
- return if connected?
889
-
890
- @waited_on_connect = true
891
- NewRelic::Agent.logger.debug("Waiting on connect to complete.")
892
-
893
- @wait_on_connect_mutex.synchronize do
894
- @wait_on_connect_condition.wait(@wait_on_connect_mutex, timeout)
895
- end
896
-
897
- unless connected?
898
- raise WaitOnConnectTimeout, "Agent was unable to connect in #{timeout} seconds."
899
- end
900
- end
901
-
902
- # Logs when we connect to the server, for debugging purposes
903
- # - makes sure we know if an agent has not connected
904
- def log_connection!(config_data)
905
- ::NewRelic::Agent.logger.debug "Connected to NewRelic Service at #{@service.collector.name}"
906
- ::NewRelic::Agent.logger.debug "Agent Run = #{@service.agent_id}."
907
- ::NewRelic::Agent.logger.debug "Connection data = #{config_data.inspect}"
908
- if config_data['messages'] && config_data['messages'].any?
909
- log_collector_messages(config_data['messages'])
910
- end
911
- end
912
-
913
- def log_collector_messages(messages)
914
- messages.each do |message|
915
- ::NewRelic::Agent.logger.send(message['level'].downcase, message['message'])
916
- end
917
- end
918
- end
919
- include Connect
920
-
921
330
  def container_for_endpoint(endpoint)
922
331
  case endpoint
923
- when :metric_data then @stats_engine
332
+ when :metric_data then @stats_engine
924
333
  when :transaction_sample_data then @transaction_sampler
925
- when :error_data then @error_collector.error_trace_aggregator
926
- when :error_event_data then @error_collector.error_event_aggregator
927
- when :analytic_event_data then transaction_event_aggregator
928
- when :custom_event_data then @custom_event_aggregator
929
- when :span_event_data then span_event_aggregator
930
- when :sql_trace_data then @sql_sampler
334
+ when :error_data then @error_collector.error_trace_aggregator
335
+ when :error_event_data then @error_collector.error_event_aggregator
336
+ when :analytic_event_data then transaction_event_aggregator
337
+ when :custom_event_data then @custom_event_aggregator
338
+ when :span_event_data then span_event_aggregator
339
+ when :sql_trace_data then @sql_sampler
340
+ when :log_event_data then @log_event_aggregator
931
341
  end
932
342
  end
933
343
 
934
344
  def merge_data_for_endpoint(endpoint, data)
935
345
  if data && !data.empty?
936
- container = container_for_endpoint endpoint
346
+ container = container_for_endpoint(endpoint)
937
347
  if container.respond_to?(:has_metadata?) && container.has_metadata?
938
348
  container_for_endpoint(endpoint).merge!(data, false)
939
349
  else
@@ -946,265 +356,11 @@ module NewRelic
946
356
 
947
357
  public :merge_data_for_endpoint
948
358
 
949
- # Establish a connection to New Relic servers.
950
- #
951
- # By default, if a connection has already been established, this method
952
- # will be a no-op.
953
- #
954
- # @param [Hash] options
955
- # @option options [Boolean] :keep_retrying (true)
956
- # If true, this method will block until a connection is successfully
957
- # established, continuing to retry upon failure. If false, this method
958
- # will return after either successfully connecting, or after failing
959
- # once.
960
- #
961
- # @option options [Boolean] :force_reconnect (false)
962
- # If true, this method will force establishment of a new connection
963
- # with New Relic, even if there is already an existing connection.
964
- # This is useful primarily when re-establishing a new connection after
965
- # forking off from a parent process.
966
- #
967
- def connect(options={})
968
- defaults = {
969
- :keep_retrying => Agent.config[:keep_retrying],
970
- :force_reconnect => Agent.config[:force_reconnect]
971
- }
972
- opts = defaults.merge(options)
973
-
974
- return unless should_connect?(opts[:force_reconnect])
975
-
976
- ::NewRelic::Agent.logger.debug "Connecting Process to New Relic: #$0"
977
- query_server_for_configuration
978
- @connected_pid = $$
979
- @connect_state = :connected
980
- signal_connected
981
- rescue NewRelic::Agent::ForceDisconnectException => e
982
- handle_force_disconnect(e)
983
- rescue NewRelic::Agent::LicenseException => e
984
- handle_license_error(e)
985
- rescue NewRelic::Agent::UnrecoverableAgentException => e
986
- handle_unrecoverable_agent_error(e)
987
- rescue StandardError, Timeout::Error, NewRelic::Agent::ServerConnectionException => e
988
- log_error(e)
989
- if opts[:keep_retrying]
990
- note_connect_failure
991
- ::NewRelic::Agent.logger.info "Will re-attempt in #{connect_retry_period} seconds"
992
- sleep connect_retry_period
993
- retry
994
- end
995
- rescue Exception => e
996
- ::NewRelic::Agent.logger.error "Exception of unexpected type during Agent#connect():", e
997
-
998
- raise
999
- end
1000
-
1001
- # Who am I? Well, this method can tell you your hostname.
1002
- def determine_host
1003
- NewRelic::Agent::Hostname.get
1004
- end
1005
-
1006
- def local_host
1007
- @local_host ||= determine_host
1008
- end
1009
-
1010
359
  # Delegates to the control class to determine the root
1011
360
  # directory of this project
1012
361
  def determine_home_directory
1013
362
  control.root
1014
363
  end
1015
-
1016
- # Harvests data from the given container, sends it to the named endpoint
1017
- # on the service, and automatically merges back in upon a recoverable
1018
- # failure.
1019
- #
1020
- # The given container should respond to:
1021
- #
1022
- # #harvest!
1023
- # returns a payload that contains enumerable collection of data items and
1024
- # optional metadata to be sent to the collector.
1025
- #
1026
- # #reset!
1027
- # drop any stored data and reset to a clean state.
1028
- #
1029
- # #merge!(payload)
1030
- # merge the given payload back into the internal buffer of the
1031
- # container, so that it may be harvested again later.
1032
- #
1033
- def harvest_and_send_from_container(container, endpoint)
1034
- payload = harvest_from_container(container, endpoint)
1035
- sample_count = harvest_size container, payload
1036
- if sample_count > 0
1037
- NewRelic::Agent.logger.debug("Sending #{sample_count} items to #{endpoint}")
1038
- send_data_to_endpoint(endpoint, payload, container)
1039
- end
1040
- end
1041
-
1042
- def harvest_size container, items
1043
- if container.respond_to?(:has_metadata?) && container.has_metadata? && !items.empty?
1044
- items.last.size
1045
- else
1046
- items.size
1047
- end
1048
- end
1049
-
1050
- def harvest_from_container(container, endpoint)
1051
- items = []
1052
- begin
1053
- items = container.harvest!
1054
- rescue => e
1055
- NewRelic::Agent.logger.error("Failed to harvest #{endpoint} data, resetting. Error: ", e)
1056
- container.reset!
1057
- end
1058
- items
1059
- end
1060
-
1061
- def send_data_to_endpoint(endpoint, payload, container)
1062
- begin
1063
- @service.send(endpoint, payload)
1064
- rescue ForceRestartException, ForceDisconnectException
1065
- raise
1066
- rescue SerializationError => e
1067
- NewRelic::Agent.logger.warn("Failed to serialize data for #{endpoint}, discarding. Error: ", e)
1068
- rescue UnrecoverableServerException => e
1069
- NewRelic::Agent.logger.warn("#{endpoint} data was rejected by remote service, discarding. Error: ", e)
1070
- rescue ServerConnectionException => e
1071
- log_remote_unavailable(endpoint, e)
1072
- container.merge!(payload)
1073
- rescue => e
1074
- NewRelic::Agent.logger.info("Unable to send #{endpoint} data, will try again later. Error: ", e)
1075
- container.merge!(payload)
1076
- end
1077
- end
1078
-
1079
- def harvest_and_send_timeslice_data
1080
- NewRelic::Agent::TransactionTimeAggregator.harvest!
1081
- harvest_and_send_from_container(@stats_engine, :metric_data)
1082
- end
1083
-
1084
- def harvest_and_send_slowest_sql
1085
- harvest_and_send_from_container(@sql_sampler, :sql_trace_data)
1086
- end
1087
-
1088
- # This handles getting the transaction traces and then sending
1089
- # them across the wire. This includes gathering SQL
1090
- # explanations, stripping out stack traces, and normalizing
1091
- # SQL. note that we explain only the sql statements whose
1092
- # nodes' execution times exceed our threshold (to avoid
1093
- # unnecessary overhead of running explains on fast queries.)
1094
- def harvest_and_send_transaction_traces
1095
- harvest_and_send_from_container(@transaction_sampler, :transaction_sample_data)
1096
- end
1097
-
1098
- def harvest_and_send_for_agent_commands
1099
- harvest_and_send_from_container(@agent_command_router, :profile_data)
1100
- end
1101
-
1102
- def harvest_and_send_errors
1103
- harvest_and_send_from_container(@error_collector.error_trace_aggregator, :error_data)
1104
- end
1105
-
1106
- def harvest_and_send_analytic_event_data
1107
- harvest_and_send_from_container(transaction_event_aggregator, :analytic_event_data)
1108
- harvest_and_send_from_container(synthetics_event_aggregator, :analytic_event_data)
1109
- harvest_and_send_from_container(@custom_event_aggregator, :custom_event_data)
1110
- harvest_and_send_from_container(span_event_aggregator, :span_event_data)
1111
- end
1112
-
1113
- def harvest_and_send_error_event_data
1114
- harvest_and_send_from_container @error_collector.error_event_aggregator, :error_event_data
1115
- end
1116
-
1117
- def check_for_and_handle_agent_commands
1118
- begin
1119
- @agent_command_router.check_for_and_handle_agent_commands
1120
- rescue ForceRestartException, ForceDisconnectException
1121
- raise
1122
- rescue ServerConnectionException => e
1123
- log_remote_unavailable(:get_agent_commands, e)
1124
- rescue => e
1125
- NewRelic::Agent.logger.info("Error during check_for_and_handle_agent_commands, will retry later: ", e)
1126
- end
1127
- end
1128
-
1129
- def log_remote_unavailable(endpoint, e)
1130
- NewRelic::Agent.logger.debug("Unable to send #{endpoint} data, will try again later. Error: ", e)
1131
- NewRelic::Agent.record_metric("Supportability/remote_unavailable", 0.0)
1132
- NewRelic::Agent.record_metric("Supportability/remote_unavailable/#{endpoint.to_s}", 0.0)
1133
- end
1134
-
1135
- def transmit_event_data
1136
- transmit_single_data_type(:harvest_and_send_analytic_event_data, "TransactionEvent")
1137
- end
1138
-
1139
- def transmit_span_event_data
1140
- transmit_single_data_type(:harvest_and_send_span_event_data, "SpanEvent")
1141
- end
1142
-
1143
- def transmit_single_data_type(harvest_method, supportability_name)
1144
- now = Time.now
1145
-
1146
- msg = "Sending #{harvest_method.to_s.gsub("harvest_and_send_", "")} to New Relic Service"
1147
- ::NewRelic::Agent.logger.debug msg
1148
-
1149
- @service.session do # use http keep-alive
1150
- self.send(harvest_method)
1151
- end
1152
- ensure
1153
- duration = (Time.now - now).to_f
1154
- NewRelic::Agent.record_metric("Supportability/#{supportability_name}Harvest", duration)
1155
- end
1156
-
1157
- def transmit_data
1158
- now = Time.now
1159
- ::NewRelic::Agent.logger.debug "Sending data to New Relic Service"
1160
-
1161
- @events.notify(:before_harvest)
1162
- @service.session do # use http keep-alive
1163
- harvest_and_send_errors
1164
- harvest_and_send_error_event_data
1165
- harvest_and_send_transaction_traces
1166
- harvest_and_send_slowest_sql
1167
- harvest_and_send_timeslice_data
1168
-
1169
- check_for_and_handle_agent_commands
1170
- harvest_and_send_for_agent_commands
1171
- end
1172
- ensure
1173
- NewRelic::Agent::Database.close_connections
1174
- duration = (Time.now - now).to_f
1175
- NewRelic::Agent.record_metric('Supportability/Harvest', duration)
1176
- end
1177
-
1178
- # This method contacts the server to send remaining data and
1179
- # let the server know that the agent is shutting down - this
1180
- # allows us to do things like accurately set the end of the
1181
- # lifetime of the process
1182
- #
1183
- # If this process comes from a parent process, it will not
1184
- # disconnect, so that the parent process can continue to send data
1185
- def graceful_disconnect
1186
- if connected?
1187
- begin
1188
- @service.request_timeout = 10
1189
-
1190
- @events.notify(:before_shutdown)
1191
- transmit_data
1192
- transmit_event_data
1193
-
1194
- if @connected_pid == $$ && !@service.kind_of?(NewRelic::Agent::NewRelicService)
1195
- ::NewRelic::Agent.logger.debug "Sending New Relic service agent run shutdown message"
1196
- @service.shutdown(Time.now.to_f)
1197
- else
1198
- ::NewRelic::Agent.logger.debug "This agent connected from parent process #{@connected_pid}--not sending shutdown"
1199
- end
1200
- ::NewRelic::Agent.logger.debug "Graceful disconnect complete"
1201
- rescue Timeout::Error, StandardError => e
1202
- ::NewRelic::Agent.logger.debug "Error when disconnecting #{e.class.name}: #{e.message}"
1203
- end
1204
- else
1205
- ::NewRelic::Agent.logger.debug "Bypassing graceful disconnect - agent not connected"
1206
- end
1207
- end
1208
364
  end
1209
365
 
1210
366
  extend ClassMethods