newrelic_rpm 9.5.0 → 9.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -6
  3. data/Rakefile +1 -1
  4. data/lib/new_relic/agent/configuration/default_source.rb +80 -33
  5. data/lib/new_relic/agent/http_clients/async_http_wrappers.rb +83 -0
  6. data/lib/new_relic/agent/http_clients/ethon_wrappers.rb +111 -0
  7. data/lib/new_relic/agent/http_clients/httpx_wrappers.rb +93 -0
  8. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +1 -2
  9. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/chain.rb +69 -0
  10. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +13 -0
  11. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/prepend.rb +37 -0
  12. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb +23 -0
  13. data/lib/new_relic/agent/instrumentation/active_support_logger.rb +3 -1
  14. data/lib/new_relic/agent/instrumentation/async_http/chain.rb +23 -0
  15. data/lib/new_relic/agent/instrumentation/async_http/instrumentation.rb +37 -0
  16. data/lib/new_relic/agent/instrumentation/async_http/prepend.rb +15 -0
  17. data/lib/new_relic/agent/instrumentation/async_http.rb +26 -0
  18. data/lib/new_relic/agent/instrumentation/ethon/chain.rb +39 -0
  19. data/lib/new_relic/agent/instrumentation/ethon/instrumentation.rb +105 -0
  20. data/lib/new_relic/agent/instrumentation/ethon/prepend.rb +35 -0
  21. data/lib/new_relic/agent/instrumentation/ethon.rb +39 -0
  22. data/lib/new_relic/agent/instrumentation/httpx/chain.rb +20 -0
  23. data/lib/new_relic/agent/instrumentation/httpx/instrumentation.rb +51 -0
  24. data/lib/new_relic/agent/instrumentation/httpx/prepend.rb +15 -0
  25. data/lib/new_relic/agent/instrumentation/httpx.rb +27 -0
  26. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +1 -3
  27. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +1 -1
  28. data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +1 -0
  29. data/lib/new_relic/agent/instrumentation/roda/ignorer.rb +45 -0
  30. data/lib/new_relic/agent/instrumentation/roda/instrumentation.rb +12 -0
  31. data/lib/new_relic/agent/instrumentation/roda/roda_transaction_namer.rb +1 -2
  32. data/lib/new_relic/agent/instrumentation/roda.rb +2 -0
  33. data/lib/new_relic/agent/instrumentation/sidekiq.rb +3 -1
  34. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +1 -3
  35. data/lib/new_relic/agent/messaging.rb +2 -2
  36. data/lib/new_relic/agent/monitors/synthetics_monitor.rb +12 -1
  37. data/lib/new_relic/agent/rules_engine.rb +1 -1
  38. data/lib/new_relic/agent/span_event_primitive.rb +16 -4
  39. data/lib/new_relic/agent/system_info.rb +26 -0
  40. data/lib/new_relic/agent/tracer.rb +1 -3
  41. data/lib/new_relic/agent/transaction/abstract_segment.rb +3 -0
  42. data/lib/new_relic/agent/transaction/external_request_segment.rb +5 -2
  43. data/lib/new_relic/agent/transaction/message_broker_segment.rb +1 -2
  44. data/lib/new_relic/agent/transaction/request_attributes.rb +1 -3
  45. data/lib/new_relic/agent/transaction.rb +25 -2
  46. data/lib/new_relic/agent/transaction_error_primitive.rb +16 -0
  47. data/lib/new_relic/agent/transaction_event_primitive.rb +19 -0
  48. data/lib/new_relic/agent/utilization/gcp.rb +1 -3
  49. data/lib/new_relic/agent.rb +6 -2
  50. data/lib/new_relic/constants.rb +3 -0
  51. data/lib/new_relic/control/frameworks/rails.rb +14 -2
  52. data/lib/new_relic/language_support.rb +4 -0
  53. data/lib/new_relic/version.rb +1 -1
  54. data/lib/tasks/instrumentation_generator/instrumentation.thor +3 -3
  55. data/lib/tasks/tests.rake +71 -0
  56. data/newrelic.yml +49 -33
  57. data/newrelic_rpm.gemspec +4 -1
  58. metadata +36 -2
@@ -0,0 +1,39 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'ethon/instrumentation'
6
+ require_relative 'ethon/chain'
7
+ require_relative 'ethon/prepend'
8
+
9
+ DependencyDetection.defer do
10
+ named :ethon
11
+
12
+ # If Ethon is being used as a dependency of Typhoeus, allow the Typhoeus
13
+ # instrumentation to handle everything. Otherwise each external network call
14
+ # will confusingly result in "Ethon" segments duplicating the information
15
+ # already provided by "Typhoeus" segments.
16
+ depends_on do
17
+ !defined?(Typhoeus)
18
+ end
19
+
20
+ depends_on do
21
+ defined?(Ethon) && Gem::Version.new(Ethon::VERSION) >= Gem::Version.new('0.12.0')
22
+ end
23
+
24
+ executes do
25
+ NewRelic::Agent.logger.info('Installing ethon instrumentation')
26
+ end
27
+
28
+ executes do
29
+ if use_prepend?
30
+ # NOTE: by default prepend_instrument will go with the module name that
31
+ # precedes 'Prepend' (so 'Easy' and 'Multi'), but we want to use
32
+ # 'Ethon::Easy' and 'Ethon::Multi' so 3rd argument is supplied
33
+ prepend_instrument Ethon::Easy, NewRelic::Agent::Instrumentation::Ethon::Easy::Prepend, Ethon::Easy.name
34
+ prepend_instrument Ethon::Multi, NewRelic::Agent::Instrumentation::Ethon::Multi::Prepend, Ethon::Multi.name
35
+ else
36
+ chain_instrument NewRelic::Agent::Instrumentation::Ethon::Chain
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module HTTPX
7
+ module Chain
8
+ def self.instrument!
9
+ ::HTTPX::Session.class_eval do
10
+ include NewRelic::Agent::Instrumentation::HTTPX
11
+
12
+ alias_method(:send_requests_without_tracing, :send_requests)
13
+ def send_requests(*requests)
14
+ send_requests_with_tracing(*requests) { send_requests_without_tracing(*requests) }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,51 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require 'new_relic/agent/http_clients/httpx_wrappers'
6
+
7
+ module NewRelic::Agent::Instrumentation::HTTPX
8
+ INSTRUMENTATION_NAME = 'HTTPX'
9
+ NOTICEABLE_ERROR_CLASS = 'HTTPX::Error'
10
+
11
+ def send_requests_with_tracing(*requests)
12
+ NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)
13
+ requests.each { |r| nr_start_segment(r) }
14
+ yield
15
+ end
16
+
17
+ def nr_start_segment(request)
18
+ return unless NewRelic::Agent::Tracer.state.is_execution_traced?
19
+
20
+ wrapped_request = NewRelic::Agent::HTTPClients::HTTPXHTTPRequest.new(request)
21
+ segment = NewRelic::Agent::Tracer.start_external_request_segment(
22
+ library: wrapped_request.type,
23
+ uri: wrapped_request.uri,
24
+ procedure: wrapped_request.method
25
+ )
26
+ segment.add_request_headers(wrapped_request)
27
+
28
+ request.on(:response) { nr_finish_segment.call(request, segment) }
29
+ end
30
+
31
+ def nr_finish_segment
32
+ proc do |request, segment|
33
+ response = @responses[request]
34
+
35
+ unless response
36
+ NewRelic::Agent.logger.debug('Processed an on-response callback for HTTPX but could not find the response!')
37
+ next
38
+ end
39
+
40
+ wrapped_response = NewRelic::Agent::HTTPClients::HTTPXHTTPResponse.new(response)
41
+ segment.process_response_headers(wrapped_response)
42
+
43
+ if response.is_a?(::HTTPX::ErrorResponse)
44
+ e = NewRelic::Agent::NoticeableError.new(NOTICEABLE_ERROR_CLASS, "Couldn't connect: #{response}")
45
+ segment.notice_error(e)
46
+ end
47
+
48
+ ::NewRelic::Agent::Transaction::Segment.finish(segment)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module HTTPX
7
+ module Prepend
8
+ include NewRelic::Agent::Instrumentation::HTTPX
9
+
10
+ def send_requests(*requests)
11
+ send_requests_with_tracing(*requests) { super }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'httpx/chain'
6
+ require_relative 'httpx/instrumentation'
7
+ require_relative 'httpx/prepend'
8
+
9
+ DependencyDetection.defer do
10
+ named :httpx
11
+
12
+ depends_on do
13
+ defined?(HTTPX) && Gem::Version.new(HTTPX::VERSION) >= Gem::Version.new('1.0.0')
14
+ end
15
+
16
+ executes do
17
+ NewRelic::Agent.logger.info('Installing httpx instrumentation')
18
+ end
19
+
20
+ executes do
21
+ if use_prepend?
22
+ prepend_instrument HTTPX::Session, NewRelic::Agent::Instrumentation::HTTPX::Prepend
23
+ else
24
+ chain_instrument NewRelic::Agent::Instrumentation::HTTPX::Chain
25
+ end
26
+ end
27
+ end
@@ -131,10 +131,8 @@ module NewRelic
131
131
  UNKNOWN
132
132
  end
133
133
 
134
- SLASH = '/'.freeze
135
-
136
134
  def unix_domain_socket?(host)
137
- host.start_with?(SLASH)
135
+ host.start_with?(NewRelic::SLASH)
138
136
  end
139
137
  end
140
138
  end
@@ -35,7 +35,7 @@ module NewRelic
35
35
  segment.process_response_headers(wrapped_response)
36
36
  response
37
37
  ensure
38
- segment.finish
38
+ segment&.finish
39
39
  end
40
40
  end
41
41
  end
@@ -34,6 +34,7 @@ DependencyDetection.defer do
34
34
 
35
35
  subs = %w[send_file
36
36
  send_data
37
+ send_stream
37
38
  redirect_to
38
39
  halted_callback
39
40
  unpermitted_parameters]
@@ -0,0 +1,45 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module Roda
7
+ module Ignorer
8
+ def self.should_ignore?(app, type)
9
+ return false unless app.opts.include?(:newrelic_ignores)
10
+
11
+ app.opts[:newrelic_ignores][type].any? do |pattern|
12
+ pattern === app.request.path_info
13
+ end
14
+ end
15
+
16
+ def newrelic_ignore(*routes)
17
+ set_newrelic_ignore(:routes, *routes)
18
+ end
19
+
20
+ def newrelic_ignore_apdex(*routes)
21
+ set_newrelic_ignore(:apdex, *routes)
22
+ end
23
+
24
+ def newrelic_ignore_enduser(*routes)
25
+ set_newrelic_ignore(:enduser, *routes)
26
+ end
27
+
28
+ private
29
+
30
+ def set_newrelic_ignore(type, *routes)
31
+ # Create a newrelic_ignores hash if one doesn't exist
32
+ opts[:newrelic_ignores] = Hash.new([]) if !opts.include?(:newrelic_ignores)
33
+
34
+ if routes.empty?
35
+ opts[:newrelic_ignores][type] += [Regexp.new('.*')]
36
+ else
37
+ opts[:newrelic_ignores][type] += routes.map do |r|
38
+ # Roda adds leading slashes to routes, so we need to do the same
39
+ "#{'/' unless r.start_with?('/')}#{r}"
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -51,6 +51,18 @@ module NewRelic::Agent::Instrumentation
51
51
  yield
52
52
  end
53
53
  end
54
+
55
+ def do_not_trace?
56
+ NewRelic::Agent::Instrumentation::Roda::Ignorer.should_ignore?(self, :routes)
57
+ end
58
+
59
+ def ignore_apdex?
60
+ NewRelic::Agent::Instrumentation::Roda::Ignorer.should_ignore?(self, :apdex)
61
+ end
62
+
63
+ def ignore_enduser?
64
+ NewRelic::Agent::Instrumentation::Roda::Ignorer.should_ignore?(self, :enduser)
65
+ end
54
66
  end
55
67
  end
56
68
  end
@@ -9,13 +9,12 @@ module NewRelic
9
9
  module TransactionNamer
10
10
  extend self
11
11
 
12
- ROOT = '/'.freeze
13
12
  REGEX_MULTIPLE_SLASHES = %r{^[/^]*(.*?)[/$?]*$}.freeze
14
13
 
15
14
  def transaction_name(request)
16
15
  path = request.path || ::NewRelic::Agent::UNKNOWN_METRIC
17
16
  name = path.gsub(REGEX_MULTIPLE_SLASHES, '\1') # remove any rogue slashes
18
- name = ROOT if name.empty?
17
+ name = NewRelic::ROOT if name.empty?
19
18
  name = "#{request.request_method} #{name}" if request.respond_to?(:request_method)
20
19
 
21
20
  name
@@ -4,6 +4,7 @@
4
4
 
5
5
  require_relative 'roda/instrumentation'
6
6
  require_relative 'roda/roda_transaction_namer'
7
+ require_relative 'roda/ignorer'
7
8
 
8
9
  DependencyDetection.defer do
9
10
  named :roda
@@ -30,5 +31,6 @@ DependencyDetection.defer do
30
31
  chain_instrument NewRelic::Agent::Instrumentation::Roda::Build::Chain
31
32
  chain_instrument NewRelic::Agent::Instrumentation::Roda::Chain
32
33
  end
34
+ Roda.class_eval { extend NewRelic::Agent::Instrumentation::Roda::Ignorer }
33
35
  end
34
36
  end
@@ -33,7 +33,9 @@ DependencyDetection.defer do
33
33
  end
34
34
 
35
35
  if config.respond_to?(:error_handlers)
36
- config.error_handlers << proc do |error, *_|
36
+ # Sidekiq 3.0.0 - 7.1.4 expect error_handlers to have 2 arguments
37
+ # Sidekiq 7.1.5+ expect error_handlers to have 3 arguments
38
+ config.error_handlers << proc do |error, _ctx, *_|
37
39
  NewRelic::Agent.notice_error(error)
38
40
  end
39
41
  end
@@ -25,14 +25,12 @@ module NewRelic
25
25
  transaction_name(::NewRelic::Agent::UNKNOWN_METRIC, request)
26
26
  end
27
27
 
28
- ROOT = '/'.freeze
29
-
30
28
  def transaction_name(route_text, request)
31
29
  verb = http_verb(request)
32
30
 
33
31
  route_text = route_text.source if route_text.is_a?(Regexp)
34
32
  name = route_text.gsub(%r{^[/^\\A]*(.*?)[/\$\?\\z]*$}, '\1')
35
- name = ROOT if name.empty?
33
+ name = NewRelic::ROOT if name.empty?
36
34
  name = "#{verb} #{name}" unless verb.nil?
37
35
  name
38
36
  rescue => e
@@ -329,9 +329,9 @@ module NewRelic
329
329
 
330
330
  def transaction_name(library, destination_type, destination_name)
331
331
  transaction_name = Transaction::MESSAGE_PREFIX + library
332
- transaction_name << Transaction::MessageBrokerSegment::SLASH
332
+ transaction_name << NewRelic::SLASH
333
333
  transaction_name << Transaction::MessageBrokerSegment::TYPES[destination_type]
334
- transaction_name << Transaction::MessageBrokerSegment::SLASH
334
+ transaction_name << NewRelic::SLASH
335
335
 
336
336
  case destination_type
337
337
  when :queue
@@ -5,7 +5,8 @@
5
5
  module NewRelic
6
6
  module Agent
7
7
  class SyntheticsMonitor < InboundRequestMonitor
8
- SYNTHETICS_HEADER_KEY = 'HTTP_X_NEWRELIC_SYNTHETICS'.freeze
8
+ SYNTHETICS_HEADER_KEY = 'HTTP_X_NEWRELIC_SYNTHETICS'
9
+ SYNTHETICS_INFO_HEADER_KEY = 'HTTP_X_NEWRELIC_SYNTHETICS_INFO'
9
10
 
10
11
  SUPPORTED_VERSION = 1
11
12
  EXPECTED_PAYLOAD_LENGTH = 5
@@ -16,6 +17,7 @@ module NewRelic
16
17
 
17
18
  def on_before_call(request) # THREAD_LOCAL_ACCESS
18
19
  encoded_header = request[SYNTHETICS_HEADER_KEY]
20
+ info_header = request[SYNTHETICS_INFO_HEADER_KEY]
19
21
  return unless encoded_header
20
22
 
21
23
  incoming_payload = deserialize_header(encoded_header, SYNTHETICS_HEADER_KEY)
@@ -27,7 +29,16 @@ module NewRelic
27
29
 
28
30
  txn = Tracer.current_transaction
29
31
  txn.raw_synthetics_header = encoded_header
32
+ txn.raw_synthetics_info_header = info_header
30
33
  txn.synthetics_payload = incoming_payload
34
+ txn.synthetics_info_payload = load_json(info_header, SYNTHETICS_INFO_HEADER_KEY)
35
+ end
36
+
37
+ def load_json(header, key)
38
+ ::JSON.load(header)
39
+ rescue => err
40
+ NewRelic::Agent.logger.debug("Failure loading json header '#{key}' in #{self.class}, #{err.class}, #{err.message}")
41
+ nil
31
42
  end
32
43
 
33
44
  class << self
@@ -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
@@ -357,9 +357,7 @@ module NewRelic
357
357
  yield
358
358
  rescue => exception
359
359
  # needs else branch coverage
360
- if segment && segment.is_a?(Transaction::AbstractSegment) # rubocop:disable Style/SafeNavigation
361
- segment.notice_error(exception)
362
- end
360
+ segment.notice_error(exception) if segment&.is_a?(Transaction::AbstractSegment)
363
361
  raise
364
362
  end
365
363
 
@@ -338,6 +338,9 @@ module NewRelic
338
338
 
339
339
  NewRelic::Agent.logger.debug("Invoking callback for #{self.class.name}...")
340
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}")
341
344
  end
342
345
 
343
346
  # Setting and invoking a segment callback
@@ -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
@@ -103,12 +103,10 @@ module NewRelic
103
103
  # rails construct the PATH_INFO enviroment variable improperly and we're generally
104
104
  # being defensive.
105
105
 
106
- ROOT_PATH = '/'.freeze
107
-
108
106
  def path_from_request(request)
109
107
  path = attribute_from_request(request, :path) || ''
110
108
  path = HTTPClients::URIUtil.strip_query_string(path)
111
- path.empty? ? ROOT_PATH : path
109
+ path.empty? ? NewRelic::ROOT : path
112
110
  end
113
111
 
114
112
  def content_length_from_request(request)
@@ -90,7 +90,7 @@ module NewRelic
90
90
  attr_reader :transaction_trace
91
91
 
92
92
  # Fields for tracking synthetics requests
93
- attr_accessor :raw_synthetics_header, :synthetics_payload
93
+ attr_accessor :raw_synthetics_header, :synthetics_payload, :synthetics_info_payload, :raw_synthetics_info_header
94
94
 
95
95
  # Return the currently active transaction, or nil.
96
96
  def self.tl_current
@@ -520,7 +520,7 @@ module NewRelic
520
520
  end
521
521
 
522
522
  def finish
523
- return unless state.is_execution_traced?
523
+ return unless state.is_execution_traced? && initial_segment
524
524
 
525
525
  @end_time = Process.clock_gettime(Process::CLOCK_REALTIME)
526
526
  @duration = @end_time - @start_time
@@ -623,11 +623,24 @@ module NewRelic
623
623
  attributes.add_intrinsic_attribute(:synthetics_resource_id, synthetics_resource_id)
624
624
  attributes.add_intrinsic_attribute(:synthetics_job_id, synthetics_job_id)
625
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
626
632
  end
627
633
 
628
634
  distributed_tracer.assign_intrinsics
629
635
  end
630
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
+
631
644
  def calculate_gc_time
632
645
  gc_stop_snapshot = NewRelic::Agent::StatsEngine::GCProfiler.take_snapshot
633
646
  NewRelic::Agent::StatsEngine::GCProfiler.record_delta(gc_start_snapshot, gc_stop_snapshot)
@@ -707,6 +720,10 @@ module NewRelic
707
720
  info[4]
708
721
  end
709
722
 
723
+ def synthetics_info(key)
724
+ synthetics_info_payload[key] if synthetics_info_payload
725
+ end
726
+
710
727
  def append_apdex_perf_zone(payload)
711
728
  if recording_web_transaction?
712
729
  bucket = apdex_bucket(duration, apdex_t)
@@ -730,6 +747,12 @@ module NewRelic
730
747
  payload[:synthetics_resource_id] = synthetics_resource_id
731
748
  payload[:synthetics_job_id] = synthetics_job_id
732
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
733
756
  end
734
757
 
735
758
  def merge_metrics