Skip to main content

Metrics collection library

Project description

Metricslib is a package that can be used to send application metrics to metric collections services.

Supported metric collection services

Installation

Metricslib requires python >= 3.5. Install the latest version using pip

pip install metricslib 

Usage

Metricslib provides a decorator that can be used on a function where we want to collect metrics about how many times it was called, how many times it executed successfully, how many times it was executed with errors and how long it took to run.

For the moment the only supported metric collection service is Statsd.

from metricslib.utils import configure_metrics_from_dict
from metricslib.decorators import capture_metrics

@capture_metrics(
    request_metric="myapp.do_something.request",
    error_metric="myapp.do_something.error",
    success_metric="myapp.do_something.success",
    execution_time_metric="myapp.do_something.execution"
)
def do_something():
    print("hello world")


@capture_metrics(
    request_metric="myapp.do_something.request",
    error_metric="myapp.do_something.error",
    success_metric="myapp.do_something.success",
    execution_time_metric="myapp.do_something.execution"
)
def do_something_bad():
    raise Exception()


def main():
    config = {
        "STATSD_HOST": "localhost",
        "STATSD_PORT": 8125
    }
    configure_metrics_from_dict(config)

    do_something()

    # we want this function to raise an exception in order to test the error
    # metric
    do_something_bad()


if __name__ == "__main__":
    main()

Instead of using the decorator you can create counter objects.

from metricslib.config import configure_metrics_from_dict
from metricslib.utils import get_metrics


def main():
    config = {
        "STATSD_HOST": "localhost",
        "STATSD_PORT": 8125
    }

    configure_metrics_from_dict(config)

    metrics = get_metrics()
    counter = metrics.counter("myapp.count")
    counter.incr()


if __name__ == "__main__":
    main()

You can also measure the time duration of an operation.

from time import sleep

from metricslib.config import configure_metrics_from_dict
from metricslib.utils import get_metrics


def main():
    config = {
        "STATSD_HOST": "localhost",
        "STATSD_PORT": 8125
    }

    configure_metrics_from_dict(config)

    metrics = get_metrics()
    duration = metrics.duration("myapp.time")

    duration_measurement = duration.begin()
    sleep(2.0)
    duration_measurement.end()


if __name__ == "__main__":
    main()

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

metricslib-0.2.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

metricslib-0.2.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file metricslib-0.2.1.tar.gz.

File metadata

  • Download URL: metricslib-0.2.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.5.2

File hashes

Hashes for metricslib-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fc7711faa19d45b6ab76478bb65c736c9b10a0e9ab123057eaee70ff980ca5c9
MD5 ec208d72f177aca919cae252147af9cb
BLAKE2b-256 3374ac757f445a82058c1363c4006b617796ceb3097add49901e4663ff4993e9

See more details on using hashes here.

File details

Details for the file metricslib-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: metricslib-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.5.2

File hashes

Hashes for metricslib-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ac085e927b40ec59eb0e39c69c623d82cb91ecfe569141e131c712d87f7065a
MD5 f080f061b6788d9940763d50e17d639b
BLAKE2b-256 1c9c62e9399b0ba935610b6b7a621e6431a658dd9e7e89efeb99e775098fd728

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