-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add configurable logging filter #49
Conversation
Filters identify log messages to filter out, so that the logger does not log messages containing any of the filters. If any matches are present in a log message, the logger will not output the message. The environment variable `LOG_FILTERS` can be used to specify filters as a comma-separated string, like `LOG_FILTERS="/health, /heartbeat"`. To then add the filters to a class instance, the `LogFilter.set_filters()` method can make the set of filters from the environment variable value. One of the primary use cases for log message filters is health checks. When applications with APIs are deployed, it is common to perform "health checks" on them. Health checks are usually performed by making HTTP requests to a designated API endpoint. These checks are made at frequent intervals, and so they can fill up the access logs with large numbers of unnecessary log records. To avoid logging health checks, add those endpoints to the `LOG_FILTERS` environment variable.
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/br3ndonland/inboard/9mpRgFfvGc84eAAZuwyARSekyLjP |
Codecov Report
@@ Coverage Diff @@
## develop #49 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 10 10
Lines 266 281 +15
=========================================
+ Hits 266 281 +15
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Sourcery Code Quality Report❌ Merging this PR will decrease code quality in the affected files by 0.93%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
#36 Type annotations were updated for Python 3.10 in #36. This `from typing import Set` was added in #49 prior to merging #36, and was overlooked when merging the PR. This commit will replace `typing.Set` with a parametrized generic. https://peps.python.org/pep-0585/ https://docs.python.org/3/whatsnew/3.9.html
#49 https://inboard.bws.bio/logging The docs on filtering log messages were initially written with examples that used the VSCode debugger. This commit will update the docs to use one of the inboard Docker images instead. This change makes the docs more relevant to users who may be using the inboard Docker images, but not working with the inboard source code directly.
#49 https://inboard.bws.bio/logging The docs on filtering log messages were initially written with examples that used the VSCode debugger. This commit will update the docs to use one of the inboard Docker images instead. This change makes the docs more relevant to users who may be using the inboard Docker images, but not working with the inboard source code directly.
Description
When applications with APIs are deployed, it is common to perform "health checks" on them. Health checks are usually performed by making HTTP requests to a designated API endpoint. These checks are made at frequent intervals, and so they can fill up the access logs with large numbers of unnecessary log records.
To avoid logging health checks, it would be helpful to have a way to filter health checks out of the logs.
Changes
This PR will add a configurable logging filter to
inboard.logging_conf
.LOG_FILTERS
, likeLOG_FILTERS="/health, /heartbeat"
.There are multiple ways to match these filters. inboard simply performs a substring membership check to see if any of the filters in the set have a match in the log message. This should work for most use cases, but the match could be overly greedy. For example, if
/health
is inLOG_FILTERS
, it would filter out calls to/health
, but it would also filter out calls to/healthy
.Another approach could be to operate on the
LogRecord
args, but the results would vary based on how the program supplies the log record.logging
module, the args can be eithertuple[object, ...]
orMapping[str, object]
. The args could be converted to a set, and compared with the set of filters usingisdisjoint
. This set comparison approach is potentially more precise than string matching.gunicorn.access
supports several custom message fields, which it calls "atoms" in its source code, and "identifiers" in the docs. Gunicorn formats these atoms and supplies them asLogRecord
args. The URL path could be logged with the%(U)s
atom, and inboard could check to see if there is an exact match in the set of filters.uvicorn.access
automatically supplies the URL path as its own arg, which could also work nicely with this approach.LogRecord
args aren't always supplied as strings directly, so there would probably need to beisinstance
checks added to check each of the args. For example, when watching files withwatchgod
(now renamed towatchfiles
), Uvicorn logs the list of files being watched as a single arg. Lists are not hashable, so the list would need to be converted to another type (likeset
, orstr
as the logger does when formatting messages) in order to perform equality comparisons.Substring membership checks should be adequate for this feature, and they avoid the complications of working with
LogRecord
args directly.Related