Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions logging_prometheus/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
39 changes: 39 additions & 0 deletions logging_prometheus/core.py
Original file line number Diff line number Diff line change
@@ -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))
5 changes: 5 additions & 0 deletions logging_prometheus/register.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down