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
@@ -1,21 +1,17 @@
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
4
5
 
5
- require 'new_relic/agent/http_clients/abstract_request'
6
+ require_relative 'abstract'
6
7
 
7
8
  module NewRelic
8
9
  module Agent
9
10
  module HTTPClients
10
- class HTTPClientResponse
11
- attr_reader :response
12
-
13
- def initialize(response)
14
- @response = response
15
- end
11
+ class HTTPClientResponse < AbstractResponse
16
12
 
17
13
  def [](key)
18
- response.headers.each do |k,v|
14
+ @wrapped_response.headers.each do |k,v|
19
15
  if key.downcase == k.downcase
20
16
  return v
21
17
  end
@@ -24,12 +20,12 @@ module NewRelic
24
20
  end
25
21
 
26
22
  def to_hash
27
- response.headers
23
+ @wrapped_response.headers
28
24
  end
29
25
  end
30
26
 
31
27
  class HTTPClientRequest < AbstractRequest
32
- attr_reader :request, :uri
28
+ attr_reader :request
33
29
 
34
30
  HTTP_CLIENT = "HTTPClient".freeze
35
31
  LHOST = 'host'.freeze
@@ -38,7 +34,6 @@ module NewRelic
38
34
 
39
35
  def initialize(request)
40
36
  @request = request
41
- @uri = request.header.request_uri
42
37
  end
43
38
 
44
39
  def type
@@ -59,6 +54,10 @@ module NewRelic
59
54
  host_from_header || uri.host.to_s
60
55
  end
61
56
 
57
+ def uri
58
+ @uri ||= URIUtil.parse_and_normalize_url(request.header.request_uri)
59
+ end
60
+
62
61
  def [](key)
63
62
  request.headers[key]
64
63
  end
@@ -1,26 +1,38 @@
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
4
5
 
5
- require 'new_relic/agent/http_clients/abstract_request'
6
+ require_relative 'abstract'
6
7
 
7
8
  module NewRelic
8
9
  module Agent
9
10
  module HTTPClients
11
+ class NetHTTPResponse < AbstractResponse
12
+
13
+ def [](key)
14
+ @wrapped_response[key]
15
+ end
16
+
17
+ def to_hash
18
+ @wrapped_response.to_hash
19
+ end
20
+ end
21
+
10
22
  class NetHTTPRequest < AbstractRequest
11
23
  def initialize(connection, request)
12
24
  @connection = connection
13
25
  @request = request
14
26
  end
15
27
 
16
- NET_HTTP = 'Net::HTTP'.freeze
28
+ NET_HTTP = 'Net::HTTP'
17
29
 
18
30
  def type
19
31
  NET_HTTP
20
32
  end
21
33
 
22
- HOST = 'host'.freeze
23
- COLON = ':'.freeze
34
+ HOST = 'host'
35
+ COLON = ':'
24
36
 
25
37
  def host_from_header
26
38
  if hostname = self[HOST]
@@ -1,16 +1,14 @@
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
4
5
 
5
- require 'new_relic/agent/http_clients/abstract_request'
6
+ require_relative 'abstract'
6
7
 
7
8
  module NewRelic
8
9
  module Agent
9
10
  module HTTPClients
10
- class TyphoeusHTTPResponse
11
- def initialize(response)
12
- @response = response
13
- end
11
+ class TyphoeusHTTPResponse < AbstractResponse
14
12
 
15
13
  def [](key)
16
14
  unless headers.nil?
@@ -33,7 +31,7 @@ module NewRelic
33
31
  private
34
32
 
35
33
  def headers
36
- @response.headers
34
+ @wrapped_response.headers
37
35
  end
38
36
  end
39
37
 
@@ -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
  # This module includes utilities for manipulating URIs, particularly from the
7
8
  # context of Net::HTTP requests. We don't always have direct access to the full
@@ -41,10 +42,10 @@ module NewRelic
41
42
  uri
42
43
  end
43
44
 
44
- QUESTION_MARK = "?".freeze
45
+ QUESTION_MARK = "?"
45
46
 
46
47
  def self.strip_query_string(fragment)
47
- if(fragment.include?(QUESTION_MARK))
48
+ if fragment.include? QUESTION_MARK
48
49
  fragment.split(QUESTION_MARK).first
49
50
  else
50
51
  fragment
@@ -29,7 +29,10 @@ module NewRelic
29
29
  def finish(name, id, payload) #THREAD_LOCAL_ACCESS
30
30
  return unless state.is_execution_traced?
31
31
 
32
- notice_error payload if payload.key? :exception
32
+ if exception = exception_object(payload)
33
+ NewRelic::Agent.notice_error exception
34
+ end
35
+
33
36
  finishable = pop_segment(id)
34
37
  finishable.finish if finishable
35
38
  rescue => e
@@ -47,14 +50,9 @@ module NewRelic
47
50
  end
48
51
 
49
52
  DOT_ACTION_CABLE = '.action_cable'.freeze
50
- EMPTY_STRING = ''.freeze
51
53
 
52
54
  def action_name(name)
53
- name.gsub DOT_ACTION_CABLE, EMPTY_STRING
54
- end
55
-
56
- def notice_error(payload)
57
- NewRelic::Agent.notice_error payload[:exception_object]
55
+ name.gsub DOT_ACTION_CABLE, NewRelic::EMPTY_STR
58
56
  end
59
57
  end
60
58
  end
@@ -35,6 +35,10 @@ module NewRelic
35
35
  if state.is_execution_traced? \
36
36
  && !should_ignore(payload, controller_class(payload))
37
37
 
38
+ if exception = exception_object(payload)
39
+ finishable.notice_error(exception)
40
+ end
41
+
38
42
  finishable.finish
39
43
  else
40
44
  Agent.instance.pop_trace_execution_flag
@@ -24,8 +24,13 @@ module NewRelic
24
24
  end
25
25
 
26
26
  def finish(name, id, payload) #THREAD_LOCAL_ACCESS
27
- event = pop_segment(id)
28
- event.finish if event
27
+ if segment = pop_segment(id)
28
+
29
+ if exception = exception_object(payload)
30
+ segment.notice_error exception
31
+ end
32
+ segment.finish
33
+ end
29
34
  rescue => e
30
35
  log_notification_error(e, name, 'finish')
31
36
  end
@@ -97,6 +102,10 @@ module NewRelic
97
102
  def finish
98
103
  @finishable.finish if @finishable
99
104
  end
105
+
106
+ def notice_error error
107
+ @finishable.notice_error error if @finishable
108
+ end
100
109
  end
101
110
  end
102
111
 
@@ -43,7 +43,7 @@ module NewRelic
43
43
  end
44
44
  end
45
45
 
46
- def log_with_newrelic_instrumentation(*args, &block) #THREAD_LOCAL_ACCESS
46
+ def log_with_newrelic_instrumentation(*args, &block)
47
47
  state = NewRelic::Agent::Tracer.state
48
48
 
49
49
  if !state.is_execution_traced?
@@ -78,7 +78,9 @@ module NewRelic
78
78
  segment._notice_sql(sql, @config, EXPLAINER)
79
79
 
80
80
  begin
81
- log_without_newrelic_instrumentation(*args, &block)
81
+ NewRelic::Agent::Tracer.capture_segment_error segment do
82
+ log_without_newrelic_instrumentation(*args, &block)
83
+ end
82
84
  ensure
83
85
  segment.finish if segment
84
86
  end
@@ -58,8 +58,13 @@ module NewRelic
58
58
  return if cached?(payload)
59
59
  return unless state.is_execution_traced?
60
60
 
61
- segment = pop_segment(id)
62
- segment.finish if segment
61
+ if segment = pop_segment(id)
62
+ if exception = exception_object(payload)
63
+ segment.notice_error(exception)
64
+ end
65
+ segment.finish
66
+ end
67
+
63
68
  rescue => e
64
69
  log_notification_error(e, name, 'finish')
65
70
  end
@@ -16,7 +16,7 @@ module NewRelic
16
16
 
17
17
  def finish name, id, payload
18
18
  return unless state.is_execution_traced?
19
- finish_segment id
19
+ finish_segment id, payload
20
20
  rescue => e
21
21
  log_notification_error e, name, 'finish'
22
22
  end
@@ -28,9 +28,13 @@ module NewRelic
28
28
  push_segment id, segment
29
29
  end
30
30
 
31
- def finish_segment id
32
- segment = pop_segment id
33
- segment.finish if segment
31
+ def finish_segment id, payload
32
+ if segment = pop_segment(id)
33
+ if exception = exception_object(payload)
34
+ segment.notice_error(exception)
35
+ end
36
+ segment.finish
37
+ end
34
38
  end
35
39
 
36
40
  def metric_name name, payload
@@ -1,6 +1,7 @@
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
4
5
 
5
6
  DependencyDetection.defer do
6
7
  named :bunny
@@ -11,7 +12,7 @@ DependencyDetection.defer do
11
12
 
12
13
  executes do
13
14
  ::NewRelic::Agent.logger.info 'Installing Bunny instrumentation'
14
- require 'new_relic/agent/cross_app_tracing'
15
+ require 'new_relic/agent/distributed_tracing/cross_app_tracing'
15
16
  require 'new_relic/agent/messaging'
16
17
  require 'new_relic/agent/transaction/message_broker_segment'
17
18
  end
@@ -41,10 +42,11 @@ DependencyDetection.defer do
41
42
  )
42
43
  rescue => e
43
44
  NewRelic::Agent.logger.error "Error starting message broker segment in Bunny::Exchange#publish", e
44
- end
45
-
46
- begin
47
45
  publish_without_new_relic payload, opts
46
+ else
47
+ NewRelic::Agent::Tracer.capture_segment_error segment do
48
+ publish_without_new_relic payload, opts
49
+ end
48
50
  ensure
49
51
  segment.finish if segment
50
52
  end
@@ -55,28 +57,40 @@ DependencyDetection.defer do
55
57
  alias_method :pop_without_new_relic, :pop
56
58
 
57
59
  def pop(opts = {:manual_ack => false}, &block)
58
- t0 = Time.now
59
- msg = pop_without_new_relic opts, &block
60
- delivery_info, message_properties, _payload = msg
60
+ bunny_error, delivery_info, message_properties, _payload = nil, nil, nil, nil
61
+ begin
62
+ t0 = Time.now
63
+ msg = pop_without_new_relic opts, &block
64
+ delivery_info, message_properties, _payload = msg
65
+ rescue StandardError => error
66
+ bunny_error = error
67
+ end
61
68
 
62
69
  begin
63
- if delivery_info
64
- exchange_name = NewRelic::Agent::Instrumentation::Bunny.exchange_name(delivery_info.exchange)
65
- exchange_type = NewRelic::Agent::Instrumentation::Bunny.exchange_type(delivery_info, channel)
66
-
67
- segment = NewRelic::Agent::Messaging.start_amqp_consume_segment(
68
- library: NewRelic::Agent::Instrumentation::Bunny::LIBRARY,
69
- destination_name: exchange_name,
70
- delivery_info: delivery_info,
71
- message_properties: message_properties,
72
- exchange_type: exchange_type,
73
- queue_name: name,
74
- start_time: t0
75
- )
70
+ exchange_name, exchange_type = if delivery_info
71
+ [ NewRelic::Agent::Instrumentation::Bunny.exchange_name(delivery_info.exchange),
72
+ NewRelic::Agent::Instrumentation::Bunny.exchange_type(delivery_info, channel) ]
73
+ else
74
+ [ NewRelic::Agent::Instrumentation::Bunny.exchange_name(NewRelic::EMPTY_STR),
75
+ NewRelic::Agent::Instrumentation::Bunny.exchange_type({}, channel) ]
76
76
  end
77
77
 
78
+ segment = NewRelic::Agent::Messaging.start_amqp_consume_segment(
79
+ library: NewRelic::Agent::Instrumentation::Bunny::LIBRARY,
80
+ destination_name: exchange_name,
81
+ delivery_info: (delivery_info || {}),
82
+ message_properties: (message_properties || {headers: {}}),
83
+ exchange_type: exchange_type,
84
+ queue_name: name,
85
+ start_time: t0
86
+ )
78
87
  rescue => e
79
88
  NewRelic::Agent.logger.error "Error starting message broker segment in Bunny::Queue#pop", e
89
+ else
90
+ if bunny_error
91
+ segment.notice_error bunny_error
92
+ raise bunny_error
93
+ end
80
94
  ensure
81
95
  segment.finish if segment
82
96
  end
@@ -97,10 +111,11 @@ DependencyDetection.defer do
97
111
  )
98
112
  rescue => e
99
113
  NewRelic::Agent.logger.error "Error starting message broker segment in Bunny::Queue#purge", e
100
- end
101
-
102
- begin
103
114
  purge_without_new_relic(*args)
115
+ else
116
+ NewRelic::Agent::Tracer.capture_segment_error segment do
117
+ purge_without_new_relic(*args)
118
+ end
104
119
  ensure
105
120
  segment.finish if segment
106
121
  end
@@ -134,18 +149,20 @@ module NewRelic
134
149
  module Agent
135
150
  module Instrumentation
136
151
  module Bunny
137
- LIBRARY = 'RabbitMQ'.freeze
138
- DEFAULT = 'Default'.freeze
139
- SLASH = '/'.freeze
152
+ LIBRARY = 'RabbitMQ'
153
+ DEFAULT_NAME = 'Default'
154
+ DEFAULT_TYPE = :direct
155
+
156
+ SLASH = '/'
140
157
 
141
158
  class << self
142
159
  def exchange_name name
143
- name.empty? ? DEFAULT : name
160
+ name.empty? ? DEFAULT_NAME : name
144
161
  end
145
162
 
146
163
  def exchange_type delivery_info, channel
147
164
  if di_exchange = delivery_info[:exchange]
148
- return :direct if di_exchange.empty?
165
+ return DEFAULT_TYPE if di_exchange.empty?
149
166
  return channel.exchanges[delivery_info[:exchange]].type if channel.exchanges[di_exchange]
150
167
  end
151
168
  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
 
@@ -22,10 +22,12 @@ DependencyDetection.defer do
22
22
  class Curl::Easy
23
23
 
24
24
  attr_accessor :_nr_instrumented,
25
+ :_nr_failure_instrumented,
25
26
  :_nr_http_verb,
26
27
  :_nr_header_str,
27
28
  :_nr_original_on_header,
28
29
  :_nr_original_on_complete,
30
+ :_nr_original_on_failure,
29
31
  :_nr_serial
30
32
 
31
33
  # We have to hook these three methods separately, as they don't use
@@ -53,7 +55,7 @@ DependencyDetection.defer do
53
55
 
54
56
 
55
57
  # Hook the #http method to set the verb.
56
- def http_with_newrelic( verb )
58
+ def http_with_newrelic verb
57
59
  self._nr_http_verb = verb.to_s.upcase
58
60
  http_without_newrelic( verb )
59
61
  end
@@ -72,9 +74,9 @@ DependencyDetection.defer do
72
74
  alias_method :perform, :perform_with_newrelic
73
75
 
74
76
  # Record the HTTP verb for future #perform calls
75
- def method_with_newrelic(m)
76
- self._nr_http_verb = m.upcase
77
- method_without_newrelic(m)
77
+ def method_with_newrelic verb
78
+ self._nr_http_verb = verb.upcase
79
+ method_without_newrelic(verb)
78
80
  end
79
81
 
80
82
  alias_method :method_without_newrelic, :method
@@ -101,12 +103,12 @@ DependencyDetection.defer do
101
103
  include NewRelic::Agent::MethodTracer
102
104
 
103
105
  # Add CAT with callbacks if the request is serial
104
- def add_with_newrelic(curl) #THREAD_LOCAL_ACCESS
106
+ def add_with_newrelic(curl)
105
107
  if curl.respond_to?(:_nr_serial) && curl._nr_serial
106
108
  hook_pending_request(curl) if NewRelic::Agent::Tracer.tracing_enabled?
107
109
  end
108
110
 
109
- return add_without_newrelic( curl )
111
+ return add_without_newrelic curl
110
112
  end
111
113
 
112
114
  alias_method :add_without_newrelic, :add
@@ -125,9 +127,9 @@ DependencyDetection.defer do
125
127
  alias_method :perform, :perform_with_newrelic
126
128
 
127
129
 
128
- # Instrument the specified +request+ (a Curl::Easy object) and set up cross-application
129
- # tracing if it's enabled.
130
- def hook_pending_request(request) #THREAD_LOCAL_ACCESS
130
+ # Instrument the specified +request+ (a Curl::Easy object)
131
+ # and set up cross-application tracing if it's enabled.
132
+ def hook_pending_request(request)
131
133
  wrapped_request, wrapped_response = wrap_request(request)
132
134
 
133
135
  segment = NewRelic::Agent::Tracer.start_external_request_segment(
@@ -138,9 +140,11 @@ DependencyDetection.defer do
138
140
 
139
141
  segment.add_request_headers wrapped_request
140
142
 
143
+ # install all callbacks
141
144
  unless request._nr_instrumented
142
145
  install_header_callback(request, wrapped_response)
143
146
  install_completion_callback(request, wrapped_response, segment)
147
+ install_failure_callback(request, wrapped_response, segment)
144
148
  request._nr_instrumented = true
145
149
  end
146
150
  rescue => err
@@ -149,35 +153,37 @@ DependencyDetection.defer do
149
153
 
150
154
 
151
155
  # Create request and response adapter objects for the specified +request+
156
+ # NOTE: Although strange to wrap request and response at once, it works
157
+ # because curb's callback mechanism updates the instantiated wrappers
158
+ # during the life-cycle of external request
152
159
  def wrap_request(request)
153
160
  return NewRelic::Agent::HTTPClients::CurbRequest.new(request),
154
161
  NewRelic::Agent::HTTPClients::CurbResponse.new(request)
155
162
  end
156
163
 
157
-
158
- # Install a callback that will record the response headers to enable
159
- # CAT linking
160
- def install_header_callback( request, wrapped_response )
164
+ # Install a callback that will record the response headers
165
+ # to enable CAT linking
166
+ def install_header_callback(request, wrapped_response)
161
167
  original_callback = request.on_header
162
168
  request._nr_original_on_header = original_callback
163
169
  request._nr_header_str = nil
164
170
  request.on_header do |header_data|
165
171
  if original_callback
166
- original_callback.call( header_data )
172
+ original_callback.call header_data
167
173
  else
168
- wrapped_response.append_header_data( header_data )
174
+ wrapped_response.append_header_data header_data
169
175
  header_data.length
170
176
  end
171
177
  end
172
178
  end
173
179
 
174
180
  # Install a callback that will finish the trace.
175
- def install_completion_callback(request, wrapped_response, segment) #THREAD_LOCAL_ACCESS
181
+ def install_completion_callback(request, wrapped_response, segment)
176
182
  original_callback = request.on_complete
177
183
  request._nr_original_on_complete = original_callback
178
184
  request.on_complete do |finished_request|
179
185
  begin
180
- segment.read_response_headers wrapped_response
186
+ segment.process_response_headers wrapped_response
181
187
  ensure
182
188
  segment.finish if segment
183
189
  # Make sure the existing completion callback is run, and restore the
@@ -188,12 +194,47 @@ DependencyDetection.defer do
188
194
  end
189
195
  end
190
196
 
197
+ # Install a callback that will fire on failures
198
+ # NOTE: on_failure is not always called, so we're not always
199
+ # unhooking the callback. No harm/no foul in production, but
200
+ # definitely something to beware of if debugging callback issues
201
+ # _nr_failure_instrumented exists to prevent infinitely chaining
202
+ # our on_failure callback hook.
203
+ def install_failure_callback(request, wrapped_response, segment)
204
+ return if request._nr_failure_instrumented
205
+ original_callback = request.on_failure
206
+ request._nr_original_on_failure = original_callback
207
+ request.on_failure do |failed_request, error|
208
+ begin
209
+ if segment
210
+ noticible_error = NewRelic::Agent::NoticibleError.new error[0].name, error[-1]
211
+ segment.notice_error noticible_error
212
+ end
213
+ ensure
214
+ original_callback.call(failed_request, error) if original_callback
215
+ remove_failure_callback(failed_request)
216
+ end
217
+ request._nr_failure_instrumented = true
218
+ end
219
+ end
220
+
221
+ # on_failure callbacks cannot be removed in the on_complete
222
+ # callback where this method is invoked because on_complete
223
+ # fires before the on_failure!
191
224
  def remove_instrumentation_callbacks(request)
192
225
  request.on_complete(&request._nr_original_on_complete)
193
226
  request.on_header(&request._nr_original_on_header)
194
227
  request._nr_instrumented = false
195
228
  end
196
229
 
230
+ # We execute customer's on_failure callback (if any) and
231
+ # uninstall our hook here since the on_complete callback
232
+ # fires before the on_failure callback.
233
+ def remove_failure_callback(request)
234
+ request.on_failure(&request._nr_original_on_failure)
235
+ request._nr_failure_instrumented = false
236
+ end
237
+
197
238
  private
198
239
 
199
240
  def first_request_is_serial?