-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(bundlers): Add orchestrion bundler plugins #22124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
timfish
wants to merge
13
commits into
develop
Choose a base branch
from
timfish/feat/orchestrion-bundler-plugins
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1c327f3
feat(bundlers): Add orchestrion bundler plugins
timfish c440ac1
shrink injected code
timfish 4675d52
Allow passing additional instrumentations
timfish f9aaa97
Merge branch 'develop' into timfish/feat/orchestrion-bundler-plugins
timfish 4108067
Merge branch 'develop' into timfish/feat/orchestrion-bundler-plugins
timfish b15cb00
Fix review issues
timfish f6fde40
More improvements
timfish c85a0c8
Merge remote-tracking branch 'upstream/develop' into timfish/feat/orc…
timfish b389846
Merge remote-tracking branch 'upstream/develop' into timfish/feat/orc…
timfish 482a4bf
PR review
timfish d1829da
Merge branch 'develop' into timfish/feat/orchestrion-bundler-plugins
timfish 7e623d0
Remove unused dep
timfish b601845
Merge branch 'timfish/feat/orchestrion-bundler-plugins' of github.com…
timfish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import codeTransformer from '@apm-js-collab/code-transformer-bundler-plugins/esbuild'; | ||
| import type { PluginOptions } from './options'; | ||
| import { orchestrionTransformOptions } from './options'; | ||
|
|
||
| /** | ||
| * esbuild plugin that runs the orchestrion code transform on the bundled output. | ||
| * | ||
| * Use when bundling a Node app with esbuild. For unbundled Node processes use the | ||
| * runtime hook instead (`node --import @sentry/node/orchestrion app.js`). | ||
| * | ||
| * esbuild does not flatten nested `plugins` arrays, so this returns a single | ||
| * plugin that strips instrumented packages from an `external` denylist before | ||
| * delegating to the upstream transform. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // build.mjs | ||
| * import { sentryOrchestrionPlugin } from '@sentry/server-utils/orchestrion/esbuild'; | ||
| * await esbuild.build({ plugins: [sentryOrchestrionPlugin()] }); | ||
| * ``` | ||
| */ | ||
| export function sentryOrchestrionPlugin(options: PluginOptions = {}): ReturnType<typeof codeTransformer> { | ||
| return codeTransformer(orchestrionTransformOptions(options)); | ||
|
timfish marked this conversation as resolved.
|
||
| } | ||
|
timfish marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { InstrumentationConfig } from '@apm-js-collab/code-transformer'; | ||
| import { SENTRY_INSTRUMENTATIONS } from '../config'; | ||
| import type codeTransformer from '@apm-js-collab/code-transformer-bundler-plugins/rollup'; | ||
|
|
||
| export type PluginOptions = { | ||
| /** | ||
| * Additional instrumentations to include with the default instrumentation. | ||
| */ | ||
| instrumentations?: InstrumentationConfig[]; | ||
| }; | ||
|
|
||
| /** | ||
| * The `@apm-js-collab/code-transformer-bundler-plugins` options shared by every | ||
| * orchestrion bundler plugin. | ||
| * | ||
| * `injectDiagnostics` sets `globalThis.__SENTRY_ORCHESTRION__.bundler = true` at | ||
| * app boot so the `_experimentalSetupOrchestrion()` detector can confirm the | ||
| * bundler path ran (rather than relying on a build-time flag that wouldn't be | ||
| * visible to the runtime). | ||
| */ | ||
| export function orchestrionTransformOptions(options: PluginOptions): Parameters<typeof codeTransformer>[0] { | ||
| return { | ||
| instrumentations: [...SENTRY_INSTRUMENTATIONS, ...(options.instrumentations || [])], | ||
| injectDiagnostics: () => { | ||
| return '(globalThis.__SENTRY_ORCHESTRION__=globalThis.__SENTRY_ORCHESTRION__||{}).bundler=true;'; | ||
|
timfish marked this conversation as resolved.
|
||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import codeTransformer from '@apm-js-collab/code-transformer-bundler-plugins/rollup'; | ||
| import type { PluginOptions } from './options'; | ||
| import { orchestrionTransformOptions } from './options'; | ||
|
|
||
| /** | ||
| * Rollup plugin that runs the orchestrion code transform on the bundled output. | ||
| * | ||
| * Use when bundling a Node app with Rollup. For unbundled Node processes use the | ||
| * runtime hook instead (`node --import @sentry/node/orchestrion app.js`). | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // rollup.config.js | ||
| * import { sentryOrchestrionPlugin } from '@sentry/server-utils/orchestrion/rollup'; | ||
| * export default { plugins: [sentryOrchestrionPlugin()] }; | ||
| * ``` | ||
| */ | ||
| export function sentryOrchestrionPlugin(options: PluginOptions = {}): ReturnType<typeof codeTransformer> { | ||
| return codeTransformer(orchestrionTransformOptions(options)); | ||
| } | ||
|
timfish marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,7 @@ | ||
| // EXPERIMENTAL — Vite plugin that runs the orchestrion code transform at build | ||
| // time, injecting `diagnostics_channel.tracingChannel` calls into the libraries | ||
| // listed in `SENTRY_INSTRUMENTATIONS`. | ||
| // | ||
| // This file is published ESM-only via the `@sentry/server-utils/orchestrion/vite` | ||
| // subpath export. `@apm-js-collab/code-transformer-bundler-plugins` is | ||
| // `"type": "module"`, so consuming it from a CJS build is intentionally | ||
| // unsupported — vite.config.ts is almost always ESM in practice. The CJS | ||
| // rollup variant still emits this file, but `package.json` only exposes the | ||
| // ESM entry, so attempts to `require('@sentry/server-utils/orchestrion/vite')` will | ||
| // fail at resolution time rather than producing a half-broken plugin. | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type UnknownPlugin = any; | ||
|
|
||
| import codeTransformer from '@apm-js-collab/code-transformer-bundler-plugins/vite'; | ||
| import MagicString from 'magic-string'; | ||
|
timfish marked this conversation as resolved.
|
||
| import { INSTRUMENTED_MODULE_NAMES, SENTRY_INSTRUMENTATIONS } from '../config'; | ||
|
sentry[bot] marked this conversation as resolved.
|
||
|
|
||
| // `vite` types live in the package's ESM-only subpath; under Node16 module | ||
| // resolution with TS treating @sentry/server-utils as CJS, importing them produces a | ||
| // false positive. We don't need the runtime value for typing — `UnknownPlugin` | ||
| // is sufficient — so we omit the import entirely. | ||
| import { instrumentedModuleNames } from '../config'; | ||
| import type { PluginOptions } from './options'; | ||
| import { orchestrionTransformOptions } from './options'; | ||
|
|
||
| /** | ||
| * Vite plugin that runs the orchestrion code transform on the bundled output. | ||
|
|
@@ -29,44 +10,16 @@ import { INSTRUMENTED_MODULE_NAMES, SENTRY_INSTRUMENTATIONS } from '../config'; | |
| * pipeline, SvelteKit). For unbundled Node processes use the runtime hook | ||
| * instead (`node --import @sentry/node/orchestrion app.js`). | ||
| * | ||
| * Returns two plugins: | ||
|
timfish marked this conversation as resolved.
|
||
| * 1. `sentry-orchestrion-marker` — a `renderChunk` hook that prepends a | ||
|
timfish marked this conversation as resolved.
|
||
| * single-line banner to entry chunks. The banner sets | ||
| * `globalThis.__SENTRY_ORCHESTRION__.bundler = true` at app boot, so the | ||
| * `_experimentalSetupOrchestrion()` detector can confirm the bundler path | ||
| * ran (rather than relying on a build-time flag that wouldn't be visible | ||
| * to the runtime). | ||
| * Also injects every instrumented package name into `ssr.noExternal` via | ||
| * the `config` hook, since externalized deps are `require()`d at runtime | ||
| * from `node_modules` and never pass through the transform. | ||
| * 2. The upstream `@apm-js-collab/code-transformer-bundler-plugins/vite` | ||
| * plugin, fed our central `SENTRY_INSTRUMENTATIONS` config. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // vite.config.ts | ||
| * import { sentryOrchestrionPlugin } from '@sentry/node/orchestrion/vite'; | ||
| * import { sentryOrchestrionPlugin } from '@sentry/server-utils/orchestrion/vite'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, missed that when we moved it into this location! |
||
| * export default { plugins: [sentryOrchestrionPlugin()] }; | ||
| * ``` | ||
| */ | ||
| export function sentryOrchestrionPlugin(): UnknownPlugin[] { | ||
| const codeTransformerPlugins = codeTransformer({ instrumentations: SENTRY_INSTRUMENTATIONS }); | ||
| const codeTransformerArray: UnknownPlugin[] = Array.isArray(codeTransformerPlugins) | ||
| ? codeTransformerPlugins | ||
| : [codeTransformerPlugins]; | ||
| return [bundlerMarkerPlugin(), ...codeTransformerArray]; | ||
| } | ||
|
|
||
| function bundlerMarkerPlugin(): UnknownPlugin { | ||
| const banner = [ | ||
| 'globalThis.__SENTRY_ORCHESTRION__ = (globalThis.__SENTRY_ORCHESTRION__ || {});', | ||
| 'globalThis.__SENTRY_ORCHESTRION__.bundler = true;', | ||
| '', | ||
| ].join('\n'); | ||
|
|
||
| export function sentryOrchestrionPlugin(options: PluginOptions = {}): ReturnType<typeof codeTransformer> { | ||
| return { | ||
| name: 'sentry-orchestrion-marker', | ||
| enforce: 'pre' as const, | ||
| ...codeTransformer(orchestrionTransformOptions(options)), | ||
| config(): { ssr: { noExternal: string[] } } { | ||
| // Force-bundle every instrumented package so the code transform actually | ||
| // sees its source. Vite externalizes dependencies in SSR builds by | ||
|
|
@@ -75,16 +28,7 @@ function bundlerMarkerPlugin(): UnknownPlugin { | |
| // diagnostics_channel calls never get injected. Vite merges array | ||
| // `noExternal` entries with the user's config, so we don't overwrite | ||
| // their additions. | ||
| return { ssr: { noExternal: INSTRUMENTED_MODULE_NAMES } }; | ||
| }, | ||
| renderChunk(code: string, chunk: { isEntry: boolean }): { code: string; map: unknown } | null { | ||
| if (!chunk.isEntry) return null; | ||
| // Prepend via magic-string so the entry chunk's sourcemap stays aligned — | ||
| // returning `map: null` here would shift every mapping by the banner's | ||
| // line count and misattribute server stack traces. | ||
| const ms = new MagicString(code); | ||
| ms.prepend(banner); | ||
| return { code: ms.toString(), map: ms.generateMap({ hires: true }) }; | ||
| return { ssr: { noExternal: instrumentedModuleNames(options.instrumentations) } }; | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.