Skip to content

Allow injecting custom fields into logger from prefect.context #2462

Closed
@jacques-

Description

Current behavior

In our system we pass trace_id from event source through to all systems to close so we can track end to end and correlate with performance traces and metrics. We put the trace_id into the prefect.context and use that to enrich traces and metrics, however the final part of the puzzle is to add to logs as well.

I spoke to Dylan on slack, and he very kindly pointed me at this: https://github.com/PrefectHQ/prefect/blob/master/src/prefect/utilities/logging.py#L170 which does exactly what I need. However, so far as I can tell only a static list of context attributes are injected by default (from PREFECT_LOG_RECORD_ATTRIBUTES).

I've found a way around this but it's super hacky:

prefect.utilities.prefect.utilities.logging.PREFECT_LOG_RECORD_ATTRIBUTES = (
            PREFECT_LOG_RECORD_ATTRIBUTES + ("trace_id",)
        )

Proposed behavior

Would be great if this Tuple was a configurable list, possibly in the context object.

I was thinking of a small patch to _log_record_context_injector that would look for a prefect.context.additionally_injected_log_attributes list and if found, include those values. If this seems like something you would merge I'd be happy to do a PR?

Example

If this list was configurable it would allow setting a custom logging format that includes these injected fields so that we have our trace_id in each log message.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementAn improvement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions