Skip to main content

Instrument asyncio Python for distributed tracing with AWS X-Ray.

Project description

xraysink (aka xray-asyncio)

Extra AWS X-Ray instrumentation to use distributed tracing with asyncio Python libraries that are not (yet) supported by the official aws_xray_sdk library.

Integrations Supported

  • Generic ASGI-compatible tracing middleware for any ASGI-compliant web framework. This has been tested with:

Installation

xraysink is distributed as a standard python package through pypi, so you can install it with your favourite Python package manager. For example:

pip install xraysink 

How to use

FastAPI

Instrument incoming requests in your FastAPI web server by adding the xray_middleware. For example:

# Basic asyncio X-Ray configuration
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)

Background 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:

# Basic asyncio X-Ray configuration
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)

schedule_recurring_task(cleanup_stale_tokens)

Process-Level Configuration

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})
    
  2. 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.

    set_xray_log_group("/example/service-name")
    

Note that this feature relies on undocumented functionality, and is not yet supported by the official Python SDK.

Licence

This project uses the Apache 2.0 licence, to make it compatible with aws_xray_sdk, the primary library for integrating with AWS X-Ray.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xraysink-1.1.0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xraysink-1.1.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file xraysink-1.1.0.tar.gz.

File metadata

  • Download URL: xraysink-1.1.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.3 CPython/3.7.3 Linux/4.4.0-19041-Microsoft

File hashes

Hashes for xraysink-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3bbee713e728ee3de5c82ee60d2baa4bf068391fe89cea8d6da52920ac517eed
MD5 7518ef4f46df6330022c1b5050ffa701
BLAKE2b-256 b7ca47bdf73c59bd5b4fef188151da39e1628a98fbf1b829142c1f1c6a5ff674

See more details on using hashes here.

File details

Details for the file xraysink-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: xraysink-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.3 CPython/3.7.3 Linux/4.4.0-19041-Microsoft

File hashes

Hashes for xraysink-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f41b997a75e3a3de6444d90b353a5999e4812591658e503dd602bd0a381bd3b5
MD5 8a22c4ed75f28621b73bb2f81e8eeb87
BLAKE2b-256 33a726c00617fa2a22e9b08a9119cdc79dbb8fefcf83e39856f4fd09eeaa77cb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page