No project description provided
Project description
Python Statsd Client
:construction: WIP :construction:
This is an implementation of a Statsd client for Python.
Motivation
I was looking for a generic Statsd client with tags support to interact with various statsd servers for an application that I distribute but don't operate, so there could be high variability in the statsd implementations used (I know one uses Telegraf and InfluxDB and one datadog and they both have different tag formats).
pystatsd
exists and works, but it intentionally does not support tags.- The docs point to an alternative supporting tags, but at the time of writing the repository leads to a 404 for me.
- There a are a few more available on PyPi that likely work, but most of the ones I've checked haven't been updated in a while, are not documented and don't support tags.
datadogpy
could be a solid solution, but I'd rather avoind pulling the full Datadog client library in projects where I don't use datadog. It also exposes some non standard metric types, and while I can always not use them I'd prefer a generic solution (ignoring tags which, while not standardized, are supported by most statsd servers).
Documentation
Installation
pip install statsd-python
Usage
from statsd import StatsdClient
# Create a UDP based client with default connection parameters.
client = StatsdClient()
client.increment('my-counter') # Increment my-counter by one
client.gauge('my-gauge', 42, sample_rate=.5) # Set a gauge value, sampling only half of the events
StatsdClient
supports all metric types defined by Statsd:
-
Counters track the total number of occurences of a given event.
client.increment('my-counter') client.decrement('my-counter', 3)
-
Gauges track a value over time.
# Set the value client.gauge('my-gauge', 42) # Deltas are supported as well client.gauge('my-gauge', 3, is_update=True) client.gauge('my-gauge', -1, is_update=True)
Warning: Some Statsd servers implementations (such as Datadog's) do not support gauge deltas.
-
Timings are used track durations in milliseconds.
# Measure a duration of 1.234 seconds client.timing('my-duration', 1234)
The library also includes helpers for measuring code execution time using perf_counter.
@timed('my-duration') def do_something(): pass with timer('my-duration'): do_some_other_thing()
-
Sets count unique occurences per key
# Record one occurence of `my-set` for the key 1234. client.set('my-set', 1234)
Sampling
All metric accept a sample_rate
parameter. This should be a float between 0 and 1 that the client will use to sample metrics. By default all metrics are sent with a sample rate of 1 (no sampling).
The client will include this information in metric packets so the server can handle this accordingly.
# Only send the metric half the time.
client.gauge('my-gauge', 42, sample_rate=0.5)
# Only send the metric 75% of the time.
client.gauge('my-gauge', 42, sample_rate=0.25)
# Only send the metric 25% of the time.
client.gauge('my-gauge', 42, sample_rate=0.75)
Tag support
Tags are supported. All metrics will accept a dictionnary for tags.
Different server implementations will accept different ways to include tags in the metric packets so this library exposes a mechanism to configure this beheaviour through the statsd.format
module.
By default the Dogstatsd format is used.
from statsd import StatsdClient
from statsd.formats import TelegrafSerializer
client = StatsdClient(serializer=TelegrafSerializer())
Transports
For now a single transport is currently supported through StatsdClient
/ UDPStatsdClient
.
Development
- All development dependencies are defined in requirements-dev.txt.
- All tests and linting steps are defined in tox.ini, you can run the all the checks with
tox
. - Code is expected to be formatted with
black
andisort
, you can run the formatters withtox -e fmt
.
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
Built Distribution
Hashes for statsd_python-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fc988f8499a6ed427b05ea3f8fbabfcd4e18eac592cf5ab05464227c2cdf255 |
|
MD5 | bd0712765b979e01ab484be2aabed442 |
|
BLAKE2b-256 | 169766c78d03d516ddb0623cd9d08cad2c18a9800481d5e50b1ef666512bb6cf |