From b09f1b08bae1b53b877f5ae748d27bbc97cdc00e Mon Sep 17 00:00:00 2001 From: alexandrovas Date: Tue, 6 Jul 2021 12:48:25 +0300 Subject: [PATCH 1/2] custom logging for WSGI server --- patroni_exporter.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/patroni_exporter.py b/patroni_exporter.py index 57373d0..c450fbd 100755 --- a/patroni_exporter.py +++ b/patroni_exporter.py @@ -18,7 +18,7 @@ from prometheus_client.exposition import choose_encoder from typing import List, Any, Dict, Union, Type, ByteString, Iterable from urllib.parse import parse_qs, urlparse -from wsgiref.simple_server import make_server, WSGIServer +from wsgiref.simple_server import make_server, WSGIServer, WSGIRequestHandler from wsgiref.util import request_uri import logging @@ -224,6 +224,20 @@ class ServerClass(WSGIServer): address_family = getattr(socket, self.cmdline.address_family) return ServerClass + + def get_request_handler_class(self) -> Type['WSGIRequestHandler']: + """ + Creates a WSGI request handler class with custom logger + """ + class LoggingWSGIRequestHandler(WSGIRequestHandler): + + def log_message(self, format, *args): + logger.debug("%s - - [%s] %s\n" % + (self.client_address[0], + self.log_date_time_string(), + format%args)) + + return LoggingWSGIRequestHandler @staticmethod def parse_args() -> argparse.Namespace: @@ -309,7 +323,8 @@ def __call__(self) -> None: httpd = make_server(self.cmdline.bind, self.cmdline.port, self.app, - self.get_server_class()) + self.get_server_class(), + handler_class=self.get_request_handler_class()) httpd.serve_forever() From 4a31bbb62a4f7955e1e91e1d5701692916175406 Mon Sep 17 00:00:00 2001 From: alexandrovas Date: Tue, 6 Jul 2021 12:58:27 +0300 Subject: [PATCH 2/2] pip packaging notes --- README.md | 10 ++++++++++ __init__.py | 9 +++++++++ setup.py | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 __init__.py diff --git a/README.md b/README.md index f1aff55..dff1b51 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,16 @@ docker build -t patroni_exporter . docker run -d -ti patroni_exporter --port some_port --patroni-url http://some_host_fqdn:some_port/patroni --timeout 5 --debug ``` +## Pip package + +To create pip-package you need execute this command: + +``` +python setup.py sdist +``` + +If the command is successful, then the directory `dist` well be contain the `.tar.gz` pip-package. + ## Known issues/limitations/workarounds - due to how Patroni replicas respond with their information, but, when compared to primary, use HTTP code 503 (Service Unavailable) to avoid being registered as write-capable endpoints on load balancers, the exporter will attempt proper parsing when the response is a JSON with key-value `{"role": "replica"}` diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..dfa4a16 --- /dev/null +++ b/__init__.py @@ -0,0 +1,9 @@ +""" +patroni_exporter + +Export Patroni metrics in Prometheus format. +""" + +__version__ = "0.0.2" +__author__ = 'Jan Tomsa' +__credits__ = 'ops@showmax.com' diff --git a/setup.py b/setup.py index a02255c..3eb3a52 100644 --- a/setup.py +++ b/setup.py @@ -3,9 +3,11 @@ setup( name='patroni_exporter', - version='0.0.1', + version='0.0.2', description='Export Patroni metrics in Prometheus format', + url='https://github.com/Showmax/patroni-exporter', author='Jan Tomsa', author_email='ops@showmax.com', scripts=['patroni_exporter.py'], + install_requires=['prometheus_client','python-dateutil','requests'], )