11.3. Activities and Correlation IDs

Another feature that will greatly improve the value of your tracing is some notion of correlation or activities. You can create additional structure that has meaning in the context of your application by grouping tracing messages by activity. For example, a user might want to transfer money from one bank account to another. That could involve multiple logical activities such as retrieving current balances, debiting from one account, crediting to another account, and updating the account balances. If each of these is defined in the code as a separate tracing activity, tracing messages can be correlated back to those activities. That makes it much easier to debug problems after the fact by giving developers a better idea of where in the process an error may have occurred. Rather than seeing just a stream of unrelated trace messages, a developer reading the logs can see a picture showing which tracing messages are associated with a single user's activity.

The easiest way to associate tracing messages with an activity is to use some form of correlation ID. Each tracing statement in the activity uses the same correlation ID. When those trace messages are written to a log sink, the sink records the correlation ID along with the message. When the trace logs are read later, either by a human or by an application designed for the purpose, those messages can be sorted by correlation ID and thereby grouped into activities. The hard part can be figuring ...

Get Code Leader: Using People, Tools, and Processes to Build Successful Software now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.