ASGI middleware to emit timing metrics with something like statsd
Low maintenance activity. Last release 9 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install timing-asgitiming-asgi
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.
installation
pip install timing-asgi
usage
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 https://github.com/steinnes/timing-starlette-asgi-example.
import logging
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()
@app.route("/")
def homepage(request):
return PlainTextResponse("hello world")
app.add_middleware(
TimingMiddleware,
client=PrintTimings(),
metric_namer=StarletteScopeToName(prefix="myapp", starlette_app=app)
)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
uvicorn.run(app)