module XRay::NetHttp::HTTPInstanceInterceptor

Instance level interceptor to capture http requests as subsegments

Public Class Methods

new(*options) click to toggle source
Calls superclass method
# File lib/aws-xray-sdk/facets/net_http.rb, line 19
def initialize(*options)
  super(*options)
end

Public Instance Methods

ec2_metadata_request?(req) click to toggle source

Instance Metadata Service provides endpoint 169.254.169.254 to provide EC2 metadata

# File lib/aws-xray-sdk/facets/net_http.rb, line 35
def ec2_metadata_request?(req)
  req.uri && req.uri.hostname == '169.254.169.254'
end
lambda_runtime_request?() click to toggle source

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

# 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) click to toggle source
Calls superclass method
# 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? || ec2_metadata_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) click to toggle source
# 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