module Oboe::Inst::TyphoeusRequestOps

Public Class Methods

included(klass) click to toggle source
# File lib/oboe/inst/typhoeus.rb, line 8
def self.included(klass)
  ::Oboe::Util.method_alias(klass, :run, ::Typhoeus::Request::Operations)
end

Public Instance Methods

run_with_oboe() click to toggle source
# File lib/oboe/inst/typhoeus.rb, line 12
def run_with_oboe
  return run_without_oboe unless Oboe.tracing?

  Oboe::API.log_entry('typhoeus')

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

  response = run_without_oboe

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

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

  uri = URI(response.effective_url)

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

  kvs['HTTPMethod'] = ::Oboe::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 && Oboe::XTrace.valid?(xtrace) && Oboe.tracing?

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

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