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

#ec2_metadata_request?(req) ⇒ Boolean

Instance Metadata Service provides endpoint 169.254.169.254 to provide EC2 metadata

Returns:

  • (Boolean)


35
36
37
# File 'lib/aws-xray-sdk/facets/net_http.rb', line 35

def (req)
  req.uri && req.uri.hostname == '169.254.169.254'
end

#initialize(*options) ⇒ Object



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

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

#lambda_runtime_request?Boolean

HTTP requests to AWS Lambda Ruby Runtime will have the address and port matching the value set in ENV

Returns:

  • (Boolean)


25
26
27
# File 'lib/aws-xray-sdk/facets/net_http.rb', line 25

def lambda_runtime_request?
  ENV['AWS_LAMBDA_RUNTIME_API'] == "#{address}:#{port}"
end

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



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
71
72
73
74
75
76
77
# File 'lib/aws-xray-sdk/facets/net_http.rb', line 39

def request(req, body = nil, &block)
  # Do not trace requests to xray or aws lambda runtime or ec2 metadata endpoint
  if xray_sampling_request?(req) || lambda_runtime_request? || (req)
    return super
  end

  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

#xray_sampling_request?(req) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/aws-xray-sdk/facets/net_http.rb', line 29

def xray_sampling_request?(req)
  req.path && (req.path == ('/GetSamplingRules') || req.path == ('/SamplingTargets'))
end