Skip to main content

aiostasd

an asyncio-based client for send metric to StatsD, Graphite.carbon and DogStatsD.

Installation

pip install aiostatsd

Usage

Usage Client

Create connection and send gauge metric. aiostatsd client will automatically send messages in the background when the loop is running

import asyncio
from aio_statsd import StatsdClient


loop = asyncio.get_event_loop()
client = StatsdClient()
loop.run_until_complete(client.connect())
client.gauge('test.key', 1)
loop.run_forever()

Create Connection Pool and use max_size=5: The maximum number of connections in the connection pool

import asyncio
from aio_statsd import StatsdClient


loop = asyncio.get_event_loop()
client = StatsdClient()
loop.run_until_complete(client.create_pool(max_size=5))
client.gauge('test.key', 1)
loop.run_forever()

Use context manager(Now, not support connection pool)

import asyncio
from aio_statsd import StatsdClient


async def main():
    async with StatsdClient() as client:
        client.gauge('test.key', 1)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Client param

  • host: default value 'localhost', Statsd Server ip
  • port: default value 8125, Statsd Server port
  • protocol: default value ProtocolFlag.udp, Transport Layer Prrotocol, Select Tcp:ProtocolFlag.udp or Udp:ProtocolFlag.tcp
  • timeout: default value 0, send msg timeout, if timeout==0, not enable timeout
  • debug: default value False, enable debug
  • close_timeout: default value 5, Within a few seconds after the client is closed, continue to send messages which in the queue
  • create_timeout: default value 5, Createe connection timeout
  • read_timeout: default value 0.5, read messages from queue timeout
  • sample_rate(Use in StatsD Client, DogStatsD Client): default value 1, use sample rate in Statsd or DogStatsD

send metric

import asyncio
from aio_statsd import StatsdClient


async def main():
    async with StatsdClient() as client:
        client.gauge('test.key', 1)
        client.counter('test.key', 1)
        client.sets('test.key', 1)
        client.timer('test.key', 1)
        with client.timeit('test'):
            pass  # run your code

        # all metric support sample rate
        client.gauge('test1.key', 1, sample_rate=0.5)

        # mutli metric support(not support sample rate, the sample rate will always be set to 1)
        from aio_statsd import StatsdProtocol
        metric = StatsdProtocol()   
        metric.gauge('test2.key', 1)
        metric.sets('test2.key', 1)
        client.send_statsd(metric)     

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Other Client

Graphite(carbon)

import asyncio
from aio_statsd import GraphiteClient


loop = asyncio.get_event_loop()
client = GraphiteClient()
loop.run_until_complete(client.connect())
client.send_graphite('test.key', 1) # Multiple clients timestamp interval synchronization
loop.run_forever()

DogStatsD

Note: Not tested in production

import asyncio
from aio_statsd import DogStatsdClient


async def main():
    async with DogStatsdClient() as client:
        client.gauge('test.key', 1)
        client.distribution('test.key', 1)
        client.increment('test.key',1)
        client.histogram('test.key', 1)
        client.timer('test.key', 1)
        with client.timeit('test'):
            pass  # run your code

        # all metric support sample rate and DogStatsD tag
        client.gauge('test1.key', 1, sample_rate=0.5, tag_dict={'tag': 'tag1'})

        # mutli metric support(
        #   DogStatsdProtocol will store the message in its own queue and
        #   DogStatsDClient traverses to read DogStatsdProtocol's message and send it
        # )
        from aio_statsd import DogStatsdProtocol
        metric = DogStatsdProtocol()   
        metric.gauge('test2.key', 1, tag_dict={'tag': 'tag1'})
        metric.histogram('test2.key', 1)
        client.send_dog_statsd(metric, sample_rate=0.5)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Metadata

Release files for aio-statsd 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aio-statsd 0.1.1
File Size Uploaded Provenance Actions
aio_statsd-0.1.1.tar.gz 8.1 kB Without provenance Details for aio_statsd-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aio-statsd 0.1.1
File Interpreter ABI Platform Actions
aio_statsd-0.1.1-py3-none-any.whl Python 3 none any Details for aio_statsd-0.1.1-py3-none-any.whl

Total release size: 21.9 kB

Release files / aio_statsd-0.1.1.tar.gz

Download URL aio_statsd-0.1.1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b0f10da064ac36fd64a23be26188485ac806b61faa34125d2432087001ae08f6
BLAKE2b-256 checksum
How to use checksums
92dc2496bb3a8f8840bb20faa74d2e7e8a02b9e9a95a8b6eb22faa5f0f597d74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.5.3

Release files / aio_statsd-0.1.1-py3-none-any.whl

Download URL aio_statsd-0.1.1-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76da2f42a0ab0845478e6403ddb5897df4a876020c9a6cb9350701d1c782c280
BLAKE2b-256 checksum
How to use checksums
74738a98d351860b5f4ec194bdbc81d883a24a60a79e05f17f660129300663a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.5.3

Security for aio-statsd 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page