Event Context
You can add custom metadata to the events sent to Honeybadger Insights by using
the honeybadger.set_event_context
function. This metadata will be included in
each event call within the current thread.
Warning
This will add the metadata to all events sent, so be careful not to include too much data. Try to keep it to simple key/value pairs.
python
from honeybadger import honeybadger
honeybadger.set_event_context(
user_id=user.id,
session_id=session.id
)
Context lifecycle
Event context is automatically cleared at the beginning of each web request in supported frameworks (Django, Flask). You can also manually clear it:
python
honeybadger.reset_event_context()
Temporary context
For temporary context that should only apply to specific operations, use the context manager:
python
with honeybadger.event_context(operation="batch_import"):
# Context is only set within this block
honeybadger.event("import.started")
process_batch()
honeybadger.event("import.completed")
# Context is automatically restored when exiting the block
Precedence
If both event context and event data contain the same key, the event data takes precedence:
python
honeybadger.set_event_context(service="api")
honeybadger.event("order.created", service="checkout") # service="checkout" is used