Skip to content

feat(cloudflare): Instrument SQL API in sqlite durable objects#21656

Open
JPeer264 wants to merge 2 commits into
developfrom
jp/cloudflare-instrument-sql-api
Open

feat(cloudflare): Instrument SQL API in sqlite durable objects#21656
JPeer264 wants to merge 2 commits into
developfrom
jp/cloudflare-instrument-sql-api

Conversation

@JPeer264

@JPeer264 JPeer264 commented Jun 19, 2026

Copy link
Copy Markdown
Member

closes #20832
closes JS-2445

This instruments sql.exec() for an SQLite DurableObject. It adds a span and a breadcrumb for the exec method.

It was inspired by the postgres integration, that is why the _INTERNAL_sanitizeSqlQuery has been now exported from core. I wanted to also add the span attribute db.query.summary, but the functionality didn't exist and didn't want to make the PR bigger. I'll add another PR for that functionality.

This PR can only be merged once getsentry/sentry-conventions#435 has been merged

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

linear-code Bot commented Jun 19, 2026

Copy link
Copy Markdown

JS-2445

@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 890e3ff. Configure here.

Comment thread packages/cloudflare/src/instrumentations/instrumentSqlStorage.ts
Comment on lines +40 to +43
span.setAttributes({
'cloudflare.durable_object.response.rows_read': cursor.rowsRead,
'cloudflare.durable_object.response.rows_written': cursor.rowsWritten,
});

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: cursor.rowsRead and cursor.rowsWritten are read before the cursor is iterated, causing SQL metrics to be incorrectly reported as 0.
Severity: MEDIUM

Suggested Fix

To fix this, the cursor's iteration methods (like next(), all(), etc.) should be wrapped. The rowsRead and rowsWritten properties should only be read and set on the span after these iteration methods have been called and the cursor has been at least partially consumed. This ensures the metrics reflect the actual state of the cursor after data access.

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/instrumentSqlStorage.ts#L40-L43

Potential issue: The instrumentation for Cloudflare's `SqlStorageCursor` reads the
`cursor.rowsRead` and `cursor.rowsWritten` properties immediately after the cursor is
created. According to official documentation, the cursor is a lazy iterator, and these
values are only populated as the cursor is iterated over. Because the instrumentation
captures these metrics before returning the cursor to the caller for consumption, the
span attributes and breadcrumbs will always record `0` for
`cloudflare.durable_object.response.rows_read` and
`cloudflare.durable_object.response.rows_written`. This renders the row tracking metrics
non-functional, as they will consistently be inaccurate in any real-world usage.

Also affects:

  • packages/cloudflare/src/instrumentations/instrumentSqlStorage.ts:48~51

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

@github-actions

github-actions Bot commented Jun 19, 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.45 kB - -
@sentry/browser - with treeshaking flags 25.88 kB - -
@sentry/browser (incl. Tracing) 45.91 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.69 kB - -
@sentry/browser (incl. Tracing, Profiling) 50.69 kB - -
@sentry/browser (incl. Tracing, Replay) 85.1 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.71 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 89.8 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 102.47 kB - -
@sentry/browser (incl. Feedback) 44.62 kB - -
@sentry/browser (incl. sendFeedback) 32.25 kB - -
@sentry/browser (incl. FeedbackAsync) 37.38 kB - -
@sentry/browser (incl. Metrics) 28.52 kB - -
@sentry/browser (incl. Logs) 28.76 kB - -
@sentry/browser (incl. Metrics & Logs) 29.45 kB - -
@sentry/react 29.25 kB - -
@sentry/react (incl. Tracing) 48.21 kB - -
@sentry/vue 32.58 kB - -
@sentry/vue (incl. Tracing) 47.78 kB - -
@sentry/svelte 27.48 kB - -
CDN Bundle 29.84 kB - -
CDN Bundle (incl. Tracing) 47.83 kB - -
CDN Bundle (incl. Logs, Metrics) 31.39 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.15 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.7 kB - -
CDN Bundle (incl. Tracing, Replay) 85.18 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.46 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.03 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.29 kB - -
CDN Bundle - uncompressed 88.8 kB - -
CDN Bundle (incl. Tracing) - uncompressed 144.78 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 93.5 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 148.75 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.33 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 263.64 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 267.6 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 277.34 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 281.29 kB - -
@sentry/nextjs (client) 50.61 kB - -
@sentry/sveltekit (client) 46.3 kB - -
@sentry/core/server 75.83 kB +0.02% +15 B 🔺
@sentry/core/browser 62.95 kB - -
@sentry/node-core 61.59 kB -0.01% -3 B 🔽
@sentry/node 124.54 kB - -
@sentry/node/import (ESM hook with diagnostics-channel injection) 70.05 kB - -
@sentry/node/light 50.53 kB -0.01% -1 B 🔽
@sentry/node - without tracing 74.1 kB +0.01% +1 B 🔺
@sentry/aws-serverless 85.2 kB - -
@sentry/cloudflare (withSentry) - minified 173.96 kB +0.69% +1.19 kB 🔺
@sentry/cloudflare (withSentry) 434.43 kB +0.53% +2.27 kB 🔺

View base workflow run

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 SQL API

1 participant