Module: TraceView::Inst::TyphoeusRequestOps

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



8
9
10
# File 'lib/traceview/inst/typhoeus.rb', line 8

def self.included(klass)
  ::TraceView::Util.method_alias(klass, :run, ::Typhoeus::Request::Operations)
end

Instance Method Details

#run_with_traceviewObject



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
64
65
66
67
68
69
70
# File 'lib/traceview/inst/typhoeus.rb', line 12

def run_with_traceview
  return run_without_traceview unless TraceView.tracing?

  TraceView::API.log_entry('typhoeus')

  # Prepare X-Trace header handling
  blacklisted = TraceView::API.blacklisted?(url)
  context = TraceView::Context.toString
  task_id = TraceView::XTrace.task_id(context)
  options[:headers]['X-Trace'] = context unless blacklisted

  response = run_without_traceview

  if response.code == 0
    TraceView::API.log('typhoeus', 'error', { :ErrorClass => response.return_code,
                                         :ErrorMsg => response.return_message })
  end

  kvs = {}
  kvs['IsService'] = 1
  kvs[:HTTPStatus] = response.code
  kvs['Backtrace'] = TraceView::API.backtrace if TraceView::Config[:typhoeus][:collect_backtraces]

  uri = URI(response.effective_url)

  # Conditionally log query params
  if TraceView::Config[:typhoeus][:log_args]
    kvs['RemoteURL'] = uri.to_s
  else
    kvs['RemoteURL'] = uri.to_s.split('?').first
  end

  kvs['HTTPMethod'] = ::TraceView::Util.upcase(options[:method])
  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 = response.headers['X-Trace']

    if xtrace && 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

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