Skip to content
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

cannot easily add labels through a logging filter #950

Open
nioncode opened this issue Oct 24, 2024 · 0 comments
Open

cannot easily add labels through a logging filter #950

nioncode opened this issue Oct 24, 2024 · 0 comments
Assignees
Labels
api: logging Issues related to the googleapis/python-logging API.

Comments

@nioncode
Copy link

We use python logging filters to automatically add labels to all our log entries without having to provide them individually for each log entry (similar to #750, but we need it with specific values for each request instead of for the entire app).

Usually, you add your filters through handler.addFilter(), but this results in our filter being called too late, because the CloudLoggingFilter already parsed the labels from the log record when itself got run and later additions to record.labels are ignored.
Interestingly, it works correctly if we use json_fields instead of labels, because the json_fields are evaluated only once the emit happens at which point all the filters have already run.

It would be great if this could be changed so that labels (and other properties) are also only evaluated once the entry is about to be emitted so that all logging filters have run already. In the meantime, this can be worked around by explicitly inserting our log filter as first filter in the filters list of a handler, but this involves messing around with internals of the logging library, which we'd like to avoid.

Is there a reason why the fields are added with a log filter anyways and not during explicitly during emit?

Environment details

  • OS type and version: Ubuntu 24.04
  • Python version: 3.9.18
  • pip version: 23.0.1
  • google-cloud-logging version: 3.11.2

Code example

def add_request_id_filter(record):
    if not hasattr(record, "labels"):
        record.labels = {}
    record.labels["request_id"] = g.request_id
    return True

# Does not work.
root_logger = logging.getLogger()
for handler in root_logger.handlers:
    handler.addFilter(add_request_id_filter)

# This works instead
root_logger = logging.getLogger()
for handler in root_logger.handlers:
    handler.filters.insert(0, add_request_id_filter)
@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/python-logging API. label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/python-logging API.
Projects
None yet
Development

No branches or pull requests

3 participants