Skip to main content

Statsd metrics classes and clients

Project description

https://travis-ci.org/farzadghanei/statsd-metrics.svg?branch=master https://ci.appveyor.com/api/projects/status/bekwcg8n1xe0w0n9/branch/master?svg=true

Metric classes for Statsd, and Statsd clients (each metric in a single request, or send batch requests).

Metric classes represent the data used in Statsd protocol excluding the IO, to create, represent and parse Statsd requests. So any Statsd server and client regardless of the IO implementation can use them to send/receive Statsd requests.

The library also comes with a rich set of Statsd clients using the same metric classes, and Python standard library socket module.

Metric Classes

  • Counter

  • Timer

  • Gauge

  • Set

  • GaugeDelta

from statsdmetrics import Counter, Timer

counter = Counter('event.login', 1, 0.2)
counter.to_request() # returns event.login:1|c|@0.2

timer = Timer('db.search.username', 27.4)
timer.to_request() # returns db.search.username:27.4|ms

Parse metrics from a Statsd request

from statsdmetrics import parse_metric_from_request

event_login = parse_metric_from_request('event.login:1|c|@.2')
# event_login is a Counter object with count = 1 and sample_rate = 0.2

mem_usage = parse_metric_from_request('resource.memory:2048|g')
# mem_usage is a Gauge object with value = 2028

Statsd Clients

  • client.Client: Default client, sends request on each call using UDP

  • client.BatchClient: Buffers metrics and flushes them in batch requests using UDP

  • client.tcp.TCPClient: Sends request on each call using TCP

  • client.tcp.TCPBatchClient: Buffers metrics and flushes them in batch requests using TCP

Send Statsd requests

from statsdmetrics.client import Client

# default client, send metrics over UDP
client = Client("stats.example.org")
client.increment("login")
client.decrement("connections", 2)
client.timing("db.search.username", 3500)
client.gauge("memory", 20480)
client.gauge_delta("memory", -256)
client.set("unique.ip_address", "10.10.10.1")

# helpers for timing operations
chronometer = client.chronometer()
chronometer.time_callable("func1_duration", func1)

# decorate functions to send timing metrics for the duration of their running time
@chronometer.wrap("func2_duration")
def func2():
    pass

# send timing for duration of a with block
with client.stopwatch("with_block_duration"):
    pass

Sending multiple metrics in batch requests by BatchClient, either by using an available client as the context manager:

from statsdmetrics.client import Client

client = Client("stats.example.org")
with client.batch_client() as batch_client:
    batch_client.increment("login")
    batch_client.decrement("connections", 2)
    batch_client.timing("db.search.username", 3500)
# now all metrics are flushed automatically in batch requests

or by creating a BatchClient object explicitly:

from statsdmetrics.client import BatchClient

client = BatchClient("stats.example.org")
client.set("unique.ip_address", "10.10.10.1")
client.gauge("memory", 20480)
client.flush() # sends one UDP packet to remote server, carrying both metrics

# timing helpers are available on all clients
chronometer = client.chronometer()
chronometer.time_callable("func1_duration", func1)

@chronometer.wrap("func2_duration")
def func2():
    pass

with client.stopwatch("with_block_duration"):
    pass

client.flush()

Installation

$ pip install statsdmetrics

The only dependencies are Python 2.7+ and setuptools. CPython 2.7, 3.4+, 3.7-dev, PyPy, and Jython 2.7 are tested)

However on development (and test) environment pytest, mock is required (for Python 2), typing is recommended.

# on dev/test env
$ pip install -r requirements-dev.txt

Development

Tests

Tox is most convenient to run tests with since it handles creation of virtualenvs

$ tox

When development dependencies are installed (preferably with a virtual environment), tests can be run by calling pytest.

$ pytest

Integration tests are available as part of the test suite, bringing up dummy servers (but actually listening on network socket) to capture requests instead of processing them. Then send some metrics and assert if the captured requests match the expected.

License

Statsd metrics is released under the terms of the MIT license.

The MIT License (MIT)

Copyright (c) 2015-2018 Farzad Ghanei

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

statsdmetrics-2.0.2.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

statsdmetrics-2.0.2-py2.py3-none-any.whl (12.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file statsdmetrics-2.0.2.tar.gz.

File metadata

  • Download URL: statsdmetrics-2.0.2.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.15

File hashes

Hashes for statsdmetrics-2.0.2.tar.gz
Algorithm Hash digest
SHA256 b4b3c20873f53b47c63c715af4f80c6505ccb7f8bc519bcfe57baccc81b555ca
MD5 a0290392af746847fc4ddb2d322f2d21
BLAKE2b-256 601fc228752fda1faed017829ab836fec7c02e93f4103f150c15d2bdd24fd35f

See more details on using hashes here.

File details

Details for the file statsdmetrics-2.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: statsdmetrics-2.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.15

File hashes

Hashes for statsdmetrics-2.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0c03e311c1a05ff89c7c5f5518c46f11b7f7a27f7d2fdc42cf7879782fc81c37
MD5 5af47722c7f32e647558c2a70b5b263d
BLAKE2b-256 141be869260d0ed0b3165061751a36d2ab90bdc38c115da325d922b0cc69af6c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page