Package: markus
Markus is a Python library for generating metrics.Code:https://github.com/willkg/markusIssues:https://github.com/willkg/markus/issuesLicense:MPL v2Documentation:http://markus.readthedocs.io/en/latest/GoalsMarkus 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 placessending metrics to multiple backends at the same timeproviding a testing framework for easy testingproviding 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 wayI use it at Mozilla in the collector of our crash ingestion pipeline. Peter used
it to build our symbols lookup server, too.InstallTo install Markus, run:$ pip install markus(Optional) To install the requirements for themarkus.backends.statsd.StatsdMetricsbackend:$ pip install 'markus[statsd]'(Optional) To install the requirements for themarkus.backends.datadog.DatadogMetricsbackend:$ pip install 'markus[datadog]'Quick startSimilar to using the logging library, every Python module can create amarkus.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 amarkus.main.MetricsInterfaceusing__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 themarkus.main.MetricsInterfaceanywhere 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 to use to
publish metrics and any options they require.For example, let us configure metrics to publish to logs and Datadog:import markus

markus.configure(
    backends=[
        {
            # Log metrics to the logs
            "class": "markus.backends.logging.LoggingMetrics",
        },
        {
            # Log metrics to Datadog
            "class": "markus.backends.datadog.DatadogMetrics",
            "options": {
                "statsd_host": "example.com",
                "statsd_port": 8125,
                "statsd_namespace": ""
            }
        }
    ]
)When you’re writing your tests, use themarkus.testing.MetricsMockto make testing 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)History4.2.0 (March 30th, 2023)Bug fixesAdd support for settingorigin_detection_enabledin Datadog backend.
(#108)Switch from Flake8 to Ruff. (#109)4.1.0 (November 7th, 2022)FeaturesAdd support for Python 3.11 (#100)Bug fixesRedo how dev environment works so it’s no longer installed via an extras but
is now in a separate requirements-dev.txt file.Split flake8 tou a separate requirements-flake8.txt and tox environment to
handle conflicts with installing other things.4.0.1 (May 10th, 2022)Bug fixesMove pytest import to a pytest plugin so it’s easier to determine when pytest
is running. (#95) Thank you, John!4.0.0 (October 22nd, 2021)FeaturesAdded support for Python 3.10 (#88)Backwards incompatibel changesDropped support for Python 3.6 (#89)3.0.0 (February 5th, 2021)FeaturesAdded support for Python 3.9 (#79). Thank you, Brady!Changedassert_*helper methods onmarkus.testing.MetricsMockto print the records to stdout if the assertion fails. This can save some
time debugging failing tests. (#74)Backwards incompatible changesDropped support for Python 3.5 (#78). Thank you, Brady!markus.testing.MetricsMock.get_recordsandmarkus.testing.MetricsMock.filter_recordsreturnmarkus.main.MetricsRecordinstances now. This might require
you to rewrite/update tests that use theMetricsMock.2.2.0 (April 15th, 2020)FeaturesAddassert_methods toMetricsMockto reduce the boilerplate for
testing. Thank you, John! (#68)Bug fixesRemove use ofsixlibrary. (#69)2.1.0 (October 7th, 2019)FeaturesFixget_metrics()so you can call it without passing in athingand it’ll now create aMetricsInterfacethat doesn’t have a key
prefix. (#59)2.0.0 (September 19th, 2019)FeaturesUsetime.perf_counter()if available. Thank you, Mike! (#34)Support Python 3.7 officially.Add filters for adjusting and dropping metrics getting emitted.
See documentation for more details. (#40)Backwards incompatible changestagsnow defaults to[]instead ofNonewhich may affect some
expected test output.Adjust internals to run.emit()on backends. If you wrote your own
backend, you may need to adjust it.Drop support for Python 3.4. (#39)Drop support for Python 2.7.If you’re still using Python 2.7, you’ll need to pin to<2.0.0. (#42)Bug fixesDocument feature support in backends. (#47)FixMetricsMock.has_record()example. Thank you, John!1.2.0 (April 27th, 2018)FeaturesAdd.clear()toMetricsMockmaking it easier to build a pytest
fixture with theMetricsMockcontext and manipulate records for easy
testing. (#29)Bug fixesUpdate Cloudwatch backend fixing.timing()and.histogram()to
sendhistogrammetrics type which Datadog now supports. (#31)1.1.2 (April 5th, 2018)Typo fixesFix the date from the previous release. Ugh.1.1.1 (April 5th, 2018)FeaturesOfficial switch to semver.Bug fixesFixMetricsMockso it continues to work even ifconfigureis called. (#27)1.1 (November 13th, 2017)FeaturesAddedmarkus.utils.generate_tagutility function1.0 (October 30th, 2017)FeaturesAdded support for Python 2.7.Added amarkus.backends.statsd.StatsdMetricsbackend that uses
pystatsd client for statsd pings. Thank you, Javier!Bug fixesAddedLoggingRollupMetricsto docs.Mozilla has been running Markus in production for 6 months so we
can mark it production-ready now.0.2 (April 19th, 2017)FeaturesAdded amarkus.backends.logging.LoggingRollupMetricsbackend that
rolls up metrics and does some light math on them. Possibly helpful
for light profiling for development.Bug fixesLots of documentation fixes. Thank you, Peter!0.1 (April 10th, 2017)Initial writing.