# xraysink
<div align="center">
<a href="https://pypi.org/project/xraysink/">
<img src="https://img.shields.io/pypi/v/xraysink.svg" alt="Package version">
</a>
<a href="https://pypi.org/project/xraysink/">
<img src="https://img.shields.io/pypi/pyversions/xraysink.svg" alt="Python versions">
</a>
<a href="https://pypi.org/project/xraysink/">
<img src="https://img.shields.io/pypi/dm/xraysink.svg" alt="Monthly downloads">
</a>
</div>
<div align="center">
<!-- Coverage badge stored in our wiki by the python-coverage-comment-action plugin -->
<a href="https://github.com/garyd203/xraysink/">
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/wiki/garyd203/xraysink/python-coverage-comment-action-badge.json" alt="Coverage">
</a>
</div>
Extra AWS X-Ray instrumentation to use distributed tracing with asyncio Python
libraries that are not (yet) supported by the official
[aws_xray_sdk](https://github.com/aws/aws-xray-sdk-python) library.
## What Problem Does xraysink Solve?
`aws_xray_sdk` is the standard library to collect trace data from your Python
code and send the trace data to the
[AWS X-Ray distributed tracing tool](https://aws.amazon.com/xray/). However,
if you have asyncio Python code, then there are some gaps and occasional
bugs in the functionality provided by that library. `xraysink` plugs those gaps.
It can be a bit confusing using two libraries together, so here's a high-level
breakdown of which library will help you do what:
* Add tracing to HTTP requests handled by FastAPI (or another async Python
web framework): `xraysink` (via [middleware](#fastapi))
* Add tracing to background (non-HTTP-request) functions written as async
Python functions: `xraysink` (via [xray_task_async](#background-jobstasks)
decorator)
* Everything else: [aws_xray_sdk](https://github.com/aws/aws-xray-sdk-python)
## Integrations Supported
* Generic ASGI-compatible tracing middleware for *any* ASGI-compliant web
framework. This has been tested with:
- [aiohttp server](https://docs.aiohttp.org/en/stable/)
- [FastAPI](https://fastapi.tiangolo.com/)
* asyncio [Task's](https://docs.python.org/3/library/asyncio-task.html)
* Background jobs/tasks
## Installation
xraysink is distributed as a standard python package through
[pypi](https://pypi.org/), so you can install it with your favourite Python
package manager. For example:
pip install xraysink
## How to use
`xraysink` augments the functionality provided by `aws_xray_sdk`. Before
using the tools in `xraysink`, you first need to configure `aws_xray_sdk` -
this will probably involve calling `xray_recorder.configure()` when your
process starts, and optionally `aws_xray_sdk.core.patch()`.
Extra instrumentation provided by `xraysink` is described below.
### FastAPI
Instrument incoming requests in your FastAPI web server by adding the
`xray_middleware` to your app. For example, you could do:
from starlette.middleware.base import BaseHTTPMiddleware
from xraysink.asgi.middleware import xray_middleware
# Standard asyncio X-Ray configuration, customise as you choose
xray_recorder.configure(context=AsyncContext(), service="my-cute-little-service")
# Create a FastAPI app with various middleware
app = FastAPI()
app.add_middleware(MyTracingDependentMiddleware) # Any middleware that is added earlier will have the X-Ray tracing context available to it
app.add_middleware(BaseHTTPMiddleware, dispatch=xray_middleware)
### Asyncio Tasks
If you start asyncio [Task's](https://docs.python.org/3/library/asyncio-task.html)
from a standard request handler, then the AWS X-Ray SDK will not correctly
instrument any outgoing requests made inside those Tasks.
Use the fixed `AsyncContext` from `xraysink` as a drop-in replacement, like so:
from aws_xray_sdk.core import xray_recorder
from xraysink.context import AsyncContext # NB: Use the AsyncContext from xraysink
# Use the fixed AsyncContext when configuring X-Ray,
# and customise other configuration as you choose.
xray_recorder.configure(context=AsyncContext(use_task_factory=True))
### Background Jobs/Tasks
If your process starts background tasks that make network calls (eg. to the
database or an API in another service), then each execution of one of those
tasks should be treated as a new X-Ray trace. Indeed, if you don't do so then
you will likely get `context_missing` errors.
An async function that implements a background task can be easily instrumented
using the `@xray_task_async()` decorator, like so:
from aws_xray_sdk.core import xray_recorder
from xraysink.tasks import xray_task_async
# Standard asyncio X-Ray configuration, customise as you choose
xray_recorder.configure(context=AsyncContext(), service="my-cute-little-service")
# Any call to this function will start a new X-Ray trace
@xray_task_async()
async def cleanup_stale_tokens():
await database.get_table("tokens").delete(age__gt=1)
# Start your background task using your scheduling system of choice :)
schedule_recurring_task(cleanup_stale_tokens)
If your background task functions are called from a function that is already
instrumented (eg. send an email immediately after handling a request), then
the background task will appear as a child segment of that trace. In this case,
you must ensure you use the non-buggy `AsyncContext` when configuring the recorder
(ie. `from xraysink.context import AsyncContext`)
### CloudWatch Logs integration
You can link your X-Ray traces to your CloudWatch Logs log records, which
enhances the integration with AWS CloudWatch ServiceLens. Take the following
steps:
1. Put the X-Ray trace ID into every log message. There is no convention for
how to do this (it just has to appear verbatim in the log message
somewhere), but if you are using structured logging then the convention is
to use a field called `traceId`. Here's an example
trace_id = xray_recorder.get_trace_entity().trace_id
logging.getLogger("example").info("Hello World!", extra={"traceId": trace_id})
1. Explicitly set the name of the CloudWatch Logs log group associated with
your process. There is no general way to detect the Log Group from inside
the process, hence it requires manual configuration as part of your process
initialisation (eg. in the same place where you call
`xray_recorder.configure`).
set_xray_log_group("/example/service-name")
Note that this feature relies on undocumented functionality, and is
[not yet](https://github.com/aws/aws-xray-sdk-python/issues/188)
supported by the official Python SDK.
## Licence
This project uses the Apache 2.0 licence, to make it compatible with
[aws_xray_sdk](https://github.com/aws/aws-xray-sdk-python), the
primary library for integrating with AWS X-Ray.
Raw data
{
"_id": null,
"home_page": "https://github.com/garyd203/xraysink",
"name": "xraysink",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7.9,<4.0.0",
"maintainer_email": "",
"keywords": "AWS,XRay,python,asyncio,distributed-tracing",
"author": "Gary Donovan",
"author_email": "gazza@gazza.id.au",
"download_url": "https://files.pythonhosted.org/packages/64/62/97840a86e20a5314c105d027682683cc2f55eb9ea567471489f30e10e1e1/xraysink-1.6.2.tar.gz",
"platform": null,
"description": "# xraysink\n\n<div align=\"center\">\n <a href=\"https://pypi.org/project/xraysink/\">\n <img src=\"https://img.shields.io/pypi/v/xraysink.svg\" alt=\"Package version\">\n </a>\n <a href=\"https://pypi.org/project/xraysink/\">\n <img src=\"https://img.shields.io/pypi/pyversions/xraysink.svg\" alt=\"Python versions\">\n </a>\n <a href=\"https://pypi.org/project/xraysink/\">\n <img src=\"https://img.shields.io/pypi/dm/xraysink.svg\" alt=\"Monthly downloads\">\n </a>\n</div>\n<div align=\"center\">\n <!-- Coverage badge stored in our wiki by the python-coverage-comment-action plugin -->\n <a href=\"https://github.com/garyd203/xraysink/\">\n <img src=\"https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/wiki/garyd203/xraysink/python-coverage-comment-action-badge.json\" alt=\"Coverage\">\n </a>\n</div>\n\nExtra AWS X-Ray instrumentation to use distributed tracing with asyncio Python\nlibraries that are not (yet) supported by the official\n[aws_xray_sdk](https://github.com/aws/aws-xray-sdk-python) library.\n\n\n## What Problem Does xraysink Solve?\n\n`aws_xray_sdk` is the standard library to collect trace data from your Python\ncode and send the trace data to the\n[AWS X-Ray distributed tracing tool](https://aws.amazon.com/xray/). However,\nif you have asyncio Python code, then there are some gaps and occasional\nbugs in the functionality provided by that library. `xraysink` plugs those gaps.\n\nIt can be a bit confusing using two libraries together, so here's a high-level\nbreakdown of which library will help you do what:\n\n* Add tracing to HTTP requests handled by FastAPI (or another async Python\n web framework): `xraysink` (via [middleware](#fastapi))\n* Add tracing to background (non-HTTP-request) functions written as async\n Python functions: `xraysink` (via [xray_task_async](#background-jobstasks)\n decorator)\n* Everything else: [aws_xray_sdk](https://github.com/aws/aws-xray-sdk-python)\n\n\n## Integrations Supported\n* Generic ASGI-compatible tracing middleware for *any* ASGI-compliant web\n framework. This has been tested with:\n - [aiohttp server](https://docs.aiohttp.org/en/stable/)\n - [FastAPI](https://fastapi.tiangolo.com/)\n* asyncio [Task's](https://docs.python.org/3/library/asyncio-task.html)\n* Background jobs/tasks\n\n\n## Installation\nxraysink is distributed as a standard python package through\n[pypi](https://pypi.org/), so you can install it with your favourite Python\npackage manager. For example:\n\n pip install xraysink\n\n\n## How to use\n`xraysink` augments the functionality provided by `aws_xray_sdk`. Before\nusing the tools in `xraysink`, you first need to configure `aws_xray_sdk` -\nthis will probably involve calling `xray_recorder.configure()` when your\nprocess starts, and optionally `aws_xray_sdk.core.patch()`.\n\nExtra instrumentation provided by `xraysink` is described below.\n\n### FastAPI\nInstrument incoming requests in your FastAPI web server by adding the\n`xray_middleware` to your app. For example, you could do:\n\n from starlette.middleware.base import BaseHTTPMiddleware\n from xraysink.asgi.middleware import xray_middleware\n \n # Standard asyncio X-Ray configuration, customise as you choose\n xray_recorder.configure(context=AsyncContext(), service=\"my-cute-little-service\")\n \n # Create a FastAPI app with various middleware\n app = FastAPI()\n app.add_middleware(MyTracingDependentMiddleware) # Any middleware that is added earlier will have the X-Ray tracing context available to it\n app.add_middleware(BaseHTTPMiddleware, dispatch=xray_middleware)\n\n\n### Asyncio Tasks\nIf you start asyncio [Task's](https://docs.python.org/3/library/asyncio-task.html)\nfrom a standard request handler, then the AWS X-Ray SDK will not correctly\ninstrument any outgoing requests made inside those Tasks.\n\nUse the fixed `AsyncContext` from `xraysink` as a drop-in replacement, like so:\n\n from aws_xray_sdk.core import xray_recorder\n from xraysink.context import AsyncContext # NB: Use the AsyncContext from xraysink\n \n # Use the fixed AsyncContext when configuring X-Ray,\n # and customise other configuration as you choose.\n xray_recorder.configure(context=AsyncContext(use_task_factory=True))\n\n\n### Background Jobs/Tasks\nIf your process starts background tasks that make network calls (eg. to the\ndatabase or an API in another service), then each execution of one of those\ntasks should be treated as a new X-Ray trace. Indeed, if you don't do so then\nyou will likely get `context_missing` errors.\n\nAn async function that implements a background task can be easily instrumented\nusing the `@xray_task_async()` decorator, like so:\n\n from aws_xray_sdk.core import xray_recorder\n from xraysink.tasks import xray_task_async\n\n # Standard asyncio X-Ray configuration, customise as you choose\n xray_recorder.configure(context=AsyncContext(), service=\"my-cute-little-service\")\n \n # Any call to this function will start a new X-Ray trace\n @xray_task_async()\n async def cleanup_stale_tokens():\n await database.get_table(\"tokens\").delete(age__gt=1)\n \n # Start your background task using your scheduling system of choice :)\n schedule_recurring_task(cleanup_stale_tokens)\n\nIf your background task functions are called from a function that is already\ninstrumented (eg. send an email immediately after handling a request), then \nthe background task will appear as a child segment of that trace. In this case,\nyou must ensure you use the non-buggy `AsyncContext` when configuring the recorder\n(ie. `from xraysink.context import AsyncContext`)\n\n\n### CloudWatch Logs integration\nYou can link your X-Ray traces to your CloudWatch Logs log records, which\nenhances the integration with AWS CloudWatch ServiceLens. Take the following\nsteps:\n\n1. Put the X-Ray trace ID into every log message. There is no convention for\n how to do this (it just has to appear verbatim in the log message\n somewhere), but if you are using structured logging then the convention is\n to use a field called `traceId`. Here's an example\n \n trace_id = xray_recorder.get_trace_entity().trace_id\n logging.getLogger(\"example\").info(\"Hello World!\", extra={\"traceId\": trace_id})\n\n1. Explicitly set the name of the CloudWatch Logs log group associated with\n your process. There is no general way to detect the Log Group from inside\n the process, hence it requires manual configuration as part of your process\n initialisation (eg. in the same place where you call\n `xray_recorder.configure`).\n \n set_xray_log_group(\"/example/service-name\")\n\nNote that this feature relies on undocumented functionality, and is\n[not yet](https://github.com/aws/aws-xray-sdk-python/issues/188)\nsupported by the official Python SDK.\n\n\n## Licence\nThis project uses the Apache 2.0 licence, to make it compatible with\n[aws_xray_sdk](https://github.com/aws/aws-xray-sdk-python), the\nprimary library for integrating with AWS X-Ray.\n\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "Instrument asyncio Python for distributed tracing with AWS X-Ray.",
"version": "1.6.2",
"project_urls": {
"Documentation": "https://github.com/garyd203/xraysink",
"Homepage": "https://github.com/garyd203/xraysink",
"Repository": "https://github.com/garyd203/xraysink"
},
"split_keywords": [
"aws",
"xray",
"python",
"asyncio",
"distributed-tracing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4c68209ea0a1ae2ea5ad48a43dad4bd53138e0548814e3cb474411caf652fb96",
"md5": "cb9f1e4c8585ee36b41ee3bb5ef3f106",
"sha256": "8e64d704effea9f625808d1608364ad4db9af28e4a0078b0bc7893c42cf45a4f"
},
"downloads": -1,
"filename": "xraysink-1.6.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cb9f1e4c8585ee36b41ee3bb5ef3f106",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7.9,<4.0.0",
"size": 15023,
"upload_time": "2023-08-23T11:17:16",
"upload_time_iso_8601": "2023-08-23T11:17:16.106391Z",
"url": "https://files.pythonhosted.org/packages/4c/68/209ea0a1ae2ea5ad48a43dad4bd53138e0548814e3cb474411caf652fb96/xraysink-1.6.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "646297840a86e20a5314c105d027682683cc2f55eb9ea567471489f30e10e1e1",
"md5": "8b82b7ae448ab361bedea1e7af7965a5",
"sha256": "05c7de4c56cb5ac02674650ba98a6ac0be2b43b20fafc21079caa343e086b156"
},
"downloads": -1,
"filename": "xraysink-1.6.2.tar.gz",
"has_sig": false,
"md5_digest": "8b82b7ae448ab361bedea1e7af7965a5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7.9,<4.0.0",
"size": 15492,
"upload_time": "2023-08-23T11:17:17",
"upload_time_iso_8601": "2023-08-23T11:17:17.251110Z",
"url": "https://files.pythonhosted.org/packages/64/62/97840a86e20a5314c105d027682683cc2f55eb9ea567471489f30e10e1e1/xraysink-1.6.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-23 11:17:17",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "garyd203",
"github_project": "xraysink",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "xraysink"
}