A library to enable collection and delayed emission of StatsD metrics using the DataDog protocol.
dogstatsd-collector is a library to make it easy to collect DataDog-style StatsD counters and histograms with tags and control when they are flushed. It gives you a drop-in wrapper for the DogStatsD library for counters and histograms and allows you to defer flushing the metrics until you choose to. This capability enables you to collect StatsD metrics at arbitrary granularity, for example on a per-web request or per-job basis.
Counters and histograms are tracked separately for each metric series (unique set of tag key-value pairs) and a single metric is emitted for each series when the collector is flushed. You don’t have to think about tracking your metric series separately; you just use the DogstatsdCollector object as you would the normal DogStatsD object, and flush when you’re ready; the library will take care of emitting all the series for you.
- Free software: BSD 3-Clause License
pip install dogstatsd-collector
Imagine you want to track a distribution of the number of queries issued by requests to your webapp, and tag them by which database is queried and which verb is used. You collect the following metrics as you issue your queries:
collector = DogstatsdCollector(dogstatsd) ... collector.histogram('query', tags=['database:master','verb:insert']) collector.histogram('query', tags=['database:master','verb:update']) collector.histogram('query', tags=['database:master','verb:update']) collector.histogram('query', tags=['database:replica','verb:select']) collector.histogram('query', tags=['database:replica','verb:select'])
Then, at the end of your web request, when you flush the collector, the following metrics will be pushed to DogStatsD:
collector.flush() # 'query,1,database:master|verb:insert' # 'query,2,database:master|verb:update' # 'query,2,database:replica|verb:select'
The StatsD model is to run an agent on each server/container in your infrastructure and periodically flush aggregations at a regular interval to a centralized location. This model scales very well because the volume of metrics sent to the centralized location grows very slowly even as you scale your application; each StatsD agent calculates aggregations to flush to the backend instead of every datapoint, so the storage volume is quite low even for a large application with lots of volume.
A drawback to this model is that you don’t have much control of the granularity that your metrics represent. When your aggregations reach the centralized location (DataDog in this case), you only know the counts or distributions within the flush interval. You can’t represent any other execution granularity beyond “across X seconds” (where X is the flush interval). This limitation precludes you from easily representings metrics on a “per-request” basis, for example.
The purpose of this library is to make it simple to control when your StatsD metrics are emitted so that you can defer emission of the metrics until a point you determine. This allows you to represent a finer granularity than “across X seconds” such as “across a web request” or “across a cron job.” It also preserves metric tags by emitting each series independently when the collector is flushed, which ensures you don’t lose any of the benefit of tagging your metrics (such as aggregating/slicing in DataDog).
The DogstatsdCollector object is a singleton that provides an identical interface as the DogStatsD increment and histogram methods. As you invoke these methods, you collect counters and histograms for each series (determined by any tags you include). After calling flush(), each series is separately emitted as a StatsD metric.
Simple Request Metrics
You can collect various metrics over a request and emit them at the end of the request to get per-request granularity.
Celery Task Metrics
Same as above, but over a Celery task.
Metrics Within a Function
Emit a set of metrics for a particular function you execute.
The DogstatsdCollector singleton is not threadsafe. TODO
Full documentation can be found on ReadTheDocs:
To run the all tests run:
- First release on PyPI.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size dogstatsd_collector-0.0.1-py2.py3-none-any.whl (5.7 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size dogstatsd-collector-0.0.1.tar.gz (15.1 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for dogstatsd_collector-0.0.1-py2.py3-none-any.whl
Hashes for dogstatsd-collector-0.0.1.tar.gz