Skip to content

Instantly share code, notes, and snippets.

@willkg
Created October 17, 2019 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willkg/4298072d1ce54d2c82ae523807361071 to your computer and use it in GitHub Desktop.
Save willkg/4298072d1ce54d2c82ae523807361071 to your computer and use it in GitHub Desktop.
import logging
import os
import markus
from markus.main import MetricsFilter
logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger(__name__)
# key -> (type, description)
# Specifying this is tricky. Maybe JSON? Maybe a registration class?
REGISTERED_METRICS = {
"locate.fallback.lookup_count": (
"incr",
(
"Counts the HTTP response codes for all outbound requests. "
"There is one counter per HTTP response code, for example 200."
),
)
}
class RegisteredMetricsFilter(MetricsFilter):
"""Contains a list of registered metrics and validator.
This is a Markus Metrics filter. It'll complain if metrics are generated
that it doesn't know about.
"""
def __init__(self, registered_metrics):
self.registered_metrics = registered_metrics
def filter(self, record):
metric = self.registered_metrics.get(record.key)
if metric is None:
LOGGER.warning("metrics key %r is unknown.", record.key)
elif record.stat_type != metric[0]:
LOGGER.warning(
"metrics key %r has wrong type; got %s expecting %s",
record.key,
record.stat_type,
metric[0],
)
return record
HOSTID = os.environ.get("HOSTID", "none")
markus.configure(
backends=[
{
"class": "markus.backends.logging.LoggingMetrics",
"filters": [RegisteredMetricsFilter(REGISTERED_METRICS)],
}
]
)
metrics = markus.get_metrics()
metrics.incr("foo")
metrics.incr("locate.fallback.lookup_count")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment