From 18e41fcc5ccd8e5f8fd282cac881280f8724c7ad Mon Sep 17 00:00:00 2001 From: Tejaa11 <229260707+Tejaa11@users.noreply.github.com> Date: Fri, 4 Dec 2026 21:42:46 +0530 Subject: [PATCH 1/2] docs: show Prometheus metrics for flight and cache reads (#10) --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index 659575d..753cb1a 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,56 @@ class StampedeShield { | `coalescedCount` | `number` | Total callers attached to the upstream flight (`0` on cache hits). | | `cacheError` | `boolean` | `true` when a cache read or write failed and fell back via `onCacheError`. | +
+Prometheus metrics for cache reads and coalesced callers + +Install `prom-client` in your application and define the instruments once. Serve +its registry from your usual metrics endpoint. These helpers return the original +value, so the caller can record a result where it already awaits the read. + +```js +import { Counter, Histogram } from 'prom-client'; + +const reads = new Counter({ + name: 'app_cache_reads_total', help: 'Completed cache reads by outcome.', labelNames: ['status'], +}); +const refreshes = new Counter({ + name: 'app_cache_refreshes_total', help: 'Started background refreshes.', labelNames: ['status'], +}); +const fallbacks = new Counter({ + name: 'app_cache_fallbacks_total', help: 'Reads served after a cache error.', +}); +const callers = new Histogram({ + name: 'app_flight_callers', help: 'Flight size seen by each foreground caller.', + labelNames: ['operation', 'shared'], buckets: [1, 2, 4, 8, 16, 32], +}); + +export function recordFlight(result) { + callers.observe({ operation: 'singleflight', shared: String(result.shared) }, result.coalescedCount); + return result.value; +} + +export function recordCache(result) { + reads.inc({ status: result.status }); + if (result.revalidated) refreshes.inc({ status: result.status }); + if (result.cacheError) fallbacks.inc(); + if (result.status === 'miss') { + callers.observe({ operation: 'cache', shared: String(result.shared) }, result.coalescedCount); + } + return result.value; +} + +// At the call site: +// recordFlight(await singleFlight.doWithMeta(key, load)); +// recordCache(await shield.getOrSetWithMeta(key, load, { ttlMs: 60_000 })); +``` + +A refresh on a `fresh` read is an early XFetch refresh. A refresh on a `stale` +read is stale-while-revalidate. Two callers sharing one flight each observe a +flight size of two. Keep cache keys and user IDs out of the metric labels. + +
+ ### Read state machine (`getOrSet`) | Cache state | Condition (`now = Date.now()`) | Caller response | Upstream loader work | From bd23f8f6d493f373e3649a5ba20924c6d56cd04b Mon Sep 17 00:00:00 2001 From: Tejaa11 <229260707+Tejaa11@users.noreply.github.com> Date: Fri, 4 Dec 2026 22:13:06 +0530 Subject: [PATCH 2/2] docs: enable cache fallback in the Prometheus example --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 753cb1a..1ced09e 100644 --- a/README.md +++ b/README.md @@ -254,11 +254,15 @@ export function recordCache(result) { // At the call site: // recordFlight(await singleFlight.doWithMeta(key, load)); -// recordCache(await shield.getOrSetWithMeta(key, load, { ttlMs: 60_000 })); +// recordCache(await shield.getOrSetWithMeta(key, load, { +// ttlMs: 60_000, +// onCacheError: (error, operation) => console.warn('Cache operation failed', operation, error), +// })); ``` -A refresh on a `fresh` read is an early XFetch refresh. A refresh on a `stale` -read is stale-while-revalidate. Two callers sharing one flight each observe a +Providing `onCacheError` lets the read return a value after a cache failure so +`recordCache` can count the fallback. A refresh on a `fresh` read is an early XFetch refresh. +A refresh on a `stale` read is stale-while-revalidate. Two callers sharing one flight each observe a flight size of two. Keep cache keys and user IDs out of the metric labels.