Class: XRay::AwsSDKPlugin::Handler

Inherits:
Seahorse::Client::Handler
  • Object
show all
Includes:
Facets::Helper
Defined in:
lib/aws-xray-sdk/facets/aws_sdk.rb

Overview

Handler to capture AWS API calls 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

#call(context) ⇒ Object



19
20
21
22
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
57
58
59
# File 'lib/aws-xray-sdk/facets/aws_sdk.rb', line 19

def call(context)
  recorder = Aws.config[:xray_recorder]
  operation = context.operation_name
  service_name = context.client.class.api.['serviceAbbreviation'] ||
                 context.client.class.to_s.split('::')[1]
  recorder.capture service_name, namespace: 'aws' do |subsegment|
    # inject header string before calling downstream AWS services
    context.http_request.headers[TRACE_HEADER] = prep_header_str entity: subsegment
    response = @handler.call(context)
    http_response = context.http_response
    resp_meta = {
      status: http_response.status_code,
      content_length: http_response.headers['content-length'].to_i
    }
    aws = {
      # XRay back-end right now has strict operation name matching
      operation: sanitize_op_name(operation),
      region: context.client.config.region,
      retries: context.retries,
      request_id: http_response.headers['x-amzn-requestid']
    }
    # S3 returns special request id in response headers
    if service_name == 'S3'
      aws[:id_2] = http_response.headers['x-amz-id-2']
    end

    operation_h = AwsParams.whitelist[:services]
                           .fetch(service_name.to_sym, {})
                           .fetch(:operations, {})[operation]
    unless operation_h.nil?
      params_capture req_params: context.params, resp_params: response.to_h,
                     capture: operation_h, meta: aws
    end
    subsegment.aws = aws
    if err = response.error
      subsegment.add_exception exception: err, remote: true
    end
    subsegment.merge_http_response response: resp_meta
    response
  end
end