Skip to content

perf: add db.* and cache.* Server-Timing fields - #948

Merged
ascorbic merged 1 commit into
mainfrom
perf/server-timing-metrics
May 8, 2026
Merged

ascorbic merged 1 commit into
mainfrom
perf/server-timing-metrics

Conversation

@ascorbic

@ascorbic ascorbic commented May 8, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Adds always-on per-request counters to Server-Timing so the render phase is diagnosable in production without enabling EMDASH_QUERY_LOG.

New fields on every response:

  • db.total — cumulative ms spent in DB queries
  • db.count — number of queries
  • db.first / db.last — offset (ms from request start) of the first and last query
  • cache.hit / cache.miss — request-scoped cache stats from requestCached

Together with the existing rt/render/mw fields, you can now tell at a glance whether a slow render is I/O-bound (high db.total/db.count), CPU-bound (high render, low db.total), or held up by a sequential query chain (db.last close to render). Background: on blog-demo.emdashcms.com the warm-isolate render is 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 — adds RequestMetrics (counts, offsets, request start time) and createRequestMetrics. Optional field on EmDashRequestContext.
  • packages/core/src/database/instrumentation.ts — kyselyLogOption() now always returns a Logger. The hook bumps metrics when present and still feeds the existing queryRecorder when EMDASH_QUERY_LOG=1. The previous "zero overhead when off" promise drops a performance.now() pair per query inside Kysely — in the noise compared to any real query.
  • packages/core/src/request-cache.ts — bumps cacheHits / cacheMisses in requestCached.
  • packages/core/src/astro/middleware.ts — always wraps the request in runWithContext({ ..., metrics }) so the Kysely log and the cache see the same metrics object. Pushes db.* / cache.* entries into the timing list before finalizeResponse. 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

server-timing: rt;dur=0;desc="Runtime init",
               render;dur=54;desc="Page render",
               mw;dur=54;desc="Total middleware",
               db.total;dur=2;desc="DB total",
               db.count;dur=8;desc="Query count",
               db.first;dur=0;desc="First query at",
               db.last;dur=54;desc="Last query at",
               cache.hit;dur=1;desc="Cache hits",
               cache.miss;dur=4;desc="Cache misses"

Closes #

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 4.7

Screenshots / test output

Test Files  196 passed (196)
Tests       3149 passed (3149)

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.
Copilot AI review requested due to automatic review settings May 8, 2026 08:55
@changeset-bot

changeset-bot Bot commented May 8, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3b81f5b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

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

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-perf-coordinator 3b81f5b May 08 2026, 08:55 AM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-i18n 3b81f5b May 08 2026, 08:56 AM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
docs 3b81f5b May 08 2026, 08:56 AM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-cache 3b81f5b May 08 2026, 08:57 AM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-playground 3b81f5b May 08 2026, 08:57 AM

@pkg-pr-new

pkg-pr-new Bot commented May 8, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@948

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@948

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@948

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@948

emdash

npm i https://pkg.pr.new/emdash@948

create-emdash

npm i https://pkg.pr.new/create-emdash@948

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@948

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@948

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@948

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@948

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@948

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@948

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@948

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@948

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@948

commit: 3b81f5b

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RequestMetrics on 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 requestCached hit/miss counts and emits cache.hit / cache.miss timings; 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.

Comment on lines +228 to +241
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" });
}
Comment on lines +107 to +108
* behaviour is one `performance.now()` pair per query inside Kysely, which
* is in the noise compared to any real query.
@ascorbic
ascorbic merged commit 8116949 into main May 8, 2026
41 checks passed
@ascorbic
ascorbic deleted the perf/server-timing-metrics branch May 8, 2026 10:20
@emdashbot emdashbot Bot mentioned this pull request May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants