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:
  • asyncio Task's
  • Background jobs/tasks

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)

Asyncio Tasks

If you start asyncio Task's 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 xraysink.context import AsyncContext  # Use the AsyncContext from xraysink
from aws_xray_sdk.core import xray_recorder

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:

# 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.2.0.tar.gz (12.9 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.2.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for xraysink-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1cc3270c51eb0a57a42d92ee2a782329c27cc89322868fd86e33aaf2b6092fd9
MD5 b3b750e119a671cb4b8b463c13f2d5a2
BLAKE2b-256 3c440553632a04c46e51cd79f928b0357977150b390767925ef3383b91e4c4bd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for xraysink-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f62d61f7bcd3ad1a500b167ef710e81a5554ae0e9ac2b1146166182865cabb91
MD5 064e8b7c3a279799bbeb4ab2ad9caa94
BLAKE2b-256 b063f107c91986d8413be6dc5989590ba27437963e38d95857d3a3dd794cddd4

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