CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-aws-xray-sdk

The AWS X-Ray SDK for Python enables Python developers to record and emit information from within their applications to the AWS X-Ray service for distributed tracing.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

library-patching.mddocs/

Library Patching

Automatic instrumentation for popular Python libraries and frameworks. The patching system provides seamless tracing integration without requiring code changes for supported libraries, enabling comprehensive distributed tracing across your application stack.

Capabilities

Patch All Libraries

Automatically patch all supported libraries with a single function call.

def patch_all(double_patch: bool = False) -> None:
    """
    Patch all supported libraries for X-Ray tracing.

    Args:
        double_patch (bool): Enable patching of indirect dependencies.
                           When True, patches libraries that other libraries depend on.

    Supported Libraries:
        - aiobotocore: Async AWS SDK
        - botocore: AWS SDK (boto3)
        - pynamodb: DynamoDB ORM
        - requests: HTTP library
        - sqlite3: SQLite database
        - mysql: MySQL database
        - httplib: Standard HTTP library
        - pymongo: MongoDB driver
        - pymysql: Pure Python MySQL driver
        - psycopg2: PostgreSQL adapter
        - pg8000: Pure Python PostgreSQL driver
        - sqlalchemy_core: SQLAlchemy Core
        - httpx: Modern async HTTP client
    """

Selective Library Patching

Patch specific libraries for more granular control over instrumentation.

def patch(
    modules_to_patch: tuple,
    raise_errors: bool = True,
    ignore_module_patterns: list = None
) -> None:
    """
    Patch specific modules for X-Ray tracing.

    Args:
        modules_to_patch (tuple): Tuple of module names to patch
        raise_errors (bool): Whether to raise errors if patching fails
        ignore_module_patterns (list): List of regex patterns for modules to ignore

    Raises:
        ImportError: If module cannot be imported and raise_errors is True
        Exception: If patching fails and raise_errors is True
    """

Supported Libraries

AWS Services

botocore (boto3)

Traces AWS service calls made through boto3 and botocore.

from aws_xray_sdk.core import patch
import boto3

# Patch botocore for AWS service tracing
patch(['botocore'])

# All AWS calls are now automatically traced
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('users')
response = table.get_item(Key={'id': '123'})  # Automatically traced

aiobotocore

Traces async AWS service calls made through aiobotocore.

from aws_xray_sdk.core import patch
import aiobotocore.session

# Patch aiobotocore for async AWS service tracing
patch(['aiobotocore'])

# Async AWS calls are automatically traced
session = aiobotocore.session.get_session()
async with session.create_client('s3') as client:
    response = await client.list_buckets()  # Automatically traced

HTTP Clients

requests

Traces HTTP requests made through the popular requests library.

from aws_xray_sdk.core import patch
import requests

# Patch requests for HTTP tracing
patch(['requests'])

# HTTP requests are automatically traced
response = requests.get('https://api.example.com/data')  # Automatically traced
response = requests.post('https://api.example.com/users', json={'name': 'John'})  # Automatically traced

httplib

Traces HTTP requests made through Python's standard HTTP library.

from aws_xray_sdk.core import patch
import http.client

# Patch httplib for HTTP tracing
patch(['httplib'])

# Standard library HTTP calls are traced
conn = http.client.HTTPSConnection('api.example.com')
conn.request('GET', '/data')
response = conn.getresponse()  # Automatically traced

HTTP Request Filtering

Control which HTTP requests are traced by adding hostname, URL, or subclass filters.

from aws_xray_sdk.ext.httplib import add_ignored, reset_ignored

# Ignore requests to specific hostnames
add_ignored(hostname='test.myapp.com')

# Ignore requests with glob patterns
add_ignored(hostname='*.internal.com')

# Ignore specific URL paths
add_ignored(urls=['/health', '/metrics'])

# Ignore requests from specific HTTP client classes
add_ignored(subclass='botocore.awsrequest.AWSHTTPConnection')

# Combine multiple filters
add_ignored(hostname='api.example.com', urls=['/internal/*'])

# Reset all ignore patterns to defaults
reset_ignored()

httpx

Traces HTTP requests made through the modern httpx library.

from aws_xray_sdk.core import patch
import httpx

# Patch httpx for HTTP tracing
patch(['httpx'])

# Both sync and async httpx calls are traced
response = httpx.get('https://api.example.com/data')  # Automatically traced

async with httpx.AsyncClient() as client:
    response = await client.get('https://api.example.com/data')  # Automatically traced

Database Libraries

SQLite3

Traces SQLite database operations.

from aws_xray_sdk.core import patch
import sqlite3

# Patch sqlite3 for database tracing
patch(['sqlite3'])

# SQLite operations are automatically traced
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM users')  # Automatically traced

MySQL Drivers

Traces MySQL database operations through various Python drivers.

from aws_xray_sdk.core import patch

# Patch MySQL drivers
patch(['mysql'])  # MySQLdb
patch(['pymysql'])  # PyMySQL

import MySQLdb
# or
import pymysql

# Database operations are automatically traced

PostgreSQL Drivers

Traces PostgreSQL database operations.

from aws_xray_sdk.core import patch

# Patch PostgreSQL drivers
patch(['psycopg2'])  # psycopg2
patch(['pg8000'])    # pg8000

import psycopg2
# or
import pg8000

# Database operations are automatically traced

MongoDB

Traces MongoDB operations through pymongo.

from aws_xray_sdk.core import patch
import pymongo

# Patch pymongo for MongoDB tracing
patch(['pymongo'])

# MongoDB operations are automatically traced
client = pymongo.MongoClient()
db = client.mydb
collection = db.users
result = collection.find_one({'name': 'John'})  # Automatically traced

SQLAlchemy

Traces SQLAlchemy Core operations.

from aws_xray_sdk.core import patch
from sqlalchemy import create_engine

# Patch SQLAlchemy Core
patch(['sqlalchemy_core'])

# SQLAlchemy operations are automatically traced
engine = create_engine('postgresql://user:pass@localhost/db')
result = engine.execute('SELECT * FROM users')  # Automatically traced

PynamoDB

Traces DynamoDB operations through PynamoDB ORM.

from aws_xray_sdk.core import patch
from pynamodb.models import Model

# Patch PynamoDB
patch(['pynamodb'])

# PynamoDB operations are automatically traced
class User(Model):
    class Meta:
        table_name = 'users'
    
    id = UnicodeAttribute(hash_key=True)
    name = UnicodeAttribute()

user = User.get('123')  # Automatically traced

Patching Patterns

Basic Patching

from aws_xray_sdk.core import patch_all, patch, xray_recorder

# Configure recorder first
xray_recorder.configure(
    sampling=False,
    context_missing='LOG_ERROR'
)

# Option 1: Patch all supported libraries
patch_all()

# Option 2: Patch specific libraries
patch(['requests', 'botocore', 'sqlite3'])

# Your application code - all patched libraries are now traced
import requests
import boto3

with xray_recorder.in_segment('my-application'):
    # These calls are automatically traced
    response = requests.get('https://api.example.com')
    s3 = boto3.client('s3')
    buckets = s3.list_buckets()

Selective Patching with Error Handling

from aws_xray_sdk.core import patch

# Patch with error handling
try:
    patch(['requests', 'botocore', 'nonexistent_library'], raise_errors=False)
    print("Patching completed - some modules may have been skipped")
except Exception as e:
    print(f"Patching failed: {e}")

# Patch with module filtering
patch(
    ['requests', 'botocore'],
    ignore_module_patterns=[r'.*test.*', r'.*mock.*']
)

Framework-Specific Patching

Some libraries require special handling or have dedicated extensions:

# For Django applications
from aws_xray_sdk.core import patch
patch(['requests', 'botocore'])

# Django middleware handles segment creation
# Add to settings.py:
# MIDDLEWARE = ['aws_xray_sdk.ext.django.middleware.XRayMiddleware', ...]

# For Flask applications  
from aws_xray_sdk.core import patch, xray_recorder
from aws_xray_sdk.ext.flask.middleware import XRayMiddleware

patch(['requests', 'botocore'])
app = Flask(__name__)
XRayMiddleware(app, xray_recorder)

Advanced Patching Options

Module Pattern Ignoring

from aws_xray_sdk.core import patch

# Ignore test and development modules
patch(
    ['requests', 'botocore'],
    ignore_module_patterns=[
        r'.*test.*',      # Ignore any module with 'test' in the name
        r'.*mock.*',      # Ignore mock modules
        r'dev\..*',       # Ignore dev.* modules
        r'.*\.test_.*'    # Ignore test_ modules
    ]
)

Double Patching

from aws_xray_sdk.core import patch_all

# Enable double patching for indirect dependencies
patch_all(double_patch=True)

This enables patching of libraries that other libraries depend on, providing more comprehensive tracing coverage.

Custom Module Patching

For libraries not in the standard supported list, you can patch entire modules recursively:

from aws_xray_sdk.core import patch

# Patch entire local modules recursively
patch(['myapp.services', 'myapp.utils'])

# This applies @xray_recorder.capture() decorator to all functions
# and class methods in the specified modules and their submodules

Error Handling and Troubleshooting

Common Patching Issues

  1. Import Order: Patch libraries before importing them
  2. Module Not Found: Use raise_errors=False for optional libraries
  3. Conflicting Patches: Avoid patching the same library multiple times
  4. Framework Integration: Some frameworks require specific middleware setup

Best Practices

from aws_xray_sdk.core import patch_all, xray_recorder

# 1. Configure recorder first
xray_recorder.configure(
    sampling=False,
    context_missing='LOG_ERROR',
    daemon_address='127.0.0.1:2000'
)

# 2. Patch libraries early in application startup
patch_all()

# 3. Import libraries after patching
import requests
import boto3
import sqlite3

# 4. Use with proper segment management
with xray_recorder.in_segment('application-startup'):
    # Initialize your application
    pass

Verification

from aws_xray_sdk.core import xray_recorder

# Check if a library is properly patched
if hasattr(requests.get, '__wrapped__'):
    print("Requests is properly patched")

# Test tracing is working
with xray_recorder.in_segment('test-segment'):
    response = requests.get('https://httpbin.org/get')
    print(f"Request traced: {response.status_code}")

Install with Tessl CLI

npx tessl i tessl/pypi-aws-xray-sdk

docs

annotations-metadata.md

aws-services.md

configuration-plugins.md

core-recording.md

database-integration.md

http-utilities.md

index.md

library-patching.md

sampling.md

web-frameworks.md

tile.json