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

LogFieldSanitizer for BigQuery #68

Open
petemounce opened this issue Jan 28, 2025 · 1 comment
Open

LogFieldSanitizer for BigQuery #68

petemounce opened this issue Jan 28, 2025 · 1 comment

Comments

@petemounce
Copy link

I can't recall whether I mentioned this, but at some point I ran into errors when I threw something into a log-field that was a list of lists. The following works around it in my particular setup:

"""Sanitize log-fields for backends"""

import structlog
from structlog.types import EventDict, Processor


class LogFieldSanitizer:
    """
    Google Logging can back onto Log Sinks, which in turn are stored in BigQuery.
    BigQuery has at least one limitation; it cannot store lists of lists.
    Reference: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#array_type

    This structlog processor adjusts for that if a log-field is a list of lists.
    """

    def setup(self) -> list[Processor]:
        return [self]

    def __call__(self, logger: structlog.typing.WrappedLogger, method_name: str, event_dict: EventDict) -> EventDict:
        del logger, method_name  # unused

        for key in event_dict:
            if isinstance(event_dict[key], list):
                orig = event_dict[key]
                if any(isinstance(x, list) for x in orig):
                    event_dict[key] = [x for xs in orig for x in xs]

        return event_dict
@multani
Copy link
Owner

multani commented Jan 28, 2025

@petemounce thanks for the report!
Would you be interested in contributing a PR to add this filter in the library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants