Skip to content

feat(cloudflare): Instrument sync KV#21316

Merged
JPeer264 merged 3 commits into
developfrom
jp/cloudflare-sync-kv-instrument
Jun 15, 2026
Merged

feat(cloudflare): Instrument sync KV#21316
JPeer264 merged 3 commits into
developfrom
jp/cloudflare-sync-kv-instrument

Conversation

@JPeer264

@JPeer264 JPeer264 commented Jun 3, 2026

Copy link
Copy Markdown
Member

closes #20830
closes JS-2443

This adds instrumentation for ctx.storage.kv.* (it only adds a span like with the async KV).

Cloudflare itself is having cloudflare-durable-object-sql as db.system.name, that is why we have it here now too.

@JPeer264 JPeer264 self-assigned this Jun 3, 2026
@JPeer264 JPeer264 requested a review from a team as a code owner June 3, 2026 08:32
@JPeer264 JPeer264 requested review from andreiborza and mydea and removed request for a team June 3, 2026 08:32
@linear-code

linear-code Bot commented Jun 3, 2026

Copy link
Copy Markdown

JS-2443

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2decb59. Configure here.

@JPeer264 JPeer264 force-pushed the jp/cloudflare-sync-kv-instrument branch from 2decb59 to 091b09b Compare June 3, 2026 08:40
@JPeer264 JPeer264 requested a review from a team as a code owner June 3, 2026 08:40
@JPeer264 JPeer264 requested review from nicohrubec and removed request for a team June 3, 2026 08:41
@JPeer264 JPeer264 force-pushed the jp/cloudflare-sync-kv-instrument branch from 0526d91 to 26e3446 Compare June 3, 2026 13:52
Comment on lines +39 to +41
if (prop === 'kv' && original != null && typeof original === 'object') {
return instrumentDurableObjectSyncKvStorage(original as SyncKvStorage);
}

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.

Bug: Accessing storage.kv repeatedly creates a new proxy object each time instead of reusing a cached one, causing unnecessary performance overhead.
Severity: MEDIUM

Suggested Fix

Use a WeakMap to cache the instrumented proxy for the kv property. On the first access, create the proxy, store it in the map, and return it. On subsequent accesses, return the cached proxy from the WeakMap. This aligns with the existing caching pattern in instrumentEnv.ts and avoids creating multiple proxy objects.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
packages/cloudflare/src/instrumentations/instrumentDurableObjectStorage.ts#L39-L41

Potential issue: The instrumentation for Durable Object storage creates a new proxy for
the `kv` property on every access. In scenarios where code makes multiple sequential
calls like `storage.kv.get(...)` and then `storage.kv.put(...)`, a new proxy object is
unnecessarily created for each call. This leads to increased object allocations and
garbage collection pressure, which can degrade performance in serverless environments.
This behavior is inconsistent with the caching pattern (using `WeakMap`) established in
other parts of the codebase, such as in `instrumentEnv.ts`.

Did we get this right? 👍 / 👎 to inform future reviews.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 27.4 kB - -
@sentry/browser - with treeshaking flags 25.84 kB - -
@sentry/browser (incl. Tracing) 45.7 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.94 kB - -
@sentry/browser (incl. Tracing, Profiling) 50.5 kB - -
@sentry/browser (incl. Tracing, Replay) 84.92 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.53 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 89.61 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 102.3 kB - -
@sentry/browser (incl. Feedback) 44.56 kB - -
@sentry/browser (incl. sendFeedback) 32.2 kB - -
@sentry/browser (incl. FeedbackAsync) 37.31 kB - -
@sentry/browser (incl. Metrics) 28.47 kB - -
@sentry/browser (incl. Logs) 28.71 kB - -
@sentry/browser (incl. Metrics & Logs) 29.4 kB - -
@sentry/react 29.2 kB - -
@sentry/react (incl. Tracing) 48 kB - -
@sentry/vue 32.42 kB - -
@sentry/vue (incl. Tracing) 47.59 kB - -
@sentry/svelte 27.42 kB - -
CDN Bundle 29.79 kB - -
CDN Bundle (incl. Tracing) 48.2 kB - -
CDN Bundle (incl. Logs, Metrics) 31.33 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.49 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.62 kB - -
CDN Bundle (incl. Tracing, Replay) 85.52 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.77 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.37 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.62 kB - -
CDN Bundle - uncompressed 88.59 kB - -
CDN Bundle (incl. Tracing) - uncompressed 145.8 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 93.29 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 149.77 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.12 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 264.67 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 268.63 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 278.37 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 282.31 kB - -
@sentry/nextjs (client) 50.45 kB - -
@sentry/sveltekit (client) 46.12 kB - -
@sentry/core/server 76.08 kB - -
@sentry/core/browser 63.22 kB - -
@sentry/node-core 61.72 kB -0.01% -3 B 🔽
@sentry/node 130.53 kB - -
@sentry/node - without tracing 74.11 kB +0.01% +1 B 🔺
@sentry/aws-serverless 86.29 kB -0.01% -1 B 🔽
@sentry/cloudflare (withSentry) - minified 174.19 kB +0.29% +500 B 🔺
@sentry/cloudflare (withSentry) 435.41 kB +0.36% +1.56 kB 🔺

View base workflow run

// reference" errors.
const original = Reflect.get(target, prop, target);

if (prop === 'kv' && original != null && typeof original === 'object') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this be hardened a bit? typeof object is a bit broad. Maybe you can check for some typical object keys.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea. I'll add that.

@JPeer264 JPeer264 merged commit 058e2ad into develop Jun 15, 2026
532 of 534 checks passed
@JPeer264 JPeer264 deleted the jp/cloudflare-sync-kv-instrument branch June 15, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloudflare instrument Sync KV API

4 participants