Show git head in health infos

This commit is contained in:
Stefan Haun 2021-02-19 16:56:52 +01:00
parent 50ebba0df6
commit b161d0d7e9

16
app.py
View file

@ -20,8 +20,9 @@ startup_timestamp = datetime.now()
class HealthHandler(tornado.web.RequestHandler, metaclass=ABCMeta): class HealthHandler(tornado.web.RequestHandler, metaclass=ABCMeta):
# noinspection PyAttributeOutsideInit # noinspection PyAttributeOutsideInit
def initialize(self): def initialize(self, sources=None):
self.git_version = self._load_git_version() self.git_version = self._load_git_version()
self.sources = sources
@staticmethod @staticmethod
def _load_git_version(): def _load_git_version():
@ -53,6 +54,12 @@ class HealthHandler(tornado.web.RequestHandler, metaclass=ABCMeta):
health['timestamp'] = isodate.datetime_isoformat(datetime.now()) health['timestamp'] = isodate.datetime_isoformat(datetime.now())
health['uptime'] = isodate.duration_isoformat(datetime.now() - startup_timestamp) health['uptime'] = isodate.duration_isoformat(datetime.now() - startup_timestamp)
if self.sources:
for s in self.sources:
h = s()
if h is not None:
health = {**health, **h}
self.set_header("Content-Type", "application/json") self.set_header("Content-Type", "application/json")
self.write(json.dumps(health, indent=4)) self.write(json.dumps(health, indent=4))
self.set_status(200) self.set_status(200)
@ -70,10 +77,11 @@ class Oas3Handler(tornado.web.RequestHandler, metaclass=ABCMeta):
self.finish() self.finish()
def make_app(_auth_provider=None): def make_app(_auth_provider=None, gitmgr=None):
version_path = r"/v[0-9]" version_path = r"/v[0-9]"
return tornado.web.Application([ return tornado.web.Application([
(version_path + r"/health", HealthHandler), (version_path + r"/health", HealthHandler,
{"sources": [lambda: {"git-head": gitmgr.head_sha}] if gitmgr else None}),
(version_path + r"/oas3", Oas3Handler), (version_path + r"/oas3", Oas3Handler),
]) ])
@ -89,7 +97,7 @@ def main():
gitmgr.setup() gitmgr.setup()
gitmgr.printout() gitmgr.printout()
util.run_tornado_server(make_app(auth_provider), util.run_tornado_server(make_app(auth_provider, gitmgr),
server_port=port) server_port=port)
# Teardown # Teardown