| 1 |
from __future__ import absolute_import |
| 2 |
# Need absolute import as botocore is also in the current folder for py27 |
| 3 |
import json |
| 4 |
|
| 5 |
from pkg_resources import resource_filename |
| 6 |
from botocore.exceptions import ClientError |
| 7 |
|
| 8 |
from aws_xray_sdk.core import xray_recorder |
| 9 |
from aws_xray_sdk.core.models import http |
| 10 |
|
| 11 |
from aws_xray_sdk.ext.util import inject_trace_header, to_snake_case |
| 12 |
|
| 13 |
|
| 14 |
with open(resource_filename(__name__, 'resources/aws_para_whitelist.json'), 'r') as data_file: |
| 15 |
whitelist = json.load(data_file) |
| 16 |
|
| 17 |
|
| 18 |
def inject_header(wrapped, instance, args, kwargs): |
| 19 |
headers = args[0] |
| 20 |
inject_trace_header(headers, xray_recorder.current_subsegment()) |
| 21 |
return wrapped(*args, **kwargs) |
| 22 |
|
| 23 |
|
| 24 |
def aws_meta_processor(wrapped, instance, args, kwargs, |
| 25 |
return_value, exception, subsegment, stack): |
| 26 |
region = instance.meta.region_name |
| 27 |
|
| 28 |
if 'operation_name' in kwargs: |
| 29 |
operation_name = kwargs['operation_name'] |
| 30 |
else: |
| 31 |
operation_name = args[0] |
| 32 |
|
| 33 |
aws_meta = { |
| 34 |
'operation': operation_name, |
| 35 |
'region': region, |
| 36 |
} |
| 37 |
|
| 38 |
if return_value: |
| 39 |
resp_meta = return_value.get('ResponseMetadata') |
| 40 |
if resp_meta: |
| 41 |
aws_meta['request_id'] = resp_meta.get('RequestId') |
| 42 |
subsegment.put_http_meta(http.STATUS, |
| 43 |
resp_meta.get('HTTPStatusCode')) |
| 44 |
# for service like S3 that returns special request id in response headers |
| 45 |
if 'HTTPHeaders' in resp_meta and resp_meta['HTTPHeaders'].get('x-amz-id-2'): |
| 46 |
aws_meta['id_2'] = resp_meta['HTTPHeaders']['x-amz-id-2'] |
| 47 |
|
| 48 |
elif exception: |
| 49 |
_aws_error_handler(exception, stack, subsegment, aws_meta) |
| 50 |
|
| 51 |
_extract_whitelisted_params(subsegment.name, operation_name, |
| 52 |
aws_meta, args, kwargs, return_value) |
| 53 |
|
| 54 |
subsegment.set_aws(aws_meta) |
| 55 |
|
| 56 |
|
| 57 |
def _aws_error_handler(exception, stack, subsegment, aws_meta): |
| 58 |
|
| 59 |
if not exception or not isinstance(exception, ClientError): |
| 60 |
return |
| 61 |
|
| 62 |
response_metadata = exception.response.get('ResponseMetadata') |
| 63 |
|
| 64 |
if not response_metadata: |
| 65 |
return |
| 66 |
|
| 67 |
aws_meta['request_id'] = response_metadata.get('RequestId') |
| 68 |
|
| 69 |
status_code = response_metadata.get('HTTPStatusCode') |
| 70 |
|
| 71 |
subsegment.put_http_meta(http.STATUS, status_code) |
| 72 |
subsegment.add_exception(exception, stack, True) |
| 73 |
|
| 74 |
|
| 75 |
def _extract_whitelisted_params(service, operation, |
| 76 |
aws_meta, args, kwargs, response): |
| 77 |
|
| 78 |
# check if service is whitelisted |
| 79 |
if service not in whitelist['services']: |
| 80 |
return |
| 81 |
operations = whitelist['services'][service]['operations'] |
| 82 |
|
| 83 |
# check if operation is whitelisted |
| 84 |
if operation not in operations: |
| 85 |
return |
| 86 |
params = operations[operation] |
| 87 |
|
| 88 |
# record whitelisted request/response parameters |
| 89 |
if 'request_parameters' in params: |
| 90 |
_record_params(params['request_parameters'], args[1], aws_meta) |
| 91 |
|
| 92 |
if 'request_descriptors' in params: |
| 93 |
_record_special_params(params['request_descriptors'], |
| 94 |
args[1], aws_meta) |
| 95 |
|
| 96 |
if 'response_parameters' in params and response: |
| 97 |
_record_params(params['response_parameters'], response, aws_meta) |
| 98 |
|
| 99 |
if 'response_descriptors' in params and response: |
| 100 |
_record_special_params(params['response_descriptors'], |
| 101 |
response, aws_meta) |
| 102 |
|
| 103 |
|
| 104 |
def _record_params(whitelisted, actual, aws_meta): |
| 105 |
|
| 106 |
for key in whitelisted: |
| 107 |
if key in actual: |
| 108 |
snake_key = to_snake_case(key) |
| 109 |
aws_meta[snake_key] = actual[key] |
| 110 |
|
| 111 |
|
| 112 |
def _record_special_params(whitelisted, actual, aws_meta): |
| 113 |
|
| 114 |
for key in whitelisted: |
| 115 |
if key in actual: |
| 116 |
_process_descriptor(whitelisted[key], actual[key], aws_meta) |
| 117 |
|
| 118 |
|
| 119 |
def _process_descriptor(descriptor, value, aws_meta): |
| 120 |
|
| 121 |
# "get_count" = true |
| 122 |
if 'get_count' in descriptor and descriptor['get_count']: |
| 123 |
value = len(value) |
| 124 |
|
| 125 |
# "get_keys" = true |
| 126 |
if 'get_keys' in descriptor and descriptor['get_keys']: |
| 127 |
value = value.keys() |
| 128 |
|
| 129 |
aws_meta[descriptor['rename_to']] = value |