Skip to content

Commit

Permalink
Add /healthcheck endpoint that doesn't collect PDU data
Browse files Browse the repository at this point in the history
When the exporter is deployed in a system that healthchecks running
processes, such as Kubernetes, it's useful to have a healthcheck
endpoint that can return quickly which still catching errors where eg
the web server is hanging.

This basic /healthcheck endpoint returns a 200 and a short message,
avoiding the call to `collect` which would normally gather all of the
PDU data and can be slow to return. All other endpoints will still
return the PDU data in Prometheus format.

Closes
#5.
  • Loading branch information
tomelliff committed Apr 30, 2024
1 parent 52edcbc commit 9c826a2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions prometheus_raritan_pdu_exporter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import logging
import time
import urllib.parse
from wsgiref.simple_server import make_server

from prometheus_client import start_http_server, REGISTRY
from prometheus_client import MetricsHandler, make_wsgi_app, REGISTRY

from . import DEFAULT_PORT
from .exporter import RaritanExporter
Expand Down Expand Up @@ -90,6 +91,17 @@ def read_config(config: str) -> List[RaritanAuth]:
return config_data


class HealthcheckHandler(MetricsHandler):
def do_GET(self):
logging.debug(self.path)
if self.path == '/healthcheck':
self.send_response(200)
self.end_headers()
self.wfile.write(b'Server is running')
else:
super().do_GET()


def main():
args = parse_args()

Expand All @@ -109,9 +121,11 @@ def main():
listen_addr = urllib.parse.urlsplit(f'//{args.listen_address}')
addr = listen_addr.hostname if listen_addr.hostname else '0.0.0.0'
port = listen_addr.port if listen_addr.port else DEFAULT_PORT
REGISTRY.register(RaritanExporter(config=config))
start_http_server(port, addr=addr)
logger.info('listening on %s' % listen_addr.netloc)
REGISTRY.register(RaritanExporter(config=config))
prometheus_application = make_wsgi_app()
httpd = make_server(addr, port, prometheus_application, handler_class=HealthcheckHandler)
httpd.serve_forever()
except KeyboardInterrupt:
logger.info('Interrupted by user')
exit(0)
Expand Down

0 comments on commit 9c826a2

Please sign in to comment.