lib/elastic_apm/spies/faraday.rb in elastic-apm-4.6.0 vs lib/elastic_apm/spies/faraday.rb in elastic-apm-4.6.1

- old
+ new

@@ -100,10 +100,11 @@ 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 @@ -120,9 +121,31 @@ 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