liveBook · Manning

Mapped diagnostic context

As explained in Grokking Software Architecture

Mapped Diagnostic Context is an application-level mechanism that associates contextual data with an execution thread so logging frameworks can attach that data to log messages.

When identical log lines arrive from different customers, tenantId and orderId can reveal whose request triggered each one. Mapped Diagnostic Context (MDC) associates such metadata with an execution thread so logging frameworks can add it to downstream log messages without threading telemetry parameters through business code. section 14.4.1

Figure 14.6 Mapped Diagnostic Context gives every execution thread a private sandbox.

The thread-local sandbox diagram illustrates how each execution thread can hold its own diagnostic context. section 14.4.1

A typical request context follows this pattern:

  • Bind request metadata, such as tenantId or orderId, to the active thread. section 14.4.1
  • Let logging statements deeper in the call stack inherit that context, avoiding telemetry arguments in domain signatures. section 14.4.1
  • Remove the context when the scoped operation ends to prevent leaks or contamination of later work. section 14.7.2

Checkout correlation

A checkout operation binds its orderId to the thread. Logs emitted by the service or deeper calls then carry that identifier, allowing an operator to search for the transaction across services. section 14.7.2

How Grokking Software Architecture covers it

How deep it goes
Defines thread-local storage and explains metadata binding, downstream logging, and scoped cleanup.
Examples
Uses tenantId and orderId metadata, including an instrumented checkout operation.
What you can do after
Apply it via SLF4J MDC to correlate structured logs with a transaction identifier.
How it connects
Connects MDC to thread-local storage, separation of concerns, structured logging, and log aggregation.

Also explained in

Compare how 2 books explain mapped diagnostic context →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.