Skip to main content

StatsD is a stats server that plays with Graphite. Together, they collect, aggregate, and show stats. If you don’t know what either of those are, well, why are you still reading this? If you write software or know someone that does, I bet collecting stats will make your software better, or at the very least give you something to look at and think about. StatsD makes it really easy to send stats within your code. This client maks it even easier to get stats out of your python code.

Install

Annoyed with managing external packages? There are plenty of statsd clients that come up under ‘pip search statsd’. Who has time to keep track of tiny dependencies for small projects? Just copy statsd.py into your project if you’re into that kind of thing. No need to depend on some multi-file package for what should be a simple client. Grab and Go! If you’re a stickler for dependencies, you probably don’t need to know how to install this, but here you go any how:

Clone and install:

git clone git@github.com:gaelenh/python-statsd-client.git
cd python-statsd-client
python setup.py install

Install with pip:

pip install statsd-client

Or like I said above, just copy statsd.py into your code base.

Usage

Basic

Setup is easy. By default, the client will connect to localhost on the default statsd port (8125).

import statsd
statsd.incr('processed') # Increment processed bucket by 1
statsd.incr('processed', 5) # This time by 5
statsd.incr('processed', sample_rate=0.9) # Increment with a sample rate of .9
statsd.timing('pipeline', 2468.34) # Pipeline took 2468.34 ms to execute

Want to connect to a non-local statsd? Use statsd.init_statsd(settings). Settings is a dict with any of these keys:

STATSD_HOST (Default 'localhost'): String host name.
STATSD_PORT (Default 8125): Integer port number.
STATSD_SAMPLE_RATE (Default None (same as 1.0)): Integer/Float between 0 and 1.
STATSD_BUCKET_PREFIX (Default None): String prefix added to all buckets. The code will handle dotting them together.

If you do not want to use init_statsd, you can always pass in your settings when you create the clients, timers or counters:

from statsd import StatsdClient
client = StatsdClient(host='127.0.0.1', port=9999, prefix='app', sample_rate=0.9)

Counters

Want to count things? Use StatsdCounter:

import statsd
statsd.init_statsd({'STATSD_BUCKET_PREFIX': 'photos'})
counter = statsd.StatsdCounter('processed')
# calls on counter will send updates to bucket named 'photos.processed'
counter += 1 # equivalent to counter.incr() or counter.incr(1)
counter += 5 # equivalent to counter.incr(5)
counter -= 10 # equivalent to counter.decr(10)

Timing

Interested in timing? Check out all the ways you can time things:

import statsd
statsd.init_statsd({'STATSD_BUCKET_PREFIX': 'photos'})
timer = statsd.StatsdTimer('pipeline')
timer.start()
# Do stuff
timer.split('stage1') # Sends timing data for bucket 'photos.pipeline.stage1'
# Do more stuff
timer.split('stage2') # Sends timing data for bucket 'photos.pipeline.stage2'
# Do even more stuff
timer.stop() # Sends timing data for bucket 'photos.pipeline.total'

Timers can be used as decorators too:

from statsd import StatsdTimer
@StatsdTimer.wrap('pipeline')
def process():
    pass
process() # Sends timing data for bucket 'pipeline.total'

Fancy with statement usage!

from statsd import StatsdTimer
with StatsdTimer('photos'):
    pass # Do stuff

Even fancier:

from statsd import StatsdTimer
with StatsdTimer('photos') as t:
    # Do stuff
    t.split('stage1')
    # Do more stuff
    t.split('stage2')
    # Finish up

Using timers with decorators or the with statement will still sends stats if an exception is raised in the code block:

from statsd import StatsdTimer
class Foo(object):
    @StatsdTimer('photos')
    def proc(self):
            # Do stuff
            raise ValueError('Whoops')
f = Foo()
f.proc() # Raises exception, but sends timing data for bucket 'photos.total-except'

Misc

The client integrates great with Flask. Just call statsd.init_statsd when you’re initializing all your other framework components. Once that’s done, you can use the timers and counters anywhere in your code.

Contributing

If you find a bug and want to fix it, fork, branch, and submit a pull request. The master branch will always have the latest working code.

Release files for statsd-client 1.0.7

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

Source distribution (sdist)

Source distribution for statsd-client 1.0.7
File Size Uploaded
statsd-client-1.0.7.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for statsd-client 1.0.7
File Interpreter ABI Platform
statsd_client-1.0.7-py3-none-any.whl Python 3 none any Details

Total release size:18.8 kB

Release files / statsd-client-1.0.7.tar.gz

Download URL statsd-client-1.0.7.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
86fd82941b95d83a9217e30d5363ececc286858a5de77cbc96ab2eec4bd6ed33
BLAKE2b-256 checksum
How to use checksums
25d9411bfd51ca4ee2e00cf7268826a5bbd73cb44b855e6fa9219de4e85a95e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / statsd_client-1.0.7-py3-none-any.whl

Download URL statsd_client-1.0.7-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4675b641bce10cbc173e15ffc03115d6e8c69e5435879a21b67995d8638f0f4a
BLAKE2b-256 checksum
How to use checksums
b4a661d842f0a6ec95eecb4558a555ccf5c5807ba5d71b95c83c3f16f57230e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release history Release notifications | RSS feed

This release

1.0.7 This release

2 release files

1.0.6

1 release file

1.0.5

1.0.4

1 release file

1.0.2

1 release file

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