newrelic_rpm 9.5.0 → 9.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +112 -7
  3. data/CONTRIBUTING.md +0 -7
  4. data/Rakefile +1 -1
  5. data/bin/newrelic +2 -9
  6. data/bin/newrelic_rpm +15 -0
  7. data/init.rb +2 -2
  8. data/lib/new_relic/agent/agent.rb +1 -1
  9. data/lib/new_relic/agent/agent_helpers/shutdown.rb +1 -1
  10. data/lib/new_relic/agent/agent_helpers/special_startup.rb +1 -1
  11. data/lib/new_relic/agent/agent_helpers/start_worker_thread.rb +2 -2
  12. data/lib/new_relic/agent/agent_helpers/startup.rb +2 -2
  13. data/lib/new_relic/agent/attribute_filter.rb +3 -3
  14. data/lib/new_relic/agent/configuration/default_source.rb +94 -35
  15. data/lib/new_relic/agent/configuration/manager.rb +8 -7
  16. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +1 -1
  17. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +3 -3
  18. data/lib/new_relic/agent/event_loop.rb +1 -1
  19. data/lib/new_relic/agent/http_clients/abstract.rb +4 -0
  20. data/lib/new_relic/agent/http_clients/async_http_wrappers.rb +80 -0
  21. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +1 -3
  22. data/lib/new_relic/agent/http_clients/ethon_wrappers.rb +109 -0
  23. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +0 -3
  24. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +1 -3
  25. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +0 -3
  26. data/lib/new_relic/agent/http_clients/httpx_wrappers.rb +91 -0
  27. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +1 -4
  28. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +0 -3
  29. data/lib/new_relic/agent/instrumentation/active_merchant.rb +1 -1
  30. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +1 -2
  31. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/chain.rb +69 -0
  32. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +13 -0
  33. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/prepend.rb +37 -0
  34. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb +23 -0
  35. data/lib/new_relic/agent/instrumentation/active_support_logger.rb +3 -1
  36. data/lib/new_relic/agent/instrumentation/async_http/chain.rb +23 -0
  37. data/lib/new_relic/agent/instrumentation/async_http/instrumentation.rb +37 -0
  38. data/lib/new_relic/agent/instrumentation/async_http/prepend.rb +15 -0
  39. data/lib/new_relic/agent/instrumentation/async_http.rb +26 -0
  40. data/lib/new_relic/agent/instrumentation/elasticsearch/instrumentation.rb +1 -1
  41. data/lib/new_relic/agent/instrumentation/ethon/chain.rb +39 -0
  42. data/lib/new_relic/agent/instrumentation/ethon/instrumentation.rb +105 -0
  43. data/lib/new_relic/agent/instrumentation/ethon/prepend.rb +35 -0
  44. data/lib/new_relic/agent/instrumentation/ethon.rb +39 -0
  45. data/lib/new_relic/agent/instrumentation/fiber/instrumentation.rb +1 -4
  46. data/lib/new_relic/agent/instrumentation/httpx/chain.rb +20 -0
  47. data/lib/new_relic/agent/instrumentation/httpx/instrumentation.rb +51 -0
  48. data/lib/new_relic/agent/instrumentation/httpx/prepend.rb +15 -0
  49. data/lib/new_relic/agent/instrumentation/httpx.rb +27 -0
  50. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +1 -3
  51. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +1 -1
  52. data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +1 -0
  53. data/lib/new_relic/agent/instrumentation/roda/ignorer.rb +45 -0
  54. data/lib/new_relic/agent/instrumentation/roda/instrumentation.rb +12 -0
  55. data/lib/new_relic/agent/instrumentation/roda/roda_transaction_namer.rb +1 -2
  56. data/lib/new_relic/agent/instrumentation/roda.rb +2 -0
  57. data/lib/new_relic/agent/instrumentation/sidekiq.rb +3 -1
  58. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +1 -1
  59. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +1 -3
  60. data/lib/new_relic/agent/instrumentation/sinatra.rb +1 -1
  61. data/lib/new_relic/agent/instrumentation/thread/instrumentation.rb +1 -4
  62. data/lib/new_relic/agent/instrumentation/view_component/chain.rb +21 -0
  63. data/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb +38 -0
  64. data/lib/new_relic/agent/instrumentation/view_component/prepend.rb +13 -0
  65. data/lib/new_relic/agent/instrumentation/view_component.rb +26 -0
  66. data/lib/new_relic/agent/javascript_instrumentor.rb +0 -1
  67. data/lib/new_relic/agent/messaging.rb +2 -2
  68. data/lib/new_relic/agent/monitors/synthetics_monitor.rb +12 -1
  69. data/lib/new_relic/agent/new_relic_service/encoders.rb +2 -2
  70. data/lib/new_relic/agent/new_relic_service.rb +8 -6
  71. data/lib/new_relic/agent/obfuscator.rb +0 -2
  72. data/lib/new_relic/agent/pipe_channel_manager.rb +2 -2
  73. data/lib/new_relic/agent/rules_engine/segment_terms_rule.rb +1 -2
  74. data/lib/new_relic/agent/rules_engine.rb +1 -1
  75. data/lib/new_relic/agent/span_event_primitive.rb +16 -4
  76. data/lib/new_relic/agent/sql_sampler.rb +0 -1
  77. data/lib/new_relic/agent/system_info.rb +26 -0
  78. data/lib/new_relic/agent/tracer.rb +5 -6
  79. data/lib/new_relic/agent/transaction/abstract_segment.rb +3 -0
  80. data/lib/new_relic/agent/transaction/external_request_segment.rb +5 -2
  81. data/lib/new_relic/agent/transaction/message_broker_segment.rb +1 -2
  82. data/lib/new_relic/agent/transaction/request_attributes.rb +1 -3
  83. data/lib/new_relic/agent/transaction/tracing.rb +11 -1
  84. data/lib/new_relic/agent/transaction.rb +25 -2
  85. data/lib/new_relic/agent/transaction_error_primitive.rb +16 -0
  86. data/lib/new_relic/agent/transaction_event_primitive.rb +19 -0
  87. data/lib/new_relic/agent/utilization/gcp.rb +1 -3
  88. data/lib/new_relic/agent/vm/{mri_vm.rb → c_ruby_vm.rb} +7 -15
  89. data/lib/new_relic/agent/vm.rb +2 -2
  90. data/lib/new_relic/agent/worker_loop.rb +1 -1
  91. data/lib/new_relic/agent.rb +11 -7
  92. data/lib/new_relic/base64.rb +25 -0
  93. data/lib/new_relic/cli/command.rb +6 -4
  94. data/lib/new_relic/constants.rb +5 -0
  95. data/lib/new_relic/control/frameworks/rails.rb +17 -5
  96. data/lib/new_relic/control/instrumentation.rb +1 -1
  97. data/lib/new_relic/language_support.rb +4 -0
  98. data/lib/new_relic/local_environment.rb +22 -13
  99. data/lib/new_relic/supportability_helper.rb +1 -1
  100. data/lib/new_relic/version.rb +1 -1
  101. data/lib/tasks/config.rake +1 -1
  102. data/lib/tasks/helpers/config.html.erb +6 -6
  103. data/lib/tasks/helpers/newrelicyml.rb +1 -1
  104. data/lib/tasks/instrumentation_generator/instrumentation.thor +3 -3
  105. data/lib/tasks/tests.rake +71 -0
  106. data/newrelic.yml +55 -35
  107. data/newrelic_rpm.gemspec +5 -4
  108. data/test/agent_helper.rb +14 -2
  109. metadata +32 -7
  110. data/bin/newrelic_cmd +0 -7
@@ -2,10 +2,10 @@
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 'base64'
6
5
  require 'json'
7
6
  require 'stringio'
8
7
  require 'zlib'
8
+ require 'new_relic/base64'
9
9
 
10
10
  module NewRelic
11
11
  module Agent
@@ -45,7 +45,7 @@ module NewRelic
45
45
  data = NewRelic::Agent::EncodingNormalizer.normalize_object(data)
46
46
  end
47
47
  json = ::JSON.dump(data)
48
- Base64.encode64(Compressed::Deflate.encode(json))
48
+ NewRelic::Base64.encode64(Compressed::Deflate.encode(json))
49
49
  end
50
50
  end
51
51
  end
@@ -414,7 +414,7 @@ module NewRelic
414
414
  else
415
415
  request = Net::HTTP::Post.new(opts[:uri], headers)
416
416
  end
417
- @audit_logger.log_request_headers(opts[:uri], headers)
417
+ @audit_logger.log_request_headers(filtered_uri(opts[:uri]), headers)
418
418
  request['user-agent'] = user_agent
419
419
  request.content_type = 'application/octet-stream'
420
420
  request.body = opts[:data]
@@ -431,7 +431,7 @@ module NewRelic
431
431
  rescue *CONNECTION_ERRORS => e
432
432
  close_shared_connection
433
433
  if attempts < MAX_ATTEMPTS
434
- ::NewRelic::Agent.logger.debug("Retrying request to #{opts[:collector]}#{opts[:uri]} after #{e}")
434
+ ::NewRelic::Agent.logger.debug("Retrying request to #{opts[:collector]}#{filtered_uri(opts[:uri])} after #{e}")
435
435
  retry
436
436
  else
437
437
  raise ServerConnectionException, "Recoverable error talking to #{@collector} after #{attempts} attempts: #{e}"
@@ -444,7 +444,7 @@ module NewRelic
444
444
 
445
445
  def attempt_request(request, opts)
446
446
  conn = http_connection
447
- ::NewRelic::Agent.logger.debug("Sending request to #{opts[:collector]}#{opts[:uri]} with #{request.method}")
447
+ ::NewRelic::Agent.logger.debug("Sending request to #{opts[:collector]}#{filtered_uri(opts[:uri])} with #{request.method}")
448
448
  conn.request(request)
449
449
  end
450
450
 
@@ -689,9 +689,7 @@ module NewRelic
689
689
 
690
690
  def invoke_remote_send_request(method, payload, data, encoding)
691
691
  uri = remote_method_uri(method)
692
- full_uri = "#{@collector}#{uri}"
693
-
694
- @audit_logger.log_request(full_uri, payload, @marshaller)
692
+ @audit_logger.log_request("#{@collector}#{filtered_uri(uri)}", payload, @marshaller)
695
693
  request_send_ts = Process.clock_gettime(Process::CLOCK_MONOTONIC)
696
694
  response = send_request(:data => data,
697
695
  :uri => uri,
@@ -700,6 +698,10 @@ module NewRelic
700
698
  :endpoint => method)
701
699
  [response, request_send_ts, Process.clock_gettime(Process::CLOCK_MONOTONIC)]
702
700
  end
701
+
702
+ def filtered_uri(uri)
703
+ uri.gsub(license_key, ASTERISK * license_key.size)
704
+ end
703
705
  end
704
706
  end
705
707
  end
@@ -2,8 +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 'base64'
6
-
7
5
  module NewRelic
8
6
  module Agent
9
7
  class Obfuscator
@@ -2,7 +2,7 @@
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 'base64'
5
+ require 'new_relic/base64'
6
6
 
7
7
  module NewRelic
8
8
  module Agent
@@ -257,7 +257,7 @@ module NewRelic
257
257
  Marshal.load(data)
258
258
  rescue StandardError => e
259
259
  ::NewRelic::Agent.logger.error('Failure unmarshalling message from Resque child process', e)
260
- ::NewRelic::Agent.logger.debug(Base64.encode64(data))
260
+ ::NewRelic::Agent.logger.debug(NewRelic::Base64.encode64(data))
261
261
  nil
262
262
  end
263
263
 
@@ -8,7 +8,6 @@ module NewRelic
8
8
  class SegmentTermsRule
9
9
  PREFIX_KEY = 'prefix'.freeze
10
10
  TERMS_KEY = 'terms'.freeze
11
- SEGMENT_PLACEHOLDER = '*'.freeze
12
11
  ADJACENT_PLACEHOLDERS_REGEX = %r{((?:^|/)\*)(?:/\*)*}.freeze
13
12
  ADJACENT_PLACEHOLDERS_REPLACEMENT = '\1'.freeze
14
13
 
@@ -52,7 +51,7 @@ module NewRelic
52
51
  rest = string[@trim_range]
53
52
  leading_slash = rest.slice!(LEADING_SLASH_REGEX)
54
53
  segments = rest.split(SEGMENT_SEPARATOR, -1)
55
- segments.map! { |s| @terms.include?(s) ? s : SEGMENT_PLACEHOLDER }
54
+ segments.map! { |s| @terms.include?(s) ? s : ASTERISK }
56
55
  transformed_suffix = collapse_adjacent_placeholder_segments(segments)
57
56
 
58
57
  "#{@prefix}#{leading_slash}#{transformed_suffix}"
@@ -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)
@@ -3,7 +3,6 @@
3
3
  # frozen_string_literal: true
4
4
 
5
5
  require 'zlib'
6
- require 'base64'
7
6
  require 'digest/md5'
8
7
 
9
8
  module NewRelic
@@ -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
 
@@ -420,16 +418,17 @@ module NewRelic
420
418
  NewRelic::Agent.config[:'instrumentation.thread.tracing']
421
419
  end
422
420
 
423
- def thread_block_with_current_transaction(segment_name:, parent: nil, &block)
421
+ def thread_block_with_current_transaction(segment_name: nil, parent: nil, &block)
424
422
  parent ||= current_segment
425
423
  current_txn = ::Thread.current[:newrelic_tracer_state]&.current_transaction if ::Thread.current[:newrelic_tracer_state]&.is_execution_traced?
426
424
  proc do |*args|
427
425
  begin
428
426
  if current_txn && !current_txn.finished?
429
427
  NewRelic::Agent::Tracer.state.current_transaction = current_txn
428
+ ::Thread.current[:newrelic_thread_span_parent] = parent
430
429
  current_txn.async = true
431
- segment_name += "/Thread#{::Thread.current.object_id}/Fiber#{::Fiber.current.object_id}" if NewRelic::Agent.config[:'thread_ids_enabled']
432
- segment = NewRelic::Agent::Tracer.start_segment(name: segment_name, parent: parent)
430
+ segment_name = "#{segment_name}/Thread#{::Thread.current.object_id}/Fiber#{::Fiber.current.object_id}" if NewRelic::Agent.config[:'thread_ids_enabled']
431
+ segment = NewRelic::Agent::Tracer.start_segment(name: segment_name, parent: parent) if segment_name
433
432
  end
434
433
  NewRelic::Agent::Tracer.capture_segment_error(segment) do
435
434
  yield(*args)
@@ -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)
@@ -22,7 +22,7 @@ module NewRelic
22
22
 
23
23
  def add_segment(segment, parent = nil)
24
24
  segment.transaction = self
25
- segment.parent = parent || current_segment
25
+ segment.parent = parent || thread_starting_span || current_segment
26
26
  set_current_segment(segment)
27
27
  if @segments.length < segment_limit
28
28
  @segments << segment
@@ -39,6 +39,16 @@ module NewRelic
39
39
  segment.transaction_assigned
40
40
  end
41
41
 
42
+ def thread_starting_span
43
+ # if the previous current segment was in another thread, use the thread local parent
44
+ if ::Thread.current[:newrelic_thread_span_parent] &&
45
+ current_segment &&
46
+ current_segment.starting_segment_key != NewRelic::Agent::Tracer.current_segment_key
47
+
48
+ ::Thread.current[:newrelic_thread_span_parent]
49
+ end
50
+ end
51
+
42
52
  def segment_complete(segment)
43
53
  # if parent was in another thread, remove the current_segment entry for this thread
44
54
  if segment.parent && segment.parent.starting_segment_key != NewRelic::Agent::Tracer.current_segment_key
@@ -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
@@ -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
@@ -8,7 +8,7 @@ require 'new_relic/agent/vm/snapshot'
8
8
  module NewRelic
9
9
  module Agent
10
10
  module VM
11
- class MriVM
11
+ class CRubyVM
12
12
  def snapshot
13
13
  snap = Snapshot.new
14
14
  gather_stats(snap)
@@ -24,7 +24,7 @@ module NewRelic
24
24
 
25
25
  def gather_gc_stats(snap)
26
26
  gather_gc_runs(snap) if supports?(:gc_runs)
27
- gather_derived_stats(snap) if GC.respond_to?(:stat)
27
+ gather_derived_stats(snap)
28
28
  end
29
29
 
30
30
  def gather_gc_runs(snap)
@@ -33,19 +33,11 @@ module NewRelic
33
33
 
34
34
  def gather_derived_stats(snap)
35
35
  stat = GC.stat
36
- snap.total_allocated_object = derive_from_gc_stats(%i[total_allocated_objects total_allocated_object], stat)
37
- snap.major_gc_count = derive_from_gc_stats(:major_gc_count, stat)
38
- snap.minor_gc_count = derive_from_gc_stats(:minor_gc_count, stat)
39
- snap.heap_live = derive_from_gc_stats(%i[heap_live_slots heap_live_slot heap_live_num], stat)
40
- snap.heap_free = derive_from_gc_stats(%i[heap_free_slots heap_free_slot heap_free_num], stat)
41
- end
42
-
43
- def derive_from_gc_stats(keys, stat)
44
- Array(keys).each do |key|
45
- value = stat[key]
46
- return value if value
47
- end
48
- nil
36
+ snap.total_allocated_object = stat.fetch(:total_allocated_objects, nil)
37
+ snap.major_gc_count = stat.fetch(:major_gc_count, nil)
38
+ snap.minor_gc_count = stat.fetch(:minor_gc_count, nil)
39
+ snap.heap_live = stat.fetch(:heap_live_slots, nil)
40
+ snap.heap_free = stat.fetch(:heap_free_slots, nil)
49
41
  end
50
42
 
51
43
  def gather_gc_time(snap)
@@ -3,7 +3,7 @@
3
3
  # frozen_string_literal: true
4
4
 
5
5
  require 'new_relic/language_support'
6
- require 'new_relic/agent/vm/mri_vm'
6
+ require 'new_relic/agent/vm/c_ruby_vm'
7
7
  require 'new_relic/agent/vm/jruby_vm'
8
8
 
9
9
  module NewRelic
@@ -21,7 +21,7 @@ module NewRelic
21
21
  if NewRelic::LanguageSupport.jruby?
22
22
  JRubyVM.new
23
23
  else
24
- MriVM.new
24
+ CRubyVM.new
25
25
  end
26
26
  end
27
27
  end
@@ -88,7 +88,7 @@ module NewRelic
88
88
  raise
89
89
  rescue => e
90
90
  # Don't blow out the stack for anything that hasn't already propagated
91
- ::NewRelic::Agent.logger.error('Error running task in Agent Worker Loop:', e)
91
+ ::NewRelic::Agent.logger.error('Error running task in agent worker loop:', e)
92
92
  end
93
93
  end
94
94
  end
@@ -252,7 +252,7 @@ module NewRelic
252
252
 
253
253
  # @!group Recording custom errors
254
254
 
255
- # Set a filter to be applied to errors that the Ruby Agent will
255
+ # Set a filter to be applied to errors that the Ruby agent will
256
256
  # track. The block should evaluate to the exception to track
257
257
  # (which could be different from the original exception) or nil to
258
258
  # ignore this exception.
@@ -391,13 +391,13 @@ module NewRelic
391
391
 
392
392
  # @!group Manual agent configuration and startup/shutdown
393
393
 
394
- # Call this to manually start the Agent in situations where the Agent does
394
+ # Call this to manually start the agent in situations where the agent does
395
395
  # not auto-start.
396
396
  #
397
- # When the app environment loads, so does the Agent. However, the
398
- # Agent will only connect to the service if a web front-end is found. If
397
+ # When the app environment loads, so does the agent. However, the
398
+ # agent will only connect to the service if a web front-end is found. If
399
399
  # you want to selectively monitor ruby processes that don't use
400
- # web plugins, then call this method in your code and the Agent
400
+ # web plugins, then call this method in your code and the agent
401
401
  # will fire up and start reporting to the service.
402
402
  #
403
403
  # Options are passed in as overrides for values in the
@@ -633,7 +633,9 @@ module NewRelic
633
633
  def add_new_segment_attributes(params, segment)
634
634
  # Make sure not to override existing segment-level custom attributes
635
635
  segment_custom_keys = segment.attributes.custom_attributes.keys.map(&:to_sym)
636
- segment.add_custom_attributes(params.reject { |k, _v| segment_custom_keys.include?(k.to_sym) })
636
+ segment.add_custom_attributes(params.reject do |k, _v|
637
+ segment_custom_keys.include?(k.to_sym) if k.respond_to?(:to_sym) # param keys can be integers
638
+ end)
637
639
  end
638
640
 
639
641
  # Add custom attributes to the span event for the current span. Attributes will be visible on spans in the
@@ -661,7 +663,9 @@ module NewRelic
661
663
  end
662
664
  end
663
665
 
664
- # Add custom attributes to log events for the current agent instance.
666
+ # Add global custom attributes to log events for the current agent instance. As these attributes are global to the
667
+ # agent instance, they will be attached to all log events generated by the agent, and this methods usage isn't
668
+ # suitable for setting dynamic values.
665
669
  #
666
670
  # @param [Hash] params A Hash of attributes to attach to log
667
671
  # events. The agent accepts up to 240 custom