Menu
AWS X-Ray
Developer Guide

Patching Libraries to Instrument Downstream Calls

To instrument downstream calls, use the X-Ray SDK for Python to patch the libraries that your application uses. The X-Ray SDK for Python can patch the following libraries.

Supported Libraries

When you use a patched library, the X-Ray SDK for Python creates a subsegment for the call and records information from the request and response. A segment must be available for the SDK to create the subsegment, either from the SDK middleware or from AWS Lambda.

To patch all available libraries, use the patch_all function in aws_xray_sdk.core.

Example main.py – patch all supported libraries

import boto3 import botocore import requests import sqlite3 from aws_xray_sdk.core import xray_recorder from aws_xray_sdk.core import patch_all patch_all()

To patch individual libraries, call patch with a tuple of library names.

Example main.py – patch specific libraries

import boto3 import botocore import requests import sqlite3 from aws_xray_sdk.core import xray_recorder from aws_xray_sdk.core import patch libraries = ('botocore', 'requests') patch(libraries)

For asyncio integrated libraries, or to create subsegments for asynchronous functions, you must also configure the X-Ray SDK for Python with an async context.

Example main.py – patch aioboto3

import asyncio import aioboto3 import requests from aws_xray_sdk.core.async_context import AsyncContext from aws_xray_sdk.core import xray_recorder xray_recorder.configure(service='my_service', context=AsyncContext()) from aws_xray_sdk.core import patch libraries = ('aioboto3') patch(libraries)