lib/elastic_apm/spies/faraday.rb in elastic-apm-4.6.1 vs lib/elastic_apm/spies/faraday.rb in elastic-apm-4.6.2
- old
+ new
@@ -100,16 +100,24 @@
subtype: SUBTYPE,
context: context
) do |span|
ElasticAPM::Spies.without_net_http do
trace_context = span&.trace_context || transaction.trace_context
- self.response :elastic_apm_middleware, span # middleware
- result = super(method, url, body, headers) do |req|
- trace_context.apply_headers { |k, v| req[k] = v }
-
- yield req if block
+ begin
+ result = super(method, url, body, headers) do |req|
+ trace_context.apply_headers { |k, v| req[k] = v }
+ yield req if block
+ end
+ rescue Faraday::ClientError, Faraday::ServerError => e # Faraday::Response::RaiseError
+ status = e.response[:status]
+ http = span&.context&.http
+ if http && status
+ http.status_code = status.to_s
+ span.outcome = Span::Outcome.from_http_status(status)
+ end
+ raise e
end
if (http = span&.context&.http)
http.status_code = result.status.to_s
end
@@ -121,31 +129,9 @@
end
end
# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
def install
- # This middleware class has to be defined here because it inherits from
- # ::Faraday::Middleware, which isn't defined when this file loads.
- tracing_middleware = Class.new ::Faraday::Middleware do
- attr_reader :span
-
- def initialize(app, span = nil, options = {})
- super(app)
- @span = span
- end
-
- def on_complete(env)
- status = env[:status]
- http = span&.context&.http
- if http && status
- http.status_code = status.to_s
- span.outcome = Span::Outcome.from_http_status(status)
- end
- end
- end
- ::Faraday::Response.register_middleware(
- elastic_apm_middleware: -> { tracing_middleware }
- )
::Faraday::Connection.prepend(Ext)
end
end
register 'Faraday', 'faraday', FaradaySpy.new