newrelic_rpm 4.8.0.341 → 6.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (343) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +31 -0
  3. data/.github/ISSUE_TEMPLATE/config.yml +5 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +24 -0
  5. data/.github/actions/annotate/README.md +79 -0
  6. data/.github/actions/annotate/action.yml +6 -0
  7. data/.github/actions/annotate/dist/index.js +433 -0
  8. data/.github/actions/annotate/index.js +25 -0
  9. data/.github/actions/annotate/package-lock.json +172 -0
  10. data/.github/actions/annotate/package.json +30 -0
  11. data/.github/actions/annotate/pre-commit +5 -0
  12. data/.github/actions/build-ruby/README.md +79 -0
  13. data/.github/actions/build-ruby/action.yml +15 -0
  14. data/.github/actions/build-ruby/dist/index.js +52683 -0
  15. data/.github/actions/build-ruby/index.js +514 -0
  16. data/.github/actions/build-ruby/package-lock.json +581 -0
  17. data/.github/actions/build-ruby/package.json +32 -0
  18. data/.github/actions/build-ruby/pre-commit +5 -0
  19. data/.github/pull_request_template.md +16 -0
  20. data/.github/workflows/ci.yml +212 -0
  21. data/.github/workflows/pr_review_checklist.yml +22 -0
  22. data/.github/workflows/release.yml +78 -0
  23. data/.github/workflows/scripts/rubygems-authenticate.py +13 -0
  24. data/.github/workflows/scripts/rubygems-publish.rb +32 -0
  25. data/.github/workflows/snyk.yml +27 -0
  26. data/.github/workflows/stale.yml +21 -0
  27. data/.gitignore +9 -0
  28. data/.yardopts +4 -1
  29. data/CHANGELOG.md +868 -3
  30. data/CONTRIBUTING.md +106 -19
  31. data/Gemfile +6 -2
  32. data/Guardfile +18 -1
  33. data/LICENSE +208 -38
  34. data/README.md +81 -88
  35. data/ROADMAP.md +24 -0
  36. data/Rakefile +2 -0
  37. data/THIRD_PARTY_NOTICES.md +213 -0
  38. data/bin/nrdebug +1 -1
  39. data/config.dot +3 -12
  40. data/init.rb +1 -1
  41. data/install.rb +1 -1
  42. data/lib/new_relic/agent.rb +145 -31
  43. data/lib/new_relic/agent/adaptive_sampler.rb +104 -0
  44. data/lib/new_relic/agent/agent.rb +192 -190
  45. data/lib/new_relic/agent/agent_logger.rb +5 -1
  46. data/lib/new_relic/agent/attribute_filter.rb +85 -25
  47. data/lib/new_relic/agent/attribute_processing.rb +1 -1
  48. data/lib/new_relic/agent/attributes.rb +152 -0
  49. data/lib/new_relic/agent/audit_logger.rb +1 -1
  50. data/lib/new_relic/agent/autostart.rb +20 -15
  51. data/lib/new_relic/agent/chained_call.rb +1 -1
  52. data/lib/new_relic/agent/commands/agent_command.rb +1 -1
  53. data/lib/new_relic/agent/commands/agent_command_router.rb +3 -22
  54. data/lib/new_relic/agent/commands/thread_profiler_session.rb +1 -1
  55. data/lib/new_relic/agent/configuration.rb +1 -1
  56. data/lib/new_relic/agent/configuration/default_source.rb +375 -67
  57. data/lib/new_relic/agent/configuration/dotted_hash.rb +1 -1
  58. data/lib/new_relic/agent/configuration/environment_source.rb +5 -3
  59. data/lib/new_relic/agent/configuration/event_harvest_config.rb +45 -0
  60. data/lib/new_relic/agent/configuration/high_security_source.rb +2 -3
  61. data/lib/new_relic/agent/configuration/manager.rb +54 -43
  62. data/lib/new_relic/agent/configuration/manual_source.rb +1 -1
  63. data/lib/new_relic/agent/configuration/mask_defaults.rb +1 -1
  64. data/lib/new_relic/agent/configuration/security_policy_source.rb +238 -0
  65. data/lib/new_relic/agent/configuration/server_source.rb +46 -5
  66. data/lib/new_relic/agent/configuration/yaml_source.rb +12 -7
  67. data/lib/new_relic/agent/connect/request_builder.rb +61 -0
  68. data/lib/new_relic/agent/connect/response_handler.rb +61 -0
  69. data/lib/new_relic/agent/custom_event_aggregator.rb +17 -6
  70. data/lib/new_relic/agent/database.rb +21 -3
  71. data/lib/new_relic/agent/database/explain_plan_helpers.rb +12 -1
  72. data/lib/new_relic/agent/database/obfuscation_helpers.rb +2 -2
  73. data/lib/new_relic/agent/database/obfuscator.rb +1 -1
  74. data/lib/new_relic/agent/database/postgres_explain_obfuscator.rb +1 -1
  75. data/lib/new_relic/agent/datastores.rb +12 -11
  76. data/lib/new_relic/agent/datastores/metric_helper.rb +2 -3
  77. data/lib/new_relic/agent/datastores/mongo.rb +2 -2
  78. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +3 -3
  79. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +1 -1
  80. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +9 -9
  81. data/lib/new_relic/agent/datastores/mongo/statement_formatter.rb +1 -1
  82. data/lib/new_relic/agent/datastores/redis.rb +1 -1
  83. data/lib/new_relic/agent/deprecator.rb +1 -1
  84. data/lib/new_relic/agent/distributed_tracing.rb +216 -0
  85. data/lib/new_relic/agent/distributed_tracing/cross_app_payload.rb +44 -0
  86. data/lib/new_relic/agent/distributed_tracing/cross_app_tracing.rb +248 -0
  87. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +84 -0
  88. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  89. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +165 -0
  90. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
  91. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
  92. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +126 -0
  93. data/lib/new_relic/agent/encoding_normalizer.rb +1 -1
  94. data/lib/new_relic/agent/error_collector.rb +51 -20
  95. data/lib/new_relic/agent/error_event_aggregator.rb +14 -7
  96. data/lib/new_relic/agent/error_trace_aggregator.rb +2 -1
  97. data/lib/new_relic/agent/event_aggregator.rb +34 -15
  98. data/lib/new_relic/agent/event_buffer.rb +1 -1
  99. data/lib/new_relic/agent/event_listener.rb +1 -1
  100. data/lib/new_relic/agent/event_loop.rb +1 -1
  101. data/lib/new_relic/agent/external.rb +31 -29
  102. data/lib/new_relic/agent/guid_generator.rb +28 -0
  103. data/lib/new_relic/agent/harvester.rb +1 -1
  104. data/lib/new_relic/agent/heap.rb +140 -0
  105. data/lib/new_relic/agent/hostname.rb +16 -2
  106. data/lib/new_relic/agent/http_clients/abstract.rb +82 -0
  107. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +25 -20
  108. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +29 -14
  109. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +18 -22
  110. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +11 -12
  111. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +17 -5
  112. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +5 -7
  113. data/lib/new_relic/agent/http_clients/uri_util.rb +12 -11
  114. data/lib/new_relic/agent/instrumentation.rb +1 -1
  115. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +26 -43
  116. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +56 -74
  117. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +82 -56
  118. data/lib/new_relic/agent/instrumentation/active_job.rb +19 -17
  119. data/lib/new_relic/agent/instrumentation/active_merchant.rb +1 -1
  120. data/lib/new_relic/agent/instrumentation/active_record.rb +85 -38
  121. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +82 -24
  122. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +168 -0
  123. data/lib/new_relic/agent/instrumentation/active_record_prepend.rb +102 -31
  124. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +47 -57
  125. data/lib/new_relic/agent/instrumentation/active_storage.rb +23 -0
  126. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +63 -0
  127. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +24 -8
  128. data/lib/new_relic/agent/instrumentation/authlogic.rb +1 -1
  129. data/lib/new_relic/agent/instrumentation/bunny.rb +115 -44
  130. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +18 -12
  131. data/lib/new_relic/agent/instrumentation/curb.rb +121 -35
  132. data/lib/new_relic/agent/instrumentation/data_mapper.rb +6 -4
  133. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +61 -25
  134. data/lib/new_relic/agent/instrumentation/excon.rb +2 -2
  135. data/lib/new_relic/agent/instrumentation/excon/connection.rb +8 -5
  136. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +4 -3
  137. data/lib/new_relic/agent/instrumentation/grape.rb +46 -35
  138. data/lib/new_relic/agent/instrumentation/http.rb +8 -5
  139. data/lib/new_relic/agent/instrumentation/httpclient.rb +7 -5
  140. data/lib/new_relic/agent/instrumentation/ignore_actions.rb +1 -1
  141. data/lib/new_relic/agent/instrumentation/memcache.rb +5 -3
  142. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +10 -6
  143. data/lib/new_relic/agent/instrumentation/merb/controller.rb +1 -1
  144. data/lib/new_relic/agent/instrumentation/merb/errors.rb +1 -1
  145. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +13 -5
  146. data/lib/new_relic/agent/instrumentation/middleware_tracing.rb +20 -11
  147. data/lib/new_relic/agent/instrumentation/mongo.rb +11 -5
  148. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +39 -7
  149. data/lib/new_relic/agent/instrumentation/net.rb +60 -23
  150. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +100 -0
  151. data/lib/new_relic/agent/instrumentation/padrino.rb +40 -16
  152. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +1 -1
  153. data/lib/new_relic/agent/instrumentation/queue_time.rb +1 -1
  154. data/lib/new_relic/agent/instrumentation/rack.rb +36 -13
  155. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +1 -1
  156. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +1 -1
  157. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +1 -1
  158. data/lib/new_relic/agent/instrumentation/rails_middleware.rb +1 -1
  159. data/lib/new_relic/agent/instrumentation/{rails5 → rails_notifications}/action_cable.rb +8 -5
  160. data/lib/new_relic/agent/instrumentation/{rails5 → rails_notifications}/action_controller.rb +4 -4
  161. data/lib/new_relic/agent/instrumentation/{rails4 → rails_notifications}/action_view.rb +4 -4
  162. data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +1 -1
  163. data/lib/new_relic/agent/instrumentation/rake.rb +56 -22
  164. data/lib/new_relic/agent/instrumentation/redis.rb +141 -56
  165. data/lib/new_relic/agent/instrumentation/resque.rb +21 -37
  166. data/lib/new_relic/agent/instrumentation/sequel.rb +2 -3
  167. data/lib/new_relic/agent/instrumentation/sequel_helper.rb +1 -1
  168. data/lib/new_relic/agent/instrumentation/sidekiq.rb +48 -24
  169. data/lib/new_relic/agent/instrumentation/sinatra.rb +62 -23
  170. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +1 -1
  171. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +1 -1
  172. data/lib/new_relic/agent/instrumentation/sunspot.rb +1 -1
  173. data/lib/new_relic/agent/instrumentation/typhoeus.rb +27 -10
  174. data/lib/new_relic/agent/internal_agent_error.rb +1 -1
  175. data/lib/new_relic/agent/javascript_instrumentor.rb +19 -16
  176. data/lib/new_relic/agent/log_once.rb +1 -1
  177. data/lib/new_relic/agent/logging.rb +139 -0
  178. data/lib/new_relic/agent/memory_logger.rb +1 -1
  179. data/lib/new_relic/agent/messaging.rb +15 -59
  180. data/lib/new_relic/agent/method_tracer.rb +42 -25
  181. data/lib/new_relic/agent/method_tracer_helpers.rb +5 -5
  182. data/lib/new_relic/agent/monitors.rb +27 -0
  183. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
  184. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
  185. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +4 -4
  186. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +4 -7
  187. data/lib/new_relic/agent/new_relic_service.rb +174 -63
  188. data/lib/new_relic/agent/new_relic_service/encoders.rb +1 -1
  189. data/lib/new_relic/agent/new_relic_service/json_marshaller.rb +1 -2
  190. data/lib/new_relic/agent/new_relic_service/marshaller.rb +6 -27
  191. data/lib/new_relic/agent/new_relic_service/security_policy_settings.rb +61 -0
  192. data/lib/new_relic/agent/noticible_error.rb +22 -0
  193. data/lib/new_relic/agent/null_logger.rb +1 -1
  194. data/lib/new_relic/agent/obfuscator.rb +1 -1
  195. data/lib/new_relic/agent/parameter_filtering.rb +19 -6
  196. data/lib/new_relic/agent/payload_metric_mapping.rb +1 -1
  197. data/lib/new_relic/agent/pipe_channel_manager.rb +1 -1
  198. data/lib/new_relic/agent/pipe_service.rb +5 -1
  199. data/lib/new_relic/agent/prepend_supportability.rb +1 -1
  200. data/lib/new_relic/agent/priority_sampled_buffer.rb +95 -0
  201. data/lib/new_relic/agent/range_extensions.rb +1 -1
  202. data/lib/new_relic/agent/rules_engine.rb +1 -1
  203. data/lib/new_relic/agent/rules_engine/replacement_rule.rb +1 -1
  204. data/lib/new_relic/agent/rules_engine/segment_terms_rule.rb +1 -1
  205. data/lib/new_relic/agent/sampler.rb +1 -1
  206. data/lib/new_relic/agent/sampler_collection.rb +1 -1
  207. data/lib/new_relic/agent/samplers/cpu_sampler.rb +1 -1
  208. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +1 -1
  209. data/lib/new_relic/agent/samplers/memory_sampler.rb +1 -1
  210. data/lib/new_relic/agent/samplers/object_sampler.rb +1 -1
  211. data/lib/new_relic/agent/samplers/vm_sampler.rb +1 -1
  212. data/lib/new_relic/agent/span_event_aggregator.rb +49 -0
  213. data/lib/new_relic/agent/span_event_primitive.rb +206 -0
  214. data/lib/new_relic/agent/sql_sampler.rb +23 -7
  215. data/lib/new_relic/agent/stats.rb +1 -1
  216. data/lib/new_relic/agent/stats_engine.rb +4 -4
  217. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -1
  218. data/lib/new_relic/agent/stats_engine/stats_hash.rb +1 -1
  219. data/lib/new_relic/agent/supported_versions.rb +3 -3
  220. data/lib/new_relic/agent/synthetics_event_aggregator.rb +6 -13
  221. data/lib/new_relic/agent/system_info.rb +18 -8
  222. data/lib/new_relic/agent/threading/agent_thread.rb +6 -4
  223. data/lib/new_relic/agent/threading/backtrace_node.rb +1 -1
  224. data/lib/new_relic/agent/threading/backtrace_service.rb +4 -4
  225. data/lib/new_relic/agent/threading/thread_profile.rb +10 -24
  226. data/lib/new_relic/agent/timestamp_sampled_buffer.rb +19 -0
  227. data/lib/new_relic/agent/tracer.rb +509 -0
  228. data/lib/new_relic/agent/transaction.rb +191 -278
  229. data/lib/new_relic/agent/transaction/abstract_segment.rb +38 -5
  230. data/lib/new_relic/agent/transaction/datastore_segment.rb +30 -5
  231. data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
  232. data/lib/new_relic/agent/transaction/distributed_tracing.rb +123 -71
  233. data/lib/new_relic/agent/transaction/external_request_segment.rb +41 -89
  234. data/lib/new_relic/agent/transaction/message_broker_segment.rb +7 -8
  235. data/lib/new_relic/agent/transaction/request_attributes.rb +1 -1
  236. data/lib/new_relic/agent/transaction/segment.rb +42 -2
  237. data/lib/new_relic/agent/transaction/slowest_sample_buffer.rb +1 -1
  238. data/lib/new_relic/agent/transaction/synthetics_sample_buffer.rb +1 -1
  239. data/lib/new_relic/agent/transaction/trace.rb +7 -23
  240. data/lib/new_relic/agent/transaction/trace_builder.rb +1 -2
  241. data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
  242. data/lib/new_relic/agent/transaction/trace_node.rb +12 -8
  243. data/lib/new_relic/agent/transaction/tracing.rb +2 -100
  244. data/lib/new_relic/agent/transaction/transaction_sample_buffer.rb +1 -1
  245. data/lib/new_relic/agent/transaction_error_primitive.rb +13 -22
  246. data/lib/new_relic/agent/transaction_event_aggregator.rb +7 -16
  247. data/lib/new_relic/agent/transaction_event_primitive.rb +31 -42
  248. data/lib/new_relic/agent/transaction_event_recorder.rb +8 -16
  249. data/lib/new_relic/agent/transaction_metrics.rb +1 -1
  250. data/lib/new_relic/agent/transaction_sampler.rb +3 -14
  251. data/lib/new_relic/agent/transaction_time_aggregator.rb +155 -0
  252. data/lib/new_relic/agent/utilization/aws.rb +1 -1
  253. data/lib/new_relic/agent/utilization/azure.rb +1 -1
  254. data/lib/new_relic/agent/utilization/gcp.rb +1 -1
  255. data/lib/new_relic/agent/utilization/pcf.rb +1 -1
  256. data/lib/new_relic/agent/utilization/vendor.rb +1 -1
  257. data/lib/new_relic/agent/utilization_data.rb +37 -2
  258. data/lib/new_relic/agent/vm.rb +1 -1
  259. data/lib/new_relic/agent/vm/jruby_vm.rb +1 -1
  260. data/lib/new_relic/agent/vm/monotonic_gc_profiler.rb +1 -1
  261. data/lib/new_relic/agent/vm/mri_vm.rb +1 -1
  262. data/lib/new_relic/agent/vm/snapshot.rb +1 -1
  263. data/lib/new_relic/agent/worker_loop.rb +1 -1
  264. data/lib/new_relic/cli/command.rb +1 -1
  265. data/lib/new_relic/cli/commands/deployments.rb +2 -2
  266. data/lib/new_relic/cli/commands/install.rb +4 -3
  267. data/lib/new_relic/coerce.rb +32 -7
  268. data/lib/new_relic/collection_helper.rb +1 -1
  269. data/lib/new_relic/constants.rb +38 -0
  270. data/lib/new_relic/control.rb +1 -1
  271. data/lib/new_relic/control/class_methods.rb +8 -2
  272. data/lib/new_relic/control/frameworks.rb +1 -1
  273. data/lib/new_relic/control/frameworks/external.rb +1 -1
  274. data/lib/new_relic/control/frameworks/merb.rb +1 -1
  275. data/lib/new_relic/control/frameworks/rails.rb +1 -1
  276. data/lib/new_relic/control/frameworks/rails3.rb +1 -1
  277. data/lib/new_relic/control/frameworks/rails4.rb +1 -1
  278. data/lib/new_relic/control/frameworks/{rails5.rb → rails_notifications.rb} +2 -2
  279. data/lib/new_relic/control/frameworks/ruby.rb +1 -1
  280. data/lib/new_relic/control/frameworks/sinatra.rb +1 -1
  281. data/lib/new_relic/control/instance_methods.rb +30 -4
  282. data/lib/new_relic/control/instrumentation.rb +1 -1
  283. data/lib/new_relic/control/server_methods.rb +1 -1
  284. data/lib/new_relic/delayed_job_injection.rb +1 -1
  285. data/lib/new_relic/dependency_detection.rb +5 -5
  286. data/lib/new_relic/environment_report.rb +6 -2
  287. data/lib/new_relic/helper.rb +1 -1
  288. data/lib/new_relic/language_support.rb +1 -1
  289. data/lib/new_relic/latest_changes.rb +4 -4
  290. data/lib/new_relic/local_environment.rb +1 -1
  291. data/lib/new_relic/metric_data.rb +1 -1
  292. data/lib/new_relic/metric_spec.rb +1 -1
  293. data/lib/new_relic/noticed_error.rb +39 -18
  294. data/lib/new_relic/rack.rb +1 -1
  295. data/lib/new_relic/rack/agent_hooks.rb +1 -1
  296. data/lib/new_relic/rack/agent_middleware.rb +2 -2
  297. data/lib/new_relic/rack/browser_monitoring.rb +16 -9
  298. data/lib/new_relic/recipes.rb +1 -1
  299. data/lib/new_relic/recipes/capistrano3.rb +6 -3
  300. data/lib/new_relic/recipes/capistrano_legacy.rb +1 -1
  301. data/lib/new_relic/supportability_helper.rb +15 -1
  302. data/lib/new_relic/version.rb +4 -4
  303. data/lib/newrelic_rpm.rb +1 -1
  304. data/lib/sequel/extensions/newrelic_instrumentation.rb +3 -3
  305. data/lib/sequel/plugins/newrelic_instrumentation.rb +2 -2
  306. data/lib/tasks/all.rb +1 -1
  307. data/lib/tasks/config.rake +1 -2
  308. data/lib/tasks/multiverse.rb +35 -1
  309. data/lib/tasks/newrelic.rb +1 -1
  310. data/lib/tasks/tests.rake +6 -1
  311. data/newrelic_rpm.gemspec +23 -17
  312. data/recipes/newrelic.rb +1 -1
  313. data/test/agent_helper.rb +357 -78
  314. metadata +154 -60
  315. data/.travis.yml +0 -173
  316. data/lib/new_relic/agent/busy_calculator.rb +0 -117
  317. data/lib/new_relic/agent/commands/xray_session.rb +0 -55
  318. data/lib/new_relic/agent/commands/xray_session_collection.rb +0 -161
  319. data/lib/new_relic/agent/cross_app_monitor.rb +0 -140
  320. data/lib/new_relic/agent/cross_app_tracing.rb +0 -143
  321. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -29
  322. data/lib/new_relic/agent/distributed_trace_payload.rb +0 -223
  323. data/lib/new_relic/agent/distributed_trace_priority_sampled_buffer.rb +0 -72
  324. data/lib/new_relic/agent/http_clients/abstract_request.rb +0 -31
  325. data/lib/new_relic/agent/instrumentation/active_record_4.rb +0 -42
  326. data/lib/new_relic/agent/instrumentation/active_record_5.rb +0 -36
  327. data/lib/new_relic/agent/instrumentation/evented_subscriber.rb +0 -104
  328. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +0 -32
  329. data/lib/new_relic/agent/instrumentation/rails5/action_view.rb +0 -27
  330. data/lib/new_relic/agent/sampled_buffer.rb +0 -68
  331. data/lib/new_relic/agent/sized_buffer.rb +0 -23
  332. data/lib/new_relic/agent/synthetics_event_buffer.rb +0 -40
  333. data/lib/new_relic/agent/throughput_monitor.rb +0 -59
  334. data/lib/new_relic/agent/transaction/attributes.rb +0 -153
  335. data/lib/new_relic/agent/transaction/xray_sample_buffer.rb +0 -64
  336. data/lib/new_relic/agent/transaction_state.rb +0 -145
  337. data/lib/new_relic/agent/transaction_timings.rb +0 -57
  338. data/lib/new_relic/metrics.rb +0 -13
  339. data/lib/tasks/versions.html.erb +0 -28
  340. data/lib/tasks/versions.postface.html +0 -8
  341. data/lib/tasks/versions.preface.html +0 -9
  342. data/lib/tasks/versions.rake +0 -65
  343. data/lib/tasks/versions.txt.erb +0 -14
@@ -0,0 +1,104 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ module NewRelic
6
+ module Agent
7
+ class AdaptiveSampler
8
+
9
+ def initialize target_samples = 10, period_duration = 60
10
+ @target = target_samples
11
+ @seen = 0
12
+ @seen_last = 0
13
+ @sampled_count = 0
14
+ @period_duration = period_duration
15
+ @first_period = true
16
+ @period_start = Time.now.to_f
17
+ @lock = Mutex.new
18
+ register_config_callbacks
19
+ end
20
+
21
+ # Called at the beginning of each transaction, increments seen and
22
+ # returns a boolean indicating if we should mark the transaction as
23
+ # sampled. This uses the adaptive sampling algorithm.
24
+ def sampled?
25
+ @lock.synchronize do
26
+ reset_if_period_expired!
27
+ sampled = if @first_period
28
+ @sampled_count < 10
29
+ elsif @sampled_count < @target
30
+ rand(@seen_last) < @target
31
+ else
32
+ rand(@seen) < (@target ** (@target / @sampled_count) - @target ** 0.5)
33
+ end
34
+
35
+ @sampled_count += 1 if sampled
36
+ @seen += 1
37
+
38
+ sampled
39
+ end
40
+ end
41
+
42
+ def stats
43
+ @lock.synchronize do
44
+ {
45
+ target: @target,
46
+ seen: @seen,
47
+ seen_last: @seen_last,
48
+ sampled_count: @sampled_count
49
+ }
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def reset_if_period_expired!
56
+ now = Time.now.to_f
57
+ return unless @period_start + @period_duration <= now
58
+
59
+ elapsed_periods = Integer((now - @period_start) / @period_duration)
60
+ @period_start = @period_start + elapsed_periods * @period_duration
61
+
62
+ @first_period = false
63
+ @seen_last = elapsed_periods > 1 ? 0 : @seen
64
+ @seen = 0
65
+ @sampled_count = 0
66
+ end
67
+
68
+ def register_config_callbacks
69
+ register_sampling_target_callback
70
+ register_sampling_period_callback
71
+ end
72
+
73
+ def register_sampling_target_callback
74
+ NewRelic::Agent.config.register_callback(:sampling_target) do |target|
75
+ target_changed = false
76
+ @lock.synchronize do
77
+ if @target != target
78
+ @target = target
79
+ target_changed = true
80
+ end
81
+ end
82
+ if target_changed
83
+ NewRelic::Agent.logger.debug "Sampling target set to: #{target}"
84
+ end
85
+ end
86
+ end
87
+
88
+ def register_sampling_period_callback
89
+ NewRelic::Agent.config.register_callback(:sampling_target_period_in_seconds) do |period_duration|
90
+ period_changed = false
91
+ @lock.synchronize do
92
+ if @period_duration != period_duration
93
+ @period_duration = period_duration
94
+ period_changed = true
95
+ end
96
+ end
97
+ if period_changed
98
+ NewRelic::Agent.logger.debug "Sampling period set to: #{period_duration}"
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
- # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
4
 
5
5
  require 'socket'
6
6
  require 'net/https'
@@ -8,7 +8,8 @@ require 'net/http'
8
8
  require 'logger'
9
9
  require 'zlib'
10
10
  require 'stringio'
11
- require 'new_relic/agent/sampled_buffer'
11
+ require 'new_relic/constants'
12
+ require 'new_relic/coerce'
12
13
  require 'new_relic/agent/autostart'
13
14
  require 'new_relic/agent/harvester'
14
15
  require 'new_relic/agent/hostname'
@@ -18,19 +19,20 @@ require 'new_relic/agent/configuration/manager'
18
19
  require 'new_relic/agent/database'
19
20
  require 'new_relic/agent/commands/agent_command_router'
20
21
  require 'new_relic/agent/event_listener'
21
- require 'new_relic/agent/cross_app_monitor'
22
- require 'new_relic/agent/distributed_trace_monitor'
23
- require 'new_relic/agent/synthetics_monitor'
24
- require 'new_relic/agent/synthetics_event_buffer'
22
+ require 'new_relic/agent/distributed_tracing'
23
+ require 'new_relic/agent/monitors'
25
24
  require 'new_relic/agent/transaction_event_recorder'
26
25
  require 'new_relic/agent/custom_event_aggregator'
26
+ require 'new_relic/agent/span_event_aggregator'
27
27
  require 'new_relic/agent/sampler_collection'
28
28
  require 'new_relic/agent/javascript_instrumentor'
29
29
  require 'new_relic/agent/vm/monotonic_gc_profiler'
30
30
  require 'new_relic/agent/utilization_data'
31
31
  require 'new_relic/environment_report'
32
32
  require 'new_relic/agent/attribute_filter'
33
- require 'new_relic/agent/throughput_monitor'
33
+ require 'new_relic/agent/adaptive_sampler'
34
+ require 'new_relic/agent/connect/request_builder'
35
+ require 'new_relic/agent/connect/response_handler'
34
36
 
35
37
  module NewRelic
36
38
  module Agent
@@ -47,34 +49,33 @@ module NewRelic
47
49
  def initialize
48
50
  @started = false
49
51
  @event_loop = nil
52
+ @worker_thread = nil
50
53
 
51
54
  @service = NewRelicService.new
52
55
 
53
- @events = NewRelic::Agent::EventListener.new
54
- @stats_engine = NewRelic::Agent::StatsEngine.new
55
- @transaction_sampler = NewRelic::Agent::TransactionSampler.new
56
- @sql_sampler = NewRelic::Agent::SqlSampler.new
57
- @agent_command_router = NewRelic::Agent::Commands::AgentCommandRouter.new(@events)
58
- @cross_app_monitor = NewRelic::Agent::CrossAppMonitor.new(@events)
59
- @distributed_trace_monitor = NewRelic::Agent::DistributedTraceMonitor.new(@events)
60
- @synthetics_monitor = NewRelic::Agent::SyntheticsMonitor.new(@events)
61
- @error_collector = NewRelic::Agent::ErrorCollector.new
62
- @transaction_rules = NewRelic::Agent::RulesEngine.new
63
- @harvest_samplers = NewRelic::Agent::SamplerCollection.new(@events)
64
- @monotonic_gc_profiler = NewRelic::Agent::VM::MonotonicGCProfiler.new
65
- @javascript_instrumentor = NewRelic::Agent::JavascriptInstrumentor.new(@events)
66
- @throughput_monitor = NewRelic::Agent::ThroughputMonitor.new
67
-
68
- @harvester = NewRelic::Agent::Harvester.new(@events)
56
+ @events = EventListener.new
57
+ @stats_engine = StatsEngine.new
58
+ @transaction_sampler = TransactionSampler.new
59
+ @sql_sampler = SqlSampler.new
60
+ @agent_command_router = Commands::AgentCommandRouter.new @events
61
+ @monitors = Monitors.new @events
62
+ @error_collector = ErrorCollector.new @events
63
+ @transaction_rules = RulesEngine.new
64
+ @harvest_samplers = SamplerCollection.new @events
65
+ @monotonic_gc_profiler = VM::MonotonicGCProfiler.new
66
+ @javascript_instrumentor = JavascriptInstrumentor.new @events
67
+ @adaptive_sampler = AdaptiveSampler.new(Agent.config[:sampling_target],
68
+ Agent.config[:sampling_target_period_in_seconds])
69
+
70
+ @harvester = Harvester.new @events
69
71
  @after_fork_lock = Mutex.new
70
72
 
71
- @transaction_event_recorder = NewRelic::Agent::TransactionEventRecorder.new
72
-
73
- @custom_event_aggregator = NewRelic::Agent::CustomEventAggregator.new
73
+ @transaction_event_recorder = TransactionEventRecorder.new @events
74
+ @custom_event_aggregator = CustomEventAggregator.new @events
75
+ @span_event_aggregator = SpanEventAggregator.new @events
74
76
 
75
77
  @connect_state = :pending
76
78
  @connect_attempts = 0
77
- @environment_report = nil
78
79
  @waited_on_connect = nil
79
80
  @connected_pid = nil
80
81
 
@@ -87,13 +88,13 @@ module NewRelic
87
88
  def setup_attribute_filter
88
89
  refresh_attribute_filter
89
90
 
90
- @events.subscribe(:finished_configuring) do
91
+ @events.subscribe(:initial_configuration_complete) do
91
92
  refresh_attribute_filter
92
93
  end
93
94
  end
94
95
 
95
96
  def refresh_attribute_filter
96
- @attribute_filter = NewRelic::Agent::AttributeFilter.new(NewRelic::Agent.config)
97
+ @attribute_filter = AttributeFilter.new(Agent.config)
97
98
  end
98
99
 
99
100
  # contains all the class-level methods for NewRelic::Agent::Agent
@@ -126,25 +127,30 @@ module NewRelic
126
127
  # cross application tracing ids and encoding
127
128
  attr_reader :cross_process_id
128
129
  attr_reader :cross_app_encoding_bytes
129
- attr_reader :cross_app_monitor
130
130
  # service for communicating with collector
131
131
  attr_accessor :service
132
132
  # Global events dispatcher. This will provides our primary mechanism
133
133
  # for agent-wide events, such as finishing configuration, error notification
134
134
  # and request before/after from Rack.
135
135
  attr_reader :events
136
+
137
+ # listens and responds to events that need to process headers
138
+ # for synthetics and distributed tracing
139
+ attr_reader :monitors
136
140
  # Transaction and metric renaming rules as provided by the
137
141
  # collector on connect. The former are applied during txns,
138
142
  # the latter during harvest.
139
- attr_reader :transaction_rules
143
+ attr_accessor :transaction_rules
140
144
  # Responsbile for restarting the harvest thread
141
145
  attr_reader :harvester
142
146
  # GC::Profiler.total_time is not monotonic so we wrap it.
143
147
  attr_reader :monotonic_gc_profiler
144
148
  attr_reader :custom_event_aggregator
149
+ attr_reader :span_event_aggregator
145
150
  attr_reader :transaction_event_recorder
146
151
  attr_reader :attribute_filter
147
- attr_reader :throughput_monitor
152
+ attr_reader :adaptive_sampler
153
+ attr_reader :environment_report
148
154
 
149
155
  def transaction_event_aggregator
150
156
  @transaction_event_recorder.transaction_event_aggregator
@@ -154,6 +160,10 @@ module NewRelic
154
160
  @transaction_event_recorder.synthetics_event_aggregator
155
161
  end
156
162
 
163
+ def agent_id=(agent_id)
164
+ @service.agent_id = agent_id
165
+ end
166
+
157
167
  # This method should be called in a forked process after a fork.
158
168
  # It assumes the parent process initialized the agent, but does
159
169
  # not assume the agent started.
@@ -183,7 +193,8 @@ module NewRelic
183
193
  return if !needs_restart ||
184
194
  !Agent.config[:agent_enabled] ||
185
195
  !Agent.config[:monitor_mode] ||
186
- disconnected?
196
+ disconnected? ||
197
+ !control.security_settings_valid?
187
198
 
188
199
  ::NewRelic::Agent.logger.debug "Starting the worker thread in #{Process.pid} (parent #{Process.ppid}) after forking."
189
200
 
@@ -198,7 +209,7 @@ module NewRelic
198
209
  end
199
210
 
200
211
  def install_pipe_service(channel_id)
201
- @service = NewRelic::Agent::PipeService.new(channel_id)
212
+ @service = PipeService.new(channel_id)
202
213
  if connected?
203
214
  @connected_pid = Process.pid
204
215
  else
@@ -229,12 +240,25 @@ module NewRelic
229
240
  end
230
241
 
231
242
  def revert_to_default_configuration
232
- NewRelic::Agent.config.remove_config_type(:manual)
233
- NewRelic::Agent.config.remove_config_type(:server)
243
+ Agent.config.remove_config_type(:manual)
244
+ Agent.config.remove_config_type(:server)
234
245
  end
235
246
 
247
+ # If the @worker_thread encounters an error during the attempt to connect to the collector
248
+ # then the connect attempts enter an exponential backoff retry loop. To avoid potential
249
+ # race conditions with shutting down while also attempting to reconnect, we join the
250
+ # @worker_thread with a timeout threshold. This allows potentially connecting and flushing
251
+ # pending data to the server, but without waiting indefinitely for a reconnect to succeed.
252
+ # The use-case where this typically arises is in cronjob scheduled rake tasks where there's
253
+ # also some network stability/latency issues happening.
236
254
  def stop_event_loop
237
255
  @event_loop.stop if @event_loop
256
+ # Wait the end of the event loop thread.
257
+ if @worker_thread
258
+ unless @worker_thread.join(3)
259
+ ::NewRelic::Agent.logger.debug "Event loop thread did not stop within 3 seconds"
260
+ end
261
+ end
238
262
  end
239
263
 
240
264
  def trap_signals_for_litespeed
@@ -260,34 +284,24 @@ module NewRelic
260
284
  # should not record sql in the current thread. Returns the
261
285
  # previous value, if there is one
262
286
  def set_record_sql(should_record) #THREAD_LOCAL_ACCESS
263
- state = TransactionState.tl_get
287
+ state = Tracer.state
264
288
  prev = state.record_sql
265
289
  state.record_sql = should_record
266
290
  prev.nil? || prev
267
291
  end
268
292
 
269
- # Sets a thread local variable as to whether we should or
270
- # should not record transaction traces in the current
271
- # thread. Returns the previous value, if there is one
272
- def set_record_tt(should_record) #THREAD_LOCAL_ACCESS
273
- state = TransactionState.tl_get
274
- prev = state.record_tt
275
- state.record_tt = should_record
276
- prev.nil? || prev
277
- end
278
-
279
293
  # Push flag indicating whether we should be tracing in this
280
294
  # thread. This uses a stack which allows us to disable tracing
281
295
  # children of a transaction without affecting the tracing of
282
296
  # the whole transaction
283
297
  def push_trace_execution_flag(should_trace=false) #THREAD_LOCAL_ACCESS
284
- TransactionState.tl_get.push_traced(should_trace)
298
+ Tracer.state.push_traced(should_trace)
285
299
  end
286
300
 
287
301
  # Pop the current trace execution status. Restore trace execution status
288
302
  # to what it was before we pushed the current flag.
289
303
  def pop_trace_execution_flag #THREAD_LOCAL_ACCESS
290
- TransactionState.tl_get.pop_traced
304
+ Tracer.state.pop_traced
291
305
  end
292
306
 
293
307
  # Herein lies the corpse of the former 'start' method. May
@@ -335,7 +349,7 @@ module NewRelic
335
349
  end
336
350
 
337
351
  def log_app_name
338
- ::NewRelic::Agent.logger.info "Application: #{Agent.config.app_names.join(", ")}"
352
+ ::NewRelic::Agent.logger.info "Application: #{Agent.config[:app_name].join(", ")}"
339
353
  end
340
354
 
341
355
  def log_ignore_url_regexes
@@ -348,7 +362,7 @@ module NewRelic
348
362
 
349
363
  # Logs the configured application names
350
364
  def app_name_configured?
351
- names = Agent.config.app_names
365
+ names = Agent.config[:app_name]
352
366
  return names.respond_to?(:any?) && names.any?
353
367
  end
354
368
 
@@ -459,11 +473,11 @@ module NewRelic
459
473
  def defer_for_resque?
460
474
  NewRelic::Agent.config[:dispatcher] == :resque &&
461
475
  NewRelic::LanguageSupport.can_fork? &&
462
- !NewRelic::Agent::PipeChannelManager.listener.started?
476
+ !PipeChannelManager.listener.started?
463
477
  end
464
478
 
465
479
  def in_resque_child_process?
466
- defined?(@service) && @service.is_a?(NewRelic::Agent::PipeService)
480
+ defined?(@service) && @service.is_a?(PipeService)
467
481
  end
468
482
 
469
483
  # Sanity-check the agent configuration and start the agent,
@@ -484,8 +498,8 @@ module NewRelic
484
498
  @harvester.mark_started
485
499
 
486
500
  unless in_resque_child_process?
487
- generate_environment_report
488
501
  install_exit_handler
502
+ environment_for_connect
489
503
  @harvest_samplers.load_samplers unless Agent.config[:disable_samplers]
490
504
  end
491
505
 
@@ -535,7 +549,7 @@ module NewRelic
535
549
  check_config_and_start_agent
536
550
  log_version_and_pid
537
551
 
538
- events.subscribe(:finished_configuring) do
552
+ events.subscribe(:initial_configuration_complete) do
539
553
  log_ignore_url_regexes
540
554
  end
541
555
  end
@@ -547,10 +561,11 @@ module NewRelic
547
561
  @transaction_sampler.reset!
548
562
  @transaction_event_recorder.drop_buffered_data
549
563
  @custom_event_aggregator.reset!
564
+ @span_event_aggregator.reset!
550
565
  @sql_sampler.reset!
551
566
 
552
567
  if Agent.config[:clear_transaction_state_after_fork]
553
- TransactionState.tl_clear
568
+ Tracer.clear_state
554
569
  end
555
570
  end
556
571
 
@@ -558,13 +573,16 @@ module NewRelic
558
573
  # This is necessary for cases where we're in a forked child and Ruby
559
574
  # might be holding locks for background thread that aren't there anymore.
560
575
  def reset_objects_with_locks
561
- @stats_engine = NewRelic::Agent::StatsEngine.new
576
+ @stats_engine = StatsEngine.new
562
577
  end
563
578
 
564
579
  def flush_pipe_data
565
- if connected? && @service.is_a?(::NewRelic::Agent::PipeService)
580
+ if connected? && @service.is_a?(PipeService)
566
581
  transmit_data
567
- transmit_event_data
582
+ transmit_analytic_event_data
583
+ transmit_custom_event_data
584
+ transmit_error_event_data
585
+ transmit_span_event_data
568
586
  end
569
587
  end
570
588
 
@@ -578,40 +596,48 @@ module NewRelic
578
596
  EventLoop.new
579
597
  end
580
598
 
581
- # Never allow any data type to be reported more frequently than once
582
- # per second.
583
- MIN_ALLOWED_REPORT_PERIOD = 1.0
599
+ LOG_ONCE_KEYS_RESET_PERIOD = 60.0
584
600
 
585
- def report_period_for(method)
586
- config_key = "data_report_periods.#{method}".to_sym
587
- period = Agent.config[config_key]
588
- if !period
589
- period = Agent.config[:data_report_period]
590
- ::NewRelic::Agent.logger.warn("Could not find configured period for #{method}, falling back to data_report_period (#{period} s)")
591
- end
592
- if period < MIN_ALLOWED_REPORT_PERIOD
593
- ::NewRelic::Agent.logger.warn("Configured #{config_key} was #{period}, but minimum allowed is #{MIN_ALLOWED_REPORT_PERIOD}, using #{MIN_ALLOWED_REPORT_PERIOD}.")
594
- period = MIN_ALLOWED_REPORT_PERIOD
595
- end
596
- period
601
+ # Certain event types may sometimes need to be on the same interval as metrics,
602
+ # so we will check config assigned in EventHarvestConfig to determine the interval
603
+ # on which to report them
604
+ def interval_for event_type
605
+ interval = Agent.config[:"event_report_period.#{event_type}"]
606
+ :"#{interval}_second_harvest"
597
607
  end
598
608
 
599
- LOG_ONCE_KEYS_RESET_PERIOD = 60.0
609
+ ANALYTIC_EVENT_DATA = "analytic_event_data".freeze
610
+ CUSTOM_EVENT_DATA = "custom_event_data".freeze
611
+ ERROR_EVENT_DATA = "error_event_data".freeze
612
+ SPAN_EVENT_DATA = "span_event_data".freeze
600
613
 
601
614
  def create_and_run_event_loop
615
+ data_harvest = :"#{Agent.config[:data_report_period]}_second_harvest"
616
+ event_harvest = :"#{Agent.config[:event_report_period]}_second_harvest"
617
+
602
618
  @event_loop = create_event_loop
603
- @event_loop.on(:report_data) do
619
+ @event_loop.on(data_harvest) do
604
620
  transmit_data
605
621
  end
606
- @event_loop.on(:report_event_data) do
607
- transmit_event_data
622
+
623
+ @event_loop.on(interval_for ANALYTIC_EVENT_DATA) do
624
+ transmit_analytic_event_data
625
+ end
626
+ @event_loop.on(interval_for CUSTOM_EVENT_DATA) do
627
+ transmit_custom_event_data
628
+ end
629
+ @event_loop.on(interval_for ERROR_EVENT_DATA) do
630
+ transmit_error_event_data
631
+ end
632
+ @event_loop.on(interval_for SPAN_EVENT_DATA) do
633
+ transmit_span_event_data
608
634
  end
609
635
  @event_loop.on(:reset_log_once_keys) do
610
636
  ::NewRelic::Agent.logger.clear_already_logged
611
637
  end
612
- @event_loop.fire_every(Agent.config[:data_report_period], :report_data)
613
- @event_loop.fire_every(report_period_for(:analytic_event_data), :report_event_data)
614
- @event_loop.fire_every(LOG_ONCE_KEYS_RESET_PERIOD, :reset_log_once_keys)
638
+ @event_loop.fire_every(Agent.config[:data_report_period], data_harvest)
639
+ @event_loop.fire_every(Agent.config[:event_report_period], event_harvest)
640
+ @event_loop.fire_every(LOG_ONCE_KEYS_RESET_PERIOD, :reset_log_once_keys)
615
641
 
616
642
  @event_loop.run
617
643
  end
@@ -631,7 +657,7 @@ module NewRelic
631
657
  # is the worker thread that gathers data and talks to the
632
658
  # server.
633
659
  def handle_force_disconnect(error)
634
- ::NewRelic::Agent.logger.warn "New Relic forced this agent to disconnect (#{error.message})"
660
+ ::NewRelic::Agent.logger.warn "Agent received a ForceDisconnectException from the server, disconnecting. (#{error.message})"
635
661
  disconnect
636
662
  end
637
663
 
@@ -639,7 +665,7 @@ module NewRelic
639
665
  # it and disconnecting the agent, since we are now in an
640
666
  # unknown state.
641
667
  def handle_other_error(error)
642
- ::NewRelic::Agent.logger.error "Unhandled error in worker thread, disconnecting this agent process:"
668
+ ::NewRelic::Agent.logger.error "Unhandled error in worker thread, disconnecting."
643
669
  # These errors are fatal (that is, they will prevent the agent from
644
670
  # reporting entirely), so we really want backtraces when they happen
645
671
  ::NewRelic::Agent.logger.log_exception(:error, error)
@@ -695,7 +721,7 @@ module NewRelic
695
721
  end
696
722
 
697
723
  ::NewRelic::Agent.logger.debug "Creating Ruby Agent worker thread."
698
- @worker_thread = NewRelic::Agent::Threading::AgentThread.create('Worker Loop') do
724
+ @worker_thread = Threading::AgentThread.create('Worker Loop') do
699
725
  deferred_work!(connection_options)
700
726
  end
701
727
  end
@@ -734,12 +760,12 @@ module NewRelic
734
760
  force || (!connected? && !disconnected?)
735
761
  end
736
762
 
737
- # Retry period is a minute for each failed attempt that
738
- # we've made. This should probably do some sort of sane TCP
739
- # backoff to prevent hammering the server, but a minute for
740
- # each attempt seems to work reasonably well.
763
+ # Per the spec at
764
+ # /agents/agent-specs/Collector-Response-Handling.md, retry
765
+ # connections after a specific backoff sequence to prevent
766
+ # hammering the server.
741
767
  def connect_retry_period
742
- [600, connect_attempts * 60].min
768
+ NewRelic::CONNECT_RETRY_PERIODS[connect_attempts] || NewRelic::MAX_RETRY_PERIOD
743
769
  end
744
770
 
745
771
  def note_connect_failure
@@ -773,91 +799,59 @@ module NewRelic
773
799
  shutdown
774
800
  end
775
801
 
776
- def generate_environment_report
777
- @environment_report = environment_for_connect
778
- end
779
-
780
802
  # Checks whether we should send environment info, and if so,
781
803
  # returns the snapshot from the local environment.
782
804
  # Generating the EnvironmentReport has the potential to trigger
783
805
  # require calls in Rails environments, so this method should only
784
806
  # be called synchronously from on the main thread.
785
807
  def environment_for_connect
786
- Agent.config[:send_environment_info] ? Array(EnvironmentReport.new) : []
808
+ @environment_report ||= Agent.config[:send_environment_info] ? Array(EnvironmentReport.new) : []
787
809
  end
788
810
 
789
- # We've seen objects in the environment report (Rails.env in
790
- # particular) that can't seralize to JSON. Cope with that here and
791
- # clear out so downstream code doesn't have to check again.
792
- def sanitize_environment_report
793
- if !@service.valid_to_marshal?(@environment_report)
794
- @environment_report = []
795
- end
811
+ # Constructs and memoizes an event_harvest_config hash to be used in
812
+ # the payload sent during connect (and reconnect)
813
+ def event_harvest_config
814
+ @event_harvest_config ||= Configuration::EventHarvestConfig.from_config(Agent.config)
796
815
  end
797
816
 
798
- # Initializes the hash of settings that we send to the
799
- # server. Returns a literal hash containing the options
800
- def connect_settings
801
- sanitize_environment_report
802
-
803
- {
804
- :pid => $$,
805
- :host => local_host,
806
- :display_host => Agent.config[:'process_host.display_name'],
807
- :app_name => Agent.config.app_names,
808
- :language => 'ruby',
809
- :labels => Agent.config.parsed_labels,
810
- :agent_version => NewRelic::VERSION::STRING,
811
- :environment => @environment_report,
812
- :settings => Agent.config.to_collector_hash,
813
- :high_security => Agent.config[:high_security],
814
- :utilization => UtilizationData.new.to_collector_hash,
815
- :identifier => "ruby:#{local_host}:#{Agent.config.app_names.sort.join(',')}"
816
- }
817
- end
818
-
819
- # Returns connect data passed back from the server
817
+ # Builds the payload to send to the connect service,
818
+ # connects, then configures the agent using the response from
819
+ # the connect service
820
820
  def connect_to_server
821
- @service.connect(connect_settings)
822
- end
821
+ request_builder = ::NewRelic::Agent::Connect::RequestBuilder.new \
822
+ @service,
823
+ Agent.config,
824
+ event_harvest_config,
825
+ environment_for_connect
826
+ connect_response = @service.connect request_builder.connect_payload
823
827
 
824
- # apdex_f is always 4 times the apdex_t
825
- def apdex_f
826
- (4 * Agent.config[:apdex_t]).to_f
827
- end
828
+ response_handler = ::NewRelic::Agent::Connect::ResponseHandler.new(self, Agent.config)
829
+ response_handler.configure_agent(connect_response)
828
830
 
829
- # Sets the collector host and connects to the server, then
830
- # invokes the final configuration with the returned data
831
- def query_server_for_configuration
832
- finish_setup(connect_to_server)
831
+ log_connection connect_response if connect_response
832
+ connect_response
833
833
  end
834
834
 
835
- # Takes a hash of configuration data returned from the
836
- # server and uses it to set local variables and to
837
- # initialize various parts of the agent that are configured
838
- # separately.
839
- #
840
- # Can accommodate most arbitrary data - anything extra is
841
- # ignored unless we say to do something with it here.
842
- def finish_setup(config_data)
843
- return if config_data == nil
844
-
845
- @service.agent_id = config_data['agent_run_id']
846
-
847
- if config_data['agent_config']
848
- ::NewRelic::Agent.logger.debug "Using config from server"
835
+ # Logs when we connect to the server, for debugging purposes
836
+ # - makes sure we know if an agent has not connected
837
+ def log_connection(config_data)
838
+ ::NewRelic::Agent.logger.debug "Connected to NewRelic Service at #{@service.collector.name}"
839
+ ::NewRelic::Agent.logger.debug "Agent Run = #{@service.agent_id}."
840
+ ::NewRelic::Agent.logger.debug "Connection data = #{config_data.inspect}"
841
+ if config_data['messages'] && config_data['messages'].any?
842
+ log_collector_messages(config_data['messages'])
849
843
  end
844
+ end
850
845
 
851
- ::NewRelic::Agent.logger.debug "Server provided config: #{config_data.inspect}"
852
- server_config = NewRelic::Agent::Configuration::ServerSource.new(config_data, Agent.config)
853
- Agent.config.replace_or_add_config(server_config)
854
- log_connection!(config_data)
855
-
856
- @transaction_rules = RulesEngine.create_transaction_rules(config_data)
857
- @stats_engine.metric_rules = RulesEngine.create_metric_rules(config_data)
846
+ def log_collector_messages(messages)
847
+ messages.each do |message|
848
+ ::NewRelic::Agent.logger.send(message['level'].downcase, message['message'])
849
+ end
850
+ end
858
851
 
859
- # If you're adding something else here to respond to the server-side config,
860
- # use Agent.instance.events.subscribe(:finished_configuring) callback instead!
852
+ # apdex_f is always 4 times the apdex_t
853
+ def apdex_f
854
+ (4 * Agent.config[:apdex_t]).to_f
861
855
  end
862
856
 
863
857
  class WaitOnConnectTimeout < StandardError
@@ -889,22 +883,6 @@ module NewRelic
889
883
  end
890
884
  end
891
885
 
892
- # Logs when we connect to the server, for debugging purposes
893
- # - makes sure we know if an agent has not connected
894
- def log_connection!(config_data)
895
- ::NewRelic::Agent.logger.debug "Connected to NewRelic Service at #{@service.collector.name}"
896
- ::NewRelic::Agent.logger.debug "Agent Run = #{@service.agent_id}."
897
- ::NewRelic::Agent.logger.debug "Connection data = #{config_data.inspect}"
898
- if config_data['messages'] && config_data['messages'].any?
899
- log_collector_messages(config_data['messages'])
900
- end
901
- end
902
-
903
- def log_collector_messages(messages)
904
- messages.each do |message|
905
- ::NewRelic::Agent.logger.send(message['level'].downcase, message['message'])
906
- end
907
- end
908
886
  end
909
887
  include Connect
910
888
 
@@ -916,6 +894,7 @@ module NewRelic
916
894
  when :error_event_data then @error_collector.error_event_aggregator
917
895
  when :analytic_event_data then transaction_event_aggregator
918
896
  when :custom_event_data then @custom_event_aggregator
897
+ when :span_event_data then span_event_aggregator
919
898
  when :sql_trace_data then @sql_sampler
920
899
  end
921
900
  end
@@ -963,7 +942,7 @@ module NewRelic
963
942
  return unless should_connect?(opts[:force_reconnect])
964
943
 
965
944
  ::NewRelic::Agent.logger.debug "Connecting Process to New Relic: #$0"
966
- query_server_for_configuration
945
+ connect_to_server
967
946
  @connected_pid = $$
968
947
  @connect_state = :connected
969
948
  signal_connected
@@ -974,6 +953,10 @@ module NewRelic
974
953
  rescue NewRelic::Agent::UnrecoverableAgentException => e
975
954
  handle_unrecoverable_agent_error(e)
976
955
  rescue StandardError, Timeout::Error, NewRelic::Agent::ServerConnectionException => e
956
+ # Allow a killed (aborting) thread to continue exiting during shutdown.
957
+ # See: https://github.com/newrelic/newrelic-ruby-agent/issues/340
958
+ raise if Thread.current.status == 'aborting'
959
+
977
960
  log_error(e)
978
961
  if opts[:keep_retrying]
979
962
  note_connect_failure
@@ -987,15 +970,6 @@ module NewRelic
987
970
  raise
988
971
  end
989
972
 
990
- # Who am I? Well, this method can tell you your hostname.
991
- def determine_host
992
- NewRelic::Agent::Hostname.get
993
- end
994
-
995
- def local_host
996
- @local_host ||= determine_host
997
- end
998
-
999
973
  # Delegates to the control class to determine the root
1000
974
  # directory of this project
1001
975
  def determine_home_directory
@@ -1066,7 +1040,7 @@ module NewRelic
1066
1040
  end
1067
1041
 
1068
1042
  def harvest_and_send_timeslice_data
1069
- NewRelic::Agent::BusyCalculator.harvest_busy
1043
+ TransactionTimeAggregator.harvest!
1070
1044
  harvest_and_send_from_container(@stats_engine, :metric_data)
1071
1045
  end
1072
1046
 
@@ -1094,19 +1068,28 @@ module NewRelic
1094
1068
 
1095
1069
  def harvest_and_send_analytic_event_data
1096
1070
  harvest_and_send_from_container(transaction_event_aggregator, :analytic_event_data)
1097
- harvest_and_send_from_container(synthetics_event_aggregator, :analytic_event_data)
1098
- harvest_and_send_from_container(@custom_event_aggregator, :custom_event_data)
1071
+ harvest_and_send_from_container(synthetics_event_aggregator, :analytic_event_data)
1072
+ end
1073
+
1074
+ def harvest_and_send_custom_event_data
1075
+ harvest_and_send_from_container(@custom_event_aggregator, :custom_event_data)
1099
1076
  end
1100
1077
 
1101
1078
  def harvest_and_send_error_event_data
1102
1079
  harvest_and_send_from_container @error_collector.error_event_aggregator, :error_event_data
1103
1080
  end
1104
1081
 
1082
+ def harvest_and_send_span_event_data
1083
+ harvest_and_send_from_container(span_event_aggregator, :span_event_data)
1084
+ end
1085
+
1105
1086
  def check_for_and_handle_agent_commands
1106
1087
  begin
1107
1088
  @agent_command_router.check_for_and_handle_agent_commands
1108
1089
  rescue ForceRestartException, ForceDisconnectException
1109
1090
  raise
1091
+ rescue UnrecoverableServerException => e
1092
+ NewRelic::Agent.logger.warn("get_agent_commands message was rejected by remote service, discarding. Error: ", e)
1110
1093
  rescue ServerConnectionException => e
1111
1094
  log_remote_unavailable(:get_agent_commands, e)
1112
1095
  rescue => e
@@ -1120,14 +1103,30 @@ module NewRelic
1120
1103
  NewRelic::Agent.record_metric("Supportability/remote_unavailable/#{endpoint.to_s}", 0.0)
1121
1104
  end
1122
1105
 
1123
- def transmit_event_data
1124
- transmit_single_data_type(:harvest_and_send_analytic_event_data, "TransactionEvent")
1106
+ TRANSACTION_EVENT = "TransactionEvent".freeze
1107
+ def transmit_analytic_event_data
1108
+ transmit_single_data_type(:harvest_and_send_analytic_event_data, TRANSACTION_EVENT)
1109
+ end
1110
+
1111
+ CUSTOM_EVENT = "CustomEvent".freeze
1112
+ def transmit_custom_event_data
1113
+ transmit_single_data_type(:harvest_and_send_custom_event_data, CUSTOM_EVENT)
1114
+ end
1115
+
1116
+ ERROR_EVENT = "ErrorEvent".freeze
1117
+ def transmit_error_event_data
1118
+ transmit_single_data_type(:harvest_and_send_error_event_data, ERROR_EVENT)
1119
+ end
1120
+
1121
+ SPAN_EVENT = "SpanEvent".freeze
1122
+ def transmit_span_event_data
1123
+ transmit_single_data_type(:harvest_and_send_span_event_data, SPAN_EVENT)
1125
1124
  end
1126
1125
 
1127
1126
  def transmit_single_data_type(harvest_method, supportability_name)
1128
1127
  now = Time.now
1129
1128
 
1130
- msg = "Sending #{harvest_method.to_s.gsub("harvest_and_send_", "")} to New Relic Service"
1129
+ msg = "Sending #{supportability_name} data to New Relic Service"
1131
1130
  ::NewRelic::Agent.logger.debug msg
1132
1131
 
1133
1132
  @service.session do # use http keep-alive
@@ -1149,12 +1148,12 @@ module NewRelic
1149
1148
  harvest_and_send_transaction_traces
1150
1149
  harvest_and_send_slowest_sql
1151
1150
  harvest_and_send_timeslice_data
1151
+ harvest_and_send_span_event_data
1152
1152
 
1153
1153
  check_for_and_handle_agent_commands
1154
1154
  harvest_and_send_for_agent_commands
1155
1155
  end
1156
1156
  ensure
1157
- throughput_monitor.reset!
1158
1157
  NewRelic::Agent::Database.close_connections
1159
1158
  duration = (Time.now - now).to_f
1160
1159
  NewRelic::Agent.record_metric('Supportability/Harvest', duration)
@@ -1174,7 +1173,10 @@ module NewRelic
1174
1173
 
1175
1174
  @events.notify(:before_shutdown)
1176
1175
  transmit_data
1177
- transmit_event_data
1176
+ transmit_analytic_event_data
1177
+ transmit_custom_event_data
1178
+ transmit_error_event_data
1179
+ transmit_span_event_data
1178
1180
 
1179
1181
  if @connected_pid == $$ && !@service.kind_of?(NewRelic::Agent::NewRelicService)
1180
1182
  ::NewRelic::Agent.logger.debug "Sending New Relic service agent run shutdown message"