#[es_event_context]Expand description
Automatically captures function arguments into the event context.
This attribute macro wraps functions to automatically insert specified arguments
into the current EventContext (es_entity::context::EventContext), making them
available for audit trails when events are persisted.
§Behavior
- For async functions: Uses the
WithEventContexttrait (es_entity::context::WithEventContext) to propagate context across async boundaries - For sync functions: Uses
EventContext::fork()(es_entity::context::EventContext::fork) to create an isolated child context
§Syntax
ⓘ
#[es_event_context] // No arguments captured
#[es_event_context(arg1)] // Capture single argument
#[es_event_context(arg1, arg2)] // Capture multiple arguments§Examples
§Async function with argument capture
ⓘ
use es_entity_macros::es_event_context;
impl UserService {
#[es_event_context(user_id, operation)]
async fn update_user(&self, user_id: UserId, operation: &str, data: UserData) -> Result<()> {
// user_id and operation are automatically added to context
// They will be included when events are persisted
self.repo.update(data).await
}
}§Sync function with context isolation
ⓘ
use es_entity_macros::es_event_context;
impl Calculator {
#[es_event_context(transaction_id)]
fn process(&mut self, transaction_id: u64, amount: i64) {
// transaction_id is captured in an isolated context
// Parent context is restored when function exits
self.apply_transaction(amount);
}
}§Manual context additions
ⓘ
use es_entity_macros::es_event_context;
use es_entity::context::EventContext;
#[es_event_context(request_id)]
async fn handle_request(request_id: String, data: RequestData) {
// request_id is automatically captured
// You can still manually add more context
let mut ctx = EventContext::current();
ctx.insert("timestamp", &chrono::Utc::now()).unwrap();
process_data(data).await;
}§Context Keys
Arguments are captured using their parameter names as keys. For example,
user_id: UserId will be stored with key "user_id" in the context.
§See Also
EventContext(es_entity::context::EventContext) - The context management systemWithEventContext(es_entity::context::WithEventContext) - Async context propagation- Event Context chapter in the book for complete usage patterns