kopf icon indicating copy to clipboard operation
kopf copied to clipboard

Make Probes access logs configurable

Open Lerentis opened this issue 1 year ago • 1 comments

Problem

With the current implementation of probes, the default logger, loggs every access request. While this can be beneficial in some cases, it is only noise in the long run. I would like to be able to disable access logs. Example log entry that i am refering to:

{"message": "10.244.0.1 [04/Aug/2024:20:41:11 +0000] \"GET /healthz HTTP/1.1\" 200 221 \"-\" \"kube-probe/1.30\"", "taskName": "Task-583", "remote_address": "10.244.0.1", "request_start_time": "[04/Aug/2024:20:41:11 +0000]", "first_request_line": "GET /healthz HTTP/1.1", "response_status": 200, "response_size": 221, "request_header": {"Referer": "-", "User-Agent": "kube-probe/1.30"}, "timestamp": "2024-08-04T20:41:11.465308+00:00", "severity": "info"}

Proposal

Make access logs configurable as mentioned in the note here: https://docs.aiohttp.org/en/stable/logging.html#access-logs :

Use web.run_app(app, access_log=None) to disable access logs.

in https://github.com/nolar/kopf/blob/main/kopf/_core/engines/probing.py#L82C26-L82C35

Code

import kopf
import logging

@kopf.on.startup()
def configure(settings: kopf.OperatorSettings, **_):
    settings.probes.access_logs = None

Additional information

Instead of None it could also just a enable boolean, like settings.probes.access_logs.enabled = False. Another alternative solution would be to drop the access logs to the DEBUG level, but this would make implementation more complex i think. Please let me know which direction would be acceptable for a PR

Lerentis avatar Aug 04 '24 21:08 Lerentis

This would definitely be nice for keeping logs more succinct.

Just also sharing my low skill workarounds in the meantime..

A simple workaround I've found to drop all access logs:

import logging

@kopf.on.startup()
async def configure(settings: kopf.OperatorSettings, **kwargs):
    logging.getLogger('aiohttp.access').propagate = False

Alternatively, this workaround drops probes selectively, in case you serve other aiohttp requests and want to continue logging them:

from logging import Filter, getLogger
import kopf

class ExcludeProbesFilter(Filter):
    def filter(self, record):
        if 'GET /healthz ' in record.getMessage():
            return False
        return True

@kopf.on.startup()
async def configure_access_logs(**kwargs):
    getLogger('aiohttp.access').addFilter(ExcludeProbesFilter())

danopia avatar Oct 09 '25 18:10 danopia