Move to use prometheus_client

This commit is contained in:
Scott Wallace 2020-10-10 16:24:44 +01:00
parent 12d09852da
commit ac5a61aad4
4 changed files with 38 additions and 28 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.pyenv/

View file

@ -12,7 +12,7 @@ ADD requirements.txt .
RUN python -m pip install -r requirements.txt RUN python -m pip install -r requirements.txt
WORKDIR /app WORKDIR /app
ADD . /app ADD dockstat.py /app
# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights # Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
# RUN useradd appuser && chown -R appuser /app # RUN useradd appuser && chown -R appuser /app

View file

@ -5,23 +5,34 @@ Module to act as a Prometheus Exporter for Docker containers with a
""" """
import argparse import argparse
import http.server
import os.path import os.path
import socketserver
import sys import sys
import time from http.server import HTTPServer
import docker import docker
from prometheus_client import (
CollectorRegistry,
Gauge,
generate_latest,
MetricsHandler,
)
LISTEN_PORT = 8080 LISTEN_PORT = 8080
HEALTHY_STR = 'healthy' HEALTHY_STR = 'healthy'
class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler): class HTTPHandler(MetricsHandler):
""" """
Class to encompass the requirements of a Prometheus Exporter Class to encompass the requirements of a Prometheus Exporter
for Docker containers with a healthcheck configured for Docker containers with a healthcheck configured
""" """
def __init__(self, *args, **kwargs):
self.docker_api = docker.APIClient()
self.docker_client = docker.from_env()
super().__init__(*args, **kwargs)
# Override built-in method
# pylint: disable=invalid-name # pylint: disable=invalid-name
def do_GET(self): def do_GET(self):
""" """
@ -33,7 +44,6 @@ class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
if self.path == '/healthcheck': if self.path == '/healthcheck':
self._healthcheck() self._healthcheck()
def _healthcheck(self, message=True): def _healthcheck(self, message=True):
""" """
Method to return 200 or 500 response and an optional message Method to return 200 or 500 response and an optional message
@ -50,35 +60,37 @@ class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
if message: if message:
self.wfile.write(b'OK') self.wfile.write(b'OK')
def _metrics(self): def _metrics(self):
""" """
Method to handle the request for metrics Method to handle the request for metrics
""" """
self._healthcheck(message=False) self._healthcheck(message=False)
api = docker.APIClient() registry = CollectorRegistry()
client = docker.from_env() gauge = Gauge(
for container in client.containers.list(): 'container_inspect_state_health_status',
now = int(round(time.time() * 1000)) "Container's healthcheck value (binary)",
data = api.inspect_container(container.id) labelnames=['id', 'name', 'value'],
registry=registry
)
for container in self.docker_client.containers.list():
data = self.docker_api.inspect_container(container.id)
try: try:
health_str = data["State"]["Health"]["Status"] health_str = data["State"]["Health"]["Status"]
label_values = [
container.id,
container.name,
health_str,
]
except KeyError: except KeyError:
pass pass
else: else:
self.wfile.write( gauge.labels(*label_values).set(int(health_str == HEALTHY_STR))
bytes(
f'container_inspect_state_health_status{{' self.wfile.write(generate_latest(registry))
f'id="{container.id}",'
f'name="{container.name}",'
f'value="{health_str}"'
f'}} '
f'{int(health_str == HEALTHY_STR)} {now}\n'.encode()
)
)
def healthy(): def healthy():
@ -105,7 +117,6 @@ if __name__ == '__main__':
return parser.parse_args() return parser.parse_args()
def main(): def main():
""" """
main() main()
@ -116,11 +127,8 @@ if __name__ == '__main__':
# Invert the sense of 'healthy' for Unix CLI usage # Invert the sense of 'healthy' for Unix CLI usage
return not healthy() return not healthy()
Handler = SimpleHTTPRequestHandler HTTPServer(('', LISTEN_PORT), HTTPHandler).serve_forever()
with socketserver.TCPServer(('', LISTEN_PORT), Handler) as httpd: return 0
httpd.serve_forever()
return True
sys.exit(main()) sys.exit(main())

View file

@ -1,2 +1,3 @@
# To ensure app dependencies are ported from your virtual environment/host machine into your container, run 'pip freeze > requirements.txt' in the terminal to overwrite this file # To ensure app dependencies are ported from your virtual environment/host machine into your container, run 'pip freeze > requirements.txt' in the terminal to overwrite this file
docker docker
prometheus_client