From 7cbb4c36badefb78b8b8455da428d3660fc69b7f Mon Sep 17 00:00:00 2001 From: Deepankar Mahapatro Date: Fri, 20 May 2022 21:29:07 +0530 Subject: [PATCH 1/2] feat(http): disable healthcheck access logs with env --- jina/serve/runtimes/gateway/http/__init__.py | 11 ++++++++ .../serve/runtimes/gateway/http/test_app.py | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/jina/serve/runtimes/gateway/http/__init__.py b/jina/serve/runtimes/gateway/http/__init__.py index 2446f6695baa1..909ee6ae2edf2 100644 --- a/jina/serve/runtimes/gateway/http/__init__.py +++ b/jina/serve/runtimes/gateway/http/__init__.py @@ -1,4 +1,5 @@ import asyncio +import logging import os from jina import __default_host__ @@ -47,6 +48,16 @@ async def serve(self, **kwargs): """ await self.main_loop() + if 'JINA_DISABLE_HEALTHCHECK_LOGS' in os.environ: + + class _EndpointFilter(logging.Filter): + def filter(self, record: logging.LogRecord) -> bool: + # NOTE: space is important after `GET /`, else all logs will be disabled. + return record.getMessage().find("GET / ") == -1 + + # Filter out healthcheck endpoint `GET /` + logging.getLogger("uvicorn.access").addFilter(_EndpointFilter()) + from jina.helper import extend_rest_interface uvicorn_kwargs = self.args.uvicorn_kwargs or {} diff --git a/tests/unit/serve/runtimes/gateway/http/test_app.py b/tests/unit/serve/runtimes/gateway/http/test_app.py index bec1e2cdda144..53046cc23dea0 100644 --- a/tests/unit/serve/runtimes/gateway/http/test_app.py +++ b/tests/unit/serve/runtimes/gateway/http/test_app.py @@ -273,3 +273,30 @@ def test_app_models_acceptance(docs_input): r = req.post(f'http://localhost:{f.port}/index', json=docs_input) assert DocumentArray.from_dict(r.json()['data'])[0].text == 'text_input' + + +def test_healthcheck_logs(capfd): + os.environ['JINA_LOG_LEVEL'] = 'INFO' + + f = Flow(protocol='http', port=12345).add() + with f: + req.get('http://localhost:12345/') + req.get('http://localhost:12345/docs') + + out, _ = capfd.readouterr() + assert '"GET / HTTP/1.1" 200 OK' in out + assert '"GET /docs HTTP/1.1" 200 OK' in out + + +def test_no_healthcheck_logs_with_env(capfd): + os.environ['JINA_LOG_LEVEL'] = 'INFO' + os.environ['JINA_DISABLE_HEALTHCHECK_LOGS'] = '1' + + f = Flow(protocol='http', port=12345).add() + with f: + req.get('http://localhost:12345/') + req.get('http://localhost:12345/docs') + + out, _ = capfd.readouterr() + assert '"GET / HTTP/1.1" 200 OK' not in out + assert '"GET /docs HTTP/1.1" 200 OK' in out From 04736ca4f667b3274a7c9e3395965bb7497995d2 Mon Sep 17 00:00:00 2001 From: Deepankar Mahapatro Date: Tue, 24 May 2022 16:50:11 +0530 Subject: [PATCH 2/2] chore: add env to init --- jina/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/jina/__init__.py b/jina/__init__.py index 9cf22be9a719d..6054d2282d4d1 100644 --- a/jina/__init__.py +++ b/jina/__init__.py @@ -105,6 +105,7 @@ def _warning_on_one_line(message, category, filename, lineno, *args, **kwargs): 'JINA_RANDOM_PORT_MAX', 'JINA_RANDOM_PORT_MIN', 'JINA_VCS_VERSION', + 'JINA_DISABLE_HEALTHCHECK_LOGS', ) __default_host__ = _os.environ.get(