8000 feat(http): disable healthcheck access logs with env by deepankarm · Pull Request #4814 · jina-ai/serve · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(http): disable healthcheck access logs with env #4814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jina/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions jina/serve/runtimes/gateway/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import logging
import os

from jina import __default_host__
Expand Down Expand Up @@ -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 {}
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/serve/runtimes/gateway/http/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
0