Skip to content

Starlette

aiodogstatsd library can be easily used with Starlette web framework by using client and middleware provided.

At first you need to install aiodogstatsd with required extras:

1
pip install aiodogstatsd[starlette]

Then you can use code below as is to get initialized client and middleware:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from starlette.applications import Starlette
from starlette.middleware import Middleware

import aiodogstatsd
from aiodogstatsd.contrib.starlette import StatsDMiddleware


client = aiodogstatsd.Client()

app = Starlette(
    middleware=[Middleware(StatsDMiddleware, client=client)],
    on_startup=[client.connect],
    on_shutdown=[client.close],
)

Optionally you can provide additional configuration to the middleware:

  • request_duration_metric_name — name of request duration metric (default: http_request_duration);
  • collect_not_allowed — collect or not 405 Method Not Allowed responses;
  • collect_not_found — collect or not 404 Not Found responses.