From c4ef5532fbf91253d3be129d7a4cee2196163dfb Mon Sep 17 00:00:00 2001 From: Peter Lieverdink Date: Fri, 30 Aug 2024 14:39:11 +1000 Subject: [PATCH 1/4] feat: Add a health check. --- html/app.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/html/app.py b/html/app.py index 540466a..8104584 100644 --- a/html/app.py +++ b/html/app.py @@ -121,6 +121,16 @@ class Response(BaseModel): #------------------------------------------------------------------------------# +# Health check endpoint. +@app.get('/status', status_code=200) +def health_status(): + """ + A very simple health check endpoint. + """ + return {"status": "ok"} + +#------------------------------------------------------------------------------# + class TextSplitRequest(Request): """ A text splitting request. From ee87f30b99bc09668eb3399fb29ef99fbb5efbee Mon Sep 17 00:00:00 2001 From: Peter Lieverdink Date: Fri, 30 Aug 2024 14:48:01 +1000 Subject: [PATCH 2/4] feat: Drop in a guessed log_config.yaml --- html/log_config.yaml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 html/log_config.yaml diff --git a/html/log_config.yaml b/html/log_config.yaml new file mode 100644 index 0000000..90328b0 --- /dev/null +++ b/html/log_config.yaml @@ -0,0 +1,33 @@ +version: 1 +disable_existing_loggers: False +formatters: + default: + "()": uvicorn.logging.DefaultFormatter + format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + access: + "()": uvicorn.logging.AccessFormatter + format: "[%(asctime)s %(process)d:%(threadName)s] %(name)s - %(levelname)s - %(message)s | %(filename)s:%(lineno)d" + +handlers: + default: + class: logging.FileHandler + level: INFO + formatter: default + filename: /var/log/uvicorn/uvicorn.log + encoding: utf8 + mode: a + + access: + formatter: access + class: logging.FileHandler + level: INFO + formatter: default + filename: /var/log/uvicorn/access.log + encoding: utf8 + mode: a + +loggers: + uvicorn.error: + level: INFO + handlers: [default] + propagate: no From a17c4ec8323b8830af92f34ca7bff5f6fbb8cd6c Mon Sep 17 00:00:00 2001 From: orakili Date: Fri, 30 Aug 2024 08:01:21 +0000 Subject: [PATCH 3/4] chore: ensure uvicorn uses the log config file and log things where they belong --- docker/Dockerfile | 2 +- html/log_config.yaml | 9 +++++++-- html/server.sh | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index e0a646c..2fe98bc 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -38,7 +38,7 @@ COPY --from=builder /opt/venv /opt/venv ENV PATH=/opt/venv/bin:$PATH # Download space models. -RUN mkdir -p /opt/models && \ +RUN mkdir -p /var/log/uvicorn /opt/models && \ python3 -m spacy download en_core_web_sm && \ python3 -m spacy download es_core_news_sm && \ python3 -m spacy download fr_core_news_sm diff --git a/html/log_config.yaml b/html/log_config.yaml index 90328b0..4b84197 100644 --- a/html/log_config.yaml +++ b/html/log_config.yaml @@ -7,7 +7,7 @@ formatters: access: "()": uvicorn.logging.AccessFormatter format: "[%(asctime)s %(process)d:%(threadName)s] %(name)s - %(levelname)s - %(message)s | %(filename)s:%(lineno)d" - + handlers: default: class: logging.FileHandler @@ -25,9 +25,14 @@ handlers: filename: /var/log/uvicorn/access.log encoding: utf8 mode: a - + loggers: uvicorn.error: level: INFO handlers: [default] propagate: no + + uvicorn.access: + level: INFO + handlers: [access] + propagate: no diff --git a/html/server.sh b/html/server.sh index b91d976..a81b50f 100755 --- a/html/server.sh +++ b/html/server.sh @@ -4,4 +4,5 @@ uvicorn app:app \ --host ${SERVER_HOST:-0.0.0.0} \ --port ${SERVER_PORT:-80} \ + --log-config=log_config.yaml \ --reload From 49ad833c30dfbb63d94a18a0d6f102c58971d405 Mon Sep 17 00:00:00 2001 From: orakili Date: Fri, 30 Aug 2024 08:06:24 +0000 Subject: [PATCH 4/4] refactor: just make the health endpoint and response more consistent with the rest --- html/app.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/html/app.py b/html/app.py index 8104584..5b2db23 100644 --- a/html/app.py +++ b/html/app.py @@ -121,13 +121,25 @@ class Response(BaseModel): #------------------------------------------------------------------------------# +class HealthCheckResponse(BaseModel): + """ + Health check response. + + Attributes: + status (str): the health status of the app, defaults to "ok". + """ + status: str + # Health check endpoint. @app.get('/status', status_code=200) -def health_status(): +def health_status() -> HealthCheckResponse: """ A very simple health check endpoint. + + Returns: + HealthCheckResponse: the health check response. """ - return {"status": "ok"} + return HealthCheckResponse(status="ok") #------------------------------------------------------------------------------#