FastStream is a powerful and easy-to-use Python framework for building asynchronous services interacting with event streams such as Apache Kafka, RabbitMQ, NATS and Redis.
def faststream_context(_, __, event_dict: EventDict) -> EventDict:
"""
Extract FastStream context information from extra and adds them to the event dict.
"""
c = context.get_local("log_context") or {}
event_context = event_dict.get(
"event_context",
c.copy(),
)
# Type check debug
event_context["context_type"] = type(context.get_local("log_context"))
if event_context:
event_dict["event_context"] = event_context
return event_dict
Expected behavior
No extra generated (or at least merged with other extra and grouped in its own key so it's easy to remove using a processor i.e. {"extra":{"faststream":{'channel': 'books_topic', 'message_id': ''}}})
Consistent behaviour of context.get_local("log_context")
Observed behavior
Console output on startup (2nd line produces extra but nothing is available using context.get_local("log_context"))
Describe the bug I use
structlog
in my application andextra
metadata are handled in an inconsistent way which break my JSON schema:extra
without any grouping, making difficult to maintain log schemaextra
metadata by using the logger, the broker context is lostcontext.get_local("log_context")
returnsNone
(Check the second line from the startup logsHandleMsg waiting for messages
)https://github.com/airtai/faststream/issues/1227 was marked as resolved but I suspect it might need some more work maybe?
How to reproduce
Broker creation
Structlog processor:
Expected behavior
extra
generated (or at least merged with otherextra
and grouped in its own key so it's easy to remove using a processor i.e.{"extra":{"faststream":{'channel': 'books_topic', 'message_id': ''}}}
)context.get_local("log_context")
Observed behavior Console output on startup (2nd line produces extra but nothing is available using
context.get_local("log_context")
)Console output on event received (here
extra
andevent_context
are consistent, except if I submitextra
via a logger, 2nd log line)Screenshots N/A
Environment
Additional context N/A