Package: aio-statsd
aiostasdan asyncio-based client for send metric toStatsD,Graphite.carbon,TelegrafStatsDandDogStatsD.Installationpipinstallaio_statsdUsageUsage ClientCreate connection and send gauge metric.
aiostatsd client will automatically send messages in the background when the loop is runningimportasynciofromaio_statsdimportStatsdClientloop=asyncio.get_event_loop()client=StatsdClient()loop.run_until_complete(client.connect())client.gauge('test.key',1)loop.run_forever()Use context managerimportasynciofromaio_statsdimportStatsdClientasyncdefmain():asyncwithStatsdClient()asclient:client.gauge('test.key',1)loop=asyncio.get_event_loop()loop.run_until_complete(main())Client paramhost: default value 'localhost', Statsd Server ipport: default value 8125, Statsd Server portprotocol: default value ProtocolFlag.udp, Transport Layer Prrotocol, Select Tcp:ProtocolFlag.udpor Udp:ProtocolFlag.tcptimeout: default value 0, send msg timeout, if timeout==0, not enable timeoutdebug: default value False, enable debugclose_timeout: default value 9, Within a few seconds after the client is closed, continue to send messages which in the queuecreate_timeout: default value 9, Create connection timeoutmax_len: default value 10000, deque lengthsample_rate(Use in StatsD Client, DogStatsD Client): default value 1, use sample rate in Statsd or DogStatsDsend metricimportasynciofromaio_statsdimportStatsdClientasyncdefmain():asyncwithStatsdClient()asclient:client.gauge('test.key',1)client.counter('test.key',1)client.sets('test.key',1)client.timer('test.key',1)withclient.timeit('test'):pass# run your code# all metric support sample rateclient.gauge('test1.key',1,sample_rate=0.5)# mutli metric support(not support sample rate, the sample rate will always be set to 1)fromaio_statsdimportStatsdProtocolmetric=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 ClientGraphite(carbon)importasynciofromaio_statsdimportGraphiteClientloop=asyncio.get_event_loop()client=GraphiteClient()loop.run_until_complete(client.connect())client.send_graphite('test.key',1)# Multiple clients timestamp interval synchronizationloop.run_forever()DogStatsDNote: Not tested in productionimportasynciofromaio_statsdimportDogStatsdClientasyncdefmain():asyncwithDogStatsdClient()asclient: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)withclient.timeit('test'):pass# run your code# all metric support sample rate and DogStatsD tagclient.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# )fromaio_statsdimportDogStatsdProtocolmetric=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())TelegrafStatsdNote: Not tested in productionimportasynciofromaio_statsdimportTelegrafStatsdClientasyncdefmain():asyncwithTelegrafStatsdClient()asclient: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)withclient.timeit('test'):pass# run your code# all metric support sample rate and TelegrafStatsd tagclient.gauge('test1.key',1,sample_rate=0.5,tag_dict={'tag':'tag1'})# mutli metric support(#   TelegrafStatsdProtocol will store the message in its own queue and#   TelegrafStatsDClient traverses to read TelegrafStatsdProtocol's message and send it# )fromaio_statsdimportTelegrafStatsdProtocolmetric=TelegrafStatsdProtocol()metric.gauge('test2.key',1,tag_dict={'tag':'tag1'})metric.histogram('test2.key',1)client.send_telegraf_statsd(metric,sample_rate=0.5)loop=asyncio.get_event_loop()loop.run_until_complete(main())TelegrafNote: Not tested in productionimportasynciofromaio_statsdimportTelegrafClientasyncdefmain():asyncwithTelegrafClient()asclient:client.send_telegraf('test.key',{"field1":100},user_server_time=True)Use in web frameworksfast_tools example