Skip to content

feat(node): Rewrite knex instrumentation to orchestrion tracing channels#22237

Open
logaretm wants to merge 1 commit into
developfrom
awad/js-2408-rewrite-opentelemetryinstrumentation-knex-to-orchestrion
Open

feat(node): Rewrite knex instrumentation to orchestrion tracing channels#22237
logaretm wants to merge 1 commit into
developfrom
awad/js-2408-rewrite-opentelemetryinstrumentation-knex-to-orchestrion

Conversation

@logaretm

@logaretm logaretm commented Jul 13, 2026

Copy link
Copy Markdown
Member

Migrates the knex integration off the vendored InstrumentationBase monkey-patch onto a node:diagnostics_channel subscriber whose channels are injected by the orchestrion code transform. The vendored OTel path stays as the fallback when orchestrion isn't injected.

closes #20757

@linear-code

linear-code Bot commented Jul 13, 2026

Copy link
Copy Markdown

JS-2408

@github-actions

github-actions Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.61 kB - -
@sentry/browser - with treeshaking flags 26.05 kB - -
@sentry/browser (incl. Tracing) 46.36 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.16 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.15 kB - -
@sentry/browser (incl. Tracing, Replay) 85.63 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.27 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 90.34 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 103 kB - -
@sentry/browser (incl. Feedback) 44.79 kB - -
@sentry/browser (incl. sendFeedback) 32.41 kB - -
@sentry/browser (incl. FeedbackAsync) 37.53 kB - -
@sentry/browser (incl. Metrics) 28.69 kB - -
@sentry/browser (incl. Logs) 28.93 kB - -
@sentry/browser (incl. Metrics & Logs) 29.62 kB - -
@sentry/react 29.4 kB - -
@sentry/react (incl. Tracing) 48.63 kB - -
@sentry/vue 33.04 kB - -
@sentry/vue (incl. Tracing) 48.34 kB - -
@sentry/svelte 27.63 kB - -
CDN Bundle 30.01 kB - -
CDN Bundle (incl. Tracing) 48.34 kB - -
CDN Bundle (incl. Logs, Metrics) 31.59 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.65 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.82 kB - -
CDN Bundle (incl. Tracing, Replay) 85.84 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.16 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.65 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.92 kB - -
CDN Bundle - uncompressed 89.37 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.12 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.07 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.09 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.8 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.32 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.28 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.02 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 282.97 kB - -
@sentry/nextjs (client) 51.18 kB - -
@sentry/sveltekit (client) 46.81 kB - -
@sentry/core/server 78.42 kB - -
@sentry/core/browser 64.77 kB - -
@sentry/node-core 62.73 kB - -
@sentry/node 125.09 kB +0.01% +1 B 🔺
@sentry/node (incl. diagnostics channel injection) 139.55 kB +0.15% +202 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 69.96 kB - -
@sentry/node/light 50.73 kB - -
@sentry/node - without tracing 74.1 kB - -
@sentry/aws-serverless 83.33 kB +0.01% +4 B 🔺
@sentry/cloudflare (withSentry) - minified 181.58 kB - -
@sentry/cloudflare (withSentry) 449.49 kB - -

View base workflow run

@logaretm logaretm force-pushed the awad/js-2408-rewrite-opentelemetryinstrumentation-knex-to-orchestrion branch 3 times, most recently from fea4e2e to f04f9e0 Compare July 13, 2026 20:46
@logaretm logaretm marked this pull request as ready for review July 13, 2026 20:53
@logaretm logaretm requested review from a team as code owners July 13, 2026 20:53
@logaretm logaretm requested review from andreiborza, isaacs and mydea and removed request for a team July 13, 2026 20:53
Comment thread packages/server-utils/src/integrations/tracing-channel/knex.ts
Migrate the knex integration off the vendored `InstrumentationBase`
monkey-patch onto a `node:diagnostics_channel` subscriber whose channels
are injected by the orchestrion code transform. The OTel path stays as the
fallback when orchestrion isn't injected.

knex is opt-in (not a default performance integration), so the `@sentry/node`
factory picks the path itself in `setupOnce` via `isOrchestrionInjected()`
rather than the central `channelIntegrations` swap.

`Runner.query` produces the span; `Client.queryBuilder`/`schemaBuilder`/`raw`
are wrapped for bookkeeping so the span active at builder-creation time is
reused as the parent when the query runs later.

Fixes #20757
@logaretm logaretm force-pushed the awad/js-2408-rewrite-opentelemetryinstrumentation-knex-to-orchestrion branch from f04f9e0 to 189751c Compare July 13, 2026 21:34

@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 2 potential issues.

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 189751c. Configure here.

// fall back to the vendored OTel instrumentation. `isOrchestrionInjected()` is only reliable by
// `setupOnce` (the runtime injection runs during `Sentry.init()`, after integrations are built).
if (isOrchestrionInjected()) {
knexChannelIntegration().setupOnce?.();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Orchestrion import breaks tree-shaking

Medium Severity

knexIntegration now statically imports @sentry/server-utils/orchestrion, and that barrel eagerly loads every channel subscriber. Because knexIntegration is re-exported from @sentry/node's public entry, ordinary import * as Sentry from '@sentry/node' pulls in the full orchestrion subscriber set even when knex and diagnostics-channel injection are unused. That undercuts the tree-shaking boundary documented for experimentalUseDiagnosticsChannelInjection. Flagged because it was mentioned in this rules file.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 189751c. Configure here.

op: 'db',
parentSpan,
attributes,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Span name omits SQL statement

Medium Severity

Query spans are started with a getName(...) title such as select tests.User instead of the SQL text. Sibling orchestrion DB integrations set the span name from the statement because applyOtelSpanData remapping only runs on the Node OTel path. Without that remapping, exported knex span descriptions stay as the synthetic name rather than the query.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 189751c. Configure here.

// Prefer the diagnostics-channel subscriber when orchestrion injected its channels; otherwise
// fall back to the vendored OTel instrumentation. `isOrchestrionInjected()` is only reliable by
// `setupOnce` (the runtime injection runs during `Sentry.init()`, after integrations are built).
if (isOrchestrionInjected()) {

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.

is this needed, I think other integrations (at least the ones I migrated) did not use this pattern?

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.

Rewrite @opentelemetry/instrumentation-knex to orchestrion

2 participants