Menu
AWS X-Ray
Developer Guide

AWS X-Ray SDK for Python

The X-Ray SDK for Python is a library for Django and Flask web applications that provides classes and methods for generating and sending trace data to the X-Ray daemon. Trace data includes information about incoming HTTP requests served by the application, and calls that the application makes to downstream services using the AWS SDK, HTTP clients, or an SQL database connector. You can also create segments manually and add debug information in annotations and metadata.

You can download the SDK with pip.

$ pip install aws-xray-sdk

Note

The X-Ray SDK for Python is an open source project. You can follow the project and submit issues and pull requests on GitHub: github.com/aws/aws-xray-sdk-python

Start by adding the SDK middleware to your application to trace incoming requests. The middleware creates a segment for each traced request, and completes the segment when the response is sent. While the segment is open, you can use the SDK client's methods to add information to the segment and create subsegments to trace downstream calls. The SDK also automatically records exceptions that your application throws while the segment is open.

Next, use the X-Ray SDK for Python to instrument downstream calls by patching your application's libraries. The SDK supports the following libraries.

Supported Libraries

Whenever your application makes calls to AWS, an SQL database, or other HTTP services, the SDK records information about the call in a subsegment. AWS services and the resources that you access within the services appear as downstream nodes on the service map to help you identify errors and throttling issues on individual connections.

Once you get going with the SDK, customize its behavior by configuring the recorder and middleware. You can add plugins to record data about the compute resources running your application, customize sampling behavior by defining sampling rules, and set the log level to see more or less information from the SDK in your application logs.

Record additional information about requests and the work that your application does in annotations and metadata. Annotations are simple key-value pairs that are indexed for use with filter expressions, so that you can search for traces that contain specific data. Metadata entries are less restrictive and can record entire objects and arrays — anything that can be serialized into JSON.

Annotations and Metadata

Annotations and metadata are arbitrary text that you add to segments with the X-Ray SDK. Annotations are indexed for use with filter expressions. Metadata are not indexed but can be viewed in the raw segment with the X-Ray console or API. Anyone that you grant read access to X-Ray can view this data.

When you have a lot of instrumented clients in your code, a single request segment can contain a large number of subsegments, one for each call made with an instrumented client. You can organize and group subsegments by wrapping client calls in custom subsegments. You can create a custom subsegment for an entire function or any section of code. You can then you can record metadata and annotations on the subsegment instead of writing everything on the parent segment.

See the AWS X-Ray SDK for Python API Reference for information about the SDK's classes and methods.

Requirements

The X-Ray SDK for Python supports the following language and library versions.

  • Python – 2.7, 3.4 and newer

  • Django – 1.10 and newer

  • Flask – 0.10 and newer

  • AWS SDK for Python (Boto) – 1.4.0 and newer

  • botocore – 1.5.0 and newer

Dependency Management

The X-Ray SDK for Python is available from pip.

  • Packageaws-xray-sdk

Add the SDK as a dependency in your requirements.txt file.

Example requirements.txt

aws-xray-sdk==0.95
boto3==1.4.4
botocore==1.5.55
Django==1.11.3

If you use Elastic Beanstalk to deploy your application, Elastic Beanstalk installs all of the packages in requirements.txt automatically.