A Python statsd client
statsd is a friendly front-end to Graphite. This is a Python client for the statsd daemon.
To use:
>>> import statsd
>>> c = statsd.StatsClient('localhost', 8125)
>>> c.incr('foo') # Increment the 'foo' counter.
>>> c.timing('stats.timed', 320) # Record a 320ms 'stats.timed'.
In Django
If you’re lucky enough to be using statsd in Django, you can configure a default client in your settings module with two values. The defaults are:
STATSD_HOST = 'localhost' STATSD_PORT = 8125
Then instead of instantiating a new client every time, you can just grab:
>>> from statsd import statsd
>>> statsd.incr('foo')
Context Manager
You can use a StatsClient instance as a context manager to easily time sections of code with the timer() method:
>>> from statsd import statsd
>>> with statsd.timer('bar'):
... func()
... func()
When the managed block exits, the client will automatically send the time it took to statsd.
Decorator
You can also use a StatsClient instance as a decorator, also with the timer() method:
>>> from statsd import statsd
>>> @statsd.timer('bar')
... def foo():
... pass
Every time foo() is called, timing information will be sent to the stat bar.
Sample Rates
All methods support an optional rate (kw)arg. This is a float between 0 and 1 that specifies what fraction of data to send through (for a specific call). Sample rates are recorded by statsd.
For example, here foo will be incremented approximately 50% of the time:
>>> from statsd import statsd
>>> statsd.incr('foo', 1, rate=0.5)
Statsd understands that this is a 50% sample rate and will adjust accordingly.
Similarly with decr() and timings:
>>> from statsd import statsd
>>> statsd.decr('foo', 1, rate=0.5)
>>> statsd.timing('foo', 320, rate=0.25)
>>> with statsd.timer('bar', rate=0.1):
... pass
>>> @statsd.timer('bar', rate=0.5)
... def foo():
... pass
Metadata
Release files for statsd 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| statsd-0.2.0.tar.gz | 4.6 kB | Details |
Release files / statsd-0.2.0.tar.gz
| Download URL | statsd-0.2.0.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
184da9c92f3bf399a40c41ee3cf053e0d23a74e71ccb9837df7add7bfdd4e35d
|
|
BLAKE2b-256 checksum How to use checksums |
6a1786a9137b9136e1538ac2125c257c950de375d43862be1dbc10f8499534bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |