newrelic_rpm 9.3.0 → 9.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.build_ignore +6 -1
  3. data/CHANGELOG.md +193 -6
  4. data/README.md +4 -0
  5. data/Rakefile +1 -1
  6. data/lib/new_relic/agent/attribute_pre_filtering.rb +109 -0
  7. data/lib/new_relic/agent/configuration/default_source.rb +177 -34
  8. data/lib/new_relic/agent/configuration/environment_source.rb +1 -1
  9. data/lib/new_relic/agent/distributed_tracing.rb +1 -1
  10. data/lib/new_relic/agent/http_clients/async_http_wrappers.rb +83 -0
  11. data/lib/new_relic/agent/http_clients/ethon_wrappers.rb +111 -0
  12. data/lib/new_relic/agent/http_clients/httpx_wrappers.rb +93 -0
  13. data/lib/new_relic/agent/instrumentation/action_controller_other_subscriber.rb +1 -1
  14. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +1 -2
  15. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/chain.rb +69 -0
  16. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +13 -0
  17. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/prepend.rb +37 -0
  18. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb +23 -0
  19. data/lib/new_relic/agent/instrumentation/active_support_logger/instrumentation.rb +4 -0
  20. data/lib/new_relic/agent/instrumentation/active_support_logger.rb +3 -1
  21. data/lib/new_relic/agent/instrumentation/async_http/chain.rb +23 -0
  22. data/lib/new_relic/agent/instrumentation/async_http/instrumentation.rb +37 -0
  23. data/lib/new_relic/agent/instrumentation/async_http/prepend.rb +15 -0
  24. data/lib/new_relic/agent/instrumentation/async_http.rb +26 -0
  25. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +9 -0
  26. data/lib/new_relic/agent/instrumentation/concurrent_ruby/instrumentation.rb +2 -2
  27. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +1 -2
  28. data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +4 -0
  29. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +3 -0
  30. data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +4 -1
  31. data/lib/new_relic/agent/instrumentation/ethon/chain.rb +39 -0
  32. data/lib/new_relic/agent/instrumentation/ethon/instrumentation.rb +105 -0
  33. data/lib/new_relic/agent/instrumentation/ethon/prepend.rb +35 -0
  34. data/lib/new_relic/agent/instrumentation/ethon.rb +39 -0
  35. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +3 -0
  36. data/lib/new_relic/agent/instrumentation/grape/instrumentation.rb +4 -0
  37. data/lib/new_relic/agent/instrumentation/grpc/client/instrumentation.rb +4 -0
  38. data/lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb +4 -0
  39. data/lib/new_relic/agent/instrumentation/grpc_client.rb +1 -1
  40. data/lib/new_relic/agent/instrumentation/grpc_server.rb +1 -1
  41. data/lib/new_relic/agent/instrumentation/httpclient/instrumentation.rb +4 -0
  42. data/lib/new_relic/agent/instrumentation/httprb/instrumentation.rb +4 -0
  43. data/lib/new_relic/agent/instrumentation/httpx/chain.rb +20 -0
  44. data/lib/new_relic/agent/instrumentation/httpx/instrumentation.rb +51 -0
  45. data/lib/new_relic/agent/instrumentation/httpx/prepend.rb +15 -0
  46. data/lib/new_relic/agent/instrumentation/httpx.rb +27 -0
  47. data/lib/new_relic/agent/instrumentation/logger/instrumentation.rb +3 -0
  48. data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +9 -0
  49. data/lib/new_relic/agent/instrumentation/memcache.rb +2 -2
  50. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +1 -3
  51. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +5 -1
  52. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +4 -0
  53. data/lib/new_relic/agent/instrumentation/padrino/instrumentation.rb +4 -0
  54. data/lib/new_relic/agent/instrumentation/queue_time.rb +1 -1
  55. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +6 -0
  56. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +4 -0
  57. data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +1 -0
  58. data/lib/new_relic/agent/instrumentation/rake/instrumentation.rb +4 -0
  59. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +4 -0
  60. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +4 -0
  61. data/lib/new_relic/agent/instrumentation/roda/chain.rb +43 -0
  62. data/lib/new_relic/agent/instrumentation/roda/ignorer.rb +45 -0
  63. data/lib/new_relic/agent/instrumentation/roda/instrumentation.rb +68 -0
  64. data/lib/new_relic/agent/instrumentation/roda/prepend.rb +24 -0
  65. data/lib/new_relic/agent/instrumentation/roda/roda_transaction_namer.rb +29 -0
  66. data/lib/new_relic/agent/instrumentation/roda.rb +36 -0
  67. data/lib/new_relic/agent/instrumentation/sequel.rb +1 -1
  68. data/lib/new_relic/agent/instrumentation/sidekiq/client.rb +4 -0
  69. data/lib/new_relic/agent/instrumentation/sidekiq/server.rb +26 -3
  70. data/lib/new_relic/agent/instrumentation/sidekiq.rb +5 -3
  71. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +4 -0
  72. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +1 -3
  73. data/lib/new_relic/agent/instrumentation/stripe.rb +28 -0
  74. data/lib/new_relic/agent/instrumentation/stripe_subscriber.rb +77 -0
  75. data/lib/new_relic/agent/instrumentation/tilt/instrumentation.rb +4 -0
  76. data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +5 -1
  77. data/lib/new_relic/agent/log_event_attributes.rb +1 -1
  78. data/lib/new_relic/agent/messaging.rb +2 -2
  79. data/lib/new_relic/agent/monitors/synthetics_monitor.rb +12 -1
  80. data/lib/new_relic/agent/new_relic_service.rb +33 -17
  81. data/lib/new_relic/agent/pipe_service.rb +1 -1
  82. data/lib/new_relic/agent/rules_engine.rb +1 -1
  83. data/lib/new_relic/agent/span_event_primitive.rb +16 -4
  84. data/lib/new_relic/agent/system_info.rb +26 -0
  85. data/lib/new_relic/agent/tracer.rb +5 -6
  86. data/lib/new_relic/agent/transaction/abstract_segment.rb +55 -0
  87. data/lib/new_relic/agent/transaction/external_request_segment.rb +5 -2
  88. data/lib/new_relic/agent/transaction/message_broker_segment.rb +1 -2
  89. data/lib/new_relic/agent/transaction/request_attributes.rb +46 -10
  90. data/lib/new_relic/agent/transaction.rb +30 -6
  91. data/lib/new_relic/agent/transaction_error_primitive.rb +16 -0
  92. data/lib/new_relic/agent/transaction_event_primitive.rb +19 -0
  93. data/lib/new_relic/agent/utilization/gcp.rb +1 -3
  94. data/lib/new_relic/agent/utilization/vendor.rb +5 -7
  95. data/lib/new_relic/agent.rb +19 -3
  96. data/lib/new_relic/cli/command.rb +1 -0
  97. data/lib/new_relic/constants.rb +3 -0
  98. data/lib/new_relic/control/class_methods.rb +1 -7
  99. data/lib/new_relic/control/frameworks/rails.rb +14 -2
  100. data/lib/new_relic/control/frameworks/roda.rb +20 -0
  101. data/lib/new_relic/language_support.rb +9 -0
  102. data/lib/new_relic/noticed_error.rb +5 -2
  103. data/lib/new_relic/rack/agent_hooks.rb +1 -1
  104. data/lib/new_relic/rack/agent_middleware.rb +0 -16
  105. data/lib/new_relic/rack/browser_monitoring.rb +1 -1
  106. data/lib/new_relic/supportability_helper.rb +1 -0
  107. data/lib/new_relic/version.rb +1 -1
  108. data/lib/tasks/bump_version.rake +1 -1
  109. data/lib/tasks/config.rake +3 -2
  110. data/lib/tasks/helpers/config.html.erb +93 -0
  111. data/lib/tasks/helpers/format.rb +11 -7
  112. data/lib/tasks/helpers/version_bump.rb +2 -2
  113. data/lib/tasks/instrumentation_generator/instrumentation.thor +3 -3
  114. data/lib/tasks/newrelicyml.rake +1 -1
  115. data/lib/tasks/tests.rake +71 -0
  116. data/newrelic.yml +103 -31
  117. data/newrelic_rpm.gemspec +12 -6
  118. data/test/agent_helper.rb +1027 -0
  119. metadata +63 -8
  120. data/lib/tasks/helpers/removers.rb +0 -33
  121. data/lib/tasks/multiverse.rake +0 -6
  122. data/lib/tasks/multiverse.rb +0 -76
@@ -3,7 +3,6 @@
3
3
  # frozen_string_literal: true
4
4
 
5
5
  require 'zlib'
6
- require 'timeout'
7
6
  require 'new_relic/agent/audit_logger'
8
7
  require 'new_relic/agent/new_relic_service/encoders'
9
8
  require 'new_relic/agent/new_relic_service/marshaller'
@@ -19,7 +18,13 @@ module NewRelic
19
18
 
20
19
  # These include Errno connection errors, and all indicate that the
21
20
  # underlying TCP connection may be in a bad state.
22
- CONNECTION_ERRORS = [Timeout::Error, EOFError, SystemCallError, SocketError].freeze
21
+ CONNECTION_ERRORS = [Net::OpenTimeout, Net::ReadTimeout, EOFError, SystemCallError, SocketError]
22
+ # TODO: MAJOR VERSION - Net::WriteTimeout wasn't defined until Ruby 2.6.
23
+ # Once support for Ruby 2.5 is dropped, we should simply include
24
+ # Net::WriteTimeout in the connection errors array directly instead
25
+ # of with a conditional
26
+ CONNECTION_ERRORS << Net::WriteTimeout if defined?(Net::WriteTimeout)
27
+ CONNECTION_ERRORS.freeze
23
28
 
24
29
  # The maximum number of times to attempt an HTTP request
25
30
  MAX_ATTEMPTS = 2
@@ -319,13 +324,15 @@ module NewRelic
319
324
 
320
325
  def start_connection(conn)
321
326
  NewRelic::Agent.logger.debug("Opening TCP connection to #{conn.address}:#{conn.port}")
322
- Timeout.timeout(@request_timeout) { conn.start }
323
- conn
327
+ conn.start
324
328
  end
325
329
 
326
330
  def setup_connection_timeouts(conn)
327
- # We use Timeout explicitly instead of this
328
- conn.read_timeout = nil
331
+ conn.open_timeout = @request_timeout
332
+ conn.read_timeout = @request_timeout
333
+ # TODO: MAJOR VERSION - #write_timeout= requires Ruby 2.6+, so remove
334
+ # the conditional check once support for Ruby 2.5 is dropped
335
+ conn.write_timeout = @request_timeout if conn.respond_to?(:write_timeout=)
329
336
 
330
337
  if conn.respond_to?(:keep_alive_timeout) && NewRelic::Agent.config[:aggressive_keepalive]
331
338
  conn.keep_alive_timeout = NewRelic::Agent.config[:keep_alive_timeout]
@@ -362,8 +369,8 @@ module NewRelic
362
369
  conn = create_http_connection
363
370
  start_connection(conn)
364
371
  conn
365
- rescue Timeout::Error
366
- ::NewRelic::Agent.logger.info('Timeout while attempting to connect. You may need to install system-level CA Certificates, as the ruby agent no longer includes these.')
372
+ rescue Net::OpenTimeout
373
+ ::NewRelic::Agent.logger.info('Timed out while attempting to connect. For SSL issues, you may need to install system-level CA Certificates to be used by Net::HTTP.')
367
374
  raise
368
375
  end
369
376
 
@@ -436,13 +443,9 @@ module NewRelic
436
443
  end
437
444
 
438
445
  def attempt_request(request, opts)
439
- response = nil
440
446
  conn = http_connection
441
447
  ::NewRelic::Agent.logger.debug("Sending request to #{opts[:collector]}#{opts[:uri]} with #{request.method}")
442
- Timeout.timeout(@request_timeout) do
443
- response = conn.request(request)
444
- end
445
- response
448
+ conn.request(request)
446
449
  end
447
450
 
448
451
  def handle_error_response(response, endpoint)
@@ -450,7 +453,9 @@ module NewRelic
450
453
  when Net::HTTPRequestTimeOut,
451
454
  Net::HTTPTooManyRequests,
452
455
  Net::HTTPInternalServerError,
453
- Net::HTTPServiceUnavailable
456
+ Net::HTTPServiceUnavailable,
457
+ Net::OpenTimeout,
458
+ Net::ReadTimeout
454
459
  handle_server_connection_exception(response, endpoint)
455
460
  when Net::HTTPBadRequest,
456
461
  Net::HTTPForbidden,
@@ -471,9 +476,20 @@ module NewRelic
471
476
  when Net::HTTPGone
472
477
  handle_gone_response(response, endpoint)
473
478
  else
474
- record_endpoint_attempts_supportability_metrics(endpoint)
475
- record_error_response_supportability_metrics(response.code)
476
- raise UnrecoverableServerException, "#{response.code}: #{response.message}"
479
+ # TODO: MAJOR VERSION - Net::WriteTimeout wasn't defined until
480
+ # Ruby 2.6, so it can't be included in the case statement
481
+ # as a constant and instead needs to be found here. Once
482
+ # support for Ruby 2.5 is dropped, we should have
483
+ # Net::WriteTimeout sit in the 'when' clause above alongside
484
+ # Net::OpenTimeout and Net::ReadTimeout and this entire if/else
485
+ # conditional can be removed.
486
+ if response.respond_to?(:name) && response.name == 'Net::WriteTimeout'
487
+ handle_server_connection_exception(response, endpoint)
488
+ else
489
+ record_endpoint_attempts_supportability_metrics(endpoint)
490
+ record_error_response_supportability_metrics(response.code)
491
+ raise UnrecoverableServerException, "#{response.code}: #{response.message}"
492
+ end
477
493
  end
478
494
  response
479
495
  end
@@ -15,7 +15,7 @@ module NewRelic
15
15
  if @pipe && @pipe.parent_pid != $$
16
16
  @pipe.after_fork_in_child
17
17
  else
18
- NewRelic::Agent.logger.error('No communication channel to parent process, please see https://newrelic.com/docs/ruby/resque-instrumentation for more information.')
18
+ NewRelic::Agent.logger.error('No communication channel to parent process, please see https://docs.newrelic.com/docs/apm/agents/ruby-agent/background-jobs/resque-instrumentation/ for more information.')
19
19
  end
20
20
  end
21
21
 
@@ -9,7 +9,7 @@ require 'new_relic/language_support'
9
9
  module NewRelic
10
10
  module Agent
11
11
  class RulesEngine
12
- SEGMENT_SEPARATOR = '/'.freeze
12
+ SEGMENT_SEPARATOR = NewRelic::SLASH
13
13
  LEADING_SLASH_REGEX = %r{^/}.freeze
14
14
 
15
15
  include Enumerable
@@ -29,12 +29,16 @@ module NewRelic
29
29
  CATEGORY_KEY = 'category'
30
30
  HTTP_URL_KEY = 'http.url'
31
31
  HTTP_METHOD_KEY = 'http.method'
32
+ HTTP_REQUEST_METHOD_KEY = 'http.request.method'
32
33
  HTTP_STATUS_CODE_KEY = 'http.statusCode'
33
34
  COMPONENT_KEY = 'component'
34
35
  DB_INSTANCE_KEY = 'db.instance'
35
36
  DB_STATEMENT_KEY = 'db.statement'
37
+ DB_SYSTEM_KEY = 'db.system'
36
38
  PEER_ADDRESS_KEY = 'peer.address'
37
39
  PEER_HOSTNAME_KEY = 'peer.hostname'
40
+ SERVER_ADDRESS_KEY = 'server.address'
41
+ SERVER_PORT_KEY = 'server.port'
38
42
  SPAN_KIND_KEY = 'span.kind'
39
43
  ENTRY_POINT_KEY = 'nr.entryPoint'
40
44
  TRUSTED_PARENT_KEY = 'trustedParentId'
@@ -69,10 +73,12 @@ module NewRelic
69
73
 
70
74
  intrinsics[COMPONENT_KEY] = segment.library
71
75
  intrinsics[HTTP_METHOD_KEY] = segment.procedure
76
+ intrinsics[HTTP_REQUEST_METHOD_KEY] = segment.procedure
72
77
  intrinsics[HTTP_STATUS_CODE_KEY] = segment.http_status_code if segment.http_status_code
73
78
  intrinsics[CATEGORY_KEY] = HTTP_CATEGORY
74
79
  intrinsics[SPAN_KIND_KEY] = CLIENT
75
-
80
+ intrinsics[SERVER_ADDRESS_KEY] = segment.uri.host
81
+ intrinsics[SERVER_PORT_KEY] = segment.uri.port
76
82
  agent_attributes = error_attributes(segment) || {}
77
83
 
78
84
  if allowed?(HTTP_URL_KEY)
@@ -86,7 +92,7 @@ module NewRelic
86
92
  [intrinsics, custom_attributes(segment), agent_attributes]
87
93
  end
88
94
 
89
- def for_datastore_segment(segment)
95
+ def for_datastore_segment(segment) # rubocop:disable Metrics/AbcSize
90
96
  intrinsics = intrinsics_for(segment)
91
97
 
92
98
  intrinsics[COMPONENT_KEY] = segment.product
@@ -101,9 +107,15 @@ module NewRelic
101
107
  if segment.host && segment.port_path_or_id && allowed?(PEER_ADDRESS_KEY)
102
108
  agent_attributes[PEER_ADDRESS_KEY] = truncate("#{segment.host}:#{segment.port_path_or_id}")
103
109
  end
104
- if segment.host && allowed?(PEER_HOSTNAME_KEY)
105
- agent_attributes[PEER_HOSTNAME_KEY] = truncate(segment.host)
110
+ if segment.host
111
+ [PEER_HOSTNAME_KEY, SERVER_ADDRESS_KEY].each do |key|
112
+ agent_attributes[key] = truncate(segment.host) if allowed?(key)
113
+ end
114
+ end
115
+ if segment.port_path_or_id&.match?(/^\d+$/) && allowed?(SERVER_PORT_KEY)
116
+ agent_attributes[SERVER_PORT_KEY] = segment.port_path_or_id
106
117
  end
118
+ agent_attributes[DB_SYSTEM_KEY] = segment.product if allowed?(DB_SYSTEM_KEY)
107
119
 
108
120
  if segment.sql_statement && allowed?(DB_STATEMENT_KEY)
109
121
  agent_attributes[DB_STATEMENT_KEY] = truncate(segment.sql_statement.safe_sql, 2000)
@@ -13,6 +13,8 @@ require 'socket'
13
13
  module NewRelic
14
14
  module Agent
15
15
  module SystemInfo
16
+ DOCKER_CGROUPS_V2_PATTERN = %r{.*/docker/containers/([0-9a-f]{64})/.*}.freeze
17
+
16
18
  def self.ruby_os_identifier
17
19
  RbConfig::CONFIG['target_os']
18
20
  end
@@ -172,15 +174,39 @@ module NewRelic
172
174
  proc_try_read('/proc/version')
173
175
  end
174
176
 
177
+ # When operating within a Docker container, attempt to obtain the
178
+ # container id.
179
+ #
180
+ # First look for `/proc/self/mountinfo` to exist on disk to signify
181
+ # cgroups v2. If that file exists, read it and expect it to contain one
182
+ # or more "/docker/containers/<container_id>/" lines from which the
183
+ # container id can be gleaned.
184
+ #
185
+ # Next look for `/proc/self/cgroup` to exist on disk to signify cgroup v1.
186
+ # If that file exists, read it and parse the "cpu" group info in the hope
187
+ # of finding a 64 character container id value.
188
+ #
189
+ # For non-cgroups based containers, use a `nil` value for the container
190
+ # id without generating any warnings or errors.
175
191
  def self.docker_container_id
176
192
  return unless ruby_os_identifier.include?('linux')
177
193
 
194
+ cgroupsv2_based_id = docker_container_id_for_cgroupsv2
195
+ return cgroupsv2_based_id if cgroupsv2_based_id
196
+
178
197
  cgroup_info = proc_try_read('/proc/self/cgroup')
179
198
  return unless cgroup_info
180
199
 
181
200
  parse_docker_container_id(cgroup_info)
182
201
  end
183
202
 
203
+ def self.docker_container_id_for_cgroupsv2
204
+ mountinfo = proc_try_read('/proc/self/mountinfo')
205
+ return unless mountinfo
206
+
207
+ Regexp.last_match(1) if mountinfo =~ DOCKER_CGROUPS_V2_PATTERN
208
+ end
209
+
184
210
  def self.parse_docker_container_id(cgroup_info)
185
211
  cpu_cgroup = parse_cgroup_ids(cgroup_info)['cpu']
186
212
  return unless cpu_cgroup
@@ -66,9 +66,10 @@ module NewRelic
66
66
  #
67
67
  # @api public
68
68
  def transaction_sampled?
69
- if txn = current_transaction
70
- txn.sampled?
71
- end
69
+ txn = current_transaction
70
+ return false unless txn
71
+
72
+ txn.sampled?
72
73
  end
73
74
  alias_method :sampled?, :transaction_sampled?
74
75
 
@@ -356,9 +357,7 @@ module NewRelic
356
357
  yield
357
358
  rescue => exception
358
359
  # needs else branch coverage
359
- if segment && segment.is_a?(Transaction::AbstractSegment) # rubocop:disable Style/SafeNavigation
360
- segment.notice_error(exception)
361
- end
360
+ segment.notice_error(exception) if segment&.is_a?(Transaction::AbstractSegment)
362
361
  raise
363
362
  end
364
363
 
@@ -24,6 +24,9 @@ module NewRelic
24
24
  attr_writer :record_metrics, :record_scoped_metric, :record_on_finish
25
25
  attr_reader :noticed_error
26
26
 
27
+ CALLBACK = :@callback
28
+ SEGMENT = 'segment'
29
+
27
30
  def initialize(name = nil, start_time = nil)
28
31
  @name = name
29
32
  @starting_segment_key = NewRelic::Agent::Tracer.current_segment_key
@@ -49,6 +52,7 @@ module NewRelic
49
52
  @code_function = nil
50
53
  @code_lineno = nil
51
54
  @code_namespace = nil
55
+ invoke_callback
52
56
  end
53
57
 
54
58
  def start
@@ -327,6 +331,57 @@ module NewRelic
327
331
  Tracer.state
328
332
  end
329
333
  end
334
+
335
+ # for segment callback usage info, see self.set_segment_callback
336
+ def invoke_callback
337
+ return unless self.class.instance_variable_defined?(CALLBACK)
338
+
339
+ NewRelic::Agent.logger.debug("Invoking callback for #{self.class.name}...")
340
+ self.class.instance_variable_get(CALLBACK).call
341
+ rescue Exception => e
342
+ NewRelic::Agent.logger.error("Error encountered while invoking callback for #{self.class.name}: " +
343
+ "#{e.class} - #{e.message}")
344
+ end
345
+
346
+ # Setting and invoking a segment callback
347
+ # =======================================
348
+ # Each individual segment class such as `ExternalRequestSegment` allows
349
+ # for exactly one instance of a `Proc` (meaning a proc or lambda) to be
350
+ # set as a callback. A callback can be set on a segment class by calling
351
+ # `.set_segment_callback` with a proc or lambda as the only argument.
352
+ # If set, the callback will be invoked with `#call` at segment class
353
+ # initialization time.
354
+ #
355
+ # Example usage:
356
+ # callback = -> { puts 'Hello, World! }
357
+ # ExternalRequestSegment.set_segment_callback(callback)
358
+ # ExternalRequestSegment.new(library, uri, procedure)
359
+ #
360
+ # A callback set on a segment class will only be called when that
361
+ # specific segment class is initialized. Other segment classes will not
362
+ # be impacted.
363
+ #
364
+ # Great caution should be taken in the defining of the callback block
365
+ # to not have the block perform anything too time consuming or resource
366
+ # intensive in order to keep the New Relic Ruby agent operating
367
+ # normally.
368
+ #
369
+ # Given that callbacks are user defined, they must be set entirely at
370
+ # the user's own risk. It is recommended that each callback use
371
+ # conditional logic that only performs work for certain qualified
372
+ # segments. It is recommended that each callback be thoroughly tested
373
+ # in non-production environments before being introduced to production
374
+ # environments.
375
+ def self.set_segment_callback(callback_proc)
376
+ unless callback_proc.is_a?(Proc)
377
+ NewRelic::Agent.logger.error("#{self}.#{__method__}: expected an argument of type Proc, " \
378
+ "got #{callback_proc.class}")
379
+ return
380
+ end
381
+
382
+ NewRelic::Agent.record_api_supportability_metric(:set_segment_callback)
383
+ instance_variable_set(CALLBACK, callback_proc)
384
+ end
330
385
  end
331
386
  end
332
387
  end
@@ -14,6 +14,7 @@ module NewRelic
14
14
  # @api public
15
15
  class ExternalRequestSegment < Segment
16
16
  NR_SYNTHETICS_HEADER = 'X-NewRelic-Synthetics'
17
+ NR_SYNTHETICS_INFO_HEADER = 'X-NewRelic-Synthetics-Info'
17
18
  APP_DATA_KEY = 'NewRelicAppData'
18
19
 
19
20
  EXTERNAL_ALL = 'External/all'
@@ -63,7 +64,8 @@ module NewRelic
63
64
  def add_request_headers(request)
64
65
  process_host_header(request)
65
66
  synthetics_header = transaction&.raw_synthetics_header
66
- insert_synthetics_header(request, synthetics_header) if synthetics_header
67
+ synthetics_info_header = transaction&.raw_synthetics_info_header
68
+ insert_synthetics_header(request, synthetics_header, synthetics_info_header) if synthetics_header
67
69
 
68
70
  return unless record_metrics?
69
71
 
@@ -207,8 +209,9 @@ module NewRelic
207
209
  end
208
210
  end
209
211
 
210
- def insert_synthetics_header(request, header)
212
+ def insert_synthetics_header(request, header, info)
211
213
  request[NR_SYNTHETICS_HEADER] = header
214
+ request[NR_SYNTHETICS_INFO_HEADER] = info if info
212
215
  end
213
216
 
214
217
  def segment_complete
@@ -15,7 +15,6 @@ module NewRelic
15
15
  PRODUCE = 'Produce'.freeze
16
16
  QUEUE = 'Queue'.freeze
17
17
  PURGE = 'Purge'.freeze
18
- SLASH = '/'.freeze
19
18
  TEMP = 'Temp'.freeze
20
19
  TOPIC = 'Topic'.freeze
21
20
  UNKNOWN = 'Unknown'.freeze
@@ -73,7 +72,7 @@ module NewRelic
73
72
  return @name if @name
74
73
 
75
74
  @name = METRIC_PREFIX + library
76
- @name << SLASH << TYPES[destination_type] << SLASH << ACTIONS[action] << SLASH
75
+ @name << NewRelic::SLASH << TYPES[destination_type] << NewRelic::SLASH << ACTIONS[action] << NewRelic::SLASH
77
76
 
78
77
  if destination_type == :temporary_queue || destination_type == :temporary_topic
79
78
  @name << TEMP
@@ -8,11 +8,17 @@ module NewRelic
8
8
  module Agent
9
9
  class Transaction
10
10
  class RequestAttributes
11
- attr_reader :request_path, :referer, :accept, :content_length, :content_type,
12
- :host, :port, :user_agent, :request_method
11
+ # the HTTP standard has "referrer" mispelled as "referer"
12
+ attr_reader :accept, :content_length, :content_type, :host, :other_headers, :port, :referer, :request_method,
13
+ :request_path, :user_agent
13
14
 
14
15
  HTTP_ACCEPT_HEADER_KEY = 'HTTP_ACCEPT'.freeze
15
16
 
17
+ BASE_HEADERS = %w[CONTENT_LENGTH CONTENT_TYPE HTTP_ACCEPT HTTP_REFERER HTTP_USER_AGENT PATH_INFO REMOTE_HOST
18
+ REQUEST_METHOD REQUEST_URI SERVER_PORT].freeze
19
+
20
+ ATTRIBUTE_PREFIX = 'request.headers.'
21
+
16
22
  def initialize(request)
17
23
  @request_path = path_from_request(request)
18
24
  @referer = referer_from_request(request)
@@ -23,6 +29,7 @@ module NewRelic
23
29
  @port = port_from_request(request)
24
30
  @user_agent = attribute_from_request(request, :user_agent)
25
31
  @request_method = attribute_from_request(request, :request_method)
32
+ @other_headers = other_headers_from_request(request)
26
33
  end
27
34
 
28
35
  def assign_agent_attributes(txn)
@@ -31,14 +38,17 @@ module NewRelic
31
38
  AttributeFilter::DST_ERROR_COLLECTOR
32
39
 
33
40
  if referer
34
- txn.add_agent_attribute(:'request.headers.referer', referer, AttributeFilter::DST_ERROR_COLLECTOR)
41
+ destinations = allow_other_headers? ? default_destinations : AttributeFilter::DST_ERROR_COLLECTOR
42
+ txn.add_agent_attribute(:'request.headers.referer', referer, destinations)
35
43
  end
36
44
 
37
45
  if request_path
38
- txn.add_agent_attribute(:'request.uri',
39
- request_path,
40
- AttributeFilter::DST_TRANSACTION_TRACER |
41
- AttributeFilter::DST_ERROR_COLLECTOR)
46
+ destinations = if allow_other_headers?
47
+ default_destinations
48
+ else
49
+ AttributeFilter::DST_TRANSACTION_TRACER | AttributeFilter::DST_ERROR_COLLECTOR
50
+ end
51
+ txn.add_agent_attribute(:'request.uri', request_path, destinations)
42
52
  end
43
53
 
44
54
  if accept
@@ -64,6 +74,14 @@ module NewRelic
64
74
  if request_method
65
75
  txn.add_agent_attribute(:'request.method', request_method, default_destinations)
66
76
  end
77
+
78
+ if port && allow_other_headers?
79
+ txn.add_agent_attribute(:'request.headers.port', port, default_destinations)
80
+ end
81
+
82
+ other_headers.each do |header, value|
83
+ txn.add_agent_attribute(header, value, default_destinations)
84
+ end
67
85
  end
68
86
 
69
87
  private
@@ -85,12 +103,10 @@ module NewRelic
85
103
  # rails construct the PATH_INFO enviroment variable improperly and we're generally
86
104
  # being defensive.
87
105
 
88
- ROOT_PATH = '/'.freeze
89
-
90
106
  def path_from_request(request)
91
107
  path = attribute_from_request(request, :path) || ''
92
108
  path = HTTPClients::URIUtil.strip_query_string(path)
93
- path.empty? ? ROOT_PATH : path
109
+ path.empty? ? NewRelic::ROOT : path
94
110
  end
95
111
 
96
112
  def content_length_from_request(request)
@@ -116,6 +132,26 @@ module NewRelic
116
132
  env[key]
117
133
  end
118
134
  end
135
+
136
+ def allow_other_headers?
137
+ NewRelic::Agent.config[:allow_all_headers] && !NewRelic::Agent.config[:high_security]
138
+ end
139
+
140
+ def other_headers_from_request(request)
141
+ # confirm that `request` is an instance of `Rack::Request` by checking
142
+ # for #each_header
143
+ return NewRelic::EMPTY_HASH unless allow_other_headers? && request.respond_to?(:each_header)
144
+
145
+ request.each_header.with_object({}) do |(header, value), hash|
146
+ next if BASE_HEADERS.include?(header)
147
+
148
+ hash[formatted_header(header)] = value
149
+ end
150
+ end
151
+
152
+ def formatted_header(raw_name)
153
+ "#{ATTRIBUTE_PREFIX}#{NewRelic::LanguageSupport.camelize_with_first_letter_downcased(raw_name)}".to_sym
154
+ end
119
155
  end
120
156
  end
121
157
  end
@@ -31,11 +31,12 @@ module NewRelic
31
31
  RAKE_PREFIX = "#{OTHER_TRANSACTION_PREFIX}Rake/"
32
32
  MESSAGE_PREFIX = "#{OTHER_TRANSACTION_PREFIX}Message/"
33
33
  RACK_PREFIX = "#{CONTROLLER_PREFIX}Rack/"
34
+ RODA_PREFIX = "#{CONTROLLER_PREFIX}Roda/"
34
35
  SINATRA_PREFIX = "#{CONTROLLER_PREFIX}Sinatra/"
35
36
  GRAPE_PREFIX = "#{CONTROLLER_PREFIX}Grape/"
36
37
  ACTION_CABLE_PREFIX = "#{CONTROLLER_PREFIX}ActionCable/"
37
38
 
38
- WEB_TRANSACTION_CATEGORIES = [:web, :controller, :uri, :rack, :sinatra, :grape, :middleware, :action_cable].freeze
39
+ WEB_TRANSACTION_CATEGORIES = %i[action_cable controller grape middleware rack roda sinatra web uri].freeze
39
40
 
40
41
  MIDDLEWARE_SUMMARY_METRICS = ['Middleware/all'].freeze
41
42
  WEB_SUMMARY_METRIC = 'HttpDispatcher'
@@ -89,7 +90,7 @@ module NewRelic
89
90
  attr_reader :transaction_trace
90
91
 
91
92
  # Fields for tracking synthetics requests
92
- attr_accessor :raw_synthetics_header, :synthetics_payload
93
+ attr_accessor :raw_synthetics_header, :synthetics_payload, :synthetics_info_payload, :raw_synthetics_info_header
93
94
 
94
95
  # Return the currently active transaction, or nil.
95
96
  def self.tl_current
@@ -289,7 +290,7 @@ module NewRelic
289
290
  end
290
291
 
291
292
  def sampled?
292
- return unless Agent.config[:'distributed_tracing.enabled']
293
+ return false unless Agent.config[:'distributed_tracing.enabled']
293
294
 
294
295
  if @sampled.nil?
295
296
  @sampled = NewRelic::Agent.instance.adaptive_sampler.sampled?
@@ -519,7 +520,7 @@ module NewRelic
519
520
  end
520
521
 
521
522
  def finish
522
- return unless state.is_execution_traced?
523
+ return unless state.is_execution_traced? && initial_segment
523
524
 
524
525
  @end_time = Process.clock_gettime(Process::CLOCK_REALTIME)
525
526
  @duration = @end_time - @start_time
@@ -545,8 +546,8 @@ module NewRelic
545
546
  end
546
547
 
547
548
  def user_defined_rules_ignore?
548
- return unless request_path
549
- return if (rules = NewRelic::Agent.config[:"rules.ignore_url_regexes"]).empty?
549
+ return false unless request_path
550
+ return false if (rules = NewRelic::Agent.config[:"rules.ignore_url_regexes"]).empty?
550
551
 
551
552
  rules.any? do |rule|
552
553
  request_path.match(rule)
@@ -622,11 +623,24 @@ module NewRelic
622
623
  attributes.add_intrinsic_attribute(:synthetics_resource_id, synthetics_resource_id)
623
624
  attributes.add_intrinsic_attribute(:synthetics_job_id, synthetics_job_id)
624
625
  attributes.add_intrinsic_attribute(:synthetics_monitor_id, synthetics_monitor_id)
626
+ attributes.add_intrinsic_attribute(:synthetics_type, synthetics_info('type'))
627
+ attributes.add_intrinsic_attribute(:synthetics_initiator, synthetics_info('initiator'))
628
+
629
+ synthetics_additional_attributes do |key, value|
630
+ attributes.add_intrinsic_attribute(key, value)
631
+ end
625
632
  end
626
633
 
627
634
  distributed_tracer.assign_intrinsics
628
635
  end
629
636
 
637
+ def synthetics_additional_attributes(&block)
638
+ synthetics_info('attributes')&.each do |k, v|
639
+ new_key = "synthetics_#{NewRelic::LanguageSupport.snakeize(k.to_s)}".to_sym
640
+ yield(new_key, v.to_s)
641
+ end
642
+ end
643
+
630
644
  def calculate_gc_time
631
645
  gc_stop_snapshot = NewRelic::Agent::StatsEngine::GCProfiler.take_snapshot
632
646
  NewRelic::Agent::StatsEngine::GCProfiler.record_delta(gc_start_snapshot, gc_stop_snapshot)
@@ -706,6 +720,10 @@ module NewRelic
706
720
  info[4]
707
721
  end
708
722
 
723
+ def synthetics_info(key)
724
+ synthetics_info_payload[key] if synthetics_info_payload
725
+ end
726
+
709
727
  def append_apdex_perf_zone(payload)
710
728
  if recording_web_transaction?
711
729
  bucket = apdex_bucket(duration, apdex_t)
@@ -729,6 +747,12 @@ module NewRelic
729
747
  payload[:synthetics_resource_id] = synthetics_resource_id
730
748
  payload[:synthetics_job_id] = synthetics_job_id
731
749
  payload[:synthetics_monitor_id] = synthetics_monitor_id
750
+ payload[:synthetics_type] = synthetics_info('type')
751
+ payload[:synthetics_initiator] = synthetics_info('initiator')
752
+
753
+ synthetics_additional_attributes do |key, value|
754
+ payload[key] = value
755
+ end
732
756
  end
733
757
 
734
758
  def merge_metrics
@@ -31,9 +31,14 @@ module NewRelic
31
31
  SYNTHETICS_RESOURCE_ID_KEY = 'nr.syntheticsResourceId'.freeze
32
32
  SYNTHETICS_JOB_ID_KEY = 'nr.syntheticsJobId'.freeze
33
33
  SYNTHETICS_MONITOR_ID_KEY = 'nr.syntheticsMonitorId'.freeze
34
+ SYNTHETICS_TYPE_KEY = 'nr.syntheticsType'
35
+ SYNTHETICS_INITIATOR_KEY = 'nr.syntheticsInitiator'
36
+ SYNTHETICS_KEY_PREFIX = 'nr.synthetics'
34
37
  PRIORITY_KEY = 'priority'.freeze
35
38
  SPAN_ID_KEY = 'spanId'.freeze
36
39
 
40
+ SYNTHETICS_PAYLOAD_EXPECTED = [:synthetics_resource_id, :synthetics_job_id, :synthetics_monitor_id, :synthetics_type, :synthetics_initiator]
41
+
37
42
  def create(noticed_error, payload, span_id)
38
43
  [
39
44
  intrinsic_attributes_for(noticed_error, payload, span_id),
@@ -71,9 +76,20 @@ module NewRelic
71
76
  end
72
77
 
73
78
  def append_synthetics(payload, sample)
79
+ return unless payload[:synthetics_job_id]
80
+
74
81
  sample[SYNTHETICS_RESOURCE_ID_KEY] = payload[:synthetics_resource_id] if payload[:synthetics_resource_id]
75
82
  sample[SYNTHETICS_JOB_ID_KEY] = payload[:synthetics_job_id] if payload[:synthetics_job_id]
76
83
  sample[SYNTHETICS_MONITOR_ID_KEY] = payload[:synthetics_monitor_id] if payload[:synthetics_monitor_id]
84
+ sample[SYNTHETICS_TYPE_KEY] = payload[:synthetics_type] if payload[:synthetics_type]
85
+ sample[SYNTHETICS_INITIATOR_KEY] = payload[:synthetics_initiator] if payload[:synthetics_initiator]
86
+
87
+ payload.each do |k, v|
88
+ next unless k.to_s.start_with?('synthetics_') && !SYNTHETICS_PAYLOAD_EXPECTED.include?(k)
89
+
90
+ new_key = SYNTHETICS_KEY_PREFIX + NewRelic::LanguageSupport.camelize(k.to_s.gsub('synthetics_', ''))
91
+ sample[new_key] = v
92
+ end
77
93
  end
78
94
 
79
95
  def append_cat(payload, sample)
@@ -38,6 +38,11 @@ module NewRelic
38
38
  SYNTHETICS_RESOURCE_ID_KEY = 'nr.syntheticsResourceId'
39
39
  SYNTHETICS_JOB_ID_KEY = 'nr.syntheticsJobId'
40
40
  SYNTHETICS_MONITOR_ID_KEY = 'nr.syntheticsMonitorId'
41
+ SYNTHETICS_TYPE_KEY = 'nr.syntheticsType'
42
+ SYNTHETICS_INITIATOR_KEY = 'nr.syntheticsInitiator'
43
+ SYNTHETICS_KEY_PREFIX = 'nr.synthetics'
44
+
45
+ SYNTHETICS_PAYLOAD_EXPECTED = [:synthetics_resource_id, :synthetics_job_id, :synthetics_monitor_id, :synthetics_type, :synthetics_initiator]
41
46
 
42
47
  def create(payload)
43
48
  intrinsics = {
@@ -71,9 +76,23 @@ module NewRelic
71
76
  optionally_append(SYNTHETICS_RESOURCE_ID_KEY, :synthetics_resource_id, sample, payload)
72
77
  optionally_append(SYNTHETICS_JOB_ID_KEY, :synthetics_job_id, sample, payload)
73
78
  optionally_append(SYNTHETICS_MONITOR_ID_KEY, :synthetics_monitor_id, sample, payload)
79
+ optionally_append(SYNTHETICS_TYPE_KEY, :synthetics_type, sample, payload)
80
+ optionally_append(SYNTHETICS_INITIATOR_KEY, :synthetics_initiator, sample, payload)
81
+ append_synthetics_info_attributes(sample, payload)
74
82
  append_cat_alternate_path_hashes(sample, payload)
75
83
  end
76
84
 
85
+ def append_synthetics_info_attributes(sample, payload)
86
+ return unless payload.include?(:synthetics_job_id)
87
+
88
+ payload.each do |k, v|
89
+ next unless k.to_s.start_with?('synthetics_') && !SYNTHETICS_PAYLOAD_EXPECTED.include?(k)
90
+
91
+ new_key = SYNTHETICS_KEY_PREFIX + NewRelic::LanguageSupport.camelize(k.to_s.gsub('synthetics_', ''))
92
+ sample[new_key] = v.to_s
93
+ end
94
+ end
95
+
77
96
  def append_cat_alternate_path_hashes(sample, payload)
78
97
  if payload.include?(:cat_alternate_path_hashes)
79
98
  sample[CAT_ALTERNATE_PATH_HASHES_KEY] = payload[:cat_alternate_path_hashes].sort.join(COMMA)
@@ -24,10 +24,8 @@ module NewRelic
24
24
  body
25
25
  end
26
26
 
27
- SLASH = '/'.freeze
28
-
29
27
  def trim_leading(value)
30
- value.split(SLASH).last
28
+ value.split(NewRelic::SLASH).last
31
29
  end
32
30
  end
33
31
  end