newrelic_rpm 6.8.0.360 → 6.9.0.363

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +16 -16
  3. data/CHANGELOG.md +64 -0
  4. data/lib/new_relic/agent.rb +6 -5
  5. data/lib/new_relic/agent/agent.rb +43 -36
  6. data/lib/new_relic/agent/attributes.rb +2 -4
  7. data/lib/new_relic/agent/configuration/default_source.rb +23 -22
  8. data/lib/new_relic/agent/configuration/server_source.rb +1 -1
  9. data/lib/new_relic/agent/configuration/yaml_source.rb +1 -1
  10. data/lib/new_relic/agent/database.rb +1 -2
  11. data/lib/new_relic/agent/distributed_tracing.rb +155 -6
  12. data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +0 -0
  13. data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +60 -45
  14. data/lib/new_relic/agent/distributed_tracing/distributed_trace_intrinsics.rb +80 -0
  15. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  16. data/lib/new_relic/agent/{distributed_trace_payload.rb → distributed_tracing/distributed_trace_payload.rb} +19 -28
  17. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
  18. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
  19. data/lib/new_relic/agent/{trace_context_payload.rb → distributed_tracing/trace_context_payload.rb} +3 -11
  20. data/lib/new_relic/agent/error_collector.rb +3 -5
  21. data/lib/new_relic/agent/error_event_aggregator.rb +3 -1
  22. data/lib/new_relic/agent/external.rb +7 -7
  23. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +1 -2
  24. data/lib/new_relic/agent/instrumentation/bunny.rb +1 -1
  25. data/lib/new_relic/agent/instrumentation/curb.rb +1 -1
  26. data/lib/new_relic/agent/instrumentation/excon.rb +1 -1
  27. data/lib/new_relic/agent/instrumentation/grape.rb +5 -10
  28. data/lib/new_relic/agent/instrumentation/http.rb +1 -1
  29. data/lib/new_relic/agent/instrumentation/httpclient.rb +1 -1
  30. data/lib/new_relic/agent/instrumentation/net.rb +1 -1
  31. data/lib/new_relic/agent/instrumentation/resque.rb +3 -0
  32. data/lib/new_relic/agent/instrumentation/typhoeus.rb +1 -1
  33. data/lib/new_relic/agent/logging.rb +13 -3
  34. data/lib/new_relic/agent/messaging.rb +5 -73
  35. data/lib/new_relic/agent/method_tracer.rb +3 -2
  36. data/lib/new_relic/agent/method_tracer_helpers.rb +1 -1
  37. data/lib/new_relic/agent/monitors.rb +27 -0
  38. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
  39. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
  40. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +1 -1
  41. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +2 -4
  42. data/lib/new_relic/agent/span_event_primitive.rb +25 -29
  43. data/lib/new_relic/agent/sql_sampler.rb +2 -2
  44. data/lib/new_relic/agent/supported_versions.rb +2 -2
  45. data/lib/new_relic/agent/tracer.rb +3 -3
  46. data/lib/new_relic/agent/transaction.rb +21 -28
  47. data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
  48. data/lib/new_relic/agent/transaction/distributed_tracing.rb +61 -69
  49. data/lib/new_relic/agent/transaction/external_request_segment.rb +8 -44
  50. data/lib/new_relic/agent/transaction/message_broker_segment.rb +3 -11
  51. data/lib/new_relic/agent/transaction/trace.rb +2 -4
  52. data/lib/new_relic/agent/transaction/trace_context.rb +88 -79
  53. data/lib/new_relic/agent/transaction/trace_node.rb +2 -5
  54. data/lib/new_relic/agent/transaction_error_primitive.rb +2 -2
  55. data/lib/new_relic/agent/transaction_event_primitive.rb +26 -29
  56. data/lib/new_relic/coerce.rb +5 -3
  57. data/lib/new_relic/constants.rb +34 -0
  58. data/lib/new_relic/noticed_error.rb +2 -4
  59. data/lib/new_relic/rack/browser_monitoring.rb +4 -0
  60. data/lib/new_relic/supportability_helper.rb +14 -0
  61. data/lib/new_relic/version.rb +1 -1
  62. data/lib/tasks/tests.rake +6 -1
  63. data/newrelic_rpm.gemspec +4 -2
  64. data/test/agent_helper.rb +21 -1
  65. metadata +49 -19
  66. data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
  67. data/lib/new_relic/agent/distributed_trace_intrinsics.rb +0 -90
  68. data/lib/new_relic/agent/distributed_trace_metrics.rb +0 -74
  69. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -30
  70. data/lib/new_relic/agent/distributed_trace_transport_type.rb +0 -43
  71. data/lib/new_relic/agent/trace_context.rb +0 -244
  72. data/lib/new_relic/agent/trace_context_request_monitor.rb +0 -42
@@ -74,7 +74,7 @@ module NewRelic
74
74
  end
75
75
 
76
76
  def self.ignore_error_filter
77
- @ignore_filter
77
+ defined?(@ignore_filter) ? @ignore_filter : nil
78
78
  end
79
79
 
80
80
  # errors is an array of Exception Class Names
@@ -227,13 +227,11 @@ module NewRelic
227
227
  truncated_trace
228
228
  end
229
229
 
230
- EMPTY_STRING = ''.freeze
231
-
232
230
  def create_noticed_error(exception, options)
233
- error_metric = options.delete(:metric) || EMPTY_STRING
231
+ error_metric = options.delete(:metric) || NewRelic::EMPTY_STR
234
232
 
235
233
  noticed_error = NewRelic::NoticedError.new(error_metric, exception)
236
- noticed_error.request_uri = options.delete(:uri) || EMPTY_STRING
234
+ noticed_error.request_uri = options.delete(:uri) || NewRelic::EMPTY_STR
237
235
  noticed_error.request_port = options.delete(:port)
238
236
  noticed_error.attributes = options.delete(:attributes)
239
237
 
@@ -2,6 +2,7 @@
2
2
  # encoding: utf-8
3
3
  # This file is distributed under New Relic's license terms.
4
4
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
5
+ # frozen_string_literal: true
5
6
 
6
7
  require 'new_relic/agent/event_aggregator'
7
8
  require 'new_relic/agent/transaction_error_primitive'
@@ -10,6 +11,7 @@ require 'new_relic/agent/priority_sampled_buffer'
10
11
  module NewRelic
11
12
  module Agent
12
13
  class ErrorEventAggregator < EventAggregator
14
+ include NewRelic::Coerce
13
15
 
14
16
  named :ErrorEventAggregator
15
17
  capacity_key :'error_collector.max_event_samples_stored'
@@ -20,7 +22,7 @@ module NewRelic
20
22
  def record noticed_error, transaction_payload = nil
21
23
  return unless enabled?
22
24
 
23
- priority = (transaction_payload && transaction_payload[:priority]) || rand.round(6)
25
+ priority = float!((transaction_payload && transaction_payload[:priority]) || rand)
24
26
 
25
27
  @lock.synchronize do
26
28
  @buffer.append(priority: priority) do
@@ -3,8 +3,8 @@
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
5
  require 'new_relic/agent/transaction/tracing'
6
- require 'new_relic/agent/cross_app_tracing'
7
- require 'new_relic/agent/cross_app_payload'
6
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
7
+ require 'new_relic/agent/distributed_tracing/cross_app_payload'
8
8
 
9
9
  module NewRelic
10
10
  module Agent
@@ -78,7 +78,7 @@ module NewRelic
78
78
  #
79
79
  if txn_info = rmd[NON_HTTP_CAT_TXN_HEADER]
80
80
  payload = CrossAppPayload.new(id, transaction, txn_info)
81
- transaction.cross_app_payload = payload
81
+ transaction.distributed_tracer.cross_app_payload = payload
82
82
 
83
83
  CrossAppTracing.assign_intrinsic_transaction_attributes state
84
84
  end
@@ -112,16 +112,16 @@ module NewRelic
112
112
  NewRelic::Agent.record_api_supportability_metric(:get_response_metadata)
113
113
  return unless CrossAppTracing.cross_app_enabled?
114
114
 
115
- return unless (transaction = Tracer.current_transaction)
116
- return unless (cross_app_payload = transaction.cross_app_payload)
115
+ return unless (txn = Tracer.current_transaction)
116
+ return unless (payload = txn.distributed_tracer.cross_app_payload)
117
117
 
118
118
  # must freeze the name since we're responding with it
119
119
  #
120
- transaction.freeze_name_and_execute_if_not_ignored do
120
+ txn.freeze_name_and_execute_if_not_ignored do
121
121
  # build response payload
122
122
  #
123
123
  rmd = {
124
- NewRelicAppData: cross_app_payload.as_json_array(NON_HTTP_CAT_CONTENT_LENGTH)
124
+ NewRelicAppData: payload.as_json_array(NON_HTTP_CAT_CONTENT_LENGTH)
125
125
  }
126
126
 
127
127
  # obfuscate the generated response metadata JSON
@@ -47,10 +47,9 @@ module NewRelic
47
47
  end
48
48
 
49
49
  DOT_ACTION_CABLE = '.action_cable'.freeze
50
- EMPTY_STRING = ''.freeze
51
50
 
52
51
  def action_name(name)
53
- name.gsub DOT_ACTION_CABLE, EMPTY_STRING
52
+ name.gsub DOT_ACTION_CABLE, NewRelic::EMPTY_STR
54
53
  end
55
54
 
56
55
  def notice_error(payload)
@@ -11,7 +11,7 @@ DependencyDetection.defer do
11
11
 
12
12
  executes do
13
13
  ::NewRelic::Agent.logger.info 'Installing Bunny instrumentation'
14
- require 'new_relic/agent/cross_app_tracing'
14
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
15
15
  require 'new_relic/agent/messaging'
16
16
  require 'new_relic/agent/transaction/message_broker_segment'
17
17
  end
@@ -14,7 +14,7 @@ DependencyDetection.defer do
14
14
 
15
15
  executes do
16
16
  ::NewRelic::Agent.logger.info 'Installing Curb instrumentation'
17
- require 'new_relic/agent/cross_app_tracing'
17
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
18
18
  require 'new_relic/agent/http_clients/curb_wrappers'
19
19
  end
20
20
 
@@ -42,7 +42,7 @@ DependencyDetection.defer do
42
42
  end
43
43
 
44
44
  def install_excon_instrumentation(excon_version)
45
- require 'new_relic/agent/cross_app_tracing'
45
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
46
46
  require 'new_relic/agent/http_clients/excon_wrappers'
47
47
 
48
48
  if excon_version >= EXCON_MIDDLEWARE_MIN_VERSION
@@ -14,7 +14,6 @@ module NewRelic
14
14
  API_VERSION = 'api.version'.freeze
15
15
  FORMAT_REGEX = /\(\/?\.[\:\w]*\)/.freeze # either :format (< 0.12.0) or .ext (>= 0.12.0)
16
16
  VERSION_REGEX = /:version(\/|$)/.freeze
17
- EMPTY_STRING = ''.freeze
18
17
  MIN_VERSION = Gem::Version.new("0.2.0")
19
18
  PIPE_STRING = '|'.freeze
20
19
 
@@ -33,7 +32,7 @@ module NewRelic
33
32
  end
34
33
 
35
34
  def name_for_transaction(route, class_name, version)
36
- action_name = route.path.sub(FORMAT_REGEX, EMPTY_STRING)
35
+ action_name = route.path.sub(FORMAT_REGEX, NewRelic::EMPTY_STR)
37
36
  method_name = route.request_method
38
37
  version ||= route.version
39
38
 
@@ -42,7 +41,7 @@ module NewRelic
42
41
  version = version.join(PIPE_STRING) if Array === version
43
42
 
44
43
  if version
45
- action_name = action_name.sub(VERSION_REGEX, EMPTY_STRING)
44
+ action_name = action_name.sub(VERSION_REGEX, NewRelic::EMPTY_STR)
46
45
  "#{class_name}-#{version}#{action_name} (#{method_name})"
47
46
  else
48
47
  "#{class_name}#{action_name} (#{method_name})"
@@ -50,12 +49,12 @@ module NewRelic
50
49
  end
51
50
 
52
51
  def name_for_transaction_deprecated(route, class_name, version)
53
- action_name = route.route_path.sub(FORMAT_REGEX, EMPTY_STRING)
52
+ action_name = route.route_path.sub(FORMAT_REGEX, NewRelic::EMPTY_STR)
54
53
  method_name = route.route_method
55
54
  version ||= route.route_version
56
55
 
57
56
  if version
58
- action_name = action_name.sub(VERSION_REGEX, EMPTY_STRING)
57
+ action_name = action_name.sub(VERSION_REGEX, NewRelic::EMPTY_STR)
59
58
  "#{class_name}-#{version}#{action_name} (#{method_name})"
60
59
  else
61
60
  "#{class_name}#{action_name} (#{method_name})"
@@ -118,11 +117,7 @@ DependencyDetection.defer do
118
117
 
119
118
  # Since 1.2.0, the class `Grape::API` no longer refers to an API instance, rather, what used to be `Grape::API` is `Grape::API::Instance`
120
119
  # https://github.com/ruby-grape/grape/blob/c20a73ac1e3f3ba1082005ed61bf69452373ba87/UPGRADING.md#upgrading-to--120
121
- grape_api_class = if defined?(Grape::API::Instance)
122
- ::Grape::API::Instance
123
- else
124
- ::Grape::API
125
- end
120
+ grape_api_class = defined?(Grape::API::Instance) ? ::Grape::API::Instance : ::Grape::API
126
121
 
127
122
  grape_api_class.class_eval do
128
123
  def call_with_new_relic(env)
@@ -11,7 +11,7 @@ DependencyDetection.defer do
11
11
 
12
12
  executes do
13
13
  ::NewRelic::Agent.logger.info 'Installing http.rb instrumentation'
14
- require 'new_relic/agent/cross_app_tracing'
14
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
15
15
  require 'new_relic/agent/http_clients/http_rb_wrappers'
16
16
  end
17
17
 
@@ -20,7 +20,7 @@ DependencyDetection.defer do
20
20
 
21
21
  executes do
22
22
  ::NewRelic::Agent.logger.info 'Installing HTTPClient instrumentation'
23
- require 'new_relic/agent/cross_app_tracing'
23
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
24
24
  require 'new_relic/agent/http_clients/httpclient_wrappers'
25
25
  end
26
26
 
@@ -11,7 +11,7 @@ DependencyDetection.defer do
11
11
 
12
12
  executes do
13
13
  ::NewRelic::Agent.logger.info 'Installing Net instrumentation'
14
- require 'new_relic/agent/cross_app_tracing'
14
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
15
15
  require 'new_relic/agent/http_clients/net_http_wrappers'
16
16
  end
17
17
 
@@ -43,6 +43,9 @@ DependencyDetection.defer do
43
43
  perform_without_instrumentation
44
44
  end
45
45
  ensure
46
+ # Stopping the event loop before flushing the pipe.
47
+ # The goal is to avoid conflict during write.
48
+ NewRelic::Agent.agent.stop_event_loop
46
49
  NewRelic::Agent.agent.flush_pipe_data
47
50
  end
48
51
  end
@@ -15,7 +15,7 @@ DependencyDetection.defer do
15
15
 
16
16
  executes do
17
17
  ::NewRelic::Agent.logger.info 'Installing Typhoeus instrumentation'
18
- require 'new_relic/agent/cross_app_tracing'
18
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
19
19
  require 'new_relic/agent/http_clients/typhoeus_wrappers'
20
20
  end
21
21
 
@@ -119,9 +119,19 @@ module NewRelic
119
119
 
120
120
  alias :write :info
121
121
 
122
- def initialize *args
123
- super
124
- self.formatter = DecoratingFormatter.new
122
+ # Positional and Keyword arguments are separated beginning with Ruby 2.7
123
+ # Signature of ::Logger constructor changes in Ruby 2.4 to have both positional and keyword args
124
+ # We pivot on Ruby 2.7 for widest supportability with least amount of hassle.
125
+ if RUBY_VERSION < "2.7.0"
126
+ def initialize(*args)
127
+ super(*args)
128
+ self.formatter = DecoratingFormatter.new
129
+ end
130
+ else
131
+ def initialize(*args, **kwargs)
132
+ super(*args, **kwargs)
133
+ self.formatter = DecoratingFormatter.new
134
+ end
125
135
  end
126
136
  end
127
137
  end
@@ -1,6 +1,8 @@
1
1
  # encoding: utf-8
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+ # frozen_string_literal: true
5
+
4
6
  require 'new_relic/agent/transaction'
5
7
 
6
8
  module NewRelic
@@ -13,12 +15,12 @@ module NewRelic
13
15
  module Messaging
14
16
  extend self
15
17
 
18
+ RABBITMQ_TRANSPORT_TYPE = "RabbitMQ"
19
+
16
20
  ATTR_DESTINATION = AttributeFilter::DST_TRANSACTION_EVENTS |
17
21
  AttributeFilter::DST_TRANSACTION_TRACER |
18
22
  AttributeFilter::DST_ERROR_COLLECTOR
19
23
 
20
- EMPTY_STRING = ''.freeze
21
-
22
24
  # Start a MessageBroker segment configured to trace a messaging action.
23
25
  # Finishing this segment will handle timing and recording of the proper
24
26
  # metrics for New Relic's messaging features..
@@ -133,7 +135,7 @@ module NewRelic
133
135
  txn = Tracer.start_transaction name: txn_name, category: :message
134
136
 
135
137
  if headers
136
- consume_message_headers headers, txn, state
138
+ txn.distributed_tracer.consume_message_headers headers, state, RABBITMQ_TRANSPORT_TYPE
137
139
  CrossAppTracing.reject_messaging_cat_headers(headers).each do |k, v|
138
140
  txn.add_agent_attribute :"message.headers.#{k}", v, AttributeFilter::DST_NONE unless v.nil?
139
141
  end
@@ -365,76 +367,6 @@ module NewRelic
365
367
  transaction_name
366
368
  end
367
369
 
368
- RABBITMQ_TRANSPORT_TYPE = "RabbitMQ".freeze
369
-
370
- def consume_message_headers headers, transaction, state
371
- consume_distributed_tracing_headers headers, transaction
372
- consume_cross_app_tracing_headers headers, state
373
-
374
- assign_synthetics_header headers[CrossAppTracing::NR_MESSAGE_BROKER_SYNTHETICS_HEADER], transaction
375
- rescue => e
376
- NewRelic::Agent.logger.error "Error in consume_message_headers", e
377
- end
378
-
379
- def decode_txn_info headers, transaction_state
380
- encoded_id = headers[CrossAppTracing::NR_MESSAGE_BROKER_ID_HEADER]
381
-
382
- decoded_id = if encoded_id.nil?
383
- EMPTY_STRING
384
- else
385
- CrossAppTracing.obfuscator.deobfuscate(encoded_id)
386
- end
387
-
388
- if CrossAppTracing.trusted_valid_cross_app_id?(decoded_id) && transaction_state.current_transaction
389
- txn_header = headers[CrossAppTracing::NR_MESSAGE_BROKER_TXN_HEADER]
390
- txn = transaction_state.current_transaction
391
- txn_info = ::JSON.load(CrossAppTracing.obfuscator.deobfuscate(txn_header))
392
- payload = CrossAppPayload.new(decoded_id, txn, txn_info)
393
-
394
- txn.cross_app_payload = payload
395
- end
396
- rescue => e
397
- NewRelic::Agent.logger.debug("Failure deserializing encoded header in #{self.class}, #{e.class}, #{e.message}")
398
- nil
399
- end
400
-
401
- CANDIDATE_HEADERS = ['newrelic'.freeze, 'NEWRELIC'.freeze, 'Newrelic'.freeze]
402
-
403
- def consume_distributed_tracing_headers headers, transaction
404
- if Agent.config[:'distributed_tracing.enabled']
405
- return unless newrelic_trace_key = CANDIDATE_HEADERS.detect do |key|
406
- headers.has_key?(key)
407
- end
408
-
409
- return unless payload = headers[newrelic_trace_key]
410
-
411
- if transaction.accept_distributed_trace_payload payload
412
- transaction.distributed_trace_payload.caller_transport_type = RABBITMQ_TRANSPORT_TYPE
413
- end
414
- end
415
- end
416
-
417
- def consume_cross_app_tracing_headers headers, state
418
- if CrossAppTracing.cross_app_enabled? && CrossAppTracing.message_has_crossapp_request_header?(headers)
419
- decode_txn_info headers, state
420
- CrossAppTracing.assign_intrinsic_transaction_attributes state
421
- end
422
- end
423
-
424
- def assign_synthetics_header synthetics_header, transaction
425
- if synthetics_header and
426
- incoming_payload = ::JSON.load(CrossAppTracing.obfuscator.deobfuscate(synthetics_header)) and
427
- SyntheticsMonitor.is_valid_payload?(incoming_payload) and
428
- SyntheticsMonitor.is_supported_version?(incoming_payload) and
429
- SyntheticsMonitor.is_trusted?(incoming_payload)
430
-
431
- transaction.raw_synthetics_header = synthetics_header
432
- transaction.synthetics_payload = incoming_payload
433
- end
434
- rescue => e
435
- NewRelic::Agent.logger.error "Error in assign_synthetics_header", e
436
- end
437
-
438
370
  end
439
371
  end
440
372
  end
@@ -47,6 +47,7 @@ module NewRelic
47
47
  #
48
48
 
49
49
  module MethodTracer
50
+
50
51
  def self.included clazz
51
52
  clazz.extend ClassMethods
52
53
  end
@@ -67,7 +68,7 @@ module NewRelic
67
68
  #
68
69
  # @api public
69
70
  #
70
- def trace_execution_scoped(metric_names, options={}) #THREAD_LOCAL_ACCESS
71
+ def trace_execution_scoped(metric_names, options=NewRelic::EMPTY_HASH) #THREAD_LOCAL_ACCESS
71
72
  NewRelic::Agent.record_api_supportability_metric :trace_execution_scoped
72
73
  NewRelic::Agent::MethodTracerHelpers.trace_execution_scoped(metric_names, options) do
73
74
  # Using an implicit block avoids object allocation for a &block param
@@ -83,7 +84,7 @@ module NewRelic
83
84
  #
84
85
  # @api public
85
86
  #
86
- def trace_execution_unscoped(metric_names, options={}) #THREAD_LOCAL_ACCESS
87
+ def trace_execution_unscoped(metric_names, options=NewRelic::EMPTY_HASH) #THREAD_LOCAL_ACCESS
87
88
  NewRelic::Agent.record_api_supportability_metric :trace_execution_unscoped
88
89
  return yield unless NewRelic::Agent.tl_is_execution_traced?
89
90
  t0 = Time.now
@@ -9,7 +9,7 @@ module NewRelic
9
9
 
10
10
  extend self
11
11
 
12
- def trace_execution_scoped(metric_names, options={}) #THREAD_LOCAL_ACCESS
12
+ def trace_execution_scoped(metric_names, options=NewRelic::EMPTY_HASH) #THREAD_LOCAL_ACCESS
13
13
  state = NewRelic::Agent::Tracer.state
14
14
  return yield unless state.is_execution_traced?
15
15
 
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require_relative 'monitors/inbound_request_monitor'
6
+
7
+ require_relative 'monitors/synthetics_monitor'
8
+
9
+ require_relative 'monitors/cross_app_monitor'
10
+ require_relative 'monitors/distributed_tracing_monitor'
11
+
12
+ module NewRelic
13
+ module Agent
14
+ class Monitors
15
+ attr_reader :cross_app_monitor
16
+ attr_reader :synthetics_monitor
17
+ attr_reader :distributed_tracing_monitor
18
+
19
+ def initialize events
20
+ @synthetics_monitor = NewRelic::Agent::SyntheticsMonitor.new events
21
+ @cross_app_monitor = NewRelic::Agent::DistributedTracing::CrossAppMonitor.new events
22
+ @distributed_tracing_monitor = NewRelic::Agent::DistributedTracing::Monitor.new events
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,110 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'digest'
6
+ require 'json'
7
+
8
+ require 'new_relic/agent/tracer'
9
+ require 'new_relic/agent/threading/agent_thread'
10
+
11
+ module NewRelic
12
+ module Agent
13
+ module DistributedTracing
14
+ class CrossAppMonitor < InboundRequestMonitor
15
+
16
+ NEWRELIC_ID_HEADER = 'X-NewRelic-ID'.freeze
17
+ NEWRELIC_TXN_HEADER = 'X-NewRelic-Transaction'.freeze
18
+ NEWRELIC_APPDATA_HEADER = 'X-NewRelic-App-Data'.freeze
19
+
20
+ NEWRELIC_ID_HEADER_KEY = 'HTTP_X_NEWRELIC_ID'.freeze
21
+ NEWRELIC_TXN_HEADER_KEY = 'HTTP_X_NEWRELIC_TRANSACTION'.freeze
22
+ CONTENT_LENGTH_HEADER_KEY = 'HTTP_CONTENT_LENGTH'.freeze
23
+
24
+ def on_finished_configuring(events)
25
+ register_event_listeners(events)
26
+ end
27
+
28
+ def path_hash(txn_name, seed)
29
+ rotated = ((seed << 1) | (seed >> 31)) & 0xffffffff
30
+ app_name = NewRelic::Agent.config[:app_name].first
31
+ identifier = "#{app_name};#{txn_name}"
32
+ sprintf("%08x", rotated ^ hash_transaction_name(identifier))
33
+ end
34
+
35
+ private
36
+
37
+ # Expected sequence of events:
38
+ # :before_call will save our cross application request id to the thread
39
+ # :after_call will write our response headers/metrics and clean up the thread
40
+ def register_event_listeners(events)
41
+ NewRelic::Agent.logger.
42
+ debug("Wiring up Cross Application Tracing to events after finished configuring")
43
+
44
+ events.subscribe(:before_call) do |env| #THREAD_LOCAL_ACCESS
45
+ if id = decoded_id(env) and should_process_request?(id)
46
+ state = NewRelic::Agent::Tracer.state
47
+
48
+ if (txn = state.current_transaction)
49
+ transaction_info = referring_transaction_info(state, env)
50
+
51
+ payload = CrossAppPayload.new(id, txn, transaction_info)
52
+ txn.distributed_tracer.cross_app_payload = payload
53
+ end
54
+
55
+ CrossAppTracing.assign_intrinsic_transaction_attributes state
56
+ end
57
+ end
58
+
59
+ events.subscribe(:after_call) do |env, (_status_code, headers, _body)| #THREAD_LOCAL_ACCESS
60
+ state = NewRelic::Agent::Tracer.state
61
+
62
+ insert_response_header(state, env, headers)
63
+ end
64
+ end
65
+
66
+ def referring_transaction_info(state, request_headers)
67
+ txn_header = request_headers[NEWRELIC_TXN_HEADER_KEY] or return
68
+ deserialize_header(txn_header, NEWRELIC_TXN_HEADER)
69
+ end
70
+
71
+ def insert_response_header(state, request_headers, response_headers)
72
+ txn = state.current_transaction
73
+ unless txn.nil? || txn.distributed_tracer.cross_app_payload.nil?
74
+ txn.freeze_name_and_execute_if_not_ignored do
75
+ content_length = content_length_from_request(request_headers)
76
+ set_response_headers(txn, response_headers, content_length)
77
+ end
78
+ end
79
+ end
80
+
81
+ def should_process_request? id
82
+ CrossAppTracing.cross_app_enabled? && CrossAppTracing.trusts?(id)
83
+ end
84
+
85
+ def set_response_headers(transaction, response_headers, content_length)
86
+ payload = obfuscator.obfuscate(
87
+ ::JSON.dump(
88
+ transaction.distributed_tracer.cross_app_payload.as_json_array(content_length)))
89
+
90
+ response_headers[NEWRELIC_APPDATA_HEADER] = payload
91
+ end
92
+
93
+ def decoded_id(request)
94
+ encoded_id = request[NEWRELIC_ID_HEADER_KEY]
95
+ return "" if encoded_id.nil? || encoded_id.empty?
96
+
97
+ obfuscator.deobfuscate(encoded_id)
98
+ end
99
+
100
+ def content_length_from_request(request)
101
+ request[CONTENT_LENGTH_HEADER_KEY] || -1
102
+ end
103
+
104
+ def hash_transaction_name(identifier)
105
+ Digest::MD5.digest(identifier).unpack("@12N").first & 0xffffffff
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end