Repository navigation
perf: add db.* and cache.* Server-Timing fields - #948
Conversation
Surfaces per-request DB query stats (db.total, db.count, db.first, db.last) and request-cache stats (cache.hit, cache.miss) so the render phase is no longer an opaque 100+ms chunk in production Server-Timing. Existing rt/render/mw fields kept; cost is one performance.now() pair per query plus an always-on ALS frame.
🦋 Changeset detectedLatest commit: 3b81f5b The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
emdash-perf-coordinator | 3b81f5b | May 08 2026, 08:55 AM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
emdash-i18n | 3b81f5b | May 08 2026, 08:56 AM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
docs | 3b81f5b | May 08 2026, 08:56 AM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
emdash-demo-cache | 3b81f5b | May 08 2026, 08:57 AM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
emdash-playground | 3b81f5b | May 08 2026, 08:57 AM |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/blocks
@emdash-cms/cloudflare
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
Pull request overview
Adds per-request DB and request-cache counters to Server-Timing so production render performance can be diagnosed without enabling verbose query logging.
Changes:
- Introduces
RequestMetricson the request context and threads it through middleware. - Makes the Kysely log hook always installed and uses it to track
db.total,db.count,db.first,db.last. - Tracks
requestCachedhit/miss counts and emitscache.hit/cache.misstimings; adds unit tests and a changeset.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/request-context.ts | Adds RequestMetrics + factory and attaches metrics to EmDashRequestContext. |
| packages/core/src/database/instrumentation.ts | Always returns a Kysely logger and bumps request metrics on query events. |
| packages/core/src/request-cache.ts | Increments metrics counters for request-cache hits/misses. |
| packages/core/src/astro/middleware.ts | Creates metrics per request and appends db.*/cache.* fields to Server-Timing. |
| packages/core/tests/unit/metrics.test.ts | Adds unit coverage for DB/query and cache metrics bumping behavior. |
| .changeset/server-timing-metrics.md | Declares a patch release for the new timing fields. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (metrics.dbCount > 0) { | ||
| timings.push({ name: "db.total", dur: metrics.dbTotalMs, desc: "DB total" }); | ||
| timings.push({ name: "db.count", dur: metrics.dbCount, desc: "Query count" }); | ||
| if (metrics.dbFirstOffset !== null) { | ||
| timings.push({ name: "db.first", dur: metrics.dbFirstOffset, desc: "First query at" }); | ||
| } | ||
| if (metrics.dbLastOffset !== null) { | ||
| timings.push({ name: "db.last", dur: metrics.dbLastOffset, desc: "Last query at" }); | ||
| } | ||
| } | ||
| if (metrics.cacheHits + metrics.cacheMisses > 0) { | ||
| timings.push({ name: "cache.hit", dur: metrics.cacheHits, desc: "Cache hits" }); | ||
| timings.push({ name: "cache.miss", dur: metrics.cacheMisses, desc: "Cache misses" }); | ||
| } |
| * behaviour is one `performance.now()` pair per query inside Kysely, which | ||
| * is in the noise compared to any real query. |
What does this PR do?
Adds always-on per-request counters to
Server-Timingso the render phase is diagnosable in production without enablingEMDASH_QUERY_LOG.New fields on every response:
db.total— cumulative ms spent in DB queriesdb.count— number of queriesdb.first/db.last— offset (ms from request start) of the first and last querycache.hit/cache.miss— request-scoped cache stats fromrequestCachedTogether with the existing
rt/render/mwfields, you can now tell at a glance whether a slow render is I/O-bound (highdb.total/db.count), CPU-bound (highrender, lowdb.total), or held up by a sequential query chain (db.lastclose torender). Background: onblog-demo.emdashcms.comthe warm-isolaterenderis 100–170ms with no breakdown today; on D1 each query is 10–20ms, so the new counters point straight at the dominant cost.Implementation
packages/core/src/request-context.ts— addsRequestMetrics(counts, offsets, request start time) andcreateRequestMetrics. Optional field onEmDashRequestContext.packages/core/src/database/instrumentation.ts—kyselyLogOption()now always returns aLogger. The hook bumps metrics when present and still feeds the existingqueryRecorderwhenEMDASH_QUERY_LOG=1. The previous "zero overhead when off" promise drops aperformance.now()pair per query inside Kysely — in the noise compared to any real query.packages/core/src/request-cache.ts— bumpscacheHits/cacheMissesinrequestCached.packages/core/src/astro/middleware.ts— always wraps the request inrunWithContext({ ..., metrics })so the Kysely log and the cache see the same metrics object. Pushesdb.*/cache.*entries into the timing list beforefinalizeResponse. The previous "skip ALS for the logged-out anonymous fast path" optimisation is dropped — cost is one ALS frame per request, sub-microsecond.Verified locally
Closes #
Type of change
Checklist
pnpm typecheckpassespnpm lintpasses (no new diagnostics in changed files; 23 pre-existing warnings unchanged)pnpm testpasses (3149 tests, including 5 new ones for this change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
Screenshots / test output