-
Notifications
You must be signed in to change notification settings - Fork 431
feat(router): add --disable-access-log-for-endpoints to silence probe logs #979
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
base: main
Are you sure you want to change the base?
Changes from all commits
edaf08b
2d18b0f
4d0e939
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |||||||||||||||||||||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||
| # limitations under the License. | ||||||||||||||||||||||||||||||||
| import asyncio | ||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||
| import threading | ||||||||||||||||||||||||||||||||
| from contextlib import asynccontextmanager | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -103,6 +104,28 @@ | |||||||||||||||||||||||||||||||
| logger = init_logger(__name__) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class _EndpointAccessLogFilter(logging.Filter): | ||||||||||||||||||||||||||||||||
| """Suppress uvicorn access-log records for a fixed set of request paths. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Handy for silencing health/readiness probes (e.g. ``/health``, | ||||||||||||||||||||||||||||||||
| ``/metrics``) that would otherwise flood the router logs while keeping | ||||||||||||||||||||||||||||||||
| access logs for real traffic. uvicorn emits each access record with | ||||||||||||||||||||||||||||||||
| positional ``args`` of ``(client_addr, method, path, http_version, | ||||||||||||||||||||||||||||||||
| status_code)``; we match on ``path`` (ignoring any query string). | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def __init__(self, endpoints): | ||||||||||||||||||||||||||||||||
| super().__init__() | ||||||||||||||||||||||||||||||||
| self._endpoints = {e.strip() for e in endpoints if e and e.strip()} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def filter(self, record: logging.LogRecord) -> bool: | ||||||||||||||||||||||||||||||||
| if self._endpoints and record.args and len(record.args) >= 3: | ||||||||||||||||||||||||||||||||
| path = str(record.args[2]).split("?", 1)[0] | ||||||||||||||||||||||||||||||||
| if path in self._endpoints: | ||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @asynccontextmanager | ||||||||||||||||||||||||||||||||
| async def lifespan(app: FastAPI): | ||||||||||||||||||||||||||||||||
| app.state.aiohttp_client_wrapper.start() | ||||||||||||||||||||||||||||||||
|
|
@@ -393,6 +416,19 @@ def main(): | |||||||||||||||||||||||||||||||
| # many concurrent requests active. | ||||||||||||||||||||||||||||||||
| set_ulimit() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Optionally drop access logs for noisy probe endpoints. The filter is | ||||||||||||||||||||||||||||||||
| # attached to the ``uvicorn.access`` logger before uvicorn configures its | ||||||||||||||||||||||||||||||||
| # own logging; uvicorn's dictConfig replaces handlers but preserves | ||||||||||||||||||||||||||||||||
| # existing logger filters, so the suppression survives both text and json | ||||||||||||||||||||||||||||||||
| # log formats. | ||||||||||||||||||||||||||||||||
| disabled_endpoints = parse_comma_separated_args( | ||||||||||||||||||||||||||||||||
| args.disable_access_log_for_endpoints | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| if disabled_endpoints: | ||||||||||||||||||||||||||||||||
| logging.getLogger("uvicorn.access").addFilter( | ||||||||||||||||||||||||||||||||
| _EndpointAccessLogFilter(disabled_endpoints) | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+419
to
+430
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Python's To resolve this, we should store the parsed endpoints in For example, in @asynccontextmanager
async def lifespan(app: FastAPI):
# ... existing code ...
disabled_endpoints = getattr(app.state, "disable_access_log_for_endpoints", None)
if disabled_endpoints:
logging.getLogger("uvicorn.access").addFilter(
_EndpointAccessLogFilter(disabled_endpoints)
)
yield
# ...
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| uvicorn_kwargs = { | ||||||||||||||||||||||||||||||||
| "host": args.host, | ||||||||||||||||||||||||||||||||
| "port": args.port, | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
record.argsis a dictionary (which can happen if a log message uses named placeholders),len(record.args)returns the number of keys. If the dictionary has 3 or more keys,record.args[2]will raise aKeyErrorbecause2is not a valid key. To prevent potential logging crashes, we should explicitly verify thatrecord.argsis a sequence (like atupleorlist) before indexing it.