Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Pip packaging and WSGI logging #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`
9 changes: 9 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
patroni_exporter

Export Patroni metrics in Prometheus format.
"""

__version__ = "0.0.2"
__author__ = 'Jan Tomsa'
__credits__ = 'ops@showmax.com'
19 changes: 17 additions & 2 deletions patroni_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()


Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
)