Module: TraceView::Inst::ExconConnection

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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

def self.included(klass)
  ::TraceView::Util.method_alias(klass, :request, ::Excon::Connection)
  ::TraceView::Util.method_alias(klass, :requests, ::Excon::Connection)
end

Instance Method Details

#request_with_traceview(params = {}, &block) ⇒ Object



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
# File 'lib/traceview/inst/excon.rb', line 52

def request_with_traceview(params={}, &block)
  # If we're not tracing, just do a fast return.
  # If making HTTP pipeline requests (ordered batched)
  # then just return as we're tracing from parent
  # <tt>requests</tt>
  if !TraceView.tracing? || params[:pipeline]
    return request_without_traceview(params, &block)
  end

  begin
    response_context = nil

    # Avoid cross host tracing for blacklisted domains
    blacklisted = TraceView::API.blacklisted?(@data[:hostname])

    req_context = TraceView::Context.toString()
    @data[:headers]['X-Trace'] = req_context unless blacklisted

    kvs = traceview_collect(params)
    kvs['Blacklisted'] = true if blacklisted

    TraceView::API.log_entry('excon', kvs)
    kvs.clear

    # The core excon call
    response = request_without_traceview(params, &block)

    # excon only passes back a hash (datum) for HTTP pipelining...
    # In that case, we should never arrive here but for the OCD, double check
    # the datatype before trying to extract pertinent info
    if response.is_a?(Excon::Response)
      response_context = response.headers['X-Trace']
      kvs['HTTPStatus'] = response.status

      # If we get a redirect, report the location header
      if ((300..308).to_a.include? response.status.to_i) && response.headers.key?("Location")
        kvs["Location"] = response.headers["Location"]
      end

      if response_context && !blacklisted
        TraceView::XTrace.continue_service_context(req_context, response_context)
      end
    end

    response
  rescue => e
    TraceView::API.log_exception('excon', e)
    raise e
  ensure
    TraceView::API.log_exit('excon', kvs) unless params[:pipeline]
  end
end

#requests_with_traceview(pipeline_params) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/traceview/inst/excon.rb', line 44

def requests_with_traceview(pipeline_params)
  responses = nil
  TraceView::API.trace('excon', traceview_collect(pipeline_params)) do
    responses = requests_without_traceview(pipeline_params)
  end
  responses
end

#traceview_collect(params) ⇒ Object



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
# File 'lib/traceview/inst/excon.rb', line 12

def traceview_collect(params)
  kvs = {}
  kvs['IsService'] = 1
  kvs['RemoteProtocol'] = ::TraceView::Util.upcase(@data[:scheme])
  kvs['RemoteHost'] = @data[:host]

  # Conditionally log query args
  if TraceView::Config[:excon][:log_args] && (@data[:query] && @data[:query].length)
    kvs['ServiceArg'] = @data[:path] + '?' + @data[:query]
  else
    kvs['ServiceArg'] = @data[:path]
  end

  # In the case of HTTP pipelining, params could be an array of
  # request hashes.
  if params.is_a?(Array)
    methods = []
    params.each do |p|
      methods << ::TraceView::Util.upcase(p[:method])
    end
    kvs['HTTPMethods'] = methods.join(', ')[0..1024]
    kvs['Pipeline'] = true
  else
    kvs['HTTPMethod'] = ::TraceView::Util.upcase(params[:method])
  end
  kvs['Backtrace'] = TraceView::API.backtrace if TraceView::Config[:excon][:collect_backtraces]
  kvs
rescue => e
  TraceView.logger.debug "[traceview/debug] Error capturing excon KVs: #{e.message}"
  TraceView.logger.debug e.backtrace.join('\n') if ::TraceView::Config[:verbose]
end