Skip to main content

Export metrics about requests in your ASGI apps to statsd or Datadog

Project description


An ASGI middleware to emit metrics to statsd for requests

Mostly designed to work with Datadog, but should work with any statsd metric

# Config

statsd_client: Pass along your own statsd client. Must implement increment and timing methods

statsd_options: Options to configure the datadog statsd client

service: Name of your service, prepended to all metrics. Defaults to asgi

logger: optional logger

# Metric naming pattern



# TimingMiddleware

Emits timing metrics for each endpoint for requests. Will emit clock time and cpu time. ## Tags =======

type: wall or cpu, depending on time source

status_code: The status code of the http request

method: the http method (get, post, put, delete) # StatusCodeMetricsMiddleware =============================

Emits counter metrics for each endpoint ## Tags =======

status_code: The status code of the http request

method: the http method (get, post, put, delete)

Usage with FastAPI

This ASGI middleware should work with any Starlette app, but I work with FastAPI so that’s the example I’ve got for you. PRs welcome for more examples

from fastapi import FastAPI

app = FastAPI()

from statsd_asgi import TimingMiddleware, StatusCodeMetricsMiddleware

from logging import getLogger

statsd*options = {'statsd*host': os.environ.get("STATSD_HOST"),

                                'statsd\_port': os.environ.get("STATSD\_PORT")


app.add*middleware(TimingMiddleware, service="testapi", statsd*options=statsd_options)

app.add*middleware(StatusCodeMetricsMiddleware, service="testapi", statsd*client=statsd_options)


async def root():

        return {"message": "Hello World"}

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

statsd-asgi-0.1.0b1.tar.gz (6.0 kB view hashes)

Uploaded source

Built Distribution

statsd_asgi-0.1.0b1-py3-none-any.whl (6.5 kB view hashes)

Uploaded py3

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