newrelic_rpm 9.0.0 → 9.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (247) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +7 -81
  4. data/.rubocop_todo.yml +4 -43
  5. data/.simplecov +2 -1
  6. data/Brewfile +1 -0
  7. data/CHANGELOG.md +101 -2
  8. data/Gemfile +1 -1
  9. data/Guardfile +8 -7
  10. data/README.md +1 -2
  11. data/Rakefile +8 -8
  12. data/Thorfile +1 -1
  13. data/bin/newrelic +1 -0
  14. data/bin/newrelic_cmd +1 -0
  15. data/bin/nrdebug +20 -20
  16. data/init.rb +1 -1
  17. data/lib/new_relic/agent/agent.rb +3 -16
  18. data/lib/new_relic/agent/agent_helpers/connect.rb +4 -9
  19. data/lib/new_relic/agent/agent_helpers/harvest.rb +4 -4
  20. data/lib/new_relic/agent/agent_helpers/shutdown.rb +5 -5
  21. data/lib/new_relic/agent/agent_helpers/special_startup.rb +2 -2
  22. data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +47 -39
  23. data/lib/new_relic/agent/agent_helpers/startup.rb +13 -13
  24. data/lib/new_relic/agent/agent_helpers/transmit.rb +6 -6
  25. data/lib/new_relic/agent/agent_logger.rb +9 -9
  26. data/lib/new_relic/agent/attribute_filter.rb +2 -2
  27. data/lib/new_relic/agent/attribute_processing.rb +2 -2
  28. data/lib/new_relic/agent/audit_logger.rb +5 -5
  29. data/lib/new_relic/agent/autostart.rb +1 -1
  30. data/lib/new_relic/agent/commands/agent_command.rb +2 -2
  31. data/lib/new_relic/agent/commands/agent_command_router.rb +5 -4
  32. data/lib/new_relic/agent/commands/thread_profiler_session.rb +5 -5
  33. data/lib/new_relic/agent/configuration/default_source.rb +106 -118
  34. data/lib/new_relic/agent/configuration/environment_source.rb +2 -2
  35. data/lib/new_relic/agent/configuration/high_security_source.rb +3 -3
  36. data/lib/new_relic/agent/configuration/manager.rb +15 -2
  37. data/lib/new_relic/agent/configuration/security_policy_source.rb +10 -10
  38. data/lib/new_relic/agent/configuration/server_source.rb +24 -24
  39. data/lib/new_relic/agent/configuration/yaml_source.rb +4 -4
  40. data/lib/new_relic/agent/connect/request_builder.rb +1 -1
  41. data/lib/new_relic/agent/connect/response_handler.rb +1 -1
  42. data/lib/new_relic/agent/custom_event_aggregator.rb +4 -4
  43. data/lib/new_relic/agent/database/explain_plan_helpers.rb +1 -1
  44. data/lib/new_relic/agent/database/obfuscation_helpers.rb +2 -2
  45. data/lib/new_relic/agent/database/obfuscator.rb +2 -2
  46. data/lib/new_relic/agent/database.rb +4 -4
  47. data/lib/new_relic/agent/datastores/metric_helper.rb +5 -5
  48. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +11 -11
  49. data/lib/new_relic/agent/datastores/redis.rb +6 -6
  50. data/lib/new_relic/agent/distributed_tracing/cross_app_tracing.rb +7 -8
  51. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +10 -10
  52. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +7 -7
  53. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +1 -1
  54. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +6 -6
  55. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +7 -7
  56. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +4 -4
  57. data/lib/new_relic/agent/distributed_tracing.rb +5 -5
  58. data/lib/new_relic/agent/error_collector.rb +35 -7
  59. data/lib/new_relic/agent/error_filter.rb +2 -2
  60. data/lib/new_relic/agent/error_trace_aggregator.rb +5 -4
  61. data/lib/new_relic/agent/event_loop.rb +3 -3
  62. data/lib/new_relic/agent/external.rb +1 -1
  63. data/lib/new_relic/agent/harvester.rb +1 -3
  64. data/lib/new_relic/agent/heap.rb +2 -1
  65. data/lib/new_relic/agent/hostname.rb +1 -1
  66. data/lib/new_relic/agent/http_clients/abstract.rb +9 -21
  67. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +7 -3
  68. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +7 -5
  69. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +4 -0
  70. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +6 -2
  71. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +5 -1
  72. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +8 -5
  73. data/lib/new_relic/agent/http_clients/uri_util.rb +2 -2
  74. data/lib/new_relic/agent/instrumentation/action_controller_other_subscriber.rb +5 -2
  75. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +3 -2
  76. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +3 -3
  77. data/lib/new_relic/agent/instrumentation/active_job.rb +1 -1
  78. data/lib/new_relic/agent/instrumentation/active_merchant.rb +3 -3
  79. data/lib/new_relic/agent/instrumentation/active_record.rb +3 -2
  80. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +38 -35
  81. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +3 -3
  82. data/lib/new_relic/agent/instrumentation/active_record_prepend.rb +2 -2
  83. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +3 -2
  84. data/lib/new_relic/agent/instrumentation/bunny/chain.rb +1 -1
  85. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +3 -3
  86. data/lib/new_relic/agent/instrumentation/bunny.rb +1 -1
  87. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +5 -3
  88. data/lib/new_relic/agent/instrumentation/curb/chain.rb +1 -1
  89. data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +5 -5
  90. data/lib/new_relic/agent/instrumentation/curb.rb +1 -1
  91. data/lib/new_relic/agent/instrumentation/custom_events_subscriber.rb +2 -1
  92. data/lib/new_relic/agent/instrumentation/delayed_job/chain.rb +1 -0
  93. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +1 -1
  94. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +2 -1
  95. data/lib/new_relic/agent/instrumentation/elasticsearch/chain.rb +2 -2
  96. data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +4 -3
  97. data/lib/new_relic/agent/instrumentation/elasticsearch.rb +2 -2
  98. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +2 -1
  99. data/lib/new_relic/agent/instrumentation/excon.rb +3 -3
  100. data/lib/new_relic/agent/instrumentation/fiber/chain.rb +1 -1
  101. data/lib/new_relic/agent/instrumentation/fiber.rb +1 -1
  102. data/lib/new_relic/agent/instrumentation/grape/instrumentation.rb +4 -4
  103. data/lib/new_relic/agent/instrumentation/grape.rb +3 -3
  104. data/lib/new_relic/agent/instrumentation/grpc/client/chain.rb +1 -1
  105. data/lib/new_relic/agent/instrumentation/grpc/client/instrumentation.rb +3 -2
  106. data/lib/new_relic/agent/instrumentation/grpc/client/prepend.rb +1 -1
  107. data/lib/new_relic/agent/instrumentation/grpc/client/request_wrapper.rb +1 -1
  108. data/lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb +4 -4
  109. data/lib/new_relic/agent/instrumentation/grpc_client.rb +1 -1
  110. data/lib/new_relic/agent/instrumentation/grpc_server.rb +1 -1
  111. data/lib/new_relic/agent/instrumentation/httprb.rb +1 -1
  112. data/lib/new_relic/agent/instrumentation/ignore_actions.rb +1 -1
  113. data/lib/new_relic/agent/instrumentation/logger.rb +1 -1
  114. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +3 -1
  115. data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +2 -2
  116. data/lib/new_relic/agent/instrumentation/memcache/prepend.rb +3 -1
  117. data/lib/new_relic/agent/instrumentation/memcache.rb +8 -8
  118. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +3 -3
  119. data/lib/new_relic/agent/instrumentation/middleware_tracing.rb +1 -1
  120. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +6 -6
  121. data/lib/new_relic/agent/instrumentation/net_http.rb +1 -1
  122. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +1 -1
  123. data/lib/new_relic/agent/instrumentation/padrino/instrumentation.rb +3 -3
  124. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +2 -2
  125. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +1 -1
  126. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +3 -2
  127. data/lib/new_relic/agent/instrumentation/rails_middleware.rb +1 -1
  128. data/lib/new_relic/agent/instrumentation/rake/instrumentation.rb +7 -7
  129. data/lib/new_relic/agent/instrumentation/rake.rb +2 -2
  130. data/lib/new_relic/agent/instrumentation/redis/constants.rb +1 -1
  131. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +2 -1
  132. data/lib/new_relic/agent/instrumentation/resque/helper.rb +1 -1
  133. data/lib/new_relic/agent/instrumentation/sequel.rb +4 -4
  134. data/lib/new_relic/agent/instrumentation/sequel_helper.rb +10 -10
  135. data/lib/new_relic/agent/instrumentation/sidekiq/extensions/delayed_class.rb +1 -1
  136. data/lib/new_relic/agent/instrumentation/sidekiq/server.rb +1 -1
  137. data/lib/new_relic/agent/instrumentation/sidekiq.rb +1 -0
  138. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +1 -1
  139. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +6 -5
  140. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +1 -1
  141. data/lib/new_relic/agent/instrumentation/tilt/instrumentation.rb +2 -1
  142. data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +5 -5
  143. data/lib/new_relic/agent/javascript_instrumentor.rb +29 -24
  144. data/lib/new_relic/agent/linking_metadata.rb +2 -2
  145. data/lib/new_relic/agent/log_event_aggregator.rb +15 -15
  146. data/lib/new_relic/agent/logging.rb +73 -33
  147. data/lib/new_relic/agent/messaging.rb +7 -5
  148. data/lib/new_relic/agent/method_tracer.rb +7 -7
  149. data/lib/new_relic/agent/method_tracer_helpers.rb +1 -1
  150. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +4 -4
  151. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +1 -1
  152. data/lib/new_relic/agent/new_relic_service/encoders.rb +1 -1
  153. data/lib/new_relic/agent/new_relic_service/json_marshaller.rb +2 -2
  154. data/lib/new_relic/agent/new_relic_service.rb +14 -15
  155. data/lib/new_relic/agent/obfuscator.rb +1 -1
  156. data/lib/new_relic/agent/parameter_filtering.rb +6 -6
  157. data/lib/new_relic/agent/pipe_channel_manager.rb +7 -6
  158. data/lib/new_relic/agent/pipe_service.rb +5 -3
  159. data/lib/new_relic/agent/priority_sampled_buffer.rb +1 -1
  160. data/lib/new_relic/agent/sampler.rb +2 -1
  161. data/lib/new_relic/agent/sampler_collection.rb +1 -1
  162. data/lib/new_relic/agent/samplers/cpu_sampler.rb +5 -5
  163. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +9 -9
  164. data/lib/new_relic/agent/samplers/memory_sampler.rb +9 -9
  165. data/lib/new_relic/agent/samplers/object_sampler.rb +1 -1
  166. data/lib/new_relic/agent/span_event_aggregator.rb +4 -4
  167. data/lib/new_relic/agent/span_event_primitive.rb +2 -2
  168. data/lib/new_relic/agent/sql_sampler.rb +2 -2
  169. data/lib/new_relic/agent/stats.rb +1 -1
  170. data/lib/new_relic/agent/synthetics_event_aggregator.rb +1 -1
  171. data/lib/new_relic/agent/system_info.rb +10 -10
  172. data/lib/new_relic/agent/threading/agent_thread.rb +1 -1
  173. data/lib/new_relic/agent/threading/backtrace_node.rb +3 -3
  174. data/lib/new_relic/agent/threading/backtrace_service.rb +3 -7
  175. data/lib/new_relic/agent/threading/thread_profile.rb +5 -5
  176. data/lib/new_relic/agent/timestamp_sampled_buffer.rb +1 -1
  177. data/lib/new_relic/agent/tracer.rb +9 -8
  178. data/lib/new_relic/agent/transaction/abstract_segment.rb +52 -46
  179. data/lib/new_relic/agent/transaction/datastore_segment.rb +1 -1
  180. data/lib/new_relic/agent/transaction/distributed_tracer.rb +7 -6
  181. data/lib/new_relic/agent/transaction/distributed_tracing.rb +13 -14
  182. data/lib/new_relic/agent/transaction/external_request_segment.rb +11 -11
  183. data/lib/new_relic/agent/transaction/message_broker_segment.rb +1 -1
  184. data/lib/new_relic/agent/transaction/request_attributes.rb +2 -2
  185. data/lib/new_relic/agent/transaction/segment.rb +1 -1
  186. data/lib/new_relic/agent/transaction/synthetics_sample_buffer.rb +1 -1
  187. data/lib/new_relic/agent/transaction/trace.rb +1 -1
  188. data/lib/new_relic/agent/transaction/trace_context.rb +4 -4
  189. data/lib/new_relic/agent/transaction/trace_node.rb +8 -7
  190. data/lib/new_relic/agent/transaction/tracing.rb +8 -2
  191. data/lib/new_relic/agent/transaction/transaction_sample_buffer.rb +1 -1
  192. data/lib/new_relic/agent/transaction.rb +43 -36
  193. data/lib/new_relic/agent/transaction_error_primitive.rb +5 -5
  194. data/lib/new_relic/agent/transaction_event_aggregator.rb +4 -4
  195. data/lib/new_relic/agent/transaction_event_primitive.rb +3 -3
  196. data/lib/new_relic/agent/transaction_sampler.rb +3 -3
  197. data/lib/new_relic/agent/transaction_time_aggregator.rb +7 -3
  198. data/lib/new_relic/agent/utilization/aws.rb +1 -1
  199. data/lib/new_relic/agent/utilization/azure.rb +3 -3
  200. data/lib/new_relic/agent/utilization/gcp.rb +3 -3
  201. data/lib/new_relic/agent/utilization/pcf.rb +1 -1
  202. data/lib/new_relic/agent/utilization/vendor.rb +1 -1
  203. data/lib/new_relic/agent/vm/monotonic_gc_profiler.rb +1 -1
  204. data/lib/new_relic/agent/vm/mri_vm.rb +9 -8
  205. data/lib/new_relic/agent/worker_loop.rb +1 -1
  206. data/lib/new_relic/agent.rb +78 -16
  207. data/lib/new_relic/cli/command.rb +3 -3
  208. data/lib/new_relic/cli/commands/deployments.rb +26 -25
  209. data/lib/new_relic/cli/commands/install.rb +14 -14
  210. data/lib/new_relic/collection_helper.rb +2 -2
  211. data/lib/new_relic/constants.rb +7 -8
  212. data/lib/new_relic/control/class_methods.rb +3 -3
  213. data/lib/new_relic/control/frameworks/rails.rb +30 -18
  214. data/lib/new_relic/control/instance_methods.rb +5 -5
  215. data/lib/new_relic/control/instrumentation.rb +1 -15
  216. data/lib/new_relic/control/private_instance_methods.rb +1 -1
  217. data/lib/new_relic/dependency_detection.rb +14 -18
  218. data/lib/new_relic/environment_report.rb +4 -4
  219. data/lib/new_relic/helper.rb +2 -1
  220. data/lib/new_relic/language_support.rb +1 -1
  221. data/lib/new_relic/latest_changes.rb +5 -5
  222. data/lib/new_relic/noticed_error.rb +20 -18
  223. data/lib/new_relic/rack/browser_monitoring.rb +20 -16
  224. data/lib/new_relic/recipes/capistrano3.rb +1 -1
  225. data/lib/new_relic/recipes/capistrano_legacy.rb +6 -6
  226. data/lib/new_relic/recipes/helpers/send_deployment.rb +5 -5
  227. data/lib/new_relic/supportability_helper.rb +2 -0
  228. data/lib/new_relic/version.rb +2 -2
  229. data/lib/newrelic_rpm.rb +3 -3
  230. data/lib/sequel/extensions/new_relic_instrumentation.rb +3 -3
  231. data/lib/sequel/plugins/new_relic_instrumentation.rb +1 -1
  232. data/lib/tasks/config.rake +12 -12
  233. data/lib/tasks/coverage_report.rake +4 -4
  234. data/lib/tasks/helpers/format.rb +9 -9
  235. data/lib/tasks/helpers/removers.rb +5 -5
  236. data/lib/tasks/install.rake +4 -4
  237. data/lib/tasks/instrumentation_generator/instrumentation.thor +8 -8
  238. data/lib/tasks/instrumentation_generator/templates/chain.tt +1 -1
  239. data/lib/tasks/instrumentation_generator/templates/dependency_detection.tt +1 -1
  240. data/lib/tasks/multiverse.rb +13 -6
  241. data/lib/tasks/newrelic.rb +1 -0
  242. data/lib/tasks/tests.rake +6 -6
  243. data/newrelic.yml +5 -5
  244. data/newrelic_rpm.gemspec +13 -13
  245. data/test/agent_helper.rb +25 -24
  246. metadata +73 -4
  247. data/lib/new_relic/agent/range_extensions.rb +0 -27
@@ -2,7 +2,6 @@
2
2
  # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
3
  # frozen_string_literal: true
4
4
 
5
- require 'new_relic/agent/range_extensions'
6
5
  require 'new_relic/agent/guid_generator'
7
6
 
8
7
  module NewRelic
@@ -56,7 +55,7 @@ module NewRelic
56
55
  @start_time ||= Process.clock_gettime(Process::CLOCK_REALTIME)
57
56
  return unless transaction
58
57
 
59
- parent.child_start(self) if parent
58
+ parent&.child_start(self)
60
59
  end
61
60
 
62
61
  def finish
@@ -105,38 +104,6 @@ module NewRelic
105
104
  @start_time.to_f..@end_time.to_f
106
105
  end
107
106
 
108
- def timings_overlap?(timing1, timing2)
109
- (timing1.first >= timing2.first && timing1.first <= timing2.last) ||
110
- (timing2.first >= timing1.first && timing2.first <= timing1.last)
111
- end
112
-
113
- def merge_timings(timing1, timing2)
114
- [([timing1.first, timing2.first].min),
115
- ([timing1.last, timing2.last].max)]
116
- end
117
-
118
- # @children_timings is an array of array, with each inner array
119
- # holding exactly 2 values, a child segment's start time and finish
120
- # time (in that order). When it's time to record, these timings are
121
- # converted into an array of range objects (using the same start and
122
- # end values as the original array). Any two range objects that
123
- # intersect and merged into a larger range. This checking for a
124
- # intersections and merging of ranges is expensive, so the operation
125
- # is only done at recording time.
126
- def children_time_ranges
127
- @children_time_ranges ||= begin
128
- overlapped = @children_timings.each_with_object([]) do |timing, timings|
129
- i = timings.index { |t| timings_overlap?(t, timing) }
130
- if i
131
- timings[i] = merge_timings(timing, timings[i])
132
- else
133
- timings << timing
134
- end
135
- end
136
- overlapped.map { |t| Range.new(t.first, t.last) }
137
- end
138
- end
139
-
140
107
  def children_time_ranges?
141
108
  !@children_timings.empty?
142
109
  end
@@ -228,7 +195,7 @@ module NewRelic
228
195
 
229
196
  if finished?
230
197
  transaction.async = true
231
- parent.descendant_complete(self, segment) if parent
198
+ parent&.descendant_complete(self, segment)
232
199
  end
233
200
  end
234
201
 
@@ -262,19 +229,19 @@ module NewRelic
262
229
 
263
230
  def force_finish
264
231
  finish
265
- NewRelic::Agent.logger.warn("Segment: #{name} was unfinished at " \
266
- "the end of transaction. Timing information for this segment's" \
267
- "parent #{parent.name} in #{transaction.best_name} may be inaccurate.")
232
+ NewRelic::Agent.logger.send(transaction.async? ? :debug : :warn, "Segment: #{name} was unfinished at " \
233
+ "the end of transaction. Timing information for this segment's " \
234
+ "parent #{parent&.name} in #{transaction.best_name} may be inaccurate.")
268
235
  end
269
236
 
270
237
  def run_complete_callbacks
271
238
  segment_complete
272
- parent.child_complete(self) if parent
239
+ parent&.child_complete(self)
273
240
  transaction.segment_complete(self)
274
241
  end
275
242
 
276
243
  def record_metrics
277
- raise NotImplementedError, "Subclasses must implement record_metrics"
244
+ raise NotImplementedError, 'Subclasses must implement record_metrics'
278
245
  end
279
246
 
280
247
  # callback for subclasses to override
@@ -299,12 +266,6 @@ module NewRelic
299
266
  end
300
267
 
301
268
  def record_exclusive_duration
302
- overlapping_duration = if children_time_ranges?
303
- RangeExtensions.compute_overlap(time_range, children_time_ranges)
304
- else
305
- 0.0
306
- end
307
-
308
269
  @exclusive_duration = duration - children_time - overlapping_duration
309
270
  transaction.total_time += @exclusive_duration
310
271
  params[:exclusive_duration_millis] = @exclusive_duration * 1000 if transaction.async?
@@ -314,6 +275,51 @@ module NewRelic
314
275
  transaction.metrics
315
276
  end
316
277
 
278
+ def ranges_intersect?(r1, r2)
279
+ r1.begin > r2.begin ? r2.cover?(r1.begin) : r1.cover?(r2.begin)
280
+ end
281
+
282
+ def range_overlap(range)
283
+ return 0.0 unless ranges_intersect?(range, time_range)
284
+
285
+ [range.end, time_range.end].min - [range.begin, time_range.begin].max
286
+ end
287
+
288
+ # Child segments operating concurrently with this segment may have
289
+ # start and end times that overlap with this segment's own times. The
290
+ # amount of overlap needs to be removed from the `children_time` total
291
+ # when calculating an `@exclusive_duration` value to be added to the
292
+ # transaction's total time.
293
+ #
294
+ # If there aren't any child segments, return 0.0. Otherwise, take the
295
+ # `@children_timings` array of arrays (each array holds a child
296
+ # segment's start time and end time), sort it by the first elements
297
+ # (start times), and use the start and finish times to create Range
298
+ # objects. Combine all of the child segment ranges that overlap with
299
+ # one another into new bigger ranges. Then take those bigger ranges
300
+ # and calculate how much overlap there is between them and this
301
+ # segment's own time range. Keep a running sum of all of the overlap
302
+ # amounts and then return it.
303
+ def overlapping_duration
304
+ sum = 0.0
305
+ return sum unless children_time_ranges?
306
+
307
+ @children_timings.sort_by!(&:first)
308
+ range = Range.new(*@children_timings.first)
309
+ (1..(@children_timings.size - 1)).each do |i|
310
+ possible = Range.new(*@children_timings[i])
311
+
312
+ if ranges_intersect?(range, possible)
313
+ range = range.begin..possible.end
314
+ else
315
+ sum += range_overlap(range)
316
+ range = possible
317
+ end
318
+ end
319
+
320
+ sum += range_overlap(range)
321
+ end
322
+
317
323
  def transaction_state
318
324
  @transaction_state ||= if @transaction
319
325
  transaction.state
@@ -24,7 +24,7 @@ module NewRelic
24
24
  @nosql_statement = nil
25
25
  @record_sql = true
26
26
  set_instance_info(host, port_path_or_id)
27
- @database_name = database_name ? database_name.to_s : nil
27
+ @database_name = database_name&.to_s
28
28
  super(Datastores::MetricHelper.scoped_metric_for(product, operation, collection),
29
29
  nil,
30
30
  start_time)
@@ -35,7 +35,7 @@ module NewRelic
35
35
  end
36
36
 
37
37
  def caller_transport_type
38
- @caller_transport_type ||= "Unknown"
38
+ @caller_transport_type ||= 'Unknown'
39
39
  end
40
40
 
41
41
  def accept_transport_type_from_api(value)
@@ -68,8 +68,9 @@ module NewRelic
68
68
  )
69
69
  end
70
70
 
71
- def log_request_headers(headers, direction = "OUTGOING")
72
- NewRelic::Agent.logger.debug("#{direction} REQUEST HEADERS: #{headers}")
71
+ def log_request_headers(headers, direction = 'OUTGOING')
72
+ printed_headers = headers.is_a?(NewRelic::Agent::HTTPClients::AbstractRequest) ? headers.headers : headers
73
+ NewRelic::Agent.logger.debug("#{direction} REQUEST HEADERS: #{printed_headers}")
73
74
  end
74
75
 
75
76
  def insert_headers(headers)
@@ -82,12 +83,12 @@ module NewRelic
82
83
  end
83
84
 
84
85
  def consume_message_headers(headers, tracer_state, transport_type)
85
- log_request_headers(headers, "INCOMING")
86
+ log_request_headers(headers, 'INCOMING')
86
87
  consume_message_distributed_tracing_headers(headers, transport_type)
87
88
  consume_message_cross_app_tracing_headers(headers, tracer_state)
88
89
  consume_message_synthetics_headers(headers)
89
90
  rescue => e
90
- NewRelic::Agent.logger.error("Error in consume_message_headers", e)
91
+ NewRelic::Agent.logger.error('Error in consume_message_headers', e)
91
92
  end
92
93
 
93
94
  def assign_intrinsics
@@ -135,7 +136,7 @@ module NewRelic
135
136
  transaction.synthetics_payload = incoming_payload
136
137
  end
137
138
  rescue => e
138
- NewRelic::Agent.logger.error("Error in consume_message_synthetics_header", e)
139
+ NewRelic::Agent.logger.error('Error in consume_message_synthetics_header', e)
139
140
  end
140
141
 
141
142
  def consume_message_distributed_tracing_headers(headers, transport_type)
@@ -13,7 +13,7 @@ module NewRelic
13
13
  attr_accessor :distributed_trace_payload
14
14
  attr_writer :distributed_trace_payload_created
15
15
 
16
- SUPPORTABILITY_DISTRIBUTED_TRACE = "Supportability/DistributedTrace"
16
+ SUPPORTABILITY_DISTRIBUTED_TRACE = 'Supportability/DistributedTrace'
17
17
  CREATE_PREFIX = "#{SUPPORTABILITY_DISTRIBUTED_TRACE}/CreatePayload"
18
18
  ACCEPT_PREFIX = "#{SUPPORTABILITY_DISTRIBUTED_TRACE}/AcceptPayload"
19
19
  IGNORE_PREFIX = "#{ACCEPT_PREFIX}/Ignored"
@@ -30,10 +30,10 @@ module NewRelic
30
30
  IGNORE_ACCEPT_MAJOR_VERSION_METRIC = "#{IGNORE_PREFIX}/MajorVersion"
31
31
  IGNORE_ACCEPT_UNTRUSTED_ACCOUNT_METRIC = "#{IGNORE_PREFIX}/UntrustedAccount"
32
32
 
33
- LBRACE = "{"
33
+ LBRACE = '{'
34
34
  NULL_PAYLOAD = 'null'
35
35
 
36
- NEWRELIC_TRACE_KEY = "HTTP_NEWRELIC"
36
+ NEWRELIC_TRACE_KEY = 'HTTP_NEWRELIC'
37
37
 
38
38
  def accept_distributed_tracing_incoming_request(request)
39
39
  return unless Agent.config[:'distributed_tracing.enabled']
@@ -55,7 +55,7 @@ module NewRelic
55
55
  payload
56
56
  rescue => e
57
57
  NewRelic::Agent.increment_metric(CREATE_EXCEPTION_METRIC)
58
- NewRelic::Agent.logger.warn("Failed to create distributed trace payload", e)
58
+ NewRelic::Agent.logger.warn('Failed to create distributed trace payload', e)
59
59
  nil
60
60
  end
61
61
 
@@ -75,7 +75,7 @@ module NewRelic
75
75
  true
76
76
  rescue => e
77
77
  NewRelic::Agent.increment_metric(ACCEPT_EXCEPTION_METRIC)
78
- NewRelic::Agent.logger.warn("Failed to accept distributed trace payload", e)
78
+ NewRelic::Agent.logger.warn('Failed to accept distributed trace payload', e)
79
79
  false
80
80
  end
81
81
 
@@ -114,19 +114,18 @@ module NewRelic
114
114
  decoded
115
115
  rescue => e
116
116
  NewRelic::Agent.increment_metric(ACCEPT_PARSE_EXCEPTION_METRIC)
117
- NewRelic::Agent.logger.warn("Error parsing distributed trace payload", e)
117
+ NewRelic::Agent.logger.warn('Error parsing distributed trace payload', e)
118
118
  nil
119
119
  end
120
120
 
121
121
  def check_required_fields_present(payload)
122
- if \
123
- !payload.version.nil? &&
124
- !payload.parent_account_id.nil? &&
125
- !payload.parent_app_id.nil? &&
126
- !payload.parent_type.nil? &&
127
- (!payload.transaction_id.nil? || !payload.id.nil?) &&
128
- !payload.trace_id.nil? &&
129
- !payload.timestamp.nil?
122
+ if !payload.version.nil? &&
123
+ !payload.parent_account_id.nil? &&
124
+ !payload.parent_app_id.nil? &&
125
+ !payload.parent_type.nil? &&
126
+ (!payload.transaction_id.nil? || !payload.id.nil?) &&
127
+ !payload.trace_id.nil? &&
128
+ !payload.timestamp.nil?
130
129
 
131
130
  true
132
131
  else
@@ -16,10 +16,10 @@ module NewRelic
16
16
  NR_SYNTHETICS_HEADER = 'X-NewRelic-Synthetics'
17
17
  APP_DATA_KEY = 'NewRelicAppData'
18
18
 
19
- EXTERNAL_ALL = "External/all"
20
- EXTERNAL_ALL_WEB = "External/allWeb"
21
- EXTERNAL_ALL_OTHER = "External/allOther"
22
- MISSING_STATUS_CODE = "MissingHTTPStatusCode"
19
+ EXTERNAL_ALL = 'External/all'
20
+ EXTERNAL_ALL_WEB = 'External/allWeb'
21
+ EXTERNAL_ALL_OTHER = 'External/allOther'
22
+ MISSING_STATUS_CODE = 'MissingHTTPStatusCode'
23
23
 
24
24
  attr_reader :library, :uri, :procedure, :http_status_code
25
25
  attr_writer :record_agent_attributes
@@ -62,14 +62,14 @@ module NewRelic
62
62
  # @api public
63
63
  def add_request_headers(request)
64
64
  process_host_header(request)
65
- synthetics_header = transaction && transaction.raw_synthetics_header
65
+ synthetics_header = transaction&.raw_synthetics_header
66
66
  insert_synthetics_header(request, synthetics_header) if synthetics_header
67
67
 
68
68
  return unless record_metrics?
69
69
 
70
70
  transaction.distributed_tracer.insert_headers(request)
71
71
  rescue => e
72
- NewRelic::Agent.logger.error("Error in add_request_headers", e)
72
+ NewRelic::Agent.logger.error('Error in add_request_headers', e)
73
73
  end
74
74
 
75
75
  # This method extracts app data from an external response if present. If
@@ -92,10 +92,10 @@ module NewRelic
92
92
  @app_data = data
93
93
  update_segment_name
94
94
  else
95
- NewRelic::Agent.logger.debug("External segment response has invalid cross_app_id")
95
+ NewRelic::Agent.logger.debug('External segment response has invalid cross_app_id')
96
96
  end
97
97
  rescue => e
98
- NewRelic::Agent.logger.error("Error in read_response_headers", e)
98
+ NewRelic::Agent.logger.error('Error in read_response_headers', e)
99
99
  end
100
100
 
101
101
  def cross_app_request? # :nodoc:
@@ -154,7 +154,7 @@ module NewRelic
154
154
 
155
155
  end
156
156
  rescue => e
157
- NewRelic::Agent.logger.error("error during get_request_metadata", e)
157
+ NewRelic::Agent.logger.error('error during get_request_metadata', e)
158
158
  end
159
159
 
160
160
  # Process obfuscated +String+ sent from a called application that is also running a New Relic agent and
@@ -176,13 +176,13 @@ module NewRelic
176
176
  @app_data = app_data
177
177
  update_segment_name
178
178
  else
179
- NewRelic::Agent.logger.error("error processing response metadata: invalid/non-trusted ID")
179
+ NewRelic::Agent.logger.error('error processing response metadata: invalid/non-trusted ID')
180
180
  end
181
181
  end
182
182
 
183
183
  nil
184
184
  rescue => e
185
- NewRelic::Agent.logger.error("error during process_response_metadata", e)
185
+ NewRelic::Agent.logger.error('error during process_response_metadata', e)
186
186
  end
187
187
 
188
188
  def record_metrics
@@ -91,7 +91,7 @@ module NewRelic
91
91
  transaction.distributed_tracer.log_request_headers(headers)
92
92
  end
93
93
  rescue => e
94
- NewRelic::Agent.logger.error("Error during message header processing", e)
94
+ NewRelic::Agent.logger.error('Error during message header processing', e)
95
95
  end
96
96
  end
97
97
  end
@@ -11,7 +11,7 @@ module NewRelic
11
11
  attr_reader :request_path, :referer, :accept, :content_length, :content_type,
12
12
  :host, :port, :user_agent, :request_method
13
13
 
14
- HTTP_ACCEPT_HEADER_KEY = "HTTP_ACCEPT".freeze
14
+ HTTP_ACCEPT_HEADER_KEY = 'HTTP_ACCEPT'.freeze
15
15
 
16
16
  def initialize(request)
17
17
  @request_path = path_from_request(request)
@@ -85,7 +85,7 @@ module NewRelic
85
85
  # rails construct the PATH_INFO enviroment variable improperly and we're generally
86
86
  # being defensive.
87
87
 
88
- ROOT_PATH = "/".freeze
88
+ ROOT_PATH = '/'.freeze
89
89
 
90
90
  def path_from_request(request)
91
91
  path = attribute_from_request(request, :path) || ''
@@ -32,7 +32,7 @@ module NewRelic
32
32
  if segment = NewRelic::Agent::Tracer.current_segment
33
33
  segment.merge_untrusted_agent_attributes(attributes, prefix, default_destinations)
34
34
  else
35
- NewRelic::Agent.logger.debug("Attempted to merge untrusted attributes without segment")
35
+ NewRelic::Agent.logger.debug('Attempted to merge untrusted attributes without segment')
36
36
  end
37
37
  end
38
38
 
@@ -13,7 +13,7 @@ module NewRelic
13
13
  end
14
14
 
15
15
  def allow_sample?(sample)
16
- sample.synthetics_resource_id != nil
16
+ !sample.synthetics_resource_id.nil?
17
17
  end
18
18
 
19
19
  def truncate_samples
@@ -14,7 +14,7 @@ module NewRelic
14
14
  attr_accessor :transaction_name, :guid, :attributes,
15
15
  :node_count, :finished, :threshold, :profile
16
16
 
17
- ROOT = "ROOT".freeze
17
+ ROOT = 'ROOT'.freeze
18
18
 
19
19
  def initialize(start_time)
20
20
  @start_time = start_time
@@ -20,7 +20,7 @@ module NewRelic
20
20
  end
21
21
  end
22
22
 
23
- SUPPORTABILITY_PREFIX = "Supportability/TraceContext"
23
+ SUPPORTABILITY_PREFIX = 'Supportability/TraceContext'
24
24
  CREATE_PREFIX = "#{SUPPORTABILITY_PREFIX}/Create"
25
25
  ACCEPT_PREFIX = "#{SUPPORTABILITY_PREFIX}/Accept"
26
26
  TRACESTATE_PREFIX = "#{SUPPORTABILITY_PREFIX}/TraceState"
@@ -73,7 +73,7 @@ module NewRelic
73
73
  true
74
74
  rescue Exception => e
75
75
  NewRelic::Agent.increment_metric(CREATE_EXCEPTION_METRIC)
76
- NewRelic::Agent.logger.warn("Failed to create trace context payload", e)
76
+ NewRelic::Agent.logger.warn('Failed to create trace context payload', e)
77
77
  false
78
78
  end
79
79
 
@@ -95,7 +95,7 @@ module NewRelic
95
95
 
96
96
  def create_trace_state_payload
97
97
  unless Agent.config[:'distributed_tracing.enabled']
98
- NewRelic::Agent.logger.warn("Not configured to create WC3 trace context payload")
98
+ NewRelic::Agent.logger.warn('Not configured to create WC3 trace context payload')
99
99
  return
100
100
  end
101
101
 
@@ -144,7 +144,7 @@ module NewRelic
144
144
  true
145
145
  rescue => e
146
146
  NewRelic::Agent.increment_metric(ACCEPT_EXCEPTION_METRIC)
147
- NewRelic::Agent.logger.warn("Failed to accept trace context payload", e)
147
+ NewRelic::Agent.logger.warn('Failed to accept trace context payload', e)
148
148
  false
149
149
  end
150
150
 
@@ -58,7 +58,7 @@ module NewRelic
58
58
  end
59
59
 
60
60
  def to_s_compact
61
- str = String.new('')
61
+ str = +''
62
62
  str << metric_name
63
63
  if children.any?
64
64
  str << "{#{children.map { |cs| cs.to_s_compact }.join(',')}}"
@@ -67,9 +67,9 @@ module NewRelic
67
67
  end
68
68
 
69
69
  def to_debug_str(depth)
70
- tab = String.new(' ') * depth
70
+ tab = (+' ') * depth
71
71
  s = tab.clone
72
- s << ">> #{'%3i ms' % (@entry_timestamp * 1000)} [#{self.class.name.split("::").last}] #{metric_name} \n"
72
+ s << ">> #{'%3i ms' % (@entry_timestamp * 1000)} [#{self.class.name.split('::').last}] #{metric_name} \n"
73
73
  unless params.empty?
74
74
  params.each do |k, v|
75
75
  s << "#{tab} -#{'%-16s' % k}: #{v.to_s[0..80]}\n"
@@ -136,10 +136,8 @@ module NewRelic
136
136
  def each_node(&block)
137
137
  yield(self)
138
138
 
139
- if @children
140
- @children.each do |node|
141
- node.each_node(&block)
142
- end
139
+ @children&.each do |node|
140
+ node.each_node(&block)
143
141
  end
144
142
  end
145
143
 
@@ -149,11 +147,14 @@ module NewRelic
149
147
  summary = yield(self)
150
148
  summary.current_nest_count += 1 if summary
151
149
 
150
+ # no then branch coverage
151
+ # rubocop:disable Style/SafeNavigation
152
152
  if @children
153
153
  @children.each do |node|
154
154
  node.each_node_with_nest_tracking(&block)
155
155
  end
156
156
  end
157
+ # rubocop:enable Style/SafeNavigation
157
158
 
158
159
  summary.current_nest_count -= 1 if summary
159
160
  end
@@ -29,6 +29,12 @@ module NewRelic
29
29
  else
30
30
  segment.record_on_finish = true
31
31
  ::NewRelic::Agent.logger.debug("Segment limit of #{segment_limit} reached, ceasing collection.")
32
+
33
+ if finished?
34
+ ::NewRelic::Agent.logger.debug("Transaction #{best_name} has finished but segments still being created, resetting state.")
35
+ NewRelic::Agent::Tracer.state.reset
36
+ NewRelic::Agent.record_metric('Supportability/Transaction/SegmentLimitReachedAfterFinished/ResetState', 1)
37
+ end
32
38
  end
33
39
  segment.transaction_assigned
34
40
  end
@@ -52,8 +58,8 @@ module NewRelic
52
58
  segments.each { |s| s.finalize }
53
59
  end
54
60
 
55
- WEB_TRANSACTION_TOTAL_TIME = "WebTransactionTotalTime".freeze
56
- OTHER_TRANSACTION_TOTAL_TIME = "OtherTransactionTotalTime".freeze
61
+ WEB_TRANSACTION_TOTAL_TIME = 'WebTransactionTotalTime'.freeze
62
+ OTHER_TRANSACTION_TOTAL_TIME = 'OtherTransactionTotalTime'.freeze
57
63
 
58
64
  def record_total_time_metrics
59
65
  total_time_metric = if recording_web_transaction?
@@ -63,7 +63,7 @@ module NewRelic
63
63
  #
64
64
  # This value will be forcibly capped by the max_capacity
65
65
  def capacity
66
- raise NotImplementedError.new("TransactionSampleBuffer subclasses must provide a capacity override")
66
+ raise NotImplementedError.new('TransactionSampleBuffer subclasses must provide a capacity override')
67
67
  end
68
68
 
69
69
  # Apply hard upper limit to the capacity to prevent users from