Module: ElasticAPM::Spies::FaradaySpy::Ext Private

Defined in:
lib/elastic_apm/spies/faraday.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#run_request(method, url, body, headers, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/elastic_apm/spies/faraday.rb', line 52

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
  # ~mikker
  unless uri.host
    tmp_request = build_request(method) do |req|
      yield(req) if block_given?
    end
    uri = tmp_request.path && URI(tmp_request.path)
  end

  host = uri&.host || 'localhost'

  upcased_method = method.to_s.upcase

  if uri
    destination = ElasticAPM::Span::Context::Destination.from_uri(uri, type: SUBTYPE)

    context =
      ElasticAPM::Span::Context.new(
        http: { url: uri, method: upcased_method },
        destination: destination
      )
  else
    context =
      ElasticAPM::Span::Context.new(http: { url: uri, method: upcased_method })
  end

  context =
    ElasticAPM::Span::Context.new(
      http: { url: uri, method: upcased_method },
      destination: destination
    )

  ElasticAPM.with_span(
    "#{upcased_method} #{host}",
    TYPE,
    subtype: SUBTYPE,
    context: context
  ) do |span|
    ElasticAPM::Spies.without_net_http do
      trace_context = span&.trace_context || transaction.trace_context

      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 =
          if e.respond_to?(:response_status)
            e.response_status
          elsif e.response && e.response.respond_to?(:status)
            e.response.status
          elsif e.response && e.response.respond_to?(:fetch)
            e.response[:status]
          end
        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

      span&.outcome = Span::Outcome.from_http_status(result.status)
      result
    end
  end
end