aiodogstatsd
aiodogstatsd is an asyncio-based client for sending metrics to StatsD with support for the DogStatsD extension. It supports gauge, counter, histogram, distribution, and timing metric types. The library is actively maintained, currently at version 0.16.0.post0, and features a consistent release cadence with updates for Python version compatibility and framework integrations.
Warnings
- breaking Python 3.6 support has been dropped in version 0.16.0. Projects using this version or newer must migrate to Python 3.7 or higher.
- breaking Support for the Sanic framework was dropped in version 0.16.0. Integrations (listeners, middlewares) for Sanic will no longer work with this version or newer.
- gotcha The aiodogstatsd client defaults to port 9125, which is the default for `statsd_exporter`. Standard StatsD and DataDog Agent typically use port 8125. If you're targeting a standard StatsD server or Datadog Agent, you must explicitly set the port during client initialization.
- gotcha While direct `Client` instantiation and manual `await client.connect()` and `await client.close()` calls are possible, using the `async with Client(...) as client:` context manager is the recommended pattern. It ensures that connections are properly initialized and gracefully closed, even in the event of exceptions.
- gotcha As StatsD uses UDP for metric submission, packets can be dropped in high-throughput network scenarios due to network congestion or OS buffer limits, especially in containerized environments. This is a general characteristic of the StatsD protocol, not specific to `aiodogstatsd`.
Install
-
pip install aiodogstatsd -
pip install aiodogstatsd[aiohttp] -
pip install aiodogstatsd[starlette]
Imports
- Client
from aiodogstatsd import Client
Quickstart
import asyncio
import os
from aiodogstatsd import Client
async def main():
# The client uses port 9125 by default, which is common for statsd_exporter.
# For DataDog Agent or standard StatsD (port 8125), configure explicitly.
async with Client(host=os.environ.get('STATSD_HOST', 'localhost'), port=int(os.environ.get('STATSD_PORT', '9125'))) as client:
client.increment("users.online")
client.gauge("server.cpu.usage", value=42.5, tags={"region": "us-east-1"})
with client.timeit("db.query.time"): # Context manager for timing
await asyncio.sleep(0.05)
print("Metrics sent!")
if __name__ == "__main__":
asyncio.run(main())