lib/elastic_apm/spies/faraday.rb in elastic-apm-4.1.0 vs lib/elastic_apm/spies/faraday.rb in elastic-apm-4.2.0

- old
+ new

@@ -20,31 +20,46 @@ module ElasticAPM # @api private module Spies # @api private class FaradaySpy - TYPE = 'ext' - SUBTYPE = 'faraday' + DISABLE_KEY = :__elastic_apm_faraday_disabled + TYPE = 'external' + SUBTYPE = 'http' - def self.without_net_http - return yield unless defined?(NetHTTPSpy) + class << self + def disabled=(disabled) + Thread.current[DISABLE_KEY] = disabled + end - # rubocop:disable Style/ExplicitBlockArgument - ElasticAPM::Spies::NetHTTPSpy.disable_in do - yield + def disabled? + Thread.current[DISABLE_KEY] ||= false end - # rubocop:enable Style/ExplicitBlockArgument + + def disable_in + self.disabled = true + + begin + yield + ensure + self.disabled = false + end + end end # @api private module Ext # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity def run_request(method, url, body, headers, &block) unless (transaction = ElasticAPM.current_transaction) return super(method, url, body, headers, &block) end + if ElasticAPM::Spies::FaradaySpy.disabled? + return super(method, url, body, headers, &block) + end + uri = URI(build_url(url)) # If url is set inside block it isn't available until yield, # so we temporarily build the request to yield. This could be a # problem if the block has side effects as it will be yielded twice @@ -59,11 +74,11 @@ host = uri&.host || 'localhost' upcased_method = method.to_s.upcase if uri - destination = ElasticAPM::Span::Context::Destination.from_uri(uri) + destination = ElasticAPM::Span::Context::Destination.from_uri(uri, type: SUBTYPE) context = ElasticAPM::Span::Context.new( http: { url: uri, method: upcased_method }, destination: destination @@ -81,13 +96,12 @@ ElasticAPM.with_span( "#{upcased_method} #{host}", TYPE, subtype: SUBTYPE, - action: upcased_method, context: context ) do |span| - ElasticAPM::Spies::FaradaySpy.without_net_http do + ElasticAPM::Spies.without_net_http do trace_context = span&.trace_context || transaction.trace_context result = super(method, url, body, headers) do |req| trace_context.apply_headers { |k, v| req[k] = v }