From b161d0d7e98c4cac0d5f3ac03dd3628f1915546b Mon Sep 17 00:00:00 2001 From: Stefan Haun Date: Fri, 19 Feb 2021 16:56:52 +0100 Subject: [PATCH] Show git head in health infos --- app.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 0347d04..cd31e35 100644 --- a/app.py +++ b/app.py @@ -20,8 +20,9 @@ startup_timestamp = datetime.now() class HealthHandler(tornado.web.RequestHandler, metaclass=ABCMeta): # noinspection PyAttributeOutsideInit - def initialize(self): + def initialize(self, sources=None): self.git_version = self._load_git_version() + self.sources = sources @staticmethod def _load_git_version(): @@ -53,6 +54,12 @@ class HealthHandler(tornado.web.RequestHandler, metaclass=ABCMeta): health['timestamp'] = isodate.datetime_isoformat(datetime.now()) 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.write(json.dumps(health, indent=4)) self.set_status(200) @@ -70,10 +77,11 @@ class Oas3Handler(tornado.web.RequestHandler, metaclass=ABCMeta): self.finish() -def make_app(_auth_provider=None): +def make_app(_auth_provider=None, gitmgr=None): version_path = r"/v[0-9]" 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), ]) @@ -89,7 +97,7 @@ def main(): gitmgr.setup() gitmgr.printout() - util.run_tornado_server(make_app(auth_provider), + util.run_tornado_server(make_app(auth_provider, gitmgr), server_port=port) # Teardown