Skip to main content

Markus is a Python library for generating metrics.

Code:

https://github.com/mozilla-services/markus

Issues:

https://github.com/mozilla-services/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)

Metadata

Release files for markus 5.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for markus 5.2.0
File Size Uploaded
markus-5.2.0.tar.gz 39.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for markus 5.2.0
File Interpreter ABI Platform
markus-5.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 65.4 kB

Release files / markus-5.2.0.tar.gz

Download URL markus-5.2.0.tar.gz
Size 39.1 kB
Tags Source
SHA-256 checksum
How to use checksums
edd47627f5feab4e3eb9844677c4a921fcd2d19d7b7d00286e31a69c7b9a8442
BLAKE2b-256 checksum
How to use checksums
2683799225d3980eb873b5bf823471f5c8219dc4186144ca4d1bb426cdaa37a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.14

Release files / markus-5.2.0-py3-none-any.whl

Download URL markus-5.2.0-py3-none-any.whl
Size 26.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ada9042662e7091c1219080a86909460b8aea4ef52574de1fd1f0d880c3bde93
BLAKE2b-256 checksum
How to use checksums
b3f209b03a984a492f1b3368338ffc56844449089e90357bfd83aa4afd404281
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.14

Release history Release notifications | RSS feed

This release

5.2.0 This release

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1

2 release files

1.0

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page