diff --git a/logging_prometheus/__init__.py b/logging_prometheus/__init__.py index 6cfb160..e69de29 100644 --- a/logging_prometheus/__init__.py +++ b/logging_prometheus/__init__.py @@ -1,33 +0,0 @@ -import prometheus_client -import logging - - -log_entries = prometheus_client.Counter( - 'python_logging_messages_total', - 'Count of log entries by logger and level.', - ['logger', 'level']) - - -class ExportingLogHandler(logging.Handler): - """A LogHandler that exports logging metrics for Prometheus.io.""" - def emit(self, record): - log_entries.labels(record.name, record.levelname).inc() - - -def export_stats_on_root_logger(): - """Attaches an ExportingLogHandler to the root logger. - - This should be sufficient to get metrics about all logging in a - Python application, unless a part of the application defines its - own logger and sets this logger's `propagate` attribute to - False. The `propagate` attribute is True by default, which means - that by default all loggers propagate all their logged messages to - the root logger. - """ - logger = logging.getLogger() - logger.addHandler(ExportingLogHandler()) - - -# Just importing this module should make us export the metrics for the -# root logger. -export_stats_on_root_logger() diff --git a/logging_prometheus/core.py b/logging_prometheus/core.py new file mode 100644 index 0000000..acb328c --- /dev/null +++ b/logging_prometheus/core.py @@ -0,0 +1,39 @@ +import prometheus_client +import logging + + +class ExportingLogHandler(logging.Handler): + """A LogHandler that exports logging metrics for Prometheus.io.""" + def __init__(self, level=logging.NOTSET, extra=None): + logging.Handler.__init__(self, level=level) + + labels = ['logger', 'level'] + self.extra = [] + if extra: + if not isinstance(extra, list): + raise Exception("Argument 'extra' must be a list") + labels = labels + extra + self.extra = extra + + self.log_entries = prometheus_client.Counter( + 'python_logging_messages_total', + 'Count of log entries by logger and level.', + labels) + + def emit(self, record): + labels = [record.name, record.levelname] + [getattr(record, v, "unknown") for v in self.extra] + self.log_entries.labels(*labels).inc() + + +def export_stats_on_root_logger(extra=None): + """Attaches an ExportingLogHandler to the root logger. + + This should be sufficient to get metrics about all logging in a + Python application, unless a part of the application defines its + own logger and sets this logger's `propagate` attribute to + False. The `propagate` attribute is True by default, which means + that by default all loggers propagate all their logged messages to + the root logger. + """ + logger = logging.getLogger() + logger.addHandler(ExportingLogHandler(extra=extra)) diff --git a/logging_prometheus/register.py b/logging_prometheus/register.py new file mode 100644 index 0000000..0a6602b --- /dev/null +++ b/logging_prometheus/register.py @@ -0,0 +1,5 @@ +from logging_prometheus.core import export_stats_on_root_logger + +# Just importing this module should make us export the metrics for the +# root logger. +export_stats_on_root_logger() diff --git a/tests/test_logging.py b/tests/test_logging.py index eea5ee5..e97c4c4 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -1,7 +1,7 @@ import unittest import logging from prometheus_client import REGISTRY -import logging_prometheus +from logging_prometheus import register class TestLoggingPrometheus(unittest.TestCase):