newrelic_rpm 9.5.0 → 9.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -6
  3. data/Rakefile +1 -1
  4. data/lib/new_relic/agent/configuration/default_source.rb +80 -33
  5. data/lib/new_relic/agent/http_clients/async_http_wrappers.rb +83 -0
  6. data/lib/new_relic/agent/http_clients/ethon_wrappers.rb +111 -0
  7. data/lib/new_relic/agent/http_clients/httpx_wrappers.rb +93 -0
  8. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +1 -2
  9. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/chain.rb +69 -0
  10. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/instrumentation.rb +13 -0
  11. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger/prepend.rb +37 -0
  12. data/lib/new_relic/agent/instrumentation/active_support_broadcast_logger.rb +23 -0
  13. data/lib/new_relic/agent/instrumentation/active_support_logger.rb +3 -1
  14. data/lib/new_relic/agent/instrumentation/async_http/chain.rb +23 -0
  15. data/lib/new_relic/agent/instrumentation/async_http/instrumentation.rb +37 -0
  16. data/lib/new_relic/agent/instrumentation/async_http/prepend.rb +15 -0
  17. data/lib/new_relic/agent/instrumentation/async_http.rb +26 -0
  18. data/lib/new_relic/agent/instrumentation/ethon/chain.rb +39 -0
  19. data/lib/new_relic/agent/instrumentation/ethon/instrumentation.rb +105 -0
  20. data/lib/new_relic/agent/instrumentation/ethon/prepend.rb +35 -0
  21. data/lib/new_relic/agent/instrumentation/ethon.rb +39 -0
  22. data/lib/new_relic/agent/instrumentation/httpx/chain.rb +20 -0
  23. data/lib/new_relic/agent/instrumentation/httpx/instrumentation.rb +51 -0
  24. data/lib/new_relic/agent/instrumentation/httpx/prepend.rb +15 -0
  25. data/lib/new_relic/agent/instrumentation/httpx.rb +27 -0
  26. data/lib/new_relic/agent/instrumentation/mongodb_command_subscriber.rb +1 -3
  27. data/lib/new_relic/agent/instrumentation/net_http/instrumentation.rb +1 -1
  28. data/lib/new_relic/agent/instrumentation/rails_notifications/action_controller.rb +1 -0
  29. data/lib/new_relic/agent/instrumentation/roda/ignorer.rb +45 -0
  30. data/lib/new_relic/agent/instrumentation/roda/instrumentation.rb +12 -0
  31. data/lib/new_relic/agent/instrumentation/roda/roda_transaction_namer.rb +1 -2
  32. data/lib/new_relic/agent/instrumentation/roda.rb +2 -0
  33. data/lib/new_relic/agent/instrumentation/sidekiq.rb +3 -1
  34. data/lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb +1 -3
  35. data/lib/new_relic/agent/messaging.rb +2 -2
  36. data/lib/new_relic/agent/monitors/synthetics_monitor.rb +12 -1
  37. data/lib/new_relic/agent/rules_engine.rb +1 -1
  38. data/lib/new_relic/agent/span_event_primitive.rb +16 -4
  39. data/lib/new_relic/agent/system_info.rb +26 -0
  40. data/lib/new_relic/agent/tracer.rb +1 -3
  41. data/lib/new_relic/agent/transaction/abstract_segment.rb +3 -0
  42. data/lib/new_relic/agent/transaction/external_request_segment.rb +5 -2
  43. data/lib/new_relic/agent/transaction/message_broker_segment.rb +1 -2
  44. data/lib/new_relic/agent/transaction/request_attributes.rb +1 -3
  45. data/lib/new_relic/agent/transaction.rb +25 -2
  46. data/lib/new_relic/agent/transaction_error_primitive.rb +16 -0
  47. data/lib/new_relic/agent/transaction_event_primitive.rb +19 -0
  48. data/lib/new_relic/agent/utilization/gcp.rb +1 -3
  49. data/lib/new_relic/agent.rb +6 -2
  50. data/lib/new_relic/constants.rb +3 -0
  51. data/lib/new_relic/control/frameworks/rails.rb +14 -2
  52. data/lib/new_relic/language_support.rb +4 -0
  53. data/lib/new_relic/version.rb +1 -1
  54. data/lib/tasks/instrumentation_generator/instrumentation.thor +3 -3
  55. data/lib/tasks/tests.rake +71 -0
  56. data/newrelic.yml +49 -33
  57. data/newrelic_rpm.gemspec +4 -1
  58. metadata +36 -2
@@ -0,0 +1,111 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require 'uri'
6
+ require_relative 'abstract'
7
+
8
+ module NewRelic
9
+ module Agent
10
+ module HTTPClients
11
+ class EthonHTTPResponse < AbstractResponse
12
+ def initialize(easy)
13
+ @easy = easy
14
+ end
15
+
16
+ def status_code
17
+ @easy.response_code
18
+ end
19
+
20
+ def [](key)
21
+ headers[format_key(key)]
22
+ end
23
+
24
+ def headers
25
+ # Ethon::Easy#response_headers will return '' if headers are unset
26
+ @easy.response_headers.scan(/\n([^:]+?): ([^:\n]+?)\r/).each_with_object({}) do |pair, hash|
27
+ hash[format_key(pair[0])] = pair[1]
28
+ end
29
+ end
30
+ alias to_hash headers
31
+
32
+ private
33
+
34
+ def format_key(key)
35
+ key.tr('-', '_').downcase
36
+ end
37
+ end
38
+
39
+ class EthonHTTPRequest < AbstractRequest
40
+ attr_reader :uri
41
+
42
+ DEFAULT_ACTION = 'GET'
43
+ DEFAULT_HOST = 'UNKNOWN_HOST'
44
+ ETHON = 'Ethon'
45
+ LHOST = 'host'.freeze
46
+ UHOST = 'Host'.freeze
47
+
48
+ def initialize(easy)
49
+ @easy = easy
50
+ @uri = uri_from_easy
51
+ end
52
+
53
+ def type
54
+ ETHON
55
+ end
56
+
57
+ def host_from_header
58
+ self[LHOST] || self[UHOST]
59
+ end
60
+
61
+ def uri_from_easy
62
+ # anticipate `Ethon::Easy#url` being `example.com` without a protocol
63
+ # defined and use an 'http' protocol prefix for `URI.parse` to work
64
+ # with the URL as desired
65
+ url_str = @easy.url.match?(':') ? @easy.url : "http://#{@easy.url}"
66
+ begin
67
+ URI.parse(url_str)
68
+ rescue URI::InvalidURIError => e
69
+ NewRelic::Agent.logger.debug("Failed to parse URI '#{url_str}': #{e.class} - #{e.message}")
70
+ URI.parse(NewRelic::EMPTY_STR)
71
+ end
72
+ end
73
+
74
+ def host
75
+ host_from_header || uri.host&.downcase || DEFAULT_HOST
76
+ end
77
+
78
+ def method
79
+ return DEFAULT_ACTION unless @easy.instance_variable_defined?(action_instance_var)
80
+
81
+ @easy.instance_variable_get(action_instance_var)
82
+ end
83
+
84
+ def action_instance_var
85
+ NewRelic::Agent::Instrumentation::Ethon::Easy::ACTION_INSTANCE_VAR
86
+ end
87
+
88
+ def []=(key, value)
89
+ headers[key] = value
90
+ @easy.headers = headers
91
+ end
92
+
93
+ def headers
94
+ @headers ||= if @easy.instance_variable_defined?(headers_instance_var)
95
+ @easy.instance_variable_get(headers_instance_var)
96
+ else
97
+ {}
98
+ end
99
+ end
100
+
101
+ def headers_instance_var
102
+ NewRelic::Agent::Instrumentation::Ethon::Easy::HEADERS_INSTANCE_VAR
103
+ end
104
+
105
+ def [](key)
106
+ headers[key]
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,93 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'abstract'
6
+
7
+ module NewRelic
8
+ module Agent
9
+ module HTTPClients
10
+ # HTTPX returns an instance of HTTPX::ErrorResponse on error,
11
+ # and that instance itself yields the underlying HTTP response
12
+ # object via #response, but depending on the error that HTTP
13
+ # response object could be unset.
14
+ class HTTPXErrorResponse
15
+ def status; end
16
+ def headers; {}; end
17
+ end
18
+
19
+ class HTTPXHTTPResponse < AbstractResponse
20
+ def initialize(response)
21
+ if response.is_a?(::HTTPX::ErrorResponse)
22
+ @response = response.response || HTTPXErrorResponse.new
23
+ else
24
+ @response = response
25
+ end
26
+ end
27
+
28
+ def status_code
29
+ @response.status
30
+ end
31
+
32
+ def [](key)
33
+ headers[format_key(key)]
34
+ end
35
+
36
+ def headers
37
+ headers ||= @response.headers.to_hash.each_with_object({}) do |(k, v), h|
38
+ h[format_key(k)] = v
39
+ end
40
+ end
41
+ alias to_hash headers
42
+
43
+ private
44
+
45
+ def format_key(key)
46
+ key.tr('-', '_').downcase
47
+ end
48
+ end
49
+
50
+ class HTTPXHTTPRequest < AbstractRequest
51
+ attr_reader :uri
52
+
53
+ DEFAULT_HOST = 'UNKNOWN_HOST'
54
+ TYPE = 'HTTPX'
55
+ LHOST = 'host'.freeze
56
+ UHOST = 'Host'.freeze
57
+
58
+ def initialize(request)
59
+ @request = request
60
+ @uri = request.uri
61
+ end
62
+
63
+ def type
64
+ TYPE
65
+ end
66
+
67
+ def host_from_header
68
+ self[LHOST] || self[UHOST]
69
+ end
70
+
71
+ def host
72
+ host_from_header || uri.host&.downcase || DEFAULT_HOST
73
+ end
74
+
75
+ def method
76
+ @request.verb
77
+ end
78
+
79
+ def []=(key, value)
80
+ @request.headers[key] = value
81
+ end
82
+
83
+ def headers
84
+ @request.headers
85
+ end
86
+
87
+ def [](key)
88
+ @request.headers[key]
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -232,7 +232,6 @@ module NewRelic
232
232
 
233
233
  DEFAULT = 'default'.freeze
234
234
  UNKNOWN = 'unknown'.freeze
235
- SLASH = '/'.freeze
236
235
  LOCALHOST = 'localhost'.freeze
237
236
 
238
237
  def adapter_from_config(config)
@@ -288,7 +287,7 @@ module NewRelic
288
287
  private
289
288
 
290
289
  def postgres_unix_domain_socket_case?(host, adapter)
291
- adapter == :postgres && host && host.start_with?(SLASH)
290
+ adapter == :postgres && host && host.start_with?(NewRelic::SLASH)
292
291
  end
293
292
 
294
293
  def mysql_default_case?(config, adapter)
@@ -0,0 +1,69 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module ActiveSupportBroadcastLogger::Chain
7
+ def self.instrument!
8
+ ::ActiveSupportBroadcastLogger.class_eval do
9
+ include NewRelic::Agent::Instrumentation::ActiveSupportBroadcastLogger
10
+
11
+ alias_method(:add_without_new_relic, :add)
12
+
13
+ def add(*args, &task)
14
+ record_one_broadcast_with_new_relic(*args) do
15
+ add_without_new_relic(*args, &traced_task)
16
+ end
17
+ end
18
+
19
+ alias_method(:debug_without_new_relic, :debug)
20
+
21
+ def debug(*args, &task)
22
+ record_one_broadcast_with_new_relic(*args) do
23
+ debug_without_new_relic(*args, &traced_task)
24
+ end
25
+ end
26
+
27
+ alias_method(:info_without_new_relic, :info)
28
+
29
+ def info(*args, &task)
30
+ record_one_broadcast_with_new_relic(*args) do
31
+ info_without_new_relic(*args, &traced_task)
32
+ end
33
+ end
34
+
35
+ alias_method(:warn_without_new_relic, :warn)
36
+
37
+ def warn(*args, &task)
38
+ record_one_broadcast_with_new_relic(*args) do
39
+ warn_without_new_relic(*args, &traced_task)
40
+ end
41
+ end
42
+
43
+ alias_method(:error_without_new_relic, :error)
44
+
45
+ def error(*args, &task)
46
+ record_one_broadcast_with_new_relic(*args) do
47
+ error_without_new_relic(*args, &traced_task)
48
+ end
49
+ end
50
+
51
+ alias_method(:fatal_without_new_relic, :fatal)
52
+
53
+ def fatal(*args, &task)
54
+ record_one_broadcast_with_new_relic(*args) do
55
+ fatal_without_new_relic(*args, &traced_task)
56
+ end
57
+ end
58
+ end
59
+
60
+ alias_method(:unknown_without_new_relic, :unknown)
61
+
62
+ def unknown(*args, &task)
63
+ record_one_broadcast_with_new_relic(*args) do
64
+ unknown_without_new_relic(*args, &traced_task)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,13 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module ActiveSupportBroadcastLogger
7
+ def record_one_broadcast_with_new_relic(*args)
8
+ broadcasts[1..-1].each { |broadcasted_logger| broadcasted_logger.instance_variable_set(:@skip_instrumenting, true) }
9
+ yield
10
+ broadcasts.each { |broadcasted_logger| broadcasted_logger.instance_variable_set(:@skip_instrumenting, false) }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module ActiveSupportBroadcastLogger::Prepend
7
+ include NewRelic::Agent::Instrumentation::ActiveSupportBroadcastLogger
8
+
9
+ def add(*args)
10
+ record_one_broadcast_with_new_relic(*args) { super }
11
+ end
12
+
13
+ def debug(*args)
14
+ record_one_broadcast_with_new_relic(*args) { super }
15
+ end
16
+
17
+ def info(*args)
18
+ record_one_broadcast_with_new_relic(*args) { super }
19
+ end
20
+
21
+ def warn(*args)
22
+ record_one_broadcast_with_new_relic(*args) { super }
23
+ end
24
+
25
+ def error(*args)
26
+ record_one_broadcast_with_new_relic(*args) { super }
27
+ end
28
+
29
+ def fatal(*args)
30
+ record_one_broadcast_with_new_relic(*args) { super }
31
+ end
32
+
33
+ def unknown(*args)
34
+ record_one_broadcast_with_new_relic(*args) { super }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'active_support_broadcast_logger/instrumentation'
6
+ require_relative 'active_support_broadcast_logger/chain'
7
+ require_relative 'active_support_broadcast_logger/prepend'
8
+
9
+ DependencyDetection.defer do
10
+ named :'active_support_broadcast_logger'
11
+
12
+ depends_on { defined?(ActiveSupport::BroadcastLogger) }
13
+
14
+ executes do
15
+ NewRelic::Agent.logger.info('Installing ActiveSupport::BroadcastLogger instrumentation')
16
+
17
+ if use_prepend?
18
+ prepend_instrument ActiveSupport::BroadcastLogger, NewRelic::Agent::Instrumentation::ActiveSupportBroadcastLogger::Prepend
19
+ else
20
+ chain_instrument NewRelic::Agent::Instrumentation::ActiveSupportBroadcastLogger::Chain
21
+ end
22
+ end
23
+ end
@@ -9,7 +9,9 @@ require_relative 'active_support_logger/prepend'
9
9
  DependencyDetection.defer do
10
10
  named :active_support_logger
11
11
 
12
- depends_on { defined?(ActiveSupport::Logger) }
12
+ depends_on do
13
+ defined?(ActiveSupport::Logger) && defined?(ActiveSupport::Logger.broadcast)
14
+ end
13
15
 
14
16
  executes do
15
17
  NewRelic::Agent.logger.info('Installing ActiveSupport::Logger instrumentation')
@@ -0,0 +1,23 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'instrumentation'
6
+
7
+ module NewRelic::Agent::Instrumentation
8
+ module AsyncHttp::Chain
9
+ def self.instrument!
10
+ ::Async::HTTP::Internet.class_eval do
11
+ include NewRelic::Agent::Instrumentation::AsyncHttp
12
+
13
+ alias_method(:call_without_new_relic, :call)
14
+
15
+ def call(method, url, headers = nil, body = nil)
16
+ call_with_new_relic(method, url, headers, body) do |hdr|
17
+ call_without_new_relic(method, url, hdr, body)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require 'new_relic/agent/http_clients/async_http_wrappers'
6
+
7
+ module NewRelic::Agent::Instrumentation
8
+ module AsyncHttp
9
+ def call_with_new_relic(method, url, headers = nil, body = nil)
10
+ headers ||= {} # if it is nil, we need to make it a hash so we can insert headers
11
+ wrapped_request = NewRelic::Agent::HTTPClients::AsyncHTTPRequest.new(self, method, url, headers)
12
+
13
+ segment = NewRelic::Agent::Tracer.start_external_request_segment(
14
+ library: wrapped_request.type,
15
+ uri: wrapped_request.uri,
16
+ procedure: wrapped_request.method
17
+ )
18
+
19
+ begin
20
+ response = nil
21
+ segment.add_request_headers(wrapped_request)
22
+
23
+ NewRelic::Agent.disable_all_tracing do
24
+ response = NewRelic::Agent::Tracer.capture_segment_error(segment) do
25
+ yield(headers)
26
+ end
27
+ end
28
+
29
+ wrapped_response = NewRelic::Agent::HTTPClients::AsyncHTTPResponse.new(response)
30
+ segment.process_response_headers(wrapped_response)
31
+ response
32
+ ensure
33
+ segment&.finish
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'instrumentation'
6
+
7
+ module NewRelic::Agent::Instrumentation
8
+ module AsyncHttp::Prepend
9
+ include NewRelic::Agent::Instrumentation::AsyncHttp
10
+
11
+ def call(method, url, headers = nil, body = nil)
12
+ call_with_new_relic(method, url, headers, body) { |hdr| super(method, url, hdr, body) }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require_relative 'async_http/instrumentation'
6
+ require_relative 'async_http/chain'
7
+ require_relative 'async_http/prepend'
8
+
9
+ DependencyDetection.defer do
10
+ named :async_http
11
+
12
+ depends_on do
13
+ defined?(Async::HTTP) && Gem::Version.new(Async::HTTP::VERSION) >= Gem::Version.new('0.59.0')
14
+ end
15
+
16
+ executes do
17
+ NewRelic::Agent.logger.info('Installing async_http instrumentation')
18
+
19
+ require 'async/http/internet'
20
+ if use_prepend?
21
+ prepend_instrument Async::HTTP::Internet, NewRelic::Agent::Instrumentation::AsyncHttp::Prepend
22
+ else
23
+ chain_instrument NewRelic::Agent::Instrumentation::AsyncHttp::Chain
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,39 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module Ethon
7
+ module Chain
8
+ def self.instrument!
9
+ ::Ethon::Easy.class_eval do
10
+ include NewRelic::Agent::Instrumentation::Ethon::Easy
11
+
12
+ alias_method(:fabricate_without_tracing, :fabricate)
13
+ def fabricate(url, action_name, options)
14
+ fabricate_with_tracing(url, action_name, options) { fabricate_without_tracing(url, action_name, options) }
15
+ end
16
+
17
+ alias_method(:headers_equals_without_tracing, :headers=)
18
+ def headers=(headers)
19
+ headers_equals_with_tracing(headers) { headers_equals_without_tracing(headers) }
20
+ end
21
+
22
+ alias_method(:perform_without_tracing, :perform)
23
+ def perform(*args)
24
+ perform_with_tracing(*args) { perform_without_tracing(*args) }
25
+ end
26
+ end
27
+
28
+ ::Ethon::Multi.class_eval do
29
+ include NewRelic::Agent::Instrumentation::Ethon::Multi
30
+
31
+ alias_method(:perform_without_tracing, :perform)
32
+ def perform(*args)
33
+ perform_with_tracing(*args) { perform_without_tracing(*args) }
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,105 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ require 'new_relic/agent/http_clients/ethon_wrappers'
6
+
7
+ module NewRelic::Agent::Instrumentation
8
+ module Ethon
9
+ module NRShared
10
+ INSTRUMENTATION_NAME = 'Ethon'
11
+ NOTICEABLE_ERROR_CLASS = 'Ethon::Errors::EthonError'
12
+
13
+ def prep_easy(easy, parent = nil)
14
+ wrapped_request = NewRelic::Agent::HTTPClients::EthonHTTPRequest.new(easy)
15
+ segment = NewRelic::Agent::Tracer.start_external_request_segment(
16
+ library: wrapped_request.type,
17
+ uri: wrapped_request.uri,
18
+ procedure: wrapped_request.method,
19
+ parent: parent
20
+ )
21
+ segment.add_request_headers(wrapped_request)
22
+
23
+ callback = proc do
24
+ wrapped_response = NewRelic::Agent::HTTPClients::EthonHTTPResponse.new(easy)
25
+ segment.process_response_headers(wrapped_response)
26
+
27
+ if easy.return_code != :ok
28
+ e = NewRelic::Agent::NoticeableError.new(NOTICEABLE_ERROR_CLASS,
29
+ "return_code: >>#{easy.return_code}<<, response_code: >>#{easy.response_code}<<")
30
+ segment.notice_error(e)
31
+ end
32
+
33
+ ::NewRelic::Agent::Transaction::Segment.finish(segment)
34
+ end
35
+
36
+ easy.on_complete { callback.call }
37
+
38
+ segment
39
+ end
40
+
41
+ def wrap_with_tracing(segment, &block)
42
+ NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)
43
+
44
+ NewRelic::Agent::Tracer.capture_segment_error(segment) do
45
+ yield
46
+ end
47
+ ensure
48
+ NewRelic::Agent::Transaction::Segment.finish(segment)
49
+ end
50
+ end
51
+
52
+ module Easy
53
+ include NRShared
54
+
55
+ ACTION_INSTANCE_VAR = :@nr_action
56
+ HEADERS_INSTANCE_VAR = :@nr_headers
57
+
58
+ # `Ethon::Easy` doesn't expose the "action name" ('GET', 'POST', etc.)
59
+ # and Ethon's fabrication of HTTP classes uses
60
+ # `Ethon::Easy::Http::Custom` for non-standard actions. To be able to
61
+ # know the action name at `#perform` time, we set a new instance variable
62
+ # on the `Ethon::Easy` instance with the base name of the fabricated
63
+ # class, respecting the 'Custom' name where appropriate.
64
+ def fabricate_with_tracing(_url, action_name, _options)
65
+ fabbed = yield
66
+ instance_variable_set(ACTION_INSTANCE_VAR, NewRelic::Agent.base_name(fabbed.class.name).upcase)
67
+ fabbed
68
+ end
69
+
70
+ # `Ethon::Easy` uses `Ethon::Easy::Header` to set request headers on
71
+ # libcurl with `#headers=`. After they are set, they aren't easy to get
72
+ # at again except via FFI so set a new instance variable on the
73
+ # `Ethon::Easy` instance to store them in Ruby hash format.
74
+ def headers_equals_with_tracing(headers)
75
+ instance_variable_set(HEADERS_INSTANCE_VAR, headers)
76
+ yield
77
+ end
78
+
79
+ def perform_with_tracing(*args)
80
+ return unless NewRelic::Agent::Tracer.state.is_execution_traced?
81
+
82
+ segment = prep_easy(self)
83
+ wrap_with_tracing(segment) { yield }
84
+ end
85
+ end
86
+
87
+ module Multi
88
+ include NRShared
89
+
90
+ MULTI_SEGMENT_NAME = 'External/Multiple/Ethon::Multi/perform'
91
+
92
+ def perform_with_tracing(*args)
93
+ return unless NewRelic::Agent::Tracer.state.is_execution_traced?
94
+
95
+ segment = NewRelic::Agent::Tracer.start_segment(name: MULTI_SEGMENT_NAME)
96
+
97
+ wrap_with_tracing(segment) do
98
+ easy_handles.each { |easy| prep_easy(easy, segment) }
99
+
100
+ yield
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,35 @@
1
+ # This file is distributed under New Relic's license terms.
2
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
3
+ # frozen_string_literal: true
4
+
5
+ module NewRelic::Agent::Instrumentation
6
+ module Ethon
7
+ module Easy
8
+ module Prepend
9
+ include NewRelic::Agent::Instrumentation::Ethon::Easy
10
+
11
+ def fabricate(url, action_name, options)
12
+ fabricate_with_tracing(url, action_name, options) { super }
13
+ end
14
+
15
+ def headers=(headers)
16
+ headers_equals_with_tracing(headers) { super }
17
+ end
18
+
19
+ def perform(*args)
20
+ perform_with_tracing(*args) { super }
21
+ end
22
+ end
23
+ end
24
+
25
+ module Multi
26
+ module Prepend
27
+ include NewRelic::Agent::Instrumentation::Ethon::Multi
28
+
29
+ def perform(*args)
30
+ perform_with_tracing(*args) { super }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end