<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Observability |</title><link>https://kristianeschenburg.netlify.app/tag/observability/</link><atom:link href="https://kristianeschenburg.netlify.app/tag/observability/index.xml" rel="self" type="application/rss+xml"/><description>Observability</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><lastBuildDate>Tue, 27 Jan 2026 09:00:00 -0800</lastBuildDate><image><url>https://kristianeschenburg.netlify.app/img/Bayes.jpg</url><title>Observability</title><link>https://kristianeschenburg.netlify.app/tag/observability/</link></image><item><title>Correlation IDs and Request Lineage Across Services</title><link>https://kristianeschenburg.netlify.app/post/request-correlation-middleware/</link><pubDate>Tue, 27 Jan 2026 09:00:00 -0800</pubDate><guid>https://kristianeschenburg.netlify.app/post/request-correlation-middleware/</guid><description>&lt;p>We are a pretty small team, yet we still have a pretty large and expansive suite of services, dashboards, and backend data systems. When the number of services was small, auditting and logging was quite easy, but over the last few years, it&amp;rsquo;s become quite a chore. Imagine the following scenario:&lt;/p>
&lt;p>A scientist tells you the page on the cell culture analysis dashboard was slow this morning.&lt;/p>
&lt;p>If you can&amp;rsquo;t answer at least one of the following, you might find this post helpful:&lt;/p>
&lt;ol>
&lt;li>Which page?&lt;/li>
&lt;li>What do they mean &amp;ldquo;slow&amp;rdquo;?&lt;/li>
&lt;li>Which user?&lt;/li>
&lt;li>When?&lt;/li>
&lt;li>What endpoint and what parameters?&lt;/li>
&lt;/ol>
&lt;p>The dashboard they&amp;rsquo;re referring to called some backend API, that API called two more, and one of those queried the LIMS system directly. That&amp;rsquo;s multiple services and log streams, and I&amp;rsquo;m willing to bet that not one line in any of them tells you which entries belong to that user&amp;rsquo;s click.&lt;/p>
&lt;p>We face this issue in a variety of places in our data platform. I don&amp;rsquo;t mean logging in the general sense of &lt;code>import logging&lt;/code>, but the narrower scope of how a request identifies itself as it moves between all of our services, so that afterwards we can put the pieces back in order and say which step was slow, or which one returned the 403 error.&lt;/p>
&lt;p>I built something that I&amp;rsquo;ve been slowly deploying and incorporating into most of our services and dashboards. Three pieces do the work: a request-scoped context, middleware that fills it in, and a handful of headers forwarded on every outbound call. It pairs with
&lt;a href="https://kristianeschenburg.netlify.app/post/service-to-service-auth-cognito/">the previous post&lt;/a> on service-to-service auth, since knowing &lt;em>who&lt;/em> called and when is half the battle.&lt;/p>
&lt;hr>
&lt;h2 id="what-i-wanted-every-line-to-carry">What I wanted every line to carry&lt;/h2>
&lt;p>My goal for this tooling was that any single &amp;ldquo;typical&amp;rdquo; log line, anywhere in the system, can answer which request, which user, which service, which endpoint, how long, and what happened. This ends up being a fixed set of fields stamped onto every record key:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Field&lt;/th>
&lt;th>Example&lt;/th>
&lt;th>Why&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>request_id&lt;/code>&lt;/td>
&lt;td>&lt;code>req-474c2493-...&lt;/code>&lt;/td>
&lt;td>Ties every line from one request together, across services&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>session_id&lt;/code>&lt;/td>
&lt;td>&lt;code>ses-a55813da-...&lt;/code>&lt;/td>
&lt;td>Ties multiple requests from one browser session together&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>user_id&lt;/code>&lt;/td>
&lt;td>&lt;code>user@example.com&lt;/code>&lt;/td>
&lt;td>Who was actually doing this&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>service&lt;/code>&lt;/td>
&lt;td>&lt;code>lims-adapter&lt;/code>&lt;/td>
&lt;td>Which service emitted the line&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>requesting_service&lt;/code>&lt;/td>
&lt;td>&lt;code>dashboard&lt;/code>&lt;/td>
&lt;td>Which service called &lt;em>this&lt;/em> one&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>method&lt;/code> / &lt;code>path&lt;/code>&lt;/td>
&lt;td>&lt;code>GET&lt;/code> &lt;code>/samples/S-123/molecule&lt;/code>&lt;/td>
&lt;td>Which endpoint&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>client&lt;/code>&lt;/td>
&lt;td>&lt;code>10.0.5.233&lt;/code>&lt;/td>
&lt;td>Source IP&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>status_code&lt;/code>&lt;/td>
&lt;td>&lt;code>403&lt;/code>&lt;/td>
&lt;td>What happened&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>duration_s&lt;/code>&lt;/td>
&lt;td>&lt;code>1.284&lt;/code>&lt;/td>
&lt;td>How long it took&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>event&lt;/code>&lt;/td>
&lt;td>&lt;code>request_end&lt;/code>&lt;/td>
&lt;td>Machine-readable label for the kind of line&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>From my perspective, &lt;code>request_id&lt;/code> and &lt;code>session_id&lt;/code> are the most helpful. A request ID covers one call to a service and everything it fans out into. A session ID covers everything one person did in one sitting. When someone says &amp;ldquo;it was slow this morning&amp;rdquo;, you find their session, then look at which requests inside it were slow.&lt;/p>
&lt;hr>
&lt;h2 id="how-the-context-travels">How the Context Travels&lt;/h2>
&lt;p>There are two mechanisms for how context travels within and between requests, one inside a process and one between processes.&lt;/p>
&lt;pre>&lt;code class="language-mermaid">graph LR
Browser(Browser)
Dash[Dashboard]
ApiA[Service A]
ApiB[Service B]
Lims[LIMS]
Logs[Log store]
Browser --&amp;gt;|&amp;#34;click&amp;#34;| Dash
Dash --&amp;gt;|&amp;#34;X-Request-ID, X-Session-ID, X-User-ID, X-Requesting-Service: dashboard&amp;#34;| ApiA
ApiA --&amp;gt;|&amp;#34;same IDs, X-Requesting-Service: service-a&amp;#34;| ApiB
ApiA --&amp;gt;|&amp;#34;same IDs&amp;#34;| Lims
Dash -.-&amp;gt;|&amp;#34;req-abc&amp;#34;| Logs
ApiA -.-&amp;gt;|&amp;#34;req-abc&amp;#34;| Logs
ApiB -.-&amp;gt;|&amp;#34;req-abc&amp;#34;| Logs
classDef store fill:#f3e8fd,stroke:#a142f4
class Logs store&lt;/code>&lt;/pre>&lt;p>Inside a process, the fields live in a
&lt;a href="https://docs.python.org/3/library/contextvars.html" target="_blank" rel="noopener">&lt;code>ContextVar&lt;/code>&lt;/a>, so they follow the request through &lt;code>await&lt;/code> chains and across threads without being passed as arguments. Between processes, they&amp;rsquo;re plain HTTP headers that outbound calls forward. &lt;code>X-Requesting-Service&lt;/code> is the one that changes at each hop, while the other three carry through unchanged. That is what lets one &lt;code>request_id&lt;/code> span the entire tree.&lt;/p>
&lt;hr>
&lt;h2 id="why-use-a-filter-not-a-loggeradapter">Why use a filter, not a LoggerAdapter&lt;/h2>
&lt;p>One way to attach context to log records is by using Python&amp;rsquo;s &lt;code>logging.LoggerAdapter&lt;/code>, but I think it&amp;rsquo;s the wrong tool for what I&amp;rsquo;m trying to do. An
&lt;a href="https://docs.python.org/3/library/logging.html#loggeradapter-objects" target="_blank" rel="noopener">adapter&lt;/a> applies only to log calls made &lt;em>through it&lt;/em>. Your own code gets the context, but the third-party library that raises the interesting exception does not, because it holds its own plain &lt;code>logging.getLogger(__name__)&lt;/code>.&lt;/p>
&lt;p>A &lt;code>logging.Filter&lt;/code> attached to the root handler sees every record in the process, irrespective of where it came from:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="n">_log_context&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">ContextVar&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="nb">dict&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">ContextVar&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;log_context&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">default&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{})&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">class&lt;/span> &lt;span class="nc">ContextFilter&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">logging&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Filter&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;Inject current context fields onto every LogRecord.&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">def&lt;/span> &lt;span class="nf">filter&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="bp">self&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">record&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">logging&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">LogRecord&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="nb">bool&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">for&lt;/span> &lt;span class="n">key&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">value&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="p">{&lt;/span>&lt;span class="o">**&lt;/span>&lt;span class="n">_global_fields&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="o">**&lt;/span>&lt;span class="n">_log_context&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">()}&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">items&lt;/span>&lt;span class="p">():&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="ow">not&lt;/span> &lt;span class="nb">hasattr&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">record&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">key&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">setattr&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">record&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">key&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">value&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="kc">True&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;code>hasattr&lt;/code> check means a field passed explicitly at the call site wins over the passive default context, so a caller can override &lt;code>user_id&lt;/code> for one line without unbinding it. And &lt;code>_global_fields&lt;/code> is a plain dict rather than a ContextVar, because ContextVar values aren&amp;rsquo;t inherited by new threads: static configuration like the service name has to live outside the context or it vanishes in a thread pool.&lt;/p>
&lt;p>Binding is a context manager that nests:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="nd">@contextmanager&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">log_context&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">**&lt;/span>&lt;span class="n">fields&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">current&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">_log_context&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">token&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">_log_context&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">set&lt;/span>&lt;span class="p">({&lt;/span>&lt;span class="o">**&lt;/span>&lt;span class="n">current&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="o">**&lt;/span>&lt;span class="n">fields&lt;/span>&lt;span class="p">})&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">try&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">yield&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">finally&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">_log_context&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">reset&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">token&lt;/span>&lt;span class="p">)&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="k">with&lt;/span> &lt;span class="n">log_context&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">request_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">rid&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">session_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">sid&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">log&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">info&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;request received&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># two fields&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">with&lt;/span> &lt;span class="n">log_context&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">step&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;validate&amp;#34;&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">log&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">info&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;validating&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># three&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">log&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">info&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;continuing&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># back to two&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;code>token&lt;/code> mechanism restores the previous context exactly, including when the block exits by exception, which a naive save-and-restore gets wrong.&lt;/p>
&lt;p>There&amp;rsquo;s also a version without a scope boundary, for values that resolve partway through a request. Identity is the usual case: you don&amp;rsquo;t know the user until auth has run, but you want every line after that point to carry it.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="k">async&lt;/span> &lt;span class="k">def&lt;/span> &lt;span class="nf">get_current_user&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">token&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">User&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">user&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">decode_token&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">token&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">update_log_context&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">user_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">user&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">email&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># every later line carries it&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="n">user&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="middleware">Middleware&lt;/h2>
&lt;p>The middleware operates once per request: accept or generate the IDs, resolve the user, bind everything, time the request, and echo the IDs back.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="n">request_id&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">request&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">headers&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;x-request-id&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="ow">or&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;req-&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">uuid&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">uuid4&lt;/span>&lt;span class="p">()&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">session_id&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">request&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">headers&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;x-session-id&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="ow">or&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;ses-&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">uuid&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">uuid4&lt;/span>&lt;span class="p">()&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Accept-or-generate is what enables cross-service correlation. The first service to see a request mints the ID, and every service after that inherits the minted ID from the header. Echoing the request and session IDs back in the response headers means a client, a browser console, or a support ticket can name the exact request, which turns &amp;ldquo;it was slow this morning&amp;rdquo; into a string I can now search for in CloudWatch. Identity resolves from whichever source is available, in priority order:&lt;/p>
&lt;ol>
&lt;li>&lt;code>x-amzn-oidc-data&lt;/code>, the ALB-injected Cognito JWT, decoded for the &lt;code>email&lt;/code> claim. This is a direct browser hit.&lt;/li>
&lt;li>&lt;code>X-User-ID&lt;/code>, forwarded by an upstream service that already decoded its own JWT.&lt;/li>
&lt;li>A dev stub, when &lt;code>APP_ENV&lt;/code> is &lt;code>dev&lt;/code>, &lt;code>local&lt;/code>, or unset.&lt;/li>
&lt;/ol>
&lt;p>The second tier carries a user&amp;rsquo;s identity into services that the user never talks to directly. Service B has no OIDC header, because its caller was service A, not a browser. It can still log which person&amp;rsquo;s click ultimately caused the call.&lt;/p>
&lt;h3 id="grading-by-status">Grading by status&lt;/h3>
&lt;p>&lt;code>request_end&lt;/code> is logged at &lt;code>ERROR&lt;/code> for 5xx, &lt;code>WARNING&lt;/code> for 4xx, and &lt;code>INFO&lt;/code> otherwise:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="n">log&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">log&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">logging&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">ERROR&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">status&lt;/span> &lt;span class="o">&amp;gt;=&lt;/span> &lt;span class="mi">500&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">else&lt;/span> &lt;span class="n">logging&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">WARNING&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">status&lt;/span> &lt;span class="o">&amp;gt;=&lt;/span> &lt;span class="mi">400&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">else&lt;/span> &lt;span class="n">logging&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">INFO&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;request completed&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">extra&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="s2">&amp;#34;event&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;request_end&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;status_code&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">status&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;duration_s&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">duration_s&lt;/span>&lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">)&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="403s-that-explain-nothing">403s that explain nothing&lt;/h2>
&lt;p>A route raises &lt;code>HTTPException(403, detail=&amp;quot;missing scope: service-b/write&amp;quot;)&lt;/code>. The client gets a useful error. The log gets a bare &lt;code>403&lt;/code> and nothing else. The reason is ordering: FastAPI&amp;rsquo;s &lt;code>ExceptionMiddleware&lt;/code> sits &lt;em>inside&lt;/em> the middleware, so by the time the middleware sees anything, the exception has already been converted into a response. The detail explaining the rejection went to the client and nowhere else. At that point, debugging a 403 means reproducing it, which is annoying and what this whole codebase is meant to fix.&lt;/p>
&lt;p>The fix is an exception handler that records the reason, registered alongside the middleware:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="k">async&lt;/span> &lt;span class="k">def&lt;/span> &lt;span class="nf">http_exception_log_handler&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">request&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">exc&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">log&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">log&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">logging&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">ERROR&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">exc&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">status_code&lt;/span> &lt;span class="o">&amp;gt;=&lt;/span> &lt;span class="mi">500&lt;/span> &lt;span class="k">else&lt;/span> &lt;span class="n">logging&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">WARNING&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;request rejected&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">extra&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;event&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;request_rejected&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;status_code&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">exc&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">status_code&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;error_detail&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">_detail_text&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">exc&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">detail&lt;/span>&lt;span class="p">),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="k">await&lt;/span> &lt;span class="n">_default_http_exception_handler&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">request&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">exc&lt;/span>&lt;span class="p">)&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_exception_handler&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">StarletteHTTPException&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">http_exception_log_handler&lt;/span>&lt;span class="p">)&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It runs inside the middleware&amp;rsquo;s context, so the request, session, and user fields are already bound. It delegates to Starlette&amp;rsquo;s default handler, so the response the client receives is unchanged. And because &lt;code>detail&lt;/code> is often a dict for structured errors, it gets JSON-serialized rather than &lt;code>str()&lt;/code>-ed, so it stays greppable instead of turning into Python repr with single quotes.&lt;/p>
&lt;hr>
&lt;h2 id="health-probe-overload">Health probe overload&lt;/h2>
&lt;p>A load balancer probes &lt;code>/health&lt;/code> every few seconds from every node, which means that those log lines can dominate log volume while reporting only that nothing happened, especially if they&amp;rsquo;re occuring frequently. I chose to suppress them, while still retaining useful information in the case that they fail:&lt;/p>
&lt;ol>
&lt;li>Skip the &lt;code>request_start&lt;/code>/&lt;code>request_end&lt;/code> pair in the middleware for &lt;em>passing&lt;/em> probes.&lt;/li>
&lt;li>Drop uvicorn&amp;rsquo;s own access-log line for the same requests, via a filter on the root handler.&lt;/li>
&lt;/ol>
&lt;p>Uvicorn logs independently of your middleware, so leaving that half in place means you&amp;rsquo;ve halved the noise and kept the volume. A probe returning 4xx or 5xx is always logged &amp;ndash; that&amp;rsquo;s the whole point of building this logging functionality. The correlation headers are still echoed on suppressed requests too, so if a probe starts failing and something retries, the retry is traceable.&lt;/p>
&lt;hr>
&lt;h2 id="auto-capturing-tracebacks">Auto-capturing tracebacks&lt;/h2>
&lt;p>A log call made from inside an &lt;code>except&lt;/code> block captures the active exception automatically, even when the caller writes &lt;code>log.warning(&amp;quot;call failed&amp;quot;)&lt;/code> with no &lt;code>exc_info=True&lt;/code>. I typically write &lt;code>logger.warning&lt;/code> in error handlers constantly, and almost never remember &lt;code>exc_info&lt;/code>. This results in a log full of &amp;ldquo;call failed&amp;rdquo; with no stacktrace. Capturing it by default means the traceback is there whether or not the author thought about it.&lt;/p>
&lt;p>The record then carries both locations, which are usually different:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;timestamp&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2026-05-26T19:09:37.015695Z&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;level&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;WARNING&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;service&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;lims-adapter&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;message&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;LIMS call failed: SQL query failed (upstream_status=405)&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;func&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;_lims_error&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;filename&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;errors.py&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;lineno&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">47&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;request_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;req-474c2493-18ea-42a0-be0f-5ce4d096b14c&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;session_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;ses-a55813da-3ab1-4471-9761-59d2ef988179&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;user_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;user@example.com&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;path&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;/samples/S-20260129-269/molecule&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;exception&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Traceback (most recent call last):\n File \&amp;#34;.../molecule.py\&amp;#34;, line 10, in get_molecule\n ...\nLimsError: SQL query failed&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>filename&lt;/code> and &lt;code>lineno&lt;/code> point at the error-handling code: the place someone decided to log. The traceback points somewhere else entirely, at the origin of the raise. That is where the bug lives. You want both, and they are rarely in the same file.&lt;/p>
&lt;hr>
&lt;h2 id="field-names-and-markers">Field names and markers&lt;/h2>
&lt;p>&lt;strong>Constants instead of string literals.&lt;/strong> A typo in &lt;code>fields.REQUEST_ID&lt;/code> is a &lt;code>NameError&lt;/code> at import. A typo in &lt;code>&amp;quot;request_id&amp;quot;&lt;/code> is a silent schema divergence that nobody notices until a dashboard query quietly returns fewer rows than it should.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">logging_lib&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">fields&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">with&lt;/span> &lt;span class="n">log_context&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">**&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="n">fields&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">REQUEST_ID&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">rid&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">fields&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">USER_ID&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">uid&lt;/span>&lt;span class="p">}):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">log&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">info&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;handling request&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">extra&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="n">fields&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">EVENT&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;request_start&amp;#34;&lt;/span>&lt;span class="p">})&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>Markers for categories.&lt;/strong> A top-level &lt;code>marker&lt;/code> field tags a record for dashboards and alerting: &lt;code>AUDIT&lt;/code> for user-initiated state changes, &lt;code>PERF&lt;/code> for timings, &lt;code>SECURITY&lt;/code> for auth and access control, &lt;code>SYSTEM&lt;/code> for lifecycle, &lt;code>PIPELINE&lt;/code> for job execution. It&amp;rsquo;s a small closed vocabulary, which is the point, and it makes queries trivial in whatever you&amp;rsquo;re storing logs in:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl"># CloudWatch Logs Insights
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">filter marker = &amp;#34;AUDIT&amp;#34;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"># Loki
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">{service=&amp;#34;lims-adapter&amp;#34;} | json | marker = &amp;#34;AUDIT&amp;#34;&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="what-i-now-get">What I now get&lt;/h2>
&lt;p>With the IDs in place, three questions become queries rather than investigations.&lt;/p>
&lt;p>&lt;strong>Sequencing.&lt;/strong> Filter on one &lt;code>request_id&lt;/code>, sort by timestamp, and I have the ordered list of everything every service did for that one call, including the services the user never touched directly.&lt;/p>
&lt;p>&lt;strong>Lineage.&lt;/strong> &lt;code>requesting_service&lt;/code> on each hop gives the call graph as it actually ran&lt;/p>
&lt;p>&lt;strong>Latency attribution.&lt;/strong> Every service records &lt;code>duration_s&lt;/code> for the same &lt;code>request_id&lt;/code>. The dashboard reports 3.1 seconds, service A reports 2.9, service B reports 2.7, and the LIMS query reports 2.6. The slow thing is the LIMS query, and it&amp;rsquo;s established without adding a single timer.&lt;/p></description></item></channel></rss>