newrelic_rpm 6.6.0.358 → 6.11.0.365

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +81 -5
  4. data/CHANGELOG.md +262 -0
  5. data/Gemfile +6 -2
  6. data/Guardfile +18 -1
  7. data/LICENSE +1 -1
  8. data/Rakefile +2 -0
  9. data/lib/new_relic/agent.rb +89 -7
  10. data/lib/new_relic/agent/agent.rb +115 -58
  11. data/lib/new_relic/agent/agent_logger.rb +4 -0
  12. data/lib/new_relic/agent/attribute_filter.rb +7 -7
  13. data/lib/new_relic/agent/attributes.rb +150 -0
  14. data/lib/new_relic/agent/autostart.rb +19 -14
  15. data/lib/new_relic/agent/configuration/default_source.rb +154 -9
  16. data/lib/new_relic/agent/configuration/event_harvest_config.rb +11 -5
  17. data/lib/new_relic/agent/configuration/manager.rb +0 -8
  18. data/lib/new_relic/agent/configuration/server_source.rb +3 -2
  19. data/lib/new_relic/agent/configuration/yaml_source.rb +11 -6
  20. data/lib/new_relic/agent/connect/request_builder.rb +5 -13
  21. data/lib/new_relic/agent/database.rb +1 -2
  22. data/lib/new_relic/agent/database/obfuscation_helpers.rb +1 -1
  23. data/lib/new_relic/agent/datastores/mongo.rb +1 -1
  24. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +2 -2
  25. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +8 -8
  26. data/lib/new_relic/agent/distributed_tracing.rb +155 -6
  27. data/lib/new_relic/agent/{cross_app_payload.rb → distributed_tracing/cross_app_payload.rb} +2 -1
  28. data/lib/new_relic/agent/{cross_app_tracing.rb → distributed_tracing/cross_app_tracing.rb} +60 -45
  29. data/lib/new_relic/agent/distributed_tracing/distributed_trace_intrinsics.rb +80 -0
  30. data/lib/new_relic/agent/distributed_tracing/distributed_trace_metrics.rb +75 -0
  31. data/lib/new_relic/agent/{distributed_trace_payload.rb → distributed_tracing/distributed_trace_payload.rb} +24 -101
  32. data/lib/new_relic/agent/distributed_tracing/distributed_trace_transport_type.rb +39 -0
  33. data/lib/new_relic/agent/distributed_tracing/trace_context.rb +246 -0
  34. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +126 -0
  35. data/lib/new_relic/agent/error_collector.rb +33 -16
  36. data/lib/new_relic/agent/error_event_aggregator.rb +7 -5
  37. data/lib/new_relic/agent/external.rb +7 -7
  38. data/lib/new_relic/agent/guid_generator.rb +28 -0
  39. data/lib/new_relic/agent/hostname.rb +7 -1
  40. data/lib/new_relic/agent/http_clients/abstract.rb +82 -0
  41. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +24 -19
  42. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +28 -13
  43. data/lib/new_relic/agent/http_clients/http_rb_wrappers.rb +17 -21
  44. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +10 -11
  45. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +16 -4
  46. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +4 -6
  47. data/lib/new_relic/agent/http_clients/uri_util.rb +3 -2
  48. data/lib/new_relic/agent/instrumentation/action_cable_subscriber.rb +5 -7
  49. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +4 -0
  50. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +11 -2
  51. data/lib/new_relic/agent/instrumentation/active_record.rb +4 -2
  52. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +7 -2
  53. data/lib/new_relic/agent/instrumentation/active_storage_subscriber.rb +8 -4
  54. data/lib/new_relic/agent/instrumentation/bunny.rb +45 -28
  55. data/lib/new_relic/agent/instrumentation/curb.rb +59 -18
  56. data/lib/new_relic/agent/instrumentation/data_mapper.rb +3 -1
  57. data/lib/new_relic/agent/instrumentation/excon.rb +1 -1
  58. data/lib/new_relic/agent/instrumentation/excon/connection.rb +6 -3
  59. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +2 -1
  60. data/lib/new_relic/agent/instrumentation/grape.rb +5 -10
  61. data/lib/new_relic/agent/instrumentation/http.rb +6 -3
  62. data/lib/new_relic/agent/instrumentation/httpclient.rb +5 -3
  63. data/lib/new_relic/agent/instrumentation/memcache.rb +3 -1
  64. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +6 -2
  65. data/lib/new_relic/agent/instrumentation/mongo.rb +9 -3
  66. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +13 -0
  67. data/lib/new_relic/agent/instrumentation/net.rb +6 -3
  68. data/lib/new_relic/agent/instrumentation/notifications_subscriber.rb +25 -1
  69. data/lib/new_relic/agent/instrumentation/redis.rb +9 -3
  70. data/lib/new_relic/agent/instrumentation/resque.rb +3 -0
  71. data/lib/new_relic/agent/instrumentation/sidekiq.rb +47 -23
  72. data/lib/new_relic/agent/instrumentation/typhoeus.rb +23 -6
  73. data/lib/new_relic/agent/logging.rb +139 -0
  74. data/lib/new_relic/agent/messaging.rb +5 -73
  75. data/lib/new_relic/agent/method_tracer.rb +18 -6
  76. data/lib/new_relic/agent/method_tracer_helpers.rb +2 -2
  77. data/lib/new_relic/agent/monitors.rb +27 -0
  78. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +110 -0
  79. data/lib/new_relic/agent/monitors/distributed_tracing_monitor.rb +27 -0
  80. data/lib/new_relic/agent/{inbound_request_monitor.rb → monitors/inbound_request_monitor.rb} +1 -1
  81. data/lib/new_relic/agent/{synthetics_monitor.rb → monitors/synthetics_monitor.rb} +2 -4
  82. data/lib/new_relic/agent/new_relic_service.rb +7 -6
  83. data/lib/new_relic/agent/noticible_error.rb +22 -0
  84. data/lib/new_relic/agent/span_event_aggregator.rb +1 -0
  85. data/lib/new_relic/agent/span_event_primitive.rb +86 -53
  86. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  87. data/lib/new_relic/agent/supported_versions.rb +2 -2
  88. data/lib/new_relic/agent/system_info.rb +12 -3
  89. data/lib/new_relic/agent/tracer.rb +65 -18
  90. data/lib/new_relic/agent/transaction.rb +84 -79
  91. data/lib/new_relic/agent/transaction/abstract_segment.rb +28 -2
  92. data/lib/new_relic/agent/transaction/distributed_tracer.rb +171 -0
  93. data/lib/new_relic/agent/transaction/distributed_tracing.rb +57 -146
  94. data/lib/new_relic/agent/transaction/external_request_segment.rb +29 -36
  95. data/lib/new_relic/agent/transaction/message_broker_segment.rb +3 -11
  96. data/lib/new_relic/agent/transaction/segment.rb +7 -1
  97. data/lib/new_relic/agent/transaction/trace.rb +2 -4
  98. data/lib/new_relic/agent/transaction/trace_context.rb +168 -0
  99. data/lib/new_relic/agent/transaction/trace_node.rb +10 -8
  100. data/lib/new_relic/agent/transaction_error_primitive.rb +10 -15
  101. data/lib/new_relic/agent/transaction_event_primitive.rb +28 -39
  102. data/lib/new_relic/cli/commands/deployments.rb +1 -1
  103. data/lib/new_relic/cli/commands/install.rb +3 -2
  104. data/lib/new_relic/coerce.rb +31 -6
  105. data/lib/new_relic/constants.rb +38 -0
  106. data/lib/new_relic/control/instance_methods.rb +10 -1
  107. data/lib/new_relic/dependency_detection.rb +4 -4
  108. data/lib/new_relic/environment_report.rb +5 -1
  109. data/lib/new_relic/noticed_error.rb +38 -17
  110. data/lib/new_relic/rack/browser_monitoring.rb +5 -0
  111. data/lib/new_relic/supportability_helper.rb +14 -0
  112. data/lib/new_relic/version.rb +1 -1
  113. data/lib/tasks/multiverse.rb +25 -0
  114. data/lib/tasks/tests.rake +6 -1
  115. data/newrelic_rpm.gemspec +19 -8
  116. data/test/agent_helper.rb +323 -71
  117. metadata +100 -33
  118. data/lib/new_relic/agent/cross_app_monitor.rb +0 -110
  119. data/lib/new_relic/agent/distributed_trace_monitor.rb +0 -40
  120. data/lib/new_relic/agent/http_clients/abstract_request.rb +0 -31
  121. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  122. data/lib/tasks/versions.html.erb +0 -28
  123. data/lib/tasks/versions.postface.html +0 -8
  124. data/lib/tasks/versions.preface.html +0 -9
  125. data/lib/tasks/versions.rake +0 -65
  126. data/lib/tasks/versions.txt.erb +0 -14
@@ -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
 
@@ -60,10 +60,10 @@ module NewRelic
60
60
  module Instrumentation
61
61
  module TyphoeusTracing
62
62
 
63
- HYDRA_SEGMENT_NAME = "External/Multiple/Typhoeus::Hydra/run"
64
-
63
+ HYDRA_SEGMENT_NAME = "External/Multiple/Typhoeus::Hydra/run"
64
+ NOTICIBLE_ERROR_CLASS = "Typhoeus::Errors::TyphoeusError"
65
+
65
66
  EARLIEST_VERSION = Gem::Version.new("0.5.3")
66
-
67
67
  def self.is_supported_version?
68
68
  Gem::Version.new(Typhoeus::VERSION) >= NewRelic::Agent::Instrumentation::TyphoeusTracing::EARLIEST_VERSION
69
69
  end
@@ -72,6 +72,17 @@ module NewRelic
72
72
  request.respond_to?(:hydra) && request.hydra
73
73
  end
74
74
 
75
+ def self.response_message(response)
76
+ if response.respond_to?(:response_message)
77
+ response.response_message
78
+ elsif response.respond_to?(:return_message)
79
+ response.return_message
80
+ else
81
+ # 0.5.4 seems to have lost xxxx_message methods altogether.
82
+ "timeout"
83
+ end
84
+ end
85
+
75
86
  def self.trace(request)
76
87
  state = NewRelic::Agent::Tracer.state
77
88
  return unless state.is_execution_traced?
@@ -92,8 +103,14 @@ module NewRelic
92
103
  segment.add_request_headers wrapped_request
93
104
 
94
105
  callback = Proc.new do
95
- wrapped_response = ::NewRelic::Agent::HTTPClients::TyphoeusHTTPResponse.new(request.response)
96
- segment.read_response_headers wrapped_response
106
+ wrapped_response = HTTPClients::TyphoeusHTTPResponse.new(request.response)
107
+
108
+ segment.process_response_headers wrapped_response
109
+
110
+ if request.response.code == 0
111
+ segment.notice_error NoticibleError.new NOTICIBLE_ERROR_CLASS, response_message(request.response)
112
+ end
113
+
97
114
  segment.finish if segment
98
115
  end
99
116
  request.on_complete.unshift(callback)
@@ -0,0 +1,139 @@
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 'json'
6
+ require 'new_relic/agent/hostname'
7
+
8
+ module NewRelic
9
+ module Agent
10
+ module Logging
11
+
12
+ # This class can be used as the formatter for an existing logger. It
13
+ # decorates log messages with trace and entity metadata, and formats each
14
+ # log messages as a JSON object.
15
+ #
16
+ # It can be added to a Rails application like this:
17
+ #
18
+ # require 'newrelic_rpm'
19
+ #
20
+ # Rails.application.configure do
21
+ # config.log_formatter = ::NewRelic::Agent::Logging::DecoratingFormatter.new
22
+ # end
23
+ #
24
+ # @api public
25
+ class DecoratingFormatter < ::Logger::Formatter
26
+ TIMESTAMP_KEY = 'timestamp'.freeze
27
+ MESSAGE_KEY = 'message'.freeze
28
+ LOG_LEVEL_KEY = 'log.level'.freeze
29
+ LOG_NAME_KEY = 'logger.name'.freeze
30
+ NEWLINE = "\n".freeze
31
+
32
+ QUOTE = '"'.freeze
33
+ COLON = ':'.freeze
34
+ COMMA = ','.freeze
35
+ CLOSING_BRACE = '}'.freeze
36
+
37
+ def initialize
38
+ Agent.config.register_callback :app_name do
39
+ @app_name = nil
40
+ end
41
+ end
42
+
43
+ def call severity, time, progname, msg
44
+ message = '{'
45
+ if app_name
46
+ add_key_value message, ENTITY_NAME_KEY, app_name
47
+ message << COMMA
48
+ end
49
+ add_key_value message, ENTITY_TYPE_KEY, ENTITY_TYPE
50
+ message << COMMA
51
+ add_key_value message, HOSTNAME_KEY, Hostname.get
52
+
53
+ if entity_guid = Agent.config[:entity_guid]
54
+ message << COMMA
55
+ add_key_value message, ENTITY_GUID_KEY, entity_guid
56
+ end
57
+
58
+ if trace_id = Tracer.trace_id
59
+ message << COMMA
60
+ add_key_value message, TRACE_ID_KEY, trace_id
61
+ end
62
+ if span_id = Tracer.span_id
63
+ message << COMMA
64
+ add_key_value message, SPAN_ID_KEY, span_id
65
+ end
66
+
67
+ message << COMMA
68
+ message << QUOTE << MESSAGE_KEY << QUOTE << COLON << escape(msg)
69
+ message << COMMA
70
+ add_key_value message, LOG_LEVEL_KEY, severity
71
+ if progname
72
+ message << COMMA
73
+ add_key_value message, LOG_NAME_KEY, progname
74
+ end
75
+
76
+ message << COMMA
77
+ message << QUOTE << TIMESTAMP_KEY << QUOTE << COLON << (time.to_f * 1000).round.to_s
78
+ message << CLOSING_BRACE << NEWLINE
79
+ end
80
+
81
+ def app_name
82
+ @app_name ||= Agent.config[:app_name][0]
83
+ end
84
+
85
+ def add_key_value message, key, value
86
+ message << QUOTE << key << QUOTE << COLON << QUOTE << value << QUOTE
87
+ end
88
+
89
+ def escape message
90
+ if String === message
91
+ message.to_json
92
+ else
93
+ message.inspect.to_json
94
+ end
95
+ end
96
+
97
+ def clear_tags!
98
+ # No-op; just avoiding issues with act-fluent-logger-rails
99
+ end
100
+ end
101
+
102
+
103
+ # This logger decorates logs with trace and entity metadata, and emits log
104
+ # messages formatted as JSON objects. It extends the Logger class from
105
+ # the Ruby standard library, and accepts the same constructor parameters.
106
+ #
107
+ # It aliases the `:info` message to overwrite the `:write` method, so it
108
+ # can be used in Rack applications that expect the logger to be a file-like
109
+ # object.
110
+ #
111
+ # It can be added to an application like this:
112
+ #
113
+ # require 'newrelic_rpm'
114
+ #
115
+ # config.logger = NewRelic::Agent::Logging::DecoratingLogger.new "log/application.log"
116
+ #
117
+ # @api public
118
+ class DecoratingLogger < (defined?(::ActiveSupport) && defined?(::ActiveSupport::Logger) ? ::ActiveSupport::Logger : ::Logger)
119
+
120
+ alias :write :info
121
+
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
135
+ end
136
+ end
137
+ end
138
+ end
139
+ 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
@@ -177,15 +178,25 @@ module NewRelic
177
178
  header
178
179
  end
179
180
 
181
+ # Positional and Keyword arguments are separated beginning with Ruby 2.7
182
+ def arguments_for_ruby_version
183
+ if RUBY_VERSION < "2.7.0"
184
+ "(*args, &block)"
185
+ else
186
+ "(*args, **kwargs, &block)"
187
+ end
188
+ end
189
+
180
190
  # returns an eval-able string that contains the traced
181
191
  # method code used if the agent is not creating a scope for
182
192
  # use in scoped metrics.
183
193
  def method_without_push_scope(method_name, metric_name_code, options)
184
- "def #{_traced_method_name(method_name, metric_name_code)}(*args, &block)
194
+ arguments = arguments_for_ruby_version
195
+ "def #{_traced_method_name(method_name, metric_name_code)}#{arguments}
185
196
  #{assemble_code_header(method_name, metric_name_code, options)}
186
197
  t0 = Time.now
187
198
  begin
188
- #{_untraced_method_name(method_name, metric_name_code)}(*args, &block)\n
199
+ #{_untraced_method_name(method_name, metric_name_code)}#{arguments}\n
189
200
  ensure
190
201
  duration = (Time.now - t0).to_f
191
202
  NewRelic::Agent.record_metric(\"#{metric_name_code}\", duration)
@@ -197,11 +208,12 @@ module NewRelic
197
208
  # returns an eval-able string that contains the tracing code
198
209
  # for a fully traced metric including scoping
199
210
  def method_with_push_scope(method_name, metric_name_code, options)
200
- "def #{_traced_method_name(method_name, metric_name_code)}(*args, &block)
211
+ arguments = arguments_for_ruby_version
212
+ "def #{_traced_method_name(method_name, metric_name_code)}#{arguments}
201
213
  #{options[:code_header]}
202
214
  result = ::NewRelic::Agent::MethodTracerHelpers.trace_execution_scoped(\"#{metric_name_code}\",
203
215
  :metric => #{options[:metric]}) do
204
- #{_untraced_method_name(method_name, metric_name_code)}(*args, &block)
216
+ #{_untraced_method_name(method_name, metric_name_code)}#{arguments}
205
217
  end
206
218
  #{options[:code_footer]}
207
219
  result
@@ -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
 
@@ -27,7 +27,7 @@ module NewRelic
27
27
  end
28
28
 
29
29
  begin
30
- yield
30
+ Tracer.capture_segment_error(segment) { yield }
31
31
  ensure
32
32
  segment.finish if segment
33
33
  end
@@ -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