Skip to content
ASGI middleware to record and emit timing metrics (to something like statsd)
Python Makefile
Branch: master
Clone or download
Latest commit bcfdc7e May 29, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Rename straggler Mar 6, 2019
tests Update interface for ASGI3, and tests to match May 21, 2019
.gitignore Fleshing out, starting tests Mar 5, 2019
Makefile Renaming to TimingMiddleware -- the client interface can take care of… Mar 6, 2019 Update May 29, 2019
pyproject.toml Update version to 0.2.0 (ie no longer a pre-release), and depend on 0… May 23, 2019
setup.cfg Fleshing out, starting tests Mar 5, 2019
tox.ini See if I can remove the 'make' warnings by using the full path in whi… Mar 5, 2019


CircleCI PyPI Downloads PyPI Version License

This is a timing middleware for ASGI, useful for automatic instrumentation of ASGI endpoints.

This was developed at GRID for use with our backend services which are built using python and the ASGI framework Starlette, and intended to emit metrics to Datadog, a statsd-based cloud monitoring service.

ASGI version

Since 0.2.0 this middleware only supports ASGI3, if you need ASGI2 support please use version 0.1.2.


pip install timing-asgi


Here's an example using the Starlette ASGI framework which prints out the timing metrics..

A more realistic example which emits the timing metrics to Datadog can be found at

import uvicorn

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from timing_asgi import TimingMiddleware, TimingClient
from timing_asgi.integrations import StarletteScopeToName

class PrintTimings(TimingClient):
    def timing(self, metric_name, timing, tags):
        print(metric_name, timing, tags)

app = Starlette()

def homepage(request):
    return PlainTextResponse("hello world")

    metric_namer=StarletteScopeToName(prefix="myapp", starlette_app=app)

if __name__ == "__main__":

Running this example and sending some requests:

$ python
INFO: Started server process [35895]
INFO: Waiting for application startup.
2019-03-07 11:38:01 INFO  [timing_asgi.middleware:44] ASGI scope of type lifespan is not supported yet
INFO: Uvicorn running on (Press CTRL+C to quit)
INFO: ('', 58668) - "GET / HTTP/1.1" 200
myapp.__main__.homepage 0.0006690025329589844 ['http_status:200', 'http_method:GET', 'time:wall']
myapp.__main__.homepage 0.0006950000000000012 ['http_status:200', 'http_method:GET', 'time:cpu']
INFO: ('', 58684) - "GET /asdf HTTP/1.1" 404
myapp.asdf 0.0005478858947753906 ['http_status:404', 'http_method:GET', 'time:wall']
myapp.asdf 0.0005909999999999804 ['http_status:404', 'http_method:GET', 'time:cpu']
You can’t perform that action at this time.