Update for strict typing

This commit is contained in:
Scott Wallace 2021-12-08 17:31:08 +00:00
parent c7d36e7f38
commit 3c3592b07c
Signed by: scott
GPG key ID: AA742FDC5AFE2A72
2 changed files with 18 additions and 18 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
Module to act as a Prometheus Exporter for Docker containers with a Module to act as a Prometheus Exporter for Docker containers with a
healthcheck configured healthcheck configured
""" """
import argparse import argparse
@ -9,33 +9,30 @@ import os
import os.path import os.path
import sys import sys
from http.server import HTTPServer from http.server import HTTPServer
from typing import Any
import docker # type: ignore import docker # type: ignore[import]
import numpy import numpy
from prometheus_client import ( # type: ignore from prometheus_client import CollectorRegistry # type: ignore[import]
CollectorRegistry, from prometheus_client import Gauge, MetricsHandler, generate_latest
Gauge,
MetricsHandler,
generate_latest,
)
LISTEN_PORT = int(os.environ.get('DOCKSTAT_LISTEN_PORT', 8080)) LISTEN_PORT = int(os.environ.get('DOCKSTAT_LISTEN_PORT', 8080))
HEALTHY_STR = 'healthy' HEALTHY_STR = 'healthy'
class HTTPHandler(MetricsHandler): class HTTPHandler(MetricsHandler): # type: ignore[misc]
""" """
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): def __init__(self, *args: Any, **kwargs: Any):
self.docker_api: docker.APIClient = docker.APIClient() self.docker_api: docker.APIClient = docker.APIClient()
self.docker_client = docker.from_env() self.docker_client = docker.from_env()
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Override built-in method # Override built-in method
def do_GET(self): def do_GET(self) -> None:
""" """
Handle GET requests Handle GET requests
""" """
@ -49,7 +46,7 @@ class HTTPHandler(MetricsHandler):
self._respond(200, 'OK') self._respond(200, 'OK')
def _respond(self, status: int, message: str): def _respond(self, status: int, message: str) -> None:
""" """
Output a simple HTTP status and string to the client Output a simple HTTP status and string to the client
@ -63,7 +60,7 @@ class HTTPHandler(MetricsHandler):
self.end_headers() self.end_headers()
self.wfile.write(bytes(str(message).encode())) self.wfile.write(bytes(str(message).encode()))
def _metrics(self): def _metrics(self) -> None:
""" """
Handle the request for metrics Handle the request for metrics
""" """
@ -108,13 +105,13 @@ class HTTPHandler(MetricsHandler):
for container in self.docker_client.containers.list(all=True): for container in self.docker_client.containers.list(all=True):
data = self.docker_api.inspect_container(container.id) data = self.docker_api.inspect_container(container.id)
running: str = bool(data['State']['Running']) running = bool(data['State']['Running'])
started_at: int = data['State']['StartedAt'] started_at = data['State']['StartedAt']
exit_code: int = int(data['State']['ExitCode']) exit_code = int(data['State']['ExitCode'])
alert_threshold = int( alert_threshold = int(
data['Config']['Labels'].get('io.prometheus.alert.downtime', 3600) data['Config']['Labels'].get('io.prometheus.alert.downtime', 3600)
) )
starttime = numpy.datetime64(started_at, 's').astype('long') starttime: numpy.longlong = numpy.datetime64(started_at, 's').astype('long')
status_gauge.labels( status_gauge.labels(
container.id, container.id,
@ -134,7 +131,7 @@ class HTTPHandler(MetricsHandler):
).set(alert_threshold) ).set(alert_threshold)
try: try:
health_str: str = data['State']['Health']['Status'] health_str = data['State']['Health']['Status']
health_gauge.labels( health_gauge.labels(
container.id, container.id,
container.name, container.name,

3
mypy.ini Normal file
View file

@ -0,0 +1,3 @@
[mypy]
plugins = numpy.typing.mypy_plugin
exclude = ['\.pyenv', 'venv']