newrelic_rpm 6.15.0 → 7.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +164 -22
  3. data/CONTRIBUTING.md +13 -2
  4. data/README.md +2 -2
  5. data/lib/new_relic/agent.rb +5 -8
  6. data/lib/new_relic/agent/agent.rb +2 -1
  7. data/lib/new_relic/agent/autostart.rb +1 -2
  8. data/lib/new_relic/agent/configuration/default_source.rb +442 -227
  9. data/lib/new_relic/agent/configuration/manager.rb +3 -3
  10. data/lib/new_relic/agent/connect/request_builder.rb +4 -2
  11. data/lib/new_relic/agent/datastores/redis.rb +0 -4
  12. data/lib/new_relic/agent/distributed_tracing.rb +0 -66
  13. data/lib/new_relic/agent/error_collector.rb +52 -37
  14. data/lib/new_relic/agent/error_filter.rb +167 -0
  15. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +0 -16
  16. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +1 -3
  17. data/lib/new_relic/agent/instrumentation/bunny.rb +10 -152
  18. data/lib/new_relic/agent/instrumentation/bunny/chain.rb +45 -0
  19. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +152 -0
  20. data/lib/new_relic/agent/instrumentation/bunny/prepend.rb +35 -0
  21. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +1 -0
  22. data/lib/new_relic/agent/instrumentation/curb.rb +9 -241
  23. data/lib/new_relic/agent/instrumentation/curb/chain.rb +93 -0
  24. data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +222 -0
  25. data/lib/new_relic/agent/instrumentation/curb/prepend.rb +63 -0
  26. data/lib/new_relic/agent/instrumentation/delayed_job/chain.rb +38 -0
  27. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +53 -0
  28. data/lib/new_relic/agent/instrumentation/delayed_job/prepend.rb +34 -0
  29. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +8 -50
  30. data/lib/new_relic/agent/instrumentation/excon.rb +2 -1
  31. data/lib/new_relic/agent/instrumentation/grape.rb +13 -113
  32. data/lib/new_relic/agent/instrumentation/grape/chain.rb +25 -0
  33. data/lib/new_relic/agent/instrumentation/grape/instrumentation.rb +100 -0
  34. data/lib/new_relic/agent/instrumentation/grape/prepend.rb +17 -0
  35. data/lib/new_relic/agent/instrumentation/httpclient.rb +8 -30
  36. data/lib/new_relic/agent/instrumentation/httpclient/chain.rb +25 -0
  37. data/lib/new_relic/agent/instrumentation/httpclient/instrumentation.rb +38 -0
  38. data/lib/new_relic/agent/instrumentation/httpclient/prepend.rb +17 -0
  39. data/lib/new_relic/agent/instrumentation/httprb.rb +29 -0
  40. data/lib/new_relic/agent/instrumentation/httprb/chain.rb +22 -0
  41. data/lib/new_relic/agent/instrumentation/httprb/instrumentation.rb +30 -0
  42. data/lib/new_relic/agent/instrumentation/httprb/prepend.rb +15 -0
  43. data/lib/new_relic/agent/instrumentation/memcache.rb +54 -69
  44. data/lib/new_relic/agent/instrumentation/memcache/chain.rb +16 -0
  45. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +38 -121
  46. data/lib/new_relic/agent/instrumentation/memcache/helper.rb +56 -0
  47. data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +88 -0
  48. data/lib/new_relic/agent/instrumentation/memcache/prepend.rb +88 -0
  49. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +2 -0
  50. data/lib/new_relic/agent/instrumentation/mongo.rb +7 -0
  51. data/lib/new_relic/agent/instrumentation/net_http.rb +44 -0
  52. data/lib/new_relic/agent/instrumentation/net_http/chain.rb +25 -0
  53. data/lib/new_relic/agent/instrumentation/{net_prepend.rb → net_http/instrumentation.rb} +3 -3
  54. data/lib/new_relic/agent/instrumentation/net_http/prepend.rb +21 -0
  55. data/lib/new_relic/agent/instrumentation/padrino.rb +18 -53
  56. data/lib/new_relic/agent/instrumentation/padrino/chain.rb +34 -0
  57. data/lib/new_relic/agent/instrumentation/padrino/instrumentation.rb +27 -0
  58. data/lib/new_relic/agent/instrumentation/padrino/prepend.rb +20 -0
  59. data/lib/new_relic/agent/instrumentation/rack.rb +29 -139
  60. data/lib/new_relic/agent/instrumentation/rack/chain.rb +58 -0
  61. data/lib/new_relic/agent/instrumentation/rack/helpers.rb +32 -0
  62. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +73 -0
  63. data/lib/new_relic/agent/instrumentation/rack/prepend.rb +37 -0
  64. data/lib/new_relic/agent/instrumentation/rake.rb +13 -154
  65. data/lib/new_relic/agent/instrumentation/rake/chain.rb +25 -0
  66. data/lib/new_relic/agent/instrumentation/rake/instrumentation.rb +144 -0
  67. data/lib/new_relic/agent/instrumentation/rake/prepend.rb +14 -0
  68. data/lib/new_relic/agent/instrumentation/redis.rb +12 -107
  69. data/lib/new_relic/agent/instrumentation/redis/chain.rb +34 -0
  70. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +65 -0
  71. data/lib/new_relic/agent/instrumentation/redis/prepend.rb +24 -0
  72. data/lib/new_relic/agent/instrumentation/resque.rb +21 -32
  73. data/lib/new_relic/agent/instrumentation/resque/chain.rb +22 -0
  74. data/lib/new_relic/agent/instrumentation/resque/helper.rb +19 -0
  75. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +35 -0
  76. data/lib/new_relic/agent/instrumentation/resque/prepend.rb +16 -0
  77. data/lib/new_relic/agent/instrumentation/sinatra.rb +20 -158
  78. data/lib/new_relic/agent/instrumentation/sinatra/chain.rb +55 -0
  79. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +29 -34
  80. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +124 -0
  81. data/lib/new_relic/agent/instrumentation/sinatra/prepend.rb +33 -0
  82. data/lib/new_relic/agent/instrumentation/typhoeus.rb +10 -89
  83. data/lib/new_relic/agent/instrumentation/typhoeus/chain.rb +22 -0
  84. data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +82 -0
  85. data/lib/new_relic/agent/instrumentation/typhoeus/prepend.rb +14 -0
  86. data/lib/new_relic/agent/javascript_instrumentor.rb +12 -7
  87. data/lib/new_relic/agent/new_relic_service.rb +15 -13
  88. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  89. data/lib/new_relic/agent/transaction.rb +1 -4
  90. data/lib/new_relic/control/frameworks/rails.rb +11 -9
  91. data/lib/new_relic/control/instance_methods.rb +1 -0
  92. data/lib/new_relic/dependency_detection.rb +116 -10
  93. data/lib/new_relic/noticed_error.rb +4 -8
  94. data/lib/new_relic/supportability_helper.rb +1 -2
  95. data/lib/new_relic/version.rb +2 -2
  96. data/lib/tasks/config.html.erb +14 -25
  97. data/lib/tasks/config.rake +8 -7
  98. data/newrelic_rpm.gemspec +1 -1
  99. metadata +55 -8
  100. data/cert/cacert.pem +0 -1177
  101. data/lib/new_relic/agent/instrumentation/http.rb +0 -49
  102. data/lib/new_relic/agent/instrumentation/net.rb +0 -70
@@ -84,8 +84,6 @@ module NewRelic
84
84
  end
85
85
 
86
86
  def active_record_config(payload)
87
- return unless payload[:connection_id]
88
-
89
87
  # handle if the notification payload provides the AR connection
90
88
  # available in Rails 6+ & our ActiveRecordNotifications#log extension
91
89
  if payload[:connection]
@@ -93,8 +91,8 @@ module NewRelic
93
91
  return connection_config if connection_config
94
92
  end
95
93
 
94
+ return unless connection_id = payload[:connection_id]
96
95
  connection = nil
97
- connection_id = payload[:connection_id]
98
96
 
99
97
  ::ActiveRecord::Base.connection_handler.connection_pool_list.each do |handler|
100
98
  connection = handler.connections.detect do |conn|
@@ -3,6 +3,10 @@
3
3
  # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
4
  # frozen_string_literal: true
5
5
 
6
+ require_relative 'bunny/instrumentation'
7
+ require_relative 'bunny/chain'
8
+ require_relative 'bunny/prepend'
9
+
6
10
  DependencyDetection.defer do
7
11
  named :bunny
8
12
 
@@ -18,158 +22,12 @@ DependencyDetection.defer do
18
22
  end
19
23
 
20
24
  executes do
21
- module Bunny
22
- class Exchange
23
- alias_method :publish_without_new_relic, :publish
24
-
25
- def publish payload, opts = {}
26
- begin
27
- destination = NewRelic::Agent::Instrumentation::Bunny.exchange_name(name)
28
-
29
- tracing_enabled =
30
- NewRelic::Agent::CrossAppTracing.cross_app_enabled? ||
31
- NewRelic::Agent.config[:'distributed_tracing.enabled']
32
- opts[:headers] ||= {} if tracing_enabled
33
-
34
- segment = NewRelic::Agent::Messaging.start_amqp_publish_segment(
35
- library: NewRelic::Agent::Instrumentation::Bunny::LIBRARY,
36
- destination_name: destination,
37
- headers: opts[:headers],
38
- routing_key: opts[:routing_key] || opts[:key],
39
- reply_to: opts[:reply_to],
40
- correlation_id: opts[:correlation_id],
41
- exchange_type: type
42
- )
43
- rescue => e
44
- NewRelic::Agent.logger.error "Error starting message broker segment in Bunny::Exchange#publish", e
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
50
- ensure
51
- segment.finish if segment
52
- end
53
- end
54
- end
55
-
56
- class Queue
57
- alias_method :pop_without_new_relic, :pop
58
-
59
- def pop(opts = {:manual_ack => false}, &block)
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
68
-
69
- begin
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
- end
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
- )
87
- rescue => e
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
94
- ensure
95
- segment.finish if segment
96
- end
97
-
98
- msg
99
- end
100
-
101
- alias_method :purge_without_new_relic, :purge
102
-
103
- def purge *args
104
- begin
105
- type = server_named? ? :temporary_queue : :queue
106
- segment = NewRelic::Agent::Tracer.start_message_broker_segment(
107
- action: :purge,
108
- library: NewRelic::Agent::Instrumentation::Bunny::LIBRARY,
109
- destination_type: type,
110
- destination_name: name
111
- )
112
- rescue => e
113
- NewRelic::Agent.logger.error "Error starting message broker segment in Bunny::Queue#purge", e
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
119
- ensure
120
- segment.finish if segment
121
- end
122
- end
123
-
124
- end
125
-
126
- class Consumer
127
- alias_method :call_without_new_relic, :call
128
-
129
- def call *args
130
- delivery_info, message_properties, _ = args
131
- queue_name = queue.respond_to?(:name) ? queue.name : queue
132
-
133
- NewRelic::Agent::Messaging.wrap_amqp_consume_transaction(
134
- library: NewRelic::Agent::Instrumentation::Bunny::LIBRARY,
135
- destination_name: NewRelic::Agent::Instrumentation::Bunny.exchange_name(delivery_info.exchange),
136
- delivery_info: delivery_info,
137
- message_properties: message_properties,
138
- exchange_type: NewRelic::Agent::Instrumentation::Bunny.exchange_type(delivery_info, channel),
139
- queue_name: queue_name) do
140
-
141
- call_without_new_relic(*args)
142
- end
143
- end
144
-
145
- end
146
- end
147
- end
148
- end
149
-
150
- module NewRelic
151
- module Agent
152
- module Instrumentation
153
- module Bunny
154
- LIBRARY = 'RabbitMQ'
155
- DEFAULT_NAME = 'Default'
156
- DEFAULT_TYPE = :direct
157
-
158
- SLASH = '/'
159
-
160
- class << self
161
- def exchange_name name
162
- name.empty? ? DEFAULT_NAME : name
163
- end
164
-
165
- def exchange_type delivery_info, channel
166
- if di_exchange = delivery_info[:exchange]
167
- return DEFAULT_TYPE if di_exchange.empty?
168
- return channel.exchanges[delivery_info[:exchange]].type if channel.exchanges[di_exchange]
169
- end
170
- end
171
- end
172
- end
25
+ if use_prepend?
26
+ prepend_instrument ::Bunny::Exchange, ::NewRelic::Agent::Instrumentation::Bunny::Prepend::Exchange
27
+ prepend_instrument ::Bunny::Queue, ::NewRelic::Agent::Instrumentation::Bunny::Prepend::Queue
28
+ prepend_instrument ::Bunny::Consumer, ::NewRelic::Agent::Instrumentation::Bunny::Prepend::Consumer
29
+ else
30
+ chain_instrument NewRelic::Agent::Instrumentation::Bunny
173
31
  end
174
32
  end
175
33
  end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module Bunny
7
+ def self.instrument!
8
+ ::Bunny::Exchange.class_eval do
9
+ include NewRelic::Agent::Instrumentation::Bunny::Exchange
10
+
11
+ alias_method :publish_without_new_relic, :publish
12
+
13
+ def publish payload, opts = {}
14
+ publish_with_tracing(payload, opts) { publish_without_new_relic payload, opts }
15
+ end
16
+ end
17
+
18
+ ::Bunny::Queue.class_eval do
19
+ include NewRelic::Agent::Instrumentation::Bunny::Queue
20
+
21
+ alias_method :pop_without_new_relic, :pop
22
+
23
+ def pop(opts = {:manual_ack => false}, &block)
24
+ pop_with_tracing { pop_without_new_relic opts, &block }
25
+ end
26
+
27
+ alias_method :purge_without_new_relic, :purge
28
+
29
+ def purge *args
30
+ purge_with_tracing { purge_without_new_relic(*args) }
31
+ end
32
+ end
33
+
34
+ ::Bunny::Consumer.class_eval do
35
+ include NewRelic::Agent::Instrumentation::Bunny::Consumer
36
+
37
+ alias_method :call_without_new_relic, :call
38
+
39
+ def call *args
40
+ call_with_tracing(*args) { call_without_new_relic(*args) }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,152 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+ # frozen_string_literal: true
5
+
6
+ module NewRelic
7
+ module Agent
8
+ module Instrumentation
9
+ module Bunny
10
+ module_function
11
+
12
+ LIBRARY = 'RabbitMQ'
13
+ DEFAULT_NAME = 'Default'
14
+ DEFAULT_TYPE = :direct
15
+ SLASH = '/'
16
+
17
+ def exchange_name name
18
+ name.empty? ? DEFAULT_NAME : name
19
+ end
20
+
21
+ def exchange_type delivery_info, channel
22
+ if di_exchange = delivery_info[:exchange]
23
+ return DEFAULT_TYPE if di_exchange.empty?
24
+ return channel.exchanges[delivery_info[:exchange]].type if channel.exchanges[di_exchange]
25
+ end
26
+ end
27
+
28
+ module Exchange
29
+ include Bunny
30
+
31
+ def publish_with_tracing payload, opts = {}
32
+ begin
33
+ destination = exchange_name(name)
34
+
35
+ tracing_enabled =
36
+ NewRelic::Agent::CrossAppTracing.cross_app_enabled? ||
37
+ NewRelic::Agent.config[:'distributed_tracing.enabled']
38
+ opts[:headers] ||= {} if tracing_enabled
39
+
40
+ segment = NewRelic::Agent::Messaging.start_amqp_publish_segment(
41
+ library: LIBRARY,
42
+ destination_name: destination,
43
+ headers: opts[:headers],
44
+ routing_key: opts[:routing_key] || opts[:key],
45
+ reply_to: opts[:reply_to],
46
+ correlation_id: opts[:correlation_id],
47
+ exchange_type: type
48
+ )
49
+ rescue => e
50
+ NewRelic::Agent.logger.error "Error starting message broker segment in Bunny::Exchange#publish", e
51
+ yield
52
+ else
53
+ NewRelic::Agent::Tracer.capture_segment_error segment do
54
+ yield
55
+ end
56
+ ensure
57
+ segment.finish if segment
58
+ end
59
+ end
60
+ end
61
+
62
+ module Queue
63
+ include Bunny
64
+
65
+ def pop_with_tracing
66
+ bunny_error, delivery_info, message_properties, _payload = nil, nil, nil, nil
67
+ begin
68
+ t0 = Time.now
69
+ msg = yield
70
+ delivery_info, message_properties, _payload = msg
71
+ rescue StandardError => error
72
+ bunny_error = error
73
+ end
74
+
75
+ begin
76
+ exch_name, exch_type = if delivery_info
77
+ [ exchange_name(delivery_info.exchange),
78
+ exchange_type(delivery_info, channel) ]
79
+ else
80
+ [ exchange_name(NewRelic::EMPTY_STR),
81
+ exchange_type({}, channel) ]
82
+ end
83
+
84
+ segment = NewRelic::Agent::Messaging.start_amqp_consume_segment(
85
+ library: LIBRARY,
86
+ destination_name: exch_name,
87
+ delivery_info: (delivery_info || {}),
88
+ message_properties: (message_properties || {headers: {}}),
89
+ exchange_type: exch_type,
90
+ queue_name: name,
91
+ start_time: t0
92
+ )
93
+ rescue => e
94
+ NewRelic::Agent.logger.error "Error starting message broker segment in Bunny::Queue#pop", e
95
+ else
96
+ if bunny_error
97
+ segment.notice_error bunny_error
98
+ raise bunny_error
99
+ end
100
+ ensure
101
+ segment.finish if segment
102
+ end
103
+
104
+ msg
105
+ end
106
+
107
+ def purge_with_tracing
108
+ begin
109
+ type = server_named? ? :temporary_queue : :queue
110
+ segment = NewRelic::Agent::Tracer.start_message_broker_segment(
111
+ action: :purge,
112
+ library: LIBRARY,
113
+ destination_type: type,
114
+ destination_name: name
115
+ )
116
+ rescue => e
117
+ NewRelic::Agent.logger.error "Error starting message broker segment in Bunny::Queue#purge", e
118
+ yield
119
+ else
120
+ NewRelic::Agent::Tracer.capture_segment_error segment do
121
+ yield
122
+ end
123
+ ensure
124
+ segment.finish if segment
125
+ end
126
+ end
127
+ end
128
+
129
+ module Consumer
130
+ include Bunny
131
+
132
+ def call_with_tracing *args
133
+ delivery_info, message_properties, _ = args
134
+ queue_name = queue.respond_to?(:name) ? queue.name : queue
135
+
136
+ NewRelic::Agent::Messaging.wrap_amqp_consume_transaction(
137
+ library: LIBRARY,
138
+ destination_name: exchange_name(delivery_info.exchange),
139
+ delivery_info: delivery_info,
140
+ message_properties: message_properties,
141
+ exchange_type: exchange_type(delivery_info, channel),
142
+ queue_name: queue_name) do
143
+
144
+ yield
145
+ end
146
+ end
147
+
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module Bunny::Prepend
7
+ module Exchange
8
+ include NewRelic::Agent::Instrumentation::Bunny::Exchange
9
+
10
+ def publish payload, opts = {}
11
+ publish_with_tracing(payload, opts) { super }
12
+ end
13
+ end
14
+
15
+ module Queue
16
+ include NewRelic::Agent::Instrumentation::Bunny::Queue
17
+
18
+ def pop(opts = {:manual_ack => false}, &block)
19
+ pop_with_tracing { super }
20
+ end
21
+
22
+ def purge *args
23
+ purge_with_tracing { super }
24
+ end
25
+ end
26
+
27
+ module Consumer
28
+ include NewRelic::Agent::Instrumentation::Bunny::Consumer
29
+
30
+ def call *args
31
+ call_with_tracing(*args) { super }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -179,6 +179,7 @@ module NewRelic
179
179
  #{without_method_name}(*args, &block)
180
180
  end
181
181
  end
182
+ ruby2_keywords(:#{with_method_name}) if respond_to?(:ruby2_keywords, true)
182
183
  EOC
183
184
 
184
185
  visibility = NewRelic::Helper.instance_method_visibility self, method
@@ -2,6 +2,9 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
4
 
5
+ require_relative 'curb/chain'
6
+ require_relative 'curb/prepend'
7
+
5
8
  DependencyDetection.defer do
6
9
  named :curb
7
10
 
@@ -19,246 +22,11 @@ DependencyDetection.defer do
19
22
  end
20
23
 
21
24
  executes do
22
- class Curl::Easy
23
-
24
- attr_accessor :_nr_instrumented,
25
- :_nr_failure_instrumented,
26
- :_nr_http_verb,
27
- :_nr_header_str,
28
- :_nr_original_on_header,
29
- :_nr_original_on_complete,
30
- :_nr_original_on_failure,
31
- :_nr_serial
32
-
33
- # We have to hook these three methods separately, as they don't use
34
- # Curl::Easy#http
35
- def http_head_with_newrelic(*args, &blk)
36
- self._nr_http_verb = :HEAD
37
- http_head_without_newrelic(*args, &blk)
38
- end
39
-
40
-
41
- alias_method :http_head_without_newrelic, :http_head
42
- alias_method :http_head, :http_head_with_newrelic
43
-
44
- def http_post_with_newrelic(*args, &blk)
45
- self._nr_http_verb = :POST
46
- http_post_without_newrelic(*args, &blk)
47
- end
48
-
49
-
50
- alias_method :http_post_without_newrelic, :http_post
51
- alias_method :http_post, :http_post_with_newrelic
52
-
53
- def http_put_with_newrelic(*args, &blk)
54
- self._nr_http_verb = :PUT
55
- http_put_without_newrelic(*args, &blk)
56
- end
57
-
58
-
59
- alias_method :http_put_without_newrelic, :http_put
60
- alias_method :http_put, :http_put_with_newrelic
61
-
62
-
63
- # Hook the #http method to set the verb.
64
- def http_with_newrelic verb
65
- self._nr_http_verb = verb.to_s.upcase
66
- http_without_newrelic( verb )
67
- end
68
-
69
- alias_method :http_without_newrelic, :http
70
- alias_method :http, :http_with_newrelic
71
-
72
- # Hook the #perform method to mark the request as non-parallel.
73
- def perform_with_newrelic
74
- self._nr_http_verb ||= :GET
75
- self._nr_serial = true
76
- perform_without_newrelic
77
- end
78
-
79
- alias_method :perform_without_newrelic, :perform
80
- alias_method :perform, :perform_with_newrelic
81
-
82
- # Record the HTTP verb for future #perform calls
83
- def method_with_newrelic verb
84
- self._nr_http_verb = verb.upcase
85
- method_without_newrelic(verb)
86
- end
87
-
88
- alias_method :method_without_newrelic, :method
89
- alias_method :method, :method_with_newrelic
90
-
91
- # We override this method in order to ensure access to header_str even
92
- # though we use an on_header callback
93
- def header_str_with_newrelic
94
- if self._nr_serial
95
- self._nr_header_str
96
- else
97
- # Since we didn't install a header callback for a non-serial request,
98
- # just fall back to the original implementation.
99
- header_str_without_newrelic
100
- end
101
- end
102
-
103
- alias_method :header_str_without_newrelic, :header_str
104
- alias_method :header_str, :header_str_with_newrelic
105
- end # class Curl::Easy
106
-
107
-
108
- class Curl::Multi
109
- include NewRelic::Agent::MethodTracer
110
-
111
- # Add CAT with callbacks if the request is serial
112
- def add_with_newrelic(curl)
113
- if curl.respond_to?(:_nr_serial) && curl._nr_serial
114
- hook_pending_request(curl) if NewRelic::Agent::Tracer.tracing_enabled?
115
- end
116
-
117
- return add_without_newrelic curl
118
- end
119
-
120
- alias_method :add_without_newrelic, :add
121
- alias_method :add, :add_with_newrelic
122
-
123
- # Trace as an External/Multiple call if the first request isn't serial.
124
- def perform_with_newrelic(&blk)
125
- return perform_without_newrelic if first_request_is_serial?
126
-
127
- trace_execution_scoped("External/Multiple/Curb::Multi/perform") do
128
- perform_without_newrelic(&blk)
129
- end
130
- end
131
-
132
- alias_method :perform_without_newrelic, :perform
133
- alias_method :perform, :perform_with_newrelic
134
-
135
-
136
- # Instrument the specified +request+ (a Curl::Easy object)
137
- # and set up cross-application tracing if it's enabled.
138
- def hook_pending_request(request)
139
- wrapped_request, wrapped_response = wrap_request(request)
140
-
141
- segment = NewRelic::Agent::Tracer.start_external_request_segment(
142
- library: wrapped_request.type,
143
- uri: wrapped_request.uri,
144
- procedure: wrapped_request.method
145
- )
146
-
147
- segment.add_request_headers wrapped_request
148
-
149
- # install all callbacks
150
- unless request._nr_instrumented
151
- install_header_callback(request, wrapped_response)
152
- install_completion_callback(request, wrapped_response, segment)
153
- install_failure_callback(request, wrapped_response, segment)
154
- request._nr_instrumented = true
155
- end
156
- rescue => err
157
- NewRelic::Agent.logger.error("Untrapped exception", err)
158
- end
159
-
160
-
161
- # Create request and response adapter objects for the specified +request+
162
- # NOTE: Although strange to wrap request and response at once, it works
163
- # because curb's callback mechanism updates the instantiated wrappers
164
- # during the life-cycle of external request
165
- def wrap_request(request)
166
- return NewRelic::Agent::HTTPClients::CurbRequest.new(request),
167
- NewRelic::Agent::HTTPClients::CurbResponse.new(request)
168
- end
169
-
170
- # Install a callback that will record the response headers
171
- # to enable CAT linking
172
- def install_header_callback(request, wrapped_response)
173
- original_callback = request.on_header
174
- request._nr_original_on_header = original_callback
175
- request._nr_header_str = nil
176
- request.on_header do |header_data|
177
- if original_callback
178
- original_callback.call header_data
179
- else
180
- wrapped_response.append_header_data header_data
181
- header_data.length
182
- end
183
- end
184
- end
185
-
186
- # Install a callback that will finish the trace.
187
- def install_completion_callback(request, wrapped_response, segment)
188
- original_callback = request.on_complete
189
- request._nr_original_on_complete = original_callback
190
- request.on_complete do |finished_request|
191
- begin
192
- segment.process_response_headers wrapped_response
193
- ensure
194
- segment.finish if segment
195
- # Make sure the existing completion callback is run, and restore the
196
- # on_complete callback to how it was before.
197
- original_callback.call(finished_request) if original_callback
198
- remove_instrumentation_callbacks(request)
199
- end
200
- end
201
- end
202
-
203
- # Install a callback that will fire on failures
204
- # NOTE: on_failure is not always called, so we're not always
205
- # unhooking the callback. No harm/no foul in production, but
206
- # definitely something to beware of if debugging callback issues
207
- # _nr_failure_instrumented exists to prevent infinitely chaining
208
- # our on_failure callback hook.
209
- def install_failure_callback(request, wrapped_response, segment)
210
- return if request._nr_failure_instrumented
211
- original_callback = request.on_failure
212
- request._nr_original_on_failure = original_callback
213
- request.on_failure do |failed_request, error|
214
- begin
215
- if segment
216
- noticible_error = NewRelic::Agent::NoticibleError.new error[0].name, error[-1]
217
- segment.notice_error noticible_error
218
- end
219
- ensure
220
- original_callback.call(failed_request, error) if original_callback
221
- remove_failure_callback(failed_request)
222
- end
223
- request._nr_failure_instrumented = true
224
- end
225
- end
226
-
227
- # on_failure callbacks cannot be removed in the on_complete
228
- # callback where this method is invoked because on_complete
229
- # fires before the on_failure!
230
- def remove_instrumentation_callbacks(request)
231
- request.on_complete(&request._nr_original_on_complete)
232
- request.on_header(&request._nr_original_on_header)
233
- request._nr_instrumented = false
234
- end
235
-
236
- # We execute customer's on_failure callback (if any) and
237
- # uninstall our hook here since the on_complete callback
238
- # fires before the on_failure callback.
239
- def remove_failure_callback(request)
240
- request.on_failure(&request._nr_original_on_failure)
241
- request._nr_failure_instrumented = false
242
- end
243
-
244
- private
245
-
246
- def first_request_is_serial?
247
- return false unless (first = self.requests.first)
248
-
249
- # Before curb 0.9.8, requests was an array of Curl::Easy
250
- # instances. Starting with 0.9.8, it's a Hash where the
251
- # values are Curl::Easy instances.
252
- #
253
- # So, requests.first will either be an_obj or [a_key, an_obj].
254
- # We need to handle either case.
255
- #
256
- first = first[-1] if first.is_a?(Array)
257
-
258
- first.respond_to?(:_nr_serial) && first._nr_serial
259
- end
260
-
261
- end # class Curl::Multi
262
-
25
+ if use_prepend?
26
+ prepend_instrument ::Curl::Easy, NewRelic::Agent::Instrumentation::Curb::Easy::Prepend
27
+ prepend_instrument ::Curl::Multi, NewRelic::Agent::Instrumentation::Curb::Multi::Prepend
28
+ else
29
+ chain_instrument ::NewRelic::Agent::Instrumentation::Curb::Chain
30
+ end
263
31
  end
264
32
  end