sprockets-statsd/README.rst

118 lines
4.4 KiB
ReStructuredText
Raw Normal View History

2021-03-21 14:22:55 +00:00
Asynchronously send metrics to a statsd_ instance.
This library provides connectors to send metrics to a statsd_ instance using either TCP or UDP.
.. code-block:: python
import asyncio
import time
import sprockets_statsd.statsd
statsd = sprockets_statsd.statsd.Connector(
host=os.environ.get('STATSD_HOST', '127.0.0.1'))
async def do_stuff():
start = time.time()
response = make_some_http_call()
statsd.timing(f'timers.http.something.{response.code}',
(time.time() - start))
2021-03-21 14:22:55 +00:00
async def main():
await statsd.start()
try:
do_stuff()
finally:
await statsd.stop()
The ``Connector`` instance maintains a resilient connection to the target StatsD instance, formats the metric data
into payloads, and sends them to the StatsD target. It defaults to using TCP as the transport but will use UDP if
the ``ip_protocol`` keyword is set to ``socket.IPPROTO_UDP``. The ``Connector.start`` method starts a background
``asyncio.Task`` that is responsible for maintaining the connection. The ``timing`` method enqueues a timing
metric to send and the task consumes the internal queue when it is connected.