Chapter 16: Following the Trail with Message Tracing
A customer reports that their order was confirmed but inventory was never updated. The support team needs to trace what happened: did the command arrive? Did the event fire? Did the handler run? Where did the chain break?
Correlation and Causation
Every message in Protean carries two tracing identifiers:
- Correlation ID: Shared by all messages in the same causal chain. When you dispatch a command with a correlation ID, every event and handler invocation in the chain inherits it.
- Causation ID: Points to the immediate parent message. If Event B was caused by Command A, then B's causation ID is A's message ID.
Together, they form a tree:
AddBook (command) ← correlation_id = "abc-123"
└─ BookAdded (event) ← causation_id = AddBook.id
├─ Inventory created (handler)
└─ Catalog updated (projector)
Setting Correlation IDs
Pass a correlation_id when dispatching commands to make tracing
easier:
# fragment
import uuid
def add_traced_book() -> str:
trace_id = str(uuid.uuid4())
domain.process(
AddBook(title="Dune", author="Frank Herbert", price_amount=15.99),
correlation_id=trace_id,
)
return trace_id
All downstream events and handler invocations inherit this correlation ID automatically.
Tracing with the CLI
Use the protean events trace command to visualize the causal chain:
$ protean events trace --domain bookshelf --correlation-id abc-123
Trace for correlation abc-123:
├─ AddBook (command) → BookCommandHandler
│ ├─ BookAdded (event) → BookEventHandler
│ │ └─ Inventory created
│ └─ BookAdded (event) → BookCatalogProjector
│ └─ BookCatalog updated
└─ Complete ✓
If a handler failed or did not run, the trace shows which step is missing.
Programmatic Tracing
You can also trace programmatically. The event store's
build_causation_tree() returns the tree for a correlation ID, starting at
the first command:
# fragment
from protean.infra.causation import CausationNode
from protean.utils.globals import current_domain
def print_trace(correlation_id: str) -> CausationNode | None:
# The tree starts at the first command that carried the correlation ID
store = current_domain.event_store
tree = store.build_causation_tree(correlation_id)
def show(node: CausationNode, depth: int = 0) -> None:
print(f"{' ' * depth}{node.message_type} ({node.kind})")
for child in node.children:
show(child, depth + 1)
if tree is not None:
show(tree)
return tree
For the book added above, this prints:
Bookshelf.AddBook.v1 (COMMAND)
Bookshelf.BookAdded.v1 (EVENT)
Debugging the Customer's Issue
With the order's correlation ID, the support team can:
- Find the
ConfirmOrdercommand in the trace. - See if it produced an
OrderConfirmedevent. - Check if the
OrderEventHandlerprocessed it. - Identify the exact point of failure.
What We Built
- Understanding of correlation IDs and causation IDs.
- Using
protean events traceto visualize causal chains. - A debugging workflow for tracing production issues.
In the next chapter, we will handle the case where a handler fails entirely, dead-letter queue management.