MoneyDeposited { amount: 250 }) is insufficient. To maintain security, auditability, and traceability across microservice boundaries, we need to know:
- Who initiated this transaction? (Actor ID)
- Which specific HTTP request caused this event? (Request ID)
- How can we trace this transaction through our asynchronous background projections? (Correlation and Causation IDs)
- What tenant does this customer belong to in our SaaS environment? (Tenant ID)
The Metadata Structure
Our framework provides a built-in Metadata struct. It is fully serialized into your SQLite or PostgreSQL tables alongside your event payload.
Attaching Metadata to Command Executions
OurMetadata struct implements a clean, fluent builder pattern. You can attach correlation headers inside your web router or application handler and pass them directly to the repository’s execute method:
The Power of Causality Tracking
By utilizing correlation and causation IDs, you can construct extremely powerful diagnostics:- Audit Trails: Show customer support exactly which administrator authorized a refund.
- Causality Trees: Trace an asynchronous chain of event reactions (e.g., Command A -> Event B -> Process Manager C -> Command D -> Event E) back to the single root request that triggered them.
- Performance Tracing: Correlate telemetry metrics and log statements across write databases, event stores, and background read projections.