![Build Status](https://github.com/aws/aws-xray-sdk-python/actions/workflows/IntegrationTesting.yaml/badge.svg)
[![codecov](https://codecov.io/gh/aws/aws-xray-sdk-python/branch/master/graph/badge.svg)](https://codecov.io/gh/aws/aws-xray-sdk-python)
### :mega: OpenTelemetry Python with AWS X-Ray
AWS X-Ray supports using OpenTelemetry Python and the AWS Distro for OpenTelemetry (ADOT) Collector to instrument your application and send trace data to X-Ray. The OpenTelemetry SDKs are an industry-wide standard for tracing instrumentation. They provide more instrumentations and have a larger community for support, but may not have complete feature parity with the X-Ray SDKs. See [choosing between the ADOT and X-Ray SDKs](https://docs.aws.amazon.com/xray/latest/devguide/xray-instrumenting-your-app.html#xray-instrumenting-choosing) for more help with choosing between the two.
If you want additional features when tracing your Python applications, please [open an issue on the OpenTelemetry Python Instrumentation repository](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/new?labels=feature-request&template=feature_request.md&title=X-Ray%20Compatible%20Feature%20Request).
### :mega: Python Versions End-of-Support Notice
AWS X-Ray SDK for Python versions `>2.11.0` has dropped support for Python 2.7, 3.4, 3.5, and 3.6.
# AWS X-Ray SDK for Python
![Screenshot of the AWS X-Ray console](/images/example_servicemap.png?raw=true)
## Installing
The AWS X-Ray SDK for Python is compatible with Python 3.7, 3.8, 3.9, 3.10, and 3.11.
Install the SDK using the following command (the SDK's non-testing dependencies will be installed).
```
pip install aws-xray-sdk
```
To install the SDK's testing dependencies, use the following command.
```
pip install tox
```
## Getting Help
Use the following community resources for getting help with the SDK. We use the GitHub
issues for tracking bugs and feature requests.
* Ask a question in the [AWS X-Ray Forum](https://forums.aws.amazon.com/forum.jspa?forumID=241&start=0).
* Open a support ticket with [AWS Support](http://docs.aws.amazon.com/awssupport/latest/user/getting-started.html).
* If you think you may have found a bug, open an [issue](https://github.com/aws/aws-xray-sdk-python/issues/new).
## Opening Issues
If you encounter a bug with the AWS X-Ray SDK for Python, we want to hear about
it. Before opening a new issue, search the [existing issues](https://github.com/aws/aws-xray-sdk-python/issues)
to see if others are also experiencing the issue. Include the version of the AWS X-Ray
SDK for Python, Python language, and botocore/boto3 if applicable. In addition,
include the repro case when appropriate.
The GitHub issues are intended for bug reports and feature requests. For help and
questions about using the AWS SDK for Python, use the resources listed
in the [Getting Help](https://github.com/aws/aws-xray-sdk-python#getting-help) section. Keeping the list of open issues lean helps us respond in a timely manner.
## Documentation
The [developer guide](https://docs.aws.amazon.com/xray/latest/devguide) provides in-depth
guidance about using the AWS X-Ray service.
The [API Reference](http://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/)
provides guidance for using the SDK and module-level documentation.
## Quick Start
### Configuration
```python
from aws_xray_sdk.core import xray_recorder
xray_recorder.configure(
sampling=False,
context_missing='LOG_ERROR',
plugins=('EC2Plugin', 'ECSPlugin', 'ElasticBeanstalkPlugin'),
daemon_address='127.0.0.1:3000',
dynamic_naming='*mysite.com*'
)
```
### Start a custom segment/subsegment
Using context managers for implicit exceptions recording:
```python
from aws_xray_sdk.core import xray_recorder
with xray_recorder.in_segment('segment_name') as segment:
# Add metadata or annotation here if necessary
segment.put_metadata('key', dict, 'namespace')
with xray_recorder.in_subsegment('subsegment_name') as subsegment:
subsegment.put_annotation('key', 'value')
# Do something here
with xray_recorder.in_subsegment('subsegment2') as subsegment:
subsegment.put_annotation('key2', 'value2')
# Do something else
```
async versions of context managers:
```python
from aws_xray_sdk.core import xray_recorder
async with xray_recorder.in_segment_async('segment_name') as segment:
# Add metadata or annotation here if necessary
segment.put_metadata('key', dict, 'namespace')
async with xray_recorder.in_subsegment_async('subsegment_name') as subsegment:
subsegment.put_annotation('key', 'value')
# Do something here
async with xray_recorder.in_subsegment_async('subsegment2') as subsegment:
subsegment.put_annotation('key2', 'value2')
# Do something else
```
Default begin/end functions:
```python
from aws_xray_sdk.core import xray_recorder
# Start a segment
segment = xray_recorder.begin_segment('segment_name')
# Start a subsegment
subsegment = xray_recorder.begin_subsegment('subsegment_name')
# Add metadata or annotation here if necessary
segment.put_metadata('key', dict, 'namespace')
subsegment.put_annotation('key', 'value')
xray_recorder.end_subsegment()
# Close the segment
xray_recorder.end_segment()
```
### Oversampling Mitigation
To modify the sampling decision at the subsegment level, subsegments that inherit the decision of their direct parent (segment or subsegment) can be created using `xray_recorder.begin_subsegment()` and unsampled subsegments can be created using
`xray_recorder.begin_subsegment_without_sampling()`.
The code snippet below demonstrates creating a sampled or unsampled subsegment based on the sampling decision of each SQS message processed by Lambda.
```python
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.models.subsegment import Subsegment
from aws_xray_sdk.core.utils.sqs_message_helper import SqsMessageHelper
def lambda_handler(event, context):
for message in event['Records']:
if SqsMessageHelper.isSampled(message):
subsegment = xray_recorder.begin_subsegment('sampled_subsegment')
print('sampled - processing SQS message')
else:
subsegment = xray_recorder.begin_subsegment_without_sampling('unsampled_subsegment')
print('unsampled - processing SQS message')
xray_recorder.end_subsegment()
```
The code snippet below demonstrates wrapping a downstream AWS SDK request with an unsampled subsegment.
```python
from aws_xray_sdk.core import xray_recorder, patch_all
import boto3
patch_all()
def lambda_handler(event, context):
subsegment = xray_recorder.begin_subsegment_without_sampling('unsampled_subsegment')
client = boto3.client('sqs')
print(client.list_queues())
xray_recorder.end_subsegment()
```
### Capture
As a decorator:
```python
from aws_xray_sdk.core import xray_recorder
@xray_recorder.capture('subsegment_name')
def myfunc():
# Do something here
myfunc()
```
or as a context manager:
```python
from aws_xray_sdk.core import xray_recorder
with xray_recorder.capture('subsegment_name') as subsegment:
# Do something here
subsegment.put_annotation('mykey', val)
# Do something more
```
Async capture as decorator:
```python
from aws_xray_sdk.core import xray_recorder
@xray_recorder.capture_async('subsegment_name')
async def myfunc():
# Do something here
async def main():
await myfunc()
```
or as context manager:
```python
from aws_xray_sdk.core import xray_recorder
async with xray_recorder.capture_async('subsegment_name') as subsegment:
# Do something here
subsegment.put_annotation('mykey', val)
# Do something more
```
### Adding annotations/metadata using recorder
```python
from aws_xray_sdk.core import xray_recorder
# Start a segment if no segment exist
segment1 = xray_recorder.begin_segment('segment_name')
# This will add the key value pair to segment1 as it is active
xray_recorder.put_annotation('key', 'value')
# Start a subsegment so it becomes the active trace entity
subsegment1 = xray_recorder.begin_subsegment('subsegment_name')
# This will add the key value pair to subsegment1 as it is active
xray_recorder.put_metadata('key', 'value')
if xray_recorder.is_sampled():
# some expensitve annotations/metadata generation code here
val = compute_annotation_val()
metadata = compute_metadata_body()
xray_recorder.put_annotation('mykey', val)
xray_recorder.put_metadata('mykey', metadata)
```
### Generate NoOp Trace and Entity Id
X-Ray Python SDK will by default generate no-op trace and entity id for unsampled requests and secure random trace and entity id for sampled requests. If customer wants to enable generating secure random trace and entity id for all the (sampled/unsampled) requests (this is applicable for trace id injection into logs use case) then they should set the `AWS_XRAY_NOOP_ID` environment variable as False.
### Disabling X-Ray
Often times, it may be useful to be able to disable X-Ray for specific use cases, whether to stop X-Ray from sending traces at any moment, or to test code functionality that originally depended on X-Ray instrumented packages to begin segments prior to the code call. For example, if your application relied on an XRayMiddleware to instrument incoming web requests, and you have a method which begins subsegments based on the segment generated by that middleware, it would be useful to be able to disable X-Ray for your unit tests so that `SegmentNotFound` exceptions are not thrown when you need to test your method.
There are two ways to disable X-Ray, one is through environment variables, and the other is through the SDKConfig module.
**Disabling through the environment variable:**
Prior to running your application, make sure to have the environment variable `AWS_XRAY_SDK_ENABLED` set to `false`.
**Disabling through the SDKConfig module:**
```
from aws_xray_sdk import global_sdk_config
global_sdk_config.set_sdk_enabled(False)
```
**Important Notes:**
* Environment Variables always take precedence over the SDKConfig module when disabling/enabling. If your environment variable is set to `false` while your code calls `global_sdk_config.set_sdk_enabled(True)`, X-Ray will still be disabled.
* If you need to re-enable X-Ray again during runtime and acknowledge disabling/enabling through the SDKConfig module, you may run the following in your application:
```
import os
from aws_xray_sdk import global_sdk_config
del os.environ['AWS_XRAY_SDK_ENABLED']
global_sdk_config.set_sdk_enabled(True)
```
### Trace AWS Lambda functions
```python
from aws_xray_sdk.core import xray_recorder
def lambda_handler(event, context):
# ... some code
subsegment = xray_recorder.begin_subsegment('subsegment_name')
# Code to record
# Add metadata or annotation here, if necessary
subsegment.put_metadata('key', dict, 'namespace')
subsegment.put_annotation('key', 'value')
xray_recorder.end_subsegment()
# ... some other code
```
### Trace ThreadPoolExecutor
```python
import concurrent.futures
import requests
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch
patch(('requests',))
URLS = ['http://www.amazon.com/',
'http://aws.amazon.com/',
'http://example.com/',
'http://www.bilibili.com/',
'http://invalid-domain.com/']
def load_url(url, trace_entity):
# Set the parent X-Ray entity for the worker thread.
xray_recorder.set_trace_entity(trace_entity)
# Subsegment captured from the following HTTP GET will be
# a child of parent entity passed from the main thread.
resp = requests.get(url)
# prevent thread pollution
xray_recorder.clear_trace_entities()
return resp
# Get the current active segment or subsegment from the main thread.
current_entity = xray_recorder.get_trace_entity()
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# Pass the active entity from main thread to worker threads.
future_to_url = {executor.submit(load_url, url, current_entity): url for url in URLS}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
data = future.result()
except Exception:
pass
```
### Trace SQL queries
By default, if no other value is provided to `.configure()`, SQL trace streaming is enabled
for all the supported DB engines. Those currently are:
- Any engine attached to the Django ORM.
- Any engine attached to SQLAlchemy.
The behaviour can be toggled by sending the appropriate `stream_sql` value, for example:
```python
from aws_xray_sdk.core import xray_recorder
xray_recorder.configure(service='fallback_name', stream_sql=True)
```
### Patch third-party libraries
```python
from aws_xray_sdk.core import patch
libs_to_patch = ('boto3', 'mysql', 'requests')
patch(libs_to_patch)
```
#### Automatic module patching
Full modules in the local codebase can be recursively patched by providing the module references
to the patch function.
```python
from aws_xray_sdk.core import patch
libs_to_patch = ('boto3', 'requests', 'local.module.ref', 'other_module')
patch(libs_to_patch)
```
An `xray_recorder.capture()` decorator will be applied to all functions and class methods in the
given module and all the modules inside them recursively. Some files/modules can be excluded by
providing to the `patch` function a regex that matches them.
```python
from aws_xray_sdk.core import patch
libs_to_patch = ('boto3', 'requests', 'local.module.ref', 'other_module')
ignore = ('local.module.ref.some_file', 'other_module.some_module\.*')
patch(libs_to_patch, ignore_module_patterns=ignore)
```
### Django
#### Add Django middleware
In django settings.py, use the following.
```python
INSTALLED_APPS = [
# ... other apps
'aws_xray_sdk.ext.django',
]
MIDDLEWARE = [
'aws_xray_sdk.ext.django.middleware.XRayMiddleware',
# ... other middlewares
]
```
You can configure the X-Ray recorder in a Django app under the ‘XRAY_RECORDER’ namespace. For a minimal configuration, the 'AWS_XRAY_TRACING_NAME' is required unless it is specified in an environment variable.
```
XRAY_RECORDER = {
'AWS_XRAY_TRACING_NAME': 'My application', # Required - the segment name for segments generated from incoming requests
}
```
For more information about configuring Django with X-Ray read more about it in the [API reference](https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/frameworks.html)
#### SQL tracing
If Django's ORM is patched - either using the `AUTO_INSTRUMENT = True` in your settings file
or explicitly calling `patch_db()` - the SQL query trace streaming can then be enabled or
disabled updating the `STREAM_SQL` variable in your settings file. It is enabled by default.
#### Automatic patching
The automatic module patching can also be configured through Django settings.
```python
XRAY_RECORDER = {
'PATCH_MODULES': [
'boto3',
'requests',
'local.module.ref',
'other_module',
],
'IGNORE_MODULE_PATTERNS': [
'local.module.ref.some_file',
'other_module.some_module\.*',
],
...
}
```
If `AUTO_PATCH_PARENT_SEGMENT_NAME` is also specified, then a segment parent will be created
with the supplied name, wrapping the automatic patching so that it captures any dangling
subsegments created on the import patching.
### Django in Lambda
X-Ray can't search on http annotations in subsegments. To enable searching the middleware adds the http values as annotations
This allows searching in the X-Ray console like so
This is configurable in settings with `URLS_AS_ANNOTATION` that has 3 valid values
`LAMBDA` - the default, which uses URLs as annotations by default if running in a lambda context
`ALL` - do this for every request (useful if running in a mixed lambda/other deployment)
`NONE` - don't do this for any (avoiding hitting the 50 annotation limit)
```
annotation.url BEGINSWITH "https://your.url.com/here"
```
### Add Flask middleware
```python
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
app = Flask(__name__)
xray_recorder.configure(service='fallback_name', dynamic_naming='*mysite.com*')
XRayMiddleware(app, xray_recorder)
```
### Add Bottle middleware(plugin)
```python
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.ext.bottle.middleware import XRayMiddleware
app = Bottle()
xray_recorder.configure(service='fallback_name', dynamic_naming='*mysite.com*')
app.install(XRayMiddleware(xray_recorder))
```
### Serverless Support for Flask & Django & Bottle Using X-Ray
Serverless is an application model that enables you to shift more of your operational responsibilities to AWS. As a result, you can focus only on your applications and services, instead of the infrastructure management tasks such as server provisioning, patching, operating system maintenance, and capacity provisioning. With serverless, you can deploy your web application to [AWS Lambda](https://aws.amazon.com/lambda/) and have customers interact with it through a Lambda-invoking endpoint, such as [Amazon API Gateway](https://aws.amazon.com/api-gateway/).
X-Ray supports the Serverless model out of the box and requires no extra configuration. The middlewares in Lambda generate `Subsegments` instead of `Segments` when an endpoint is reached. This is because `Segments` cannot be generated inside the Lambda function, but it is generated automatically by the Lambda container. Therefore, when using the middlewares with this model, it is important to make sure that your methods only generate `Subsegments`.
The following guide shows an example of setting up a Serverless application that utilizes API Gateway and Lambda:
[Instrumenting Web Frameworks in a Serverless Environment](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-python-serverless.html)
### Working with aiohttp
Adding aiohttp middleware. Support aiohttp >= 2.3.
```python
from aiohttp import web
from aws_xray_sdk.ext.aiohttp.middleware import middleware
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.async_context import AsyncContext
xray_recorder.configure(service='fallback_name', context=AsyncContext())
app = web.Application(middlewares=[middleware])
app.router.add_get("/", handler)
web.run_app(app)
```
Tracing aiohttp client. Support aiohttp >=3.
```python
from aws_xray_sdk.ext.aiohttp.client import aws_xray_trace_config
async def foo():
trace_config = aws_xray_trace_config()
async with ClientSession(loop=loop, trace_configs=[trace_config]) as session:
async with session.get(url) as resp
await resp.read()
```
### Use SQLAlchemy ORM
The SQLAlchemy integration requires you to override the Session and Query Classes for SQL Alchemy
SQLAlchemy integration uses subsegments so you need to have a segment started before you make a query.
```python
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.ext.sqlalchemy.query import XRaySessionMaker
xray_recorder.begin_segment('SQLAlchemyTest')
Session = XRaySessionMaker(bind=engine)
session = Session()
xray_recorder.end_segment()
app = Flask(__name__)
xray_recorder.configure(service='fallback_name', dynamic_naming='*mysite.com*')
XRayMiddleware(app, xray_recorder)
```
### Add Flask-SQLAlchemy
```python
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
from aws_xray_sdk.ext.flask_sqlalchemy.query import XRayFlaskSqlAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
XRayMiddleware(app, xray_recorder)
db = XRayFlaskSqlAlchemy(app)
```
### Ignoring httplib requests
If you want to ignore certain httplib requests you can do so based on the hostname or URL that is being requsted. The hostname is matched using the Python [fnmatch library](https://docs.python.org/3/library/fnmatch.html) which does Unix glob style matching.
```python
from aws_xray_sdk.ext.httplib import add_ignored as xray_add_ignored
# ignore requests to test.myapp.com
xray_add_ignored(hostname='test.myapp.com')
# ignore requests to a subdomain of myapp.com with a glob pattern
xray_add_ignored(hostname='*.myapp.com')
# ignore requests to /test-url and /other-test-url
xray_add_ignored(urls=['/test-path', '/other-test-path'])
# ignore requests to myapp.com for /test-url
xray_add_ignored(hostname='myapp.com', urls=['/test-url'])
```
If you use a subclass of httplib to make your requests, you can also filter on the class name that initiates the request. This must use the complete package name to do the match.
```python
from aws_xray_sdk.ext.httplib import add_ignored as xray_add_ignored
# ignore all requests made by botocore
xray_add_ignored(subclass='botocore.awsrequest.AWSHTTPConnection')
```
## License
The AWS X-Ray SDK for Python is licensed under the Apache 2.0 License. See LICENSE and NOTICE.txt for more information.
Raw data
{
"_id": null,
"home_page": "https://github.com/aws/aws-xray-sdk-python",
"name": "aws-xray-sdk",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "aws xray sdk",
"author": "Amazon Web Services",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/e0/6c/8e7fb2a45f20afc5c19d52807b560793fb48b0feca1de7de116b62a7893e/aws_xray_sdk-2.14.0.tar.gz",
"platform": null,
"description": "![Build Status](https://github.com/aws/aws-xray-sdk-python/actions/workflows/IntegrationTesting.yaml/badge.svg)\n[![codecov](https://codecov.io/gh/aws/aws-xray-sdk-python/branch/master/graph/badge.svg)](https://codecov.io/gh/aws/aws-xray-sdk-python)\n\n### :mega: OpenTelemetry Python with AWS X-Ray\n\nAWS X-Ray supports using OpenTelemetry Python and the AWS Distro for OpenTelemetry (ADOT) Collector to instrument your application and send trace data to X-Ray. The OpenTelemetry SDKs are an industry-wide standard for tracing instrumentation. They provide more instrumentations and have a larger community for support, but may not have complete feature parity with the X-Ray SDKs. See [choosing between the ADOT and X-Ray SDKs](https://docs.aws.amazon.com/xray/latest/devguide/xray-instrumenting-your-app.html#xray-instrumenting-choosing) for more help with choosing between the two.\n\nIf you want additional features when tracing your Python applications, please [open an issue on the OpenTelemetry Python Instrumentation repository](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/new?labels=feature-request&template=feature_request.md&title=X-Ray%20Compatible%20Feature%20Request).\n\n### :mega: Python Versions End-of-Support Notice\n\nAWS X-Ray SDK for Python versions `>2.11.0` has dropped support for Python 2.7, 3.4, 3.5, and 3.6.\n\n# AWS X-Ray SDK for Python\n\n![Screenshot of the AWS X-Ray console](/images/example_servicemap.png?raw=true)\n\n## Installing\n\nThe AWS X-Ray SDK for Python is compatible with Python 3.7, 3.8, 3.9, 3.10, and 3.11.\n\nInstall the SDK using the following command (the SDK's non-testing dependencies will be installed).\n\n```\npip install aws-xray-sdk\n```\n\nTo install the SDK's testing dependencies, use the following command.\n\n```\npip install tox\n```\n\n## Getting Help\n\nUse the following community resources for getting help with the SDK. We use the GitHub\nissues for tracking bugs and feature requests.\n\n* Ask a question in the [AWS X-Ray Forum](https://forums.aws.amazon.com/forum.jspa?forumID=241&start=0).\n* Open a support ticket with [AWS Support](http://docs.aws.amazon.com/awssupport/latest/user/getting-started.html).\n* If you think you may have found a bug, open an [issue](https://github.com/aws/aws-xray-sdk-python/issues/new).\n\n## Opening Issues\n\nIf you encounter a bug with the AWS X-Ray SDK for Python, we want to hear about\nit. Before opening a new issue, search the [existing issues](https://github.com/aws/aws-xray-sdk-python/issues)\nto see if others are also experiencing the issue. Include the version of the AWS X-Ray\nSDK for Python, Python language, and botocore/boto3 if applicable. In addition, \ninclude the repro case when appropriate.\n\nThe GitHub issues are intended for bug reports and feature requests. For help and\nquestions about using the AWS SDK for Python, use the resources listed\nin the [Getting Help](https://github.com/aws/aws-xray-sdk-python#getting-help) section. Keeping the list of open issues lean helps us respond in a timely manner.\n\n## Documentation\n\nThe [developer guide](https://docs.aws.amazon.com/xray/latest/devguide) provides in-depth\nguidance about using the AWS X-Ray service.\nThe [API Reference](http://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/)\nprovides guidance for using the SDK and module-level documentation.\n\n## Quick Start\n\n### Configuration\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\nxray_recorder.configure(\n sampling=False,\n context_missing='LOG_ERROR',\n plugins=('EC2Plugin', 'ECSPlugin', 'ElasticBeanstalkPlugin'),\n daemon_address='127.0.0.1:3000',\n dynamic_naming='*mysite.com*'\n)\n```\n\n### Start a custom segment/subsegment\n\nUsing context managers for implicit exceptions recording:\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\nwith xray_recorder.in_segment('segment_name') as segment:\n # Add metadata or annotation here if necessary\n segment.put_metadata('key', dict, 'namespace')\n with xray_recorder.in_subsegment('subsegment_name') as subsegment:\n subsegment.put_annotation('key', 'value')\n # Do something here\n with xray_recorder.in_subsegment('subsegment2') as subsegment:\n subsegment.put_annotation('key2', 'value2')\n # Do something else \n```\n\nasync versions of context managers:\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\nasync with xray_recorder.in_segment_async('segment_name') as segment:\n # Add metadata or annotation here if necessary\n segment.put_metadata('key', dict, 'namespace')\n async with xray_recorder.in_subsegment_async('subsegment_name') as subsegment:\n subsegment.put_annotation('key', 'value')\n # Do something here\n async with xray_recorder.in_subsegment_async('subsegment2') as subsegment:\n subsegment.put_annotation('key2', 'value2')\n # Do something else \n```\n\nDefault begin/end functions:\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\n# Start a segment\nsegment = xray_recorder.begin_segment('segment_name')\n# Start a subsegment\nsubsegment = xray_recorder.begin_subsegment('subsegment_name')\n\n# Add metadata or annotation here if necessary\nsegment.put_metadata('key', dict, 'namespace')\nsubsegment.put_annotation('key', 'value')\nxray_recorder.end_subsegment()\n\n# Close the segment\nxray_recorder.end_segment()\n```\n\n### Oversampling Mitigation\nTo modify the sampling decision at the subsegment level, subsegments that inherit the decision of their direct parent (segment or subsegment) can be created using `xray_recorder.begin_subsegment()` and unsampled subsegments can be created using\n`xray_recorder.begin_subsegment_without_sampling()`.\n\nThe code snippet below demonstrates creating a sampled or unsampled subsegment based on the sampling decision of each SQS message processed by Lambda.\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\nfrom aws_xray_sdk.core.models.subsegment import Subsegment\nfrom aws_xray_sdk.core.utils.sqs_message_helper import SqsMessageHelper\n\ndef lambda_handler(event, context):\n\n for message in event['Records']:\n if SqsMessageHelper.isSampled(message):\n subsegment = xray_recorder.begin_subsegment('sampled_subsegment')\n print('sampled - processing SQS message')\n\n else:\n subsegment = xray_recorder.begin_subsegment_without_sampling('unsampled_subsegment')\n print('unsampled - processing SQS message')\n \n xray_recorder.end_subsegment() \n```\n\nThe code snippet below demonstrates wrapping a downstream AWS SDK request with an unsampled subsegment.\n```python\nfrom aws_xray_sdk.core import xray_recorder, patch_all\nimport boto3\n\npatch_all()\n\ndef lambda_handler(event, context):\n subsegment = xray_recorder.begin_subsegment_without_sampling('unsampled_subsegment')\n client = boto3.client('sqs')\n print(client.list_queues())\n \n xray_recorder.end_subsegment()\n```\n\n### Capture\n\nAs a decorator:\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\n@xray_recorder.capture('subsegment_name')\ndef myfunc():\n # Do something here\n\nmyfunc()\n```\n\nor as a context manager:\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\nwith xray_recorder.capture('subsegment_name') as subsegment:\n # Do something here\n subsegment.put_annotation('mykey', val)\n # Do something more\n```\n\nAsync capture as decorator:\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\n@xray_recorder.capture_async('subsegment_name')\nasync def myfunc():\n # Do something here\n\nasync def main():\n await myfunc()\n```\n\nor as context manager:\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\nasync with xray_recorder.capture_async('subsegment_name') as subsegment:\n # Do something here\n subsegment.put_annotation('mykey', val)\n # Do something more\n```\n\n### Adding annotations/metadata using recorder\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\n# Start a segment if no segment exist\nsegment1 = xray_recorder.begin_segment('segment_name')\n\n# This will add the key value pair to segment1 as it is active\nxray_recorder.put_annotation('key', 'value')\n\n# Start a subsegment so it becomes the active trace entity\nsubsegment1 = xray_recorder.begin_subsegment('subsegment_name')\n\n# This will add the key value pair to subsegment1 as it is active\nxray_recorder.put_metadata('key', 'value')\n\nif xray_recorder.is_sampled():\n # some expensitve annotations/metadata generation code here\n val = compute_annotation_val()\n metadata = compute_metadata_body()\n xray_recorder.put_annotation('mykey', val)\n xray_recorder.put_metadata('mykey', metadata)\n```\n\n### Generate NoOp Trace and Entity Id\nX-Ray Python SDK will by default generate no-op trace and entity id for unsampled requests and secure random trace and entity id for sampled requests. If customer wants to enable generating secure random trace and entity id for all the (sampled/unsampled) requests (this is applicable for trace id injection into logs use case) then they should set the `AWS_XRAY_NOOP_ID` environment variable as False.\n\n### Disabling X-Ray\nOften times, it may be useful to be able to disable X-Ray for specific use cases, whether to stop X-Ray from sending traces at any moment, or to test code functionality that originally depended on X-Ray instrumented packages to begin segments prior to the code call. For example, if your application relied on an XRayMiddleware to instrument incoming web requests, and you have a method which begins subsegments based on the segment generated by that middleware, it would be useful to be able to disable X-Ray for your unit tests so that `SegmentNotFound` exceptions are not thrown when you need to test your method.\n\nThere are two ways to disable X-Ray, one is through environment variables, and the other is through the SDKConfig module.\n\n**Disabling through the environment variable:**\n\nPrior to running your application, make sure to have the environment variable `AWS_XRAY_SDK_ENABLED` set to `false`. \n\n**Disabling through the SDKConfig module:**\n```\nfrom aws_xray_sdk import global_sdk_config\n\nglobal_sdk_config.set_sdk_enabled(False)\n```\n\n**Important Notes:**\n* Environment Variables always take precedence over the SDKConfig module when disabling/enabling. If your environment variable is set to `false` while your code calls `global_sdk_config.set_sdk_enabled(True)`, X-Ray will still be disabled.\n\n* If you need to re-enable X-Ray again during runtime and acknowledge disabling/enabling through the SDKConfig module, you may run the following in your application:\n```\nimport os\nfrom aws_xray_sdk import global_sdk_config\n\ndel os.environ['AWS_XRAY_SDK_ENABLED']\nglobal_sdk_config.set_sdk_enabled(True)\n```\n\n### Trace AWS Lambda functions\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\ndef lambda_handler(event, context):\n # ... some code\n\n subsegment = xray_recorder.begin_subsegment('subsegment_name')\n # Code to record\n # Add metadata or annotation here, if necessary\n subsegment.put_metadata('key', dict, 'namespace')\n subsegment.put_annotation('key', 'value')\n\n xray_recorder.end_subsegment()\n\n # ... some other code\n```\n\n### Trace ThreadPoolExecutor\n\n```python\nimport concurrent.futures\n\nimport requests\n\nfrom aws_xray_sdk.core import xray_recorder\nfrom aws_xray_sdk.core import patch\n\npatch(('requests',))\n\nURLS = ['http://www.amazon.com/',\n 'http://aws.amazon.com/',\n 'http://example.com/',\n 'http://www.bilibili.com/',\n 'http://invalid-domain.com/']\n\ndef load_url(url, trace_entity):\n # Set the parent X-Ray entity for the worker thread.\n xray_recorder.set_trace_entity(trace_entity)\n # Subsegment captured from the following HTTP GET will be\n # a child of parent entity passed from the main thread.\n resp = requests.get(url)\n # prevent thread pollution\n xray_recorder.clear_trace_entities()\n return resp\n\n# Get the current active segment or subsegment from the main thread.\ncurrent_entity = xray_recorder.get_trace_entity()\nwith concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:\n # Pass the active entity from main thread to worker threads.\n future_to_url = {executor.submit(load_url, url, current_entity): url for url in URLS}\n for future in concurrent.futures.as_completed(future_to_url):\n url = future_to_url[future]\n try:\n data = future.result()\n except Exception:\n pass\n```\n\n### Trace SQL queries\nBy default, if no other value is provided to `.configure()`, SQL trace streaming is enabled\nfor all the supported DB engines. Those currently are:\n- Any engine attached to the Django ORM.\n- Any engine attached to SQLAlchemy.\n\nThe behaviour can be toggled by sending the appropriate `stream_sql` value, for example:\n```python\nfrom aws_xray_sdk.core import xray_recorder\n\nxray_recorder.configure(service='fallback_name', stream_sql=True)\n```\n\n### Patch third-party libraries\n\n```python\nfrom aws_xray_sdk.core import patch\n\nlibs_to_patch = ('boto3', 'mysql', 'requests')\npatch(libs_to_patch)\n```\n\n#### Automatic module patching\n\nFull modules in the local codebase can be recursively patched by providing the module references\nto the patch function.\n```python\nfrom aws_xray_sdk.core import patch\n\nlibs_to_patch = ('boto3', 'requests', 'local.module.ref', 'other_module')\npatch(libs_to_patch)\n```\nAn `xray_recorder.capture()` decorator will be applied to all functions and class methods in the\ngiven module and all the modules inside them recursively. Some files/modules can be excluded by\nproviding to the `patch` function a regex that matches them.\n```python\nfrom aws_xray_sdk.core import patch\n\nlibs_to_patch = ('boto3', 'requests', 'local.module.ref', 'other_module')\nignore = ('local.module.ref.some_file', 'other_module.some_module\\.*')\npatch(libs_to_patch, ignore_module_patterns=ignore)\n```\n\n### Django\n#### Add Django middleware\n\nIn django settings.py, use the following.\n\n```python\nINSTALLED_APPS = [\n # ... other apps\n 'aws_xray_sdk.ext.django',\n]\n\nMIDDLEWARE = [\n 'aws_xray_sdk.ext.django.middleware.XRayMiddleware',\n # ... other middlewares\n]\n```\n\nYou can configure the X-Ray recorder in a Django app under the \u2018XRAY_RECORDER\u2019 namespace. For a minimal configuration, the 'AWS_XRAY_TRACING_NAME' is required unless it is specified in an environment variable.\n```\nXRAY_RECORDER = {\n 'AWS_XRAY_TRACING_NAME': 'My application', # Required - the segment name for segments generated from incoming requests\n}\n```\nFor more information about configuring Django with X-Ray read more about it in the [API reference](https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/frameworks.html)\n\n#### SQL tracing\nIf Django's ORM is patched - either using the `AUTO_INSTRUMENT = True` in your settings file\nor explicitly calling `patch_db()` - the SQL query trace streaming can then be enabled or \ndisabled updating the `STREAM_SQL` variable in your settings file. It is enabled by default.\n\n#### Automatic patching\nThe automatic module patching can also be configured through Django settings.\n```python\nXRAY_RECORDER = {\n 'PATCH_MODULES': [\n 'boto3',\n 'requests',\n 'local.module.ref',\n 'other_module',\n ],\n 'IGNORE_MODULE_PATTERNS': [\n 'local.module.ref.some_file',\n 'other_module.some_module\\.*',\n ],\n ...\n}\n```\nIf `AUTO_PATCH_PARENT_SEGMENT_NAME` is also specified, then a segment parent will be created \nwith the supplied name, wrapping the automatic patching so that it captures any dangling\nsubsegments created on the import patching.\n\n### Django in Lambda\nX-Ray can't search on http annotations in subsegments. To enable searching the middleware adds the http values as annotations\nThis allows searching in the X-Ray console like so\n\nThis is configurable in settings with `URLS_AS_ANNOTATION` that has 3 valid values\n`LAMBDA` - the default, which uses URLs as annotations by default if running in a lambda context\n`ALL` - do this for every request (useful if running in a mixed lambda/other deployment)\n`NONE` - don't do this for any (avoiding hitting the 50 annotation limit)\n\n```\nannotation.url BEGINSWITH \"https://your.url.com/here\"\n```\n\n### Add Flask middleware\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\nfrom aws_xray_sdk.ext.flask.middleware import XRayMiddleware\n\napp = Flask(__name__)\n\nxray_recorder.configure(service='fallback_name', dynamic_naming='*mysite.com*')\nXRayMiddleware(app, xray_recorder)\n```\n\n### Add Bottle middleware(plugin)\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\nfrom aws_xray_sdk.ext.bottle.middleware import XRayMiddleware\n\napp = Bottle()\n\nxray_recorder.configure(service='fallback_name', dynamic_naming='*mysite.com*')\napp.install(XRayMiddleware(xray_recorder))\n```\n\n### Serverless Support for Flask & Django & Bottle Using X-Ray\nServerless is an application model that enables you to shift more of your operational responsibilities to AWS. As a result, you can focus only on your applications and services, instead of the infrastructure management tasks such as server provisioning, patching, operating system maintenance, and capacity provisioning. With serverless, you can deploy your web application to [AWS Lambda](https://aws.amazon.com/lambda/) and have customers interact with it through a Lambda-invoking endpoint, such as [Amazon API Gateway](https://aws.amazon.com/api-gateway/). \n\nX-Ray supports the Serverless model out of the box and requires no extra configuration. The middlewares in Lambda generate `Subsegments` instead of `Segments` when an endpoint is reached. This is because `Segments` cannot be generated inside the Lambda function, but it is generated automatically by the Lambda container. Therefore, when using the middlewares with this model, it is important to make sure that your methods only generate `Subsegments`.\n\nThe following guide shows an example of setting up a Serverless application that utilizes API Gateway and Lambda:\n\n[Instrumenting Web Frameworks in a Serverless Environment](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-python-serverless.html)\n\n### Working with aiohttp\n\nAdding aiohttp middleware. Support aiohttp >= 2.3.\n\n```python\nfrom aiohttp import web\n\nfrom aws_xray_sdk.ext.aiohttp.middleware import middleware\nfrom aws_xray_sdk.core import xray_recorder\nfrom aws_xray_sdk.core.async_context import AsyncContext\n\nxray_recorder.configure(service='fallback_name', context=AsyncContext())\n\napp = web.Application(middlewares=[middleware])\napp.router.add_get(\"/\", handler)\n\nweb.run_app(app)\n```\n\nTracing aiohttp client. Support aiohttp >=3.\n\n```python\nfrom aws_xray_sdk.ext.aiohttp.client import aws_xray_trace_config\n\nasync def foo():\n trace_config = aws_xray_trace_config()\n async with ClientSession(loop=loop, trace_configs=[trace_config]) as session:\n async with session.get(url) as resp\n await resp.read()\n```\n\n### Use SQLAlchemy ORM\nThe SQLAlchemy integration requires you to override the Session and Query Classes for SQL Alchemy\n\nSQLAlchemy integration uses subsegments so you need to have a segment started before you make a query.\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\nfrom aws_xray_sdk.ext.sqlalchemy.query import XRaySessionMaker\n\nxray_recorder.begin_segment('SQLAlchemyTest')\n\nSession = XRaySessionMaker(bind=engine)\nsession = Session()\n\nxray_recorder.end_segment()\napp = Flask(__name__)\n\nxray_recorder.configure(service='fallback_name', dynamic_naming='*mysite.com*')\nXRayMiddleware(app, xray_recorder)\n```\n\n### Add Flask-SQLAlchemy\n\n```python\nfrom aws_xray_sdk.core import xray_recorder\nfrom aws_xray_sdk.ext.flask.middleware import XRayMiddleware\nfrom aws_xray_sdk.ext.flask_sqlalchemy.query import XRayFlaskSqlAlchemy\n\napp = Flask(__name__)\napp.config[\"SQLALCHEMY_DATABASE_URI\"] = \"sqlite:///:memory:\"\n\nXRayMiddleware(app, xray_recorder)\ndb = XRayFlaskSqlAlchemy(app)\n\n```\n\n### Ignoring httplib requests\n\nIf you want to ignore certain httplib requests you can do so based on the hostname or URL that is being requsted. The hostname is matched using the Python [fnmatch library](https://docs.python.org/3/library/fnmatch.html) which does Unix glob style matching.\n\n```python\nfrom aws_xray_sdk.ext.httplib import add_ignored as xray_add_ignored\n\n# ignore requests to test.myapp.com\nxray_add_ignored(hostname='test.myapp.com')\n\n# ignore requests to a subdomain of myapp.com with a glob pattern\nxray_add_ignored(hostname='*.myapp.com')\n\n# ignore requests to /test-url and /other-test-url\nxray_add_ignored(urls=['/test-path', '/other-test-path'])\n\n# ignore requests to myapp.com for /test-url\nxray_add_ignored(hostname='myapp.com', urls=['/test-url'])\n```\n\nIf you use a subclass of httplib to make your requests, you can also filter on the class name that initiates the request. This must use the complete package name to do the match.\n\n```python\nfrom aws_xray_sdk.ext.httplib import add_ignored as xray_add_ignored\n\n# ignore all requests made by botocore\nxray_add_ignored(subclass='botocore.awsrequest.AWSHTTPConnection')\n```\n\n## License\n\nThe AWS X-Ray SDK for Python is licensed under the Apache 2.0 License. See LICENSE and NOTICE.txt for more information.\n",
"bugtrack_url": null,
"license": "Apache License 2.0",
"summary": "The AWS X-Ray SDK for Python (the SDK) enables Python developers to record and emit information from within their applications to the AWS X-Ray service.",
"version": "2.14.0",
"project_urls": {
"Homepage": "https://github.com/aws/aws-xray-sdk-python"
},
"split_keywords": [
"aws",
"xray",
"sdk"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4169b417833a8926fa5491e5346d7c233bf7d8a9b12ba1f4ef41ccea2494000c",
"md5": "faf100dabc632e9ca8880e9b336c12b6",
"sha256": "cfbe6feea3d26613a2a869d14c9246a844285c97087ad8f296f901633554ad94"
},
"downloads": -1,
"filename": "aws_xray_sdk-2.14.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "faf100dabc632e9ca8880e9b336c12b6",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.7",
"size": 101922,
"upload_time": "2024-06-04T22:12:25",
"upload_time_iso_8601": "2024-06-04T22:12:25.729944Z",
"url": "https://files.pythonhosted.org/packages/41/69/b417833a8926fa5491e5346d7c233bf7d8a9b12ba1f4ef41ccea2494000c/aws_xray_sdk-2.14.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e06c8e7fb2a45f20afc5c19d52807b560793fb48b0feca1de7de116b62a7893e",
"md5": "c950da68bb289d37cd4ccdfa4238f8c2",
"sha256": "aab843c331af9ab9ba5cefb3a303832a19db186140894a523edafc024cc0493c"
},
"downloads": -1,
"filename": "aws_xray_sdk-2.14.0.tar.gz",
"has_sig": false,
"md5_digest": "c950da68bb289d37cd4ccdfa4238f8c2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 93976,
"upload_time": "2024-06-04T22:11:38",
"upload_time_iso_8601": "2024-06-04T22:11:38.124410Z",
"url": "https://files.pythonhosted.org/packages/e0/6c/8e7fb2a45f20afc5c19d52807b560793fb48b0feca1de7de116b62a7893e/aws_xray_sdk-2.14.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-04 22:11:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "aws",
"github_project": "aws-xray-sdk-python",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "aws-xray-sdk"
}