Skip to main content

Unofficial ddtrace integration for ASGI apps and frameworks

Project description

ddtrace-asgi

Build Status Coverage Package version

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+.
  • ddtrace must be installed to use the ddtrace-run command.
  • 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")

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 ddtrace 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", distributed_tracing=True):
        ...

An ASGI middleware that sends traces of HTTP requests to Datadog APM.

Parameters

  • app - An ASGI application.
  • tracer - (optional) A Tracer object. Defaults to the global ddtrace.tracer object.
  • service - (optional) Name of the service as it will appear on Datadog.
  • 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.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, service and distributed_tracing parameters of TraceMiddleware are now keyword-only. (Pull #10)

Added

  • The tracer for TraceMiddleware is now the global ddtrace.tracer by default. (Pull #10)

0.1.0 - 2019-10-23

Initial release.

Added

  • Add TracingMiddleware.

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

ddtrace-asgi-0.2.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

ddtrace_asgi-0.2.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file ddtrace-asgi-0.2.2.tar.gz.

File metadata

  • Download URL: ddtrace-asgi-0.2.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for ddtrace-asgi-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f9c8d9be37d11d4992f8cf86bc0d1d15faf7bf5bd090677450b09649c1b88dc4
MD5 db504b1021c7851694927667ed3521bd
BLAKE2b-256 39421914b6f99f9a38849d18b82f49dbdc0ce66326676317061a7b0f3c91c9f0

See more details on using hashes here.

File details

Details for the file ddtrace_asgi-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: ddtrace_asgi-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for ddtrace_asgi-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 57b9d002c0831ee7b4b016de7bca734cf3ba4b7d59a6fa93784f30c389b849f2
MD5 af966750a269b89f3e5c828d15721689
BLAKE2b-256 89270e600a1e75167774121c59b93ccc6d997f0de26b4ca1847bad58fea9ee5f

See more details on using hashes here.

Supported by

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