Add missing content-type header

This commit is contained in:
Scott Wallace 2024-11-15 09:18:51 +00:00
parent 1999536b50
commit 23ccb252da
Signed by: scott
SSH key fingerprint: SHA256:+LJug6Dj01Jdg86CILGng9r0lJseUrpI0xfRqdW9Uws

View file

@ -11,18 +11,17 @@ import sys
from http.server import HTTPServer from http.server import HTTPServer
from typing import Any from typing import Any
import docker # type: ignore[import] import docker
import docker.errors # type: ignore[import] import docker.errors
import numpy import numpy
from docker.models.containers import Container # type: ignore[import] from docker.models.containers import Container
from prometheus_client import CollectorRegistry # type: ignore[import] from prometheus_client import CollectorRegistry, Gauge, MetricsHandler, generate_latest
from prometheus_client import 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): # type: ignore[misc] 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
@ -62,6 +61,7 @@ class HTTPHandler(MetricsHandler): # type: ignore[misc]
message (str): String to output message (str): String to output
""" """
self.send_response(int(status) or 500) self.send_response(int(status) or 500)
self.send_header("content-type", "text/plain")
self.end_headers() self.end_headers()
try: try:
self.wfile.write(bytes(str(message).encode())) self.wfile.write(bytes(str(message).encode()))
@ -111,9 +111,9 @@ class HTTPHandler(MetricsHandler): # type: ignore[misc]
) )
container: Container container: Container
for container in self.docker_client.containers.list(all=True): # type: ignore for container in self.docker_client.containers.list(all=True):
try: try:
data = self.docker_api.inspect_container(container.id) data = self.docker_api.inspect_container(getattr(container, "id"))
except docker.errors.NotFound: except docker.errors.NotFound:
print(f"WARNING: Container {container.id} does not exist. Skipping.") print(f"WARNING: Container {container.id} does not exist. Skipping.")
continue continue