Class: Atatus::Spies::FaradaySpy Private

Inherits:
Object
  • Object
show all
Defined in:
lib/atatus/spies/faraday.rb

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

Constant Summary collapse

TYPE =

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

'ext'
SUBTYPE =

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

'faraday'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.without_net_httpObject

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.



28
29
30
31
32
33
34
# File 'lib/atatus/spies/faraday.rb', line 28

def self.without_net_http
  return yield unless defined?(NetHTTPSpy)

  Atatus::Spies::NetHTTPSpy.disable_in do
    yield
  end
end

Instance Method Details

#installObject

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/CyclomaticComplexity



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/atatus/spies/faraday.rb', line 37

def install
  if defined?(::Faraday) && defined?(::Faraday::Connection)

    ::Faraday::Connection.class_eval do
      alias run_request_without_apm run_request

      def run_request(method, url, body, headers, &block)
        unless (transaction = Atatus.current_transaction)
          return run_request_without_apm(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 = URI(tmp_request.path)
        end

        host = uri.host

        upcased_method = method.to_s.upcase

        destination = Atatus::Span::Context::Destination.from_uri(uri)

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

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

            result =
              run_request_without_apm(method, url, body, headers) do |req|
                trace_context.apply_headers { |k, v| req[k] = v }

                yield req if block_given?
              end

            if (http = span&.context&.http)
              http.status_code = result.status.to_s
            end

            result
          end
        end
      end
    end

  end
end