Module: TraceView::Inst::FaradayConnection

Defined in:
lib/traceview/inst/faraday.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
# File 'lib/traceview/inst/faraday.rb', line 4

def self.included(klass)
  ::TraceView::Util.method_alias(klass, :run_request, ::Faraday::Connection)
end

Instance Method Details

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/traceview/inst/faraday.rb', line 8

def run_request_with_traceview(method, url, body, headers, &block)
  # Only send service KVs if we're not using the Net::HTTP adapter
  # Otherwise, the Net::HTTP instrumentation will send the service KVs
  handle_service = !@builder.handlers.include?(Faraday::Adapter::NetHttp) &&
                    !@builder.handlers.include?(Faraday::Adapter::Excon)
  TraceView::API.log_entry('faraday')

  result = run_request_without_traceview(method, url, body, headers, &block)

  kvs = {}
  kvs['Middleware'] = @builder.handlers
  kvs['Backtrace'] = TraceView::API.backtrace if TraceView::Config[:faraday][:collect_backtraces]

  if handle_service
    blacklisted = TraceView::API.blacklisted?(@url_prefix.to_s)
    context = TraceView::Context.toString
    task_id = TraceView::XTrace.task_id(context)

    # Avoid cross host tracing for blacklisted domains
    # Conditionally add the X-Trace header to the outgoing request
    @headers['X-Trace'] = context unless blacklisted

    kvs['IsService'] = 1
    kvs['RemoteProtocol'] = (@url_prefix.scheme == 'https') ? 'HTTPS' : 'HTTP'
    kvs['RemoteHost'] = @url_prefix.host
    kvs['RemotePort'] = @url_prefix.port
    kvs['ServiceArg'] = url
    kvs['HTTPMethod'] = method
    kvs[:HTTPStatus] = result.status
    kvs['Blacklisted'] = true if blacklisted

    # Re-attach net::http edge unless it's blacklisted or if we don't have a
    # valid X-Trace header
    unless blacklisted
      xtrace = result.headers['X-Trace']

      if TraceView::XTrace.valid?(xtrace) && TraceView.tracing?

        # Assure that we received back a valid X-Trace with the same task_id
        if task_id == TraceView::XTrace.task_id(xtrace)
          TraceView::Context.fromString(xtrace)
        else
          TraceView.logger.debug "Mismatched returned X-Trace ID: #{xtrace}"
        end
      end
    end
  end

  TraceView::API.log('faraday', 'info', kvs)
  result
rescue => e
  TraceView::API.log_exception('faraday', e)
  raise e
ensure
  TraceView::API.log_exit('faraday')
end