| 1 |
import aiobotocore.client |
| 2 |
import wrapt |
| 3 |
|
| 4 |
from aws_xray_sdk.core import xray_recorder |
| 5 |
from aws_xray_sdk.ext.boto_utils import inject_header, aws_meta_processor |
| 6 |
|
| 7 |
|
| 8 |
def patch(): |
| 9 |
""" |
| 10 |
Patch aiobotocore client so it generates subsegments |
| 11 |
when calling AWS services. |
| 12 |
""" |
| 13 |
if hasattr(aiobotocore.client, '_xray_enabled'): |
| 14 |
return |
| 15 |
setattr(aiobotocore.client, '_xray_enabled', True) |
| 16 |
|
| 17 |
wrapt.wrap_function_wrapper( |
| 18 |
'aiobotocore.client', |
| 19 |
'AioBaseClient._make_api_call', |
| 20 |
_xray_traced_aiobotocore, |
| 21 |
) |
| 22 |
|
| 23 |
wrapt.wrap_function_wrapper( |
| 24 |
'aiobotocore.endpoint', |
| 25 |
'AioEndpoint._encode_headers', |
| 26 |
inject_header, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
async def _xray_traced_aiobotocore(wrapped, instance, args, kwargs): |
| 31 |
service = instance._service_model.metadata["endpointPrefix"] |
| 32 |
result = await xray_recorder.record_subsegment_async( |
| 33 |
wrapped, instance, args, kwargs, |
| 34 |
name=service, |
| 35 |
namespace='aws', |
| 36 |
meta_processor=aws_meta_processor, |
| 37 |
) |
| 38 |
|
| 39 |
return result |