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.3.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: metricslib-0.3.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for metricslib-0.3.0.tar.gz
Algorithm Hash digest
SHA256 438c184314b5ebbed562859889d49c31cce1860f82dff71de294516696c8d0f8
MD5 f315aa95248bd058cf42a4d3d02cb298
BLAKE2b-256 c35797173087a9cf86ab9f80d75c9c213468fb1f126c138003b2cc4344b845d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: metricslib-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for metricslib-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19ba7d1a34149e696b321dd77c60658ef7deb0a2da2b5a6363f89026c785185e
MD5 603bc58874d9ba174794c939f5bd7497
BLAKE2b-256 09b8fb28173642f69c2ea27c682457a96e6fe344e85201266f129d62b75af5e3

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