Module: XRay::NetHttp::HTTPInstanceInterceptor

Includes:
Facets::Helper
Defined in:
lib/aws-xray-sdk/facets/net_http.rb

Overview

Instance level interceptor to capture http requests as subsegments

Constant Summary

Constants included from Facets::Helper

Facets::Helper::TRACE_HEADER, Facets::Helper::TRACE_HEADER_PROXY

Instance Method Summary collapse

Methods included from Facets::Helper

#construct_header, #prep_header_str, #should_sample?

Instance Method Details

#initialize(*options) ⇒ Object



19
20
21
# File 'lib/aws-xray-sdk/facets/net_http.rb', line 19

def initialize(*options)
  super(*options)
end

#request(req, body = nil, &block) ⇒ Object



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
# File 'lib/aws-xray-sdk/facets/net_http.rb', line 23

def request(req, body = nil, &block)
  entity = XRay.recorder.current_entity
  capture = !(entity && entity.namespace && entity.namespace == 'aws'.freeze)
  if started? && capture && entity
    XRay.recorder.capture(address, namespace: 'remote') do |subsegment|
      protocol = use_ssl? ? 'https'.freeze : 'http'.freeze
      # avoid modifying original variable
      iport = port.nil? ? nil : %(:#{port})
      # do not capture query string
      path = req.path.split('?')[0] if req.path
      uri = %(#{protocol}://#{address}#{iport}#{path})
      req_meta = {
        url: uri,
        method: req.method
      }
      subsegment.merge_http_request request: req_meta
      req[TRACE_HEADER] = prep_header_str entity: subsegment
      begin
        res = super
        res_meta = {
          status: res.code.to_i,
          content_length: res.content_length
        }
        subsegment.merge_http_response response: res_meta
        res
      rescue Exception => e
        subsegment.add_exception exception: e
        raise e
      end
    end
  else
    super
  end
end