Skip to main content

Simple and fast statsd async client

Project description

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 nax_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()
client.gauge('test.key', 1)
loop.run_until_complete(client.create_pool(max_size=5))
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(Statsd, DogStatsd): default value 1, use sample rate in Statsd

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 action

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

        # mutli metric support(not support sample rate)
        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()
client.send_graphite('test.key', 1)
loop.run_until_complete(client.create_pool(max_size=5))
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 action

        # 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())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aio_statsd-0.0.1.tar.gz (7.9 kB view hashes)

Uploaded Source

Built Distribution

aio_statsd-0.0.1-py3-none-any.whl (13.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page