newrelic_rpm 6.15.0 → 8.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/CHANGELOG.md +260 -22
  4. data/CONTRIBUTING.md +13 -2
  5. data/README.md +4 -2
  6. data/lib/new_relic/agent/adaptive_sampler.rb +2 -2
  7. data/lib/new_relic/agent/agent.rb +8 -7
  8. data/lib/new_relic/agent/autostart.rb +1 -2
  9. data/lib/new_relic/agent/commands/thread_profiler_session.rb +7 -3
  10. data/lib/new_relic/agent/configuration/default_source.rb +456 -233
  11. data/lib/new_relic/agent/configuration/event_harvest_config.rb +28 -12
  12. data/lib/new_relic/agent/configuration/manager.rb +3 -4
  13. data/lib/new_relic/agent/configuration/server_source.rb +3 -2
  14. data/lib/new_relic/agent/configuration/yaml_source.rb +22 -1
  15. data/lib/new_relic/agent/connect/request_builder.rb +4 -2
  16. data/lib/new_relic/agent/custom_event_aggregator.rb +2 -1
  17. data/lib/new_relic/agent/database.rb +5 -2
  18. data/lib/new_relic/agent/datastores/mongo.rb +5 -10
  19. data/lib/new_relic/agent/datastores/redis.rb +0 -4
  20. data/lib/new_relic/agent/datastores.rb +7 -7
  21. data/lib/new_relic/agent/distributed_tracing/cross_app_payload.rb +5 -5
  22. data/lib/new_relic/agent/distributed_tracing/cross_app_tracing.rb +10 -6
  23. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +0 -1
  24. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +1 -1
  25. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +1 -1
  26. data/lib/new_relic/agent/distributed_tracing.rb +0 -66
  27. data/lib/new_relic/agent/error_collector.rb +52 -37
  28. data/lib/new_relic/agent/error_filter.rb +167 -0
  29. data/lib/new_relic/agent/event_loop.rb +6 -6
  30. data/lib/new_relic/agent/external.rb +0 -32
  31. data/lib/new_relic/agent/http_clients/abstract.rb +2 -2
  32. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +1 -1
  33. data/lib/new_relic/agent/instrumentation/active_merchant.rb +3 -3
  34. data/lib/new_relic/agent/instrumentation/active_record_notifications.rb +0 -16
  35. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +1 -3
  36. data/lib/new_relic/agent/instrumentation/bunny/chain.rb +45 -0
  37. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +152 -0
  38. data/lib/new_relic/agent/instrumentation/bunny/prepend.rb +35 -0
  39. data/lib/new_relic/agent/instrumentation/bunny.rb +10 -152
  40. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +2 -1
  41. data/lib/new_relic/agent/instrumentation/curb/chain.rb +93 -0
  42. data/lib/new_relic/agent/instrumentation/curb/instrumentation.rb +222 -0
  43. data/lib/new_relic/agent/instrumentation/curb/prepend.rb +63 -0
  44. data/lib/new_relic/agent/instrumentation/curb.rb +9 -241
  45. data/lib/new_relic/agent/instrumentation/delayed_job/chain.rb +38 -0
  46. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +53 -0
  47. data/lib/new_relic/agent/instrumentation/delayed_job/prepend.rb +34 -0
  48. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +8 -50
  49. data/lib/new_relic/agent/instrumentation/excon.rb +4 -22
  50. data/lib/new_relic/agent/instrumentation/grape/chain.rb +25 -0
  51. data/lib/new_relic/agent/instrumentation/grape/instrumentation.rb +100 -0
  52. data/lib/new_relic/agent/instrumentation/grape/prepend.rb +17 -0
  53. data/lib/new_relic/agent/instrumentation/grape.rb +13 -113
  54. data/lib/new_relic/agent/instrumentation/httpclient/chain.rb +25 -0
  55. data/lib/new_relic/agent/instrumentation/httpclient/instrumentation.rb +38 -0
  56. data/lib/new_relic/agent/instrumentation/httpclient/prepend.rb +17 -0
  57. data/lib/new_relic/agent/instrumentation/httpclient.rb +8 -30
  58. data/lib/new_relic/agent/instrumentation/httprb/chain.rb +22 -0
  59. data/lib/new_relic/agent/instrumentation/httprb/instrumentation.rb +30 -0
  60. data/lib/new_relic/agent/instrumentation/httprb/prepend.rb +15 -0
  61. data/lib/new_relic/agent/instrumentation/httprb.rb +29 -0
  62. data/lib/new_relic/agent/instrumentation/memcache/chain.rb +16 -0
  63. data/lib/new_relic/agent/instrumentation/memcache/dalli.rb +38 -121
  64. data/lib/new_relic/agent/instrumentation/memcache/helper.rb +56 -0
  65. data/lib/new_relic/agent/instrumentation/memcache/instrumentation.rb +88 -0
  66. data/lib/new_relic/agent/instrumentation/memcache/prepend.rb +88 -0
  67. data/lib/new_relic/agent/instrumentation/memcache.rb +54 -69
  68. data/lib/new_relic/agent/instrumentation/middleware_proxy.rb +2 -0
  69. data/lib/new_relic/agent/instrumentation/mongo.rb +3 -134
  70. data/lib/new_relic/agent/instrumentation/net_http/chain.rb +25 -0
  71. data/lib/new_relic/agent/instrumentation/{net_prepend.rb → net_http/instrumentation.rb} +3 -3
  72. data/lib/new_relic/agent/instrumentation/net_http/prepend.rb +21 -0
  73. data/lib/new_relic/agent/instrumentation/net_http.rb +44 -0
  74. data/lib/new_relic/agent/instrumentation/padrino/chain.rb +34 -0
  75. data/lib/new_relic/agent/instrumentation/padrino/instrumentation.rb +27 -0
  76. data/lib/new_relic/agent/instrumentation/padrino/prepend.rb +20 -0
  77. data/lib/new_relic/agent/instrumentation/padrino.rb +18 -53
  78. data/lib/new_relic/agent/instrumentation/queue_time.rb +4 -4
  79. data/lib/new_relic/agent/instrumentation/rack/chain.rb +58 -0
  80. data/lib/new_relic/agent/instrumentation/rack/helpers.rb +32 -0
  81. data/lib/new_relic/agent/instrumentation/rack/instrumentation.rb +73 -0
  82. data/lib/new_relic/agent/instrumentation/rack/prepend.rb +37 -0
  83. data/lib/new_relic/agent/instrumentation/rack.rb +29 -139
  84. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +5 -5
  85. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +1 -1
  86. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +5 -41
  87. data/lib/new_relic/agent/instrumentation/rake/chain.rb +25 -0
  88. data/lib/new_relic/agent/instrumentation/rake/instrumentation.rb +144 -0
  89. data/lib/new_relic/agent/instrumentation/rake/prepend.rb +14 -0
  90. data/lib/new_relic/agent/instrumentation/rake.rb +13 -154
  91. data/lib/new_relic/agent/instrumentation/redis/chain.rb +34 -0
  92. data/lib/new_relic/agent/instrumentation/redis/instrumentation.rb +65 -0
  93. data/lib/new_relic/agent/instrumentation/redis/prepend.rb +24 -0
  94. data/lib/new_relic/agent/instrumentation/redis.rb +12 -107
  95. data/lib/new_relic/agent/instrumentation/resque/chain.rb +22 -0
  96. data/lib/new_relic/agent/instrumentation/resque/helper.rb +19 -0
  97. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +35 -0
  98. data/lib/new_relic/agent/instrumentation/resque/prepend.rb +16 -0
  99. data/lib/new_relic/agent/instrumentation/resque.rb +21 -32
  100. data/lib/new_relic/agent/instrumentation/sidekiq.rb +6 -1
  101. data/lib/new_relic/agent/instrumentation/sinatra/chain.rb +55 -0
  102. data/lib/new_relic/agent/instrumentation/sinatra/ignorer.rb +29 -34
  103. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +124 -0
  104. data/lib/new_relic/agent/instrumentation/sinatra/prepend.rb +33 -0
  105. data/lib/new_relic/agent/instrumentation/sinatra.rb +20 -158
  106. data/lib/new_relic/agent/instrumentation/typhoeus/chain.rb +22 -0
  107. data/lib/new_relic/agent/instrumentation/typhoeus/instrumentation.rb +82 -0
  108. data/lib/new_relic/agent/instrumentation/typhoeus/prepend.rb +14 -0
  109. data/lib/new_relic/agent/instrumentation/typhoeus.rb +10 -89
  110. data/lib/new_relic/agent/javascript_instrumentor.rb +15 -10
  111. data/lib/new_relic/agent/messaging.rb +10 -24
  112. data/lib/new_relic/agent/method_tracer.rb +132 -138
  113. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +6 -1
  114. data/lib/new_relic/agent/new_relic_service.rb +24 -22
  115. data/lib/new_relic/agent/pipe_channel_manager.rb +10 -6
  116. data/lib/new_relic/agent/pipe_service.rb +1 -1
  117. data/lib/new_relic/agent/samplers/cpu_sampler.rb +1 -1
  118. data/lib/new_relic/agent/span_event_aggregator.rb +2 -2
  119. data/lib/new_relic/agent/sql_sampler.rb +4 -4
  120. data/lib/new_relic/agent/stats_engine/stats_hash.rb +1 -1
  121. data/lib/new_relic/agent/stats_engine.rb +1 -1
  122. data/lib/new_relic/agent/supported_versions.rb +1 -1
  123. data/lib/new_relic/agent/threading/backtrace_service.rb +4 -5
  124. data/lib/new_relic/agent/threading/thread_profile.rb +1 -1
  125. data/lib/new_relic/agent/tracer.rb +15 -37
  126. data/lib/new_relic/agent/transaction/abstract_segment.rb +3 -3
  127. data/lib/new_relic/agent/transaction/message_broker_segment.rb +5 -11
  128. data/lib/new_relic/agent/transaction.rb +8 -32
  129. data/lib/new_relic/agent/transaction_time_aggregator.rb +5 -5
  130. data/lib/new_relic/agent/vm/snapshot.rb +1 -1
  131. data/lib/new_relic/agent/worker_loop.rb +5 -5
  132. data/lib/new_relic/agent.rb +10 -13
  133. data/lib/new_relic/cli/commands/deployments.rb +2 -2
  134. data/lib/new_relic/constants.rb +0 -4
  135. data/lib/new_relic/control/frameworks/rails.rb +11 -9
  136. data/lib/new_relic/control/instance_methods.rb +1 -0
  137. data/lib/new_relic/dependency_detection.rb +116 -10
  138. data/lib/new_relic/noticed_error.rb +5 -9
  139. data/lib/new_relic/supportability_helper.rb +1 -2
  140. data/lib/new_relic/version.rb +2 -2
  141. data/lib/newrelic_rpm.rb +10 -34
  142. data/lib/tasks/all.rb +1 -1
  143. data/lib/tasks/config.html.erb +14 -25
  144. data/lib/tasks/config.rake +8 -7
  145. data/newrelic.yml +593 -3
  146. data/newrelic_rpm.gemspec +1 -1
  147. data/test/agent_helper.rb +27 -2
  148. metadata +56 -14
  149. data/cert/cacert.pem +0 -1177
  150. data/lib/new_relic/agent/datastores/mongo/statement_formatter.rb +0 -53
  151. data/lib/new_relic/agent/instrumentation/excon/connection.rb +0 -49
  152. data/lib/new_relic/agent/instrumentation/http.rb +0 -49
  153. data/lib/new_relic/agent/instrumentation/merb/controller.rb +0 -44
  154. data/lib/new_relic/agent/instrumentation/merb/errors.rb +0 -33
  155. data/lib/new_relic/agent/instrumentation/net.rb +0 -70
  156. data/lib/new_relic/control/frameworks/merb.rb +0 -29
@@ -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
@@ -0,0 +1,38 @@
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
+ require_relative 'instrumentation'
6
+
7
+ module NewRelic::Agent::Instrumentation
8
+ module DelayedJob
9
+ module Chain
10
+ def self.instrument!
11
+ Delayed::Worker.class_eval do
12
+ include NewRelic::Agent::Instrumentation::DelayedJob
13
+
14
+ def initialize_with_new_relic(*args)
15
+ initialize_with_tracing { initialize_without_new_relic(*args) }
16
+ end
17
+
18
+ alias initialize_without_new_relic initialize
19
+ alias initialize initialize_with_new_relic
20
+
21
+
22
+ def install_newrelic_job_tracer
23
+ Delayed::Job.class_eval do
24
+ include NewRelic::Agent::Instrumentation::DelayedJobTracer
25
+
26
+ alias_method :invoke_job_without_new_relic, :invoke_job
27
+
28
+ def invoke_job(*args, &block)
29
+ invoke_job_with_tracing { invoke_job_without_new_relic(*args, &block) }
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,53 @@
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
+
7
+ module NewRelic
8
+ module Agent
9
+ module Instrumentation
10
+ module DelayedJob
11
+
12
+ def initialize_with_tracing
13
+ yield
14
+ worker_name = case
15
+ when self.respond_to?(:name) then self.name
16
+ when self.class.respond_to?(:default_name) then self.class.default_name
17
+ end
18
+ NewRelic::DelayedJobInjection.worker_name = worker_name
19
+
20
+ if defined?(::Delayed::Job) && ::Delayed::Job.method_defined?(:invoke_job) &&
21
+ !(::Delayed::Job.method_defined?(:invoke_job_without_new_relic) )
22
+
23
+ ::NewRelic::Agent.logger.info 'Installing DelayedJob instrumentation [part 2/2]'
24
+ install_newrelic_job_tracer
25
+ NewRelic::Control.instance.init_plugin :dispatcher => :delayed_job
26
+ else
27
+ NewRelic::Agent.logger.warn("Did not find a Delayed::Job class responding to invoke_job, aborting DJ instrumentation")
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ module DelayedJobTracer
34
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
35
+
36
+ NR_TRANSACTION_CATEGORY = 'OtherTransaction/DelayedJob'.freeze
37
+
38
+ def invoke_job_with_tracing
39
+ options = {
40
+ :category => NR_TRANSACTION_CATEGORY,
41
+ :path => ::NewRelic::Agent::Instrumentation::DelayedJob::Naming.name_from_payload(payload_object)
42
+ }
43
+
44
+ perform_action_with_newrelic_trace(options) do
45
+ yield
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,34 @@
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
+ require_relative 'instrumentation'
7
+
8
+ module NewRelic
9
+ module Agent
10
+ module Instrumentation
11
+ module DelayedJob
12
+ module Prepend
13
+ include NewRelic::Agent::Instrumentation::DelayedJob
14
+
15
+ def initialize(*args)
16
+ initialize_with_tracing { super }
17
+ end
18
+
19
+ def install_newrelic_job_tracer
20
+ Delayed::Job.send :prepend, ::NewRelic::Agent::Instrumentation::DelayedJobTracerPrepend
21
+ end
22
+ end
23
+ end
24
+
25
+ module DelayedJobTracerPrepend
26
+ include NewRelic::Agent::Instrumentation::DelayedJobTracer
27
+
28
+ def invoke_job(*args, &block)
29
+ invoke_job_with_tracing { super }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -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 'delayed_job/chain'
6
+ require_relative 'delayed_job/prepend'
7
+
5
8
  require 'new_relic/agent/instrumentation/controller_instrumentation'
6
9
 
7
10
  module NewRelic
@@ -76,11 +79,7 @@ DependencyDetection.defer do
76
79
  @name = :delayed_job
77
80
 
78
81
  depends_on do
79
- !NewRelic::Agent.config[:disable_dj]
80
- end
81
-
82
- depends_on do
83
- defined?(::Delayed) && defined?(::Delayed::Worker) && !NewRelic::Agent.config[:disable_dj]
82
+ defined?(::Delayed) && defined?(::Delayed::Worker)
84
83
  end
85
84
 
86
85
  executes do
@@ -88,51 +87,10 @@ DependencyDetection.defer do
88
87
  end
89
88
 
90
89
  executes do
91
- Delayed::Worker.class_eval do
92
-
93
- def initialize_with_new_relic(*args)
94
- initialize_without_new_relic(*args)
95
- worker_name = case
96
- when self.respond_to?(:name) then self.name
97
- when self.class.respond_to?(:default_name) then self.class.default_name
98
- end
99
- NewRelic::DelayedJobInjection.worker_name = worker_name
100
-
101
- if defined?(::Delayed::Job) && ::Delayed::Job.method_defined?(:invoke_job) &&
102
- !(::Delayed::Job.method_defined?(:invoke_job_without_new_relic) )
103
-
104
- ::NewRelic::Agent.logger.info 'Installing DelayedJob instrumentation [part 2/2]'
105
- install_newrelic_job_tracer
106
- NewRelic::Control.instance.init_plugin :dispatcher => :delayed_job
107
- else
108
- NewRelic::Agent.logger.warn("Did not find a Delayed::Job class responding to invoke_job, aborting DJ instrumentation")
109
- end
110
- end
111
-
112
- alias initialize_without_new_relic initialize
113
- alias initialize initialize_with_new_relic
114
-
115
- NR_TRANSACTION_CATEGORY = 'OtherTransaction/DelayedJob'.freeze
116
-
117
- def install_newrelic_job_tracer
118
- Delayed::Job.class_eval do
119
- include NewRelic::Agent::Instrumentation::ControllerInstrumentation
120
-
121
- alias_method :invoke_job_without_new_relic, :invoke_job
122
-
123
- def invoke_job(*args, &block)
124
- options = {
125
- :category => NR_TRANSACTION_CATEGORY,
126
- :path => ::NewRelic::Agent::Instrumentation::DelayedJob::Naming.name_from_payload(payload_object)
127
- }
128
-
129
- perform_action_with_newrelic_trace(options) do
130
- invoke_job_without_new_relic(*args, &block)
131
- end
132
- end
133
-
134
- end
135
- end
90
+ if use_prepend?
91
+ prepend_instrument ::Delayed::Worker, ::NewRelic::Agent::Instrumentation::DelayedJob::Prepend
92
+ else
93
+ chain_instrument ::NewRelic::Agent::Instrumentation::DelayedJob::Chain
136
94
  end
137
95
  end
138
96
  end
@@ -5,17 +5,9 @@
5
5
  DependencyDetection.defer do
6
6
  named :excon
7
7
 
8
- # We have two ways of instrumenting Excon:
9
- # - For newer versions, use the middleware mechanism Excon exposes
10
- # - For older versions, monkey-patch Excon::Connection#request
8
+ # We instrument Excon 0.19.0 and above using the middleware mechanism
11
9
  #
12
10
  # EXCON_MIN_VERSION is the minimum version we attempt to instrument at all.
13
- # EXCON_MIDDLEWARE_MIN_VERSION is the min version we use the newer
14
- # instrumentation for.
15
- #
16
- # Note that middlewares were added to Excon prior to 0.19, but we don't
17
- # use middleware-based instrumentation prior to that version because it didn't
18
- # expose a way for middlewares to know about request failures.
19
11
  #
20
12
  # Why don't we use Excon.defaults[:instrumentor]?
21
13
  # While this might seem a perfect fit, it unfortunately isn't suitable in
@@ -25,8 +17,7 @@ DependencyDetection.defer do
25
17
  # so we could safely subscribe and not be clobbered by future subscribers,
26
18
  # but alas, it does not yet.
27
19
 
28
- EXCON_MIN_VERSION = Gem::Version.new("0.10.1")
29
- EXCON_MIDDLEWARE_MIN_VERSION = Gem::Version.new("0.19.0")
20
+ EXCON_MIN_VERSION = Gem::Version.new("0.19.0")
30
21
 
31
22
  depends_on do
32
23
  defined?(::Excon) && defined?(::Excon::VERSION)
@@ -45,11 +36,7 @@ DependencyDetection.defer do
45
36
  require 'new_relic/agent/distributed_tracing/cross_app_tracing'
46
37
  require 'new_relic/agent/http_clients/excon_wrappers'
47
38
 
48
- if excon_version >= EXCON_MIDDLEWARE_MIN_VERSION
49
- install_middleware_excon_instrumentation
50
- else
51
- install_legacy_excon_instrumentation
52
- end
39
+ install_middleware_excon_instrumentation
53
40
  end
54
41
 
55
42
  def install_middleware_excon_instrumentation
@@ -63,10 +50,5 @@ DependencyDetection.defer do
63
50
  ::NewRelic::Agent.logger.warn("Did not find :middlewares key in Excon.defaults, skipping Excon instrumentation")
64
51
  end
65
52
  end
66
-
67
- def install_legacy_excon_instrumentation
68
- ::NewRelic::Agent.logger.info 'Installing legacy Excon instrumentation'
69
- require 'new_relic/agent/instrumentation/excon/connection'
70
- ::Excon::Connection.install_newrelic_instrumentation
71
- end
72
53
  end
54
+
@@ -0,0 +1,25 @@
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 Grape
7
+ module Chain
8
+ def self.instrument!
9
+ Grape::Instrumentation.instrumented_class.class_eval do
10
+ def call_with_new_relic env
11
+ begin
12
+ call_without_new_relic env
13
+ ensure
14
+ Grape::Instrumentation.capture_transaction env, self
15
+ end
16
+ end
17
+
18
+ alias_method :call_without_new_relic, :call
19
+ alias_method :call, :call_with_new_relic
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,100 @@
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
+ require 'new_relic/agent/parameter_filtering'
6
+
7
+ module NewRelic::Agent::Instrumentation
8
+ module Grape
9
+ module Instrumentation
10
+
11
+ extend self
12
+
13
+ # 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`
14
+ # https://github.com/ruby-grape/grape/blob/c20a73ac1e3f3ba1082005ed61bf69452373ba87/UPGRADING.md#upgrading-to--120
15
+ def instrumented_class
16
+ defined?(::Grape::API::Instance) ? ::Grape::API::Instance : ::Grape::API
17
+ end
18
+
19
+ def capture_transaction env, context
20
+ begin
21
+ endpoint = env[API_ENDPOINT]
22
+ version = env[API_VERSION]
23
+
24
+ api_class = (context.class.respond_to?(:base) && context.class.base) || context.class
25
+ handle_transaction(endpoint, api_class.name, version)
26
+ rescue => e
27
+ ::NewRelic::Agent.logger.warn("Error in Grape instrumentation", e)
28
+ end
29
+ end
30
+
31
+ def prepare!
32
+ if defined?(::Grape::VERSION) && Gem::Version.new(::Grape::VERSION) >= Gem::Version.new("0.16.0")
33
+ send :remove_method, :name_for_transaction_deprecated
34
+ else
35
+ send :remove_method, :name_for_transaction
36
+ send :alias_method, :name_for_transaction, :name_for_transaction_deprecated
37
+ end
38
+ end
39
+
40
+ API_ENDPOINT = 'api.endpoint'.freeze
41
+ API_VERSION = 'api.version'.freeze
42
+ FORMAT_REGEX = /\(\/?\.[\:\w]*\)/.freeze # either :format (< 0.12.0) or .ext (>= 0.12.0)
43
+ VERSION_REGEX = /:version(\/|$)/.freeze
44
+ MIN_VERSION = Gem::Version.new("0.2.0")
45
+ PIPE_STRING = '|'.freeze
46
+
47
+ def handle_transaction(endpoint, class_name, version)
48
+ return unless endpoint && route = endpoint.route
49
+ name_transaction(route, class_name, version)
50
+ capture_params(endpoint)
51
+ end
52
+
53
+ def name_transaction(route, class_name, version)
54
+ txn_name = name_for_transaction(route, class_name, version)
55
+ segment_name = "Middleware/Grape/#{class_name}/call"
56
+ NewRelic::Agent::Transaction.set_default_transaction_name(txn_name, :grape)
57
+ txn = NewRelic::Agent::Transaction.tl_current
58
+ txn.segments.last.name = segment_name
59
+ end
60
+
61
+ def name_for_transaction(route, class_name, version)
62
+ action_name = route.path.sub(FORMAT_REGEX, NewRelic::EMPTY_STR)
63
+ method_name = route.request_method
64
+ version ||= route.version
65
+
66
+ # defaulting does not set rack.env['api.version'] and route.version may return Array
67
+ #
68
+ version = version.join(PIPE_STRING) if Array === version
69
+
70
+ if version
71
+ action_name = action_name.sub(VERSION_REGEX, NewRelic::EMPTY_STR)
72
+ "#{class_name}-#{version}#{action_name} (#{method_name})"
73
+ else
74
+ "#{class_name}#{action_name} (#{method_name})"
75
+ end
76
+ end
77
+
78
+ def name_for_transaction_deprecated(route, class_name, version)
79
+ action_name = route.route_path.sub(FORMAT_REGEX, NewRelic::EMPTY_STR)
80
+ method_name = route.route_method
81
+ version ||= route.route_version
82
+ if version
83
+ action_name = action_name.sub(VERSION_REGEX, NewRelic::EMPTY_STR)
84
+ "#{class_name}-#{version}#{action_name} (#{method_name})"
85
+ else
86
+ "#{class_name}#{action_name} (#{method_name})"
87
+ end
88
+ end
89
+
90
+ def capture_params(endpoint)
91
+ txn = ::NewRelic::Agent::Transaction.tl_current
92
+ env = endpoint.request.env
93
+ params = ::NewRelic::Agent::ParameterFiltering::apply_filters(env, endpoint.params)
94
+ params.delete("route_info")
95
+ txn.filtered_params = params
96
+ txn.merge_request_parameters(params)
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,17 @@
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 Grape
7
+ module Prepend
8
+ def call env
9
+ begin
10
+ super env
11
+ ensure
12
+ Grape::Instrumentation.capture_transaction env, self
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end