Update for strict typing
This commit is contained in:
parent
c7d36e7f38
commit
3c3592b07c
31
dockstat.py
31
dockstat.py
|
@ -9,33 +9,30 @@ import os
|
|||
import os.path
|
||||
import sys
|
||||
from http.server import HTTPServer
|
||||
from typing import Any
|
||||
|
||||
import docker # type: ignore
|
||||
import docker # type: ignore[import]
|
||||
import numpy
|
||||
from prometheus_client import ( # type: ignore
|
||||
CollectorRegistry,
|
||||
Gauge,
|
||||
MetricsHandler,
|
||||
generate_latest,
|
||||
)
|
||||
from prometheus_client import CollectorRegistry # type: ignore[import]
|
||||
from prometheus_client import Gauge, MetricsHandler, generate_latest
|
||||
|
||||
LISTEN_PORT = int(os.environ.get('DOCKSTAT_LISTEN_PORT', 8080))
|
||||
HEALTHY_STR = 'healthy'
|
||||
|
||||
|
||||
class HTTPHandler(MetricsHandler):
|
||||
class HTTPHandler(MetricsHandler): # type: ignore[misc]
|
||||
"""
|
||||
Class to encompass the requirements of a Prometheus Exporter
|
||||
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_client = docker.from_env()
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# Override built-in method
|
||||
def do_GET(self):
|
||||
def do_GET(self) -> None:
|
||||
"""
|
||||
Handle GET requests
|
||||
"""
|
||||
|
@ -49,7 +46,7 @@ class HTTPHandler(MetricsHandler):
|
|||
|
||||
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
|
||||
|
||||
|
@ -63,7 +60,7 @@ class HTTPHandler(MetricsHandler):
|
|||
self.end_headers()
|
||||
self.wfile.write(bytes(str(message).encode()))
|
||||
|
||||
def _metrics(self):
|
||||
def _metrics(self) -> None:
|
||||
"""
|
||||
Handle the request for metrics
|
||||
"""
|
||||
|
@ -108,13 +105,13 @@ class HTTPHandler(MetricsHandler):
|
|||
for container in self.docker_client.containers.list(all=True):
|
||||
data = self.docker_api.inspect_container(container.id)
|
||||
|
||||
running: str = bool(data['State']['Running'])
|
||||
started_at: int = data['State']['StartedAt']
|
||||
exit_code: int = int(data['State']['ExitCode'])
|
||||
running = bool(data['State']['Running'])
|
||||
started_at = data['State']['StartedAt']
|
||||
exit_code = int(data['State']['ExitCode'])
|
||||
alert_threshold = int(
|
||||
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(
|
||||
container.id,
|
||||
|
@ -134,7 +131,7 @@ class HTTPHandler(MetricsHandler):
|
|||
).set(alert_threshold)
|
||||
|
||||
try:
|
||||
health_str: str = data['State']['Health']['Status']
|
||||
health_str = data['State']['Health']['Status']
|
||||
health_gauge.labels(
|
||||
container.id,
|
||||
container.name,
|
||||
|
|
Loading…
Reference in a new issue