ddtrace-asgi
Unofficial ddtrace integration for ASGI apps and frameworks.
Should work seamlessly for any ASGI web framework, e.g. Starlette, FastAPI, Quart, etc.
Note: This project is in alpha stage.
Requirements
- Python 3.6+.
ddtracemust be installed to use theddtrace-runcommand.- The Datadog Agent must be installed and running for traces to be effectively sent to Datadog APM.
Installation
pip install ddtrace-asgi
Quickstart
To automatically send traces to Datadog APM on each HTTP request, wrap your ASGI application around TraceMiddleware:
# app.py
from ddtrace_asgi.middleware import TraceMiddleware
async def app(scope, receive, send):
assert scope["type"] == "http"
headers = [[b"content-type", b"text/plain"]]
await send({"type": "http.response.start", "status": 200, "headers": headers})
await send({"type": "http.response.body", "body": b"Hello, world!"})
app = TraceMiddleware(
app, service="asgi-hello-world", tags={"env": "local"},
)
Then use ddtrace-run when serving your application. For example, if serving with Uvicorn:
ddtrace-run uvicorn app:app
For more information on using ddtrace, please see the official dd-trace-py repository.
Examples
Starlette
from ddtrace_asgi.middleware import TraceMiddleware
from starlette.applications import Starlette
app = Starlette()
app.add_middleware(TraceMiddleware, service="my-starlette-app")
FastAPI
from ddtrace_asgi.middleware import TraceMiddleware
from fastapi import FastAPI
app = FastAPI()
app.add_middleware(TraceMiddleware, service="my-fastapi-app")
API Reference
TracingMiddleware
class TracingMiddleware:
def __init__(self, app, tracer=None, service="asgi", tags=None, distributed_tracing=True):
...
An ASGI middleware that sends traces of HTTP requests to Datadog APM.
Parameters
- app - An [ASGI] application.
- tracer - (optional) A
Tracerobject. Defaults to the globalddtrace.tracerobject. - service - (optional) Name of the service as it will appear on Datadog.
- tags - (optional) Default tags attached to all request spans. Either a dictionary, or a string of comma-separated tags (e.g.
"env:staging, app:shop"). See also Tagging. - distributed_tracing - (optional) Whether to enable distributed tracing.
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog.
0.3.0 - 2019-11-15
Added
- Allow to set default tags on all request spans. (Pull #24)
0.2.2 - 2019-11-03
Added
- Now ships with binary distribution (wheel) in addition to source distribution (sdist). (Pull #16)
0.2.1 - 2019-10-31
Fixed
- Improve resilience to ASGI protocol violations. (Pull #11)
0.2.0 - 2019-10-30
Changed
- The
tracer,serviceanddistributed_tracingparameters ofTraceMiddlewareare now keyword-only. (Pull #10)
Added
- The
tracerforTraceMiddlewareis now the globalddtrace.tracerby default. (Pull #10)
0.1.0 - 2019-10-23
Initial release.
Added
- Add
TracingMiddleware.
Metadata
Release files for ddtrace-asgi 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ddtrace-asgi-0.3.0.tar.gz | 7.2 kB | Without provenance | Details for ddtrace-asgi-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ddtrace_asgi-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for ddtrace_asgi-0.3.0-py3-none-any.whl |
Total release size: 13.2 kB
Release files / ddtrace-asgi-0.3.0.tar.gz
| Download URL | ddtrace-asgi-0.3.0.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
424f3847eb4ad57897ad2c92c737365d0c5157bb9b6f21fafc74f975c235ff21
|
|
BLAKE2b-256 checksum How to use checksums |
ffc1646ebf480dc528000cf432d4f3598eb07503684cc495eddce16b755b9223
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.8.0
|
Release files / ddtrace_asgi-0.3.0-py3-none-any.whl
| Download URL | ddtrace_asgi-0.3.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1628c8e3bcdbfba2cbfb879c2eae7710b147a22c913f9c7f7e634b7ae0aba71a
|
|
BLAKE2b-256 checksum How to use checksums |
793903f23cf30c24ed410f3d6e746b5b5800fb2d0948aaaf2e5d0690993a7eba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.8.0
|
Security for ddtrace-asgi 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.