Testing with Markus

Markus comes with a MetricsMock that makes it easier to write tests and assert things about generated metrics.

class markus.testing.MetricsMock

Mock for recording metrics events and testing them

Mimics a metrics backend as a context manager. Keeps records of what got metricfied so that you can print them out, filter them, assert various things about them, etc.

To use:

from markus.testing import MetricsMock
from markus import INCR, GAUGE, TIMING, HISTOGRAM

def test_something():
    with MetricsMock() as mm:
        # do things that might record metrics

        # Print the metrics recorded (helps with debugging)

        # Assert something about the metrics recorded
        assert mm.has_record(INCR, 'some.random.key', {'value': 1})
filter_records(fun_name=None, stat=None, value=None, tags=None)

Filters collected metircs records for ones that match specified criteria


Returns set of collected metrics records

has_record(fun_name=None, stat=None, value=None, tags=None)

Returns True/False regarding whether collected metrics match specified criteria


Prints all the collected metrics