Skip to main content

Metrics system for generating statistics about your app

Project description

Markus is a Python library for generating metrics.

Code:

https://github.com/willkg/markus

Issues:

https://github.com/willkg/markus/issues

License:

MPL v2

Documentation:

http://markus.readthedocs.io/en/latest/

Goals

Markus makes it easier to generate metrics in your program by:

  • providing multiple backends (Datadog statsd, statsd, logging, logging rollup, and so on) for sending data to different places

  • sending metrics to multiple backends at the same time

  • providing a testing framework for easy testing

  • providing a decoupled architecture making it easier to write code to generate metrics without having to worry about making sure creating and configuring a metrics client has been done–similar to the Python logging Python logging module in this way

I use it at Mozilla in the collector of our crash ingestion pipeline. Peter used it to build our symbols lookup server, too.

Install

To install Markus, run:

$ pip install markus

(Optional) To install the requirements for the markus.backends.statsd.StatsdMetrics backend:

$ pip install 'markus[statsd]'

(Optional) To install the requirements for the markus.backends.datadog.DatadogMetrics backend:

$ pip install 'markus[datadog]'

Quick start

Similar to using the logging library, every Python module can create a markus.main.MetricsInterface (loosely equivalent to a Python logging logger) at any time including at module import time and use that to generate metrics.

For example:

import markus

metrics = markus.get_metrics(__name__)

Creating a markus.main.MetricsInterface using __name__ will cause it to generate all stats keys with a prefix determined from __name__ which is a dotted Python path to that module.

Then you can use the markus.main.MetricsInterface anywhere in that module:

@metrics.timer_decorator("chopping_vegetables")
def some_long_function(vegetable):
    for veg in vegetable:
        chop_vegetable()
        metrics.incr("vegetable", value=1)

At application startup, configure Markus with the backends you want and any options they require to publish metrics.

For example, let us configure Markus to publish metrics to the Python logging infrastructure and Datadog:

import markus

markus.configure(
    backends=[
        {
            # Publish metrics to the Python logging infrastructure
            "class": "markus.backends.logging.LoggingMetrics",
        },
        {
            # Publish metrics to Datadog
            "class": "markus.backends.datadog.DatadogMetrics",
            "options": {
                "statsd_host": "example.com",
                "statsd_port": 8125,
                "statsd_namespace": ""
            }
        }
    ]
)

Once you’ve added code that publishes metrics, you’ll want to test it and make sure it’s working correctly. Markus comes with a markus.testing.MetricsMock to make testing and asserting specific outcomes easier:

from markus.testing import MetricsMock


def test_something():
    with MetricsMock() as mm:
        # ... Do things that might publish metrics

        # Make assertions on metrics published
        mm.assert_incr_once("some.key", value=1)

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

markus-5.0.0.tar.gz (37.7 kB view details)

Uploaded Source

Built Distribution

markus-5.0.0-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file markus-5.0.0.tar.gz.

File metadata

  • Download URL: markus-5.0.0.tar.gz
  • Upload date:
  • Size: 37.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.14

File hashes

Hashes for markus-5.0.0.tar.gz
Algorithm Hash digest
SHA256 14fe47ebe3d3447cc5eebcd4691f71e7f38dee6338e4eb5d72d64d67f289c6ff
MD5 b722c421329cde20858109b0ff440e8d
BLAKE2b-256 e007321805bbf0bfd92f32e8a88a405890a9e7267bdfff924648a0c3076e85c8

See more details on using hashes here.

File details

Details for the file markus-5.0.0-py3-none-any.whl.

File metadata

  • Download URL: markus-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.14

File hashes

Hashes for markus-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd0f0de0914a3ae645cc1eac760dca2fa28943fbbb3671692e09916f7f2e2237
MD5 dcabb50bd42b306db8c25144b8e718aa
BLAKE2b-256 5b3b5ecbedc3b54a51a37d33792c49965cc8a6fcf33ca9ce1b5324c8cb287dc0

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