-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(server-utils): Allow integrations to be part of marker #22094
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
467790d
feat(server-utils): Allow integrations to be part of marker
JPeer264 6afe0be
fixup! feat(server-utils): Allow integrations to be part of marker
JPeer264 aea4ea2
ref(server-utils): add registerOrchestrionChannelIntegration helper m…
isaacs 022aa6a
feat: Add missing subscribe injections
JPeer264 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
87 changes: 87 additions & 0 deletions
87
packages/server-utils/src/orchestrion/bundler/subscribeInjection.ts
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,87 @@ | ||
| import type { CustomTransform } from '@apm-js-collab/code-transformer'; | ||
| import { parse } from 'meriyah'; | ||
| import { SUBSCRIBE_INJECTIONS } from '../config'; | ||
| import { subscriberExportForModule } from '../config/channel-integration-definitions'; | ||
| import { SUBSCRIBE_TRANSFORM_NAME } from '../config/subscribe-injection'; | ||
| import type { PluginOptions } from './options'; | ||
|
|
||
| // Tracks Program nodes we already injected into, so a package with several | ||
| // instrumented files (or several configs pointing at one file) is injected only | ||
| // once per file. A `WeakSet` keyed by the node avoids mutating the emitted AST. | ||
| const injectedPrograms = new WeakSet<object>(); | ||
|
|
||
| interface ProgramNode { | ||
| type: string; | ||
| body: Array<{ type: string; directive?: string }>; | ||
| } | ||
|
|
||
| /** | ||
| * Snippet injected into each instrumented module. It imports ONLY that package's | ||
| * channel-subscriber factory (plus the `registerOrchestrionChannelIntegration` | ||
| * helper) from `@sentry/server-utils/orchestrion`, and hands both to the helper, | ||
| * which stores the factory on the global marker and live-registers it on any | ||
| * existing client (see that helper for the load-order and dedup rationale). | ||
| * | ||
| * Importing the single named factory (rather than a central dispatch that pulls | ||
| * in every subscriber) is what makes this tree-shake: a bundle carries only the | ||
| * subscriber code for packages actually transformed into it. The same | ||
| * "only-active-when-bundled" property the runtime module hook gives unbundled | ||
| * Node, but without a hook (workerd can't monkey-patch requires). The helper is | ||
| * generic (references no factory), so importing it alongside doesn't pull siblings. | ||
| */ | ||
| function subscribeSnippet(exportName: string, esm: boolean): string { | ||
| const importStmt = esm | ||
| ? `import { ${exportName}, registerOrchestrionChannelIntegration } from '@sentry/server-utils/orchestrion';` | ||
| : `const { ${exportName}, registerOrchestrionChannelIntegration } = require('@sentry/server-utils/orchestrion');`; | ||
|
|
||
| return `${importStmt}\nregisterOrchestrionChannelIntegration(${JSON.stringify(exportName)}, ${exportName});`; | ||
| } | ||
|
|
||
| /** | ||
| * The custom transform registered under {@link SUBSCRIBE_TRANSFORM_NAME}. It is | ||
| * invoked with the matched `Program` node and mutates it in place, splicing the | ||
| * marker-push snippet in after any `'use strict'` directive. | ||
| * | ||
| * `state` carries the matched config spread with `{ moduleType }`; the config's | ||
| * `channelName` carries the package name (see `toSubscribeInjections`), which | ||
| * maps to the subscriber's export name. | ||
| */ | ||
| const injectSubscribe: CustomTransform = (state, program) => { | ||
| const node = program as ProgramNode; | ||
| if (injectedPrograms.has(node)) { | ||
| return; | ||
| } | ||
|
|
||
| const { moduleType, channelName } = state as { moduleType?: string; channelName?: string }; | ||
| const exportName = channelName ? subscriberExportForModule(channelName) : undefined; | ||
| if (!exportName) { | ||
| return; | ||
| } | ||
|
|
||
| injectedPrograms.add(node); | ||
|
|
||
| const statements = parse(subscribeSnippet(exportName, moduleType === 'esm'), { | ||
| module: moduleType === 'esm', | ||
| next: true, | ||
| }).body as ProgramNode['body']; | ||
|
|
||
| const directiveIndex = node.body.findIndex(n => n.type === 'ExpressionStatement' && n.directive === 'use strict'); | ||
| node.body.splice(directiveIndex + 1, 0, ...statements); | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| /** | ||
| * The `instrumentations` + `customTransforms` a bundler plugin passes to | ||
| * {@link orchestrionTransformOptions} to enable the marker-push subscribe | ||
| * injection used by bundler-only SDKs (e.g. `@sentry/cloudflare`). | ||
| * | ||
| * The `SUBSCRIBE_INJECTIONS` configs ride alongside the real channel-publishing | ||
| * configs, and `injectSubscribe` runs on each matched module, so every | ||
| * transformed package self-registers its subscriber on the global marker | ||
| * without a runtime module hook. | ||
| */ | ||
| export function subscribeInjectionOptions(): Pick<PluginOptions, 'instrumentations' | 'customTransforms'> { | ||
| return { | ||
| instrumentations: SUBSCRIBE_INJECTIONS, | ||
| customTransforms: { [SUBSCRIBE_TRANSFORM_NAME]: injectSubscribe }, | ||
| }; | ||
| } | ||
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
48 changes: 48 additions & 0 deletions
48
packages/server-utils/src/orchestrion/config/channel-integration-definitions.ts
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,48 @@ | ||
| /** | ||
| * Build-time metadata mapping each instrumented package (orchestrion | ||
| * `module.name`) to the channel-subscriber integration that consumes the | ||
| * channels injected into it — by the `exportName` it is published under from | ||
| * `@sentry/server-utils/orchestrion`. | ||
| * | ||
| * Kept in a separate, factory-free module on purpose: the subscribe-injection | ||
| * transform (reachable from every orchestrion bundler plugin) reads this to | ||
| * generate the tiny snippet it injects into each instrumented file, and must | ||
| * not drag any subscriber code — or its `@sentry/core` span machinery — into | ||
| * the plugin's own build to do so. | ||
| * | ||
| * `exportName` must be a named export of `@sentry/server-utils/orchestrion`. | ||
| * `modules` must match `module.name` values in `SENTRY_INSTRUMENTATIONS` — e.g. | ||
| * `postgresChannelIntegration` covers both `pg` and `pg-pool`, and | ||
| * `redisChannelIntegration` both `redis` and `@redis/client`. | ||
| * | ||
| * `redis`, `ioredis` and `dataloader` are included even though they're not in | ||
| * the node SDK's `channelIntegrations` (they only partially replace an OTel | ||
| * integration there): in a bundler-only runtime like Cloudflare Workers there | ||
| * is no OTel integration to coordinate with, so subscribing whenever the | ||
| * package is bundled is unconditionally correct. | ||
| */ | ||
| export const CHANNEL_INTEGRATION_DEFINITIONS = [ | ||
| { exportName: 'postgresChannelIntegration', modules: ['pg', 'pg-pool'] }, | ||
| { exportName: 'postgresJsChannelIntegration', modules: ['postgres'] }, | ||
| { exportName: 'mysqlChannelIntegration', modules: ['mysql'] }, | ||
| { exportName: 'mysql2ChannelIntegration', modules: ['mysql2'] }, | ||
| { exportName: 'genericPoolChannelIntegration', modules: ['generic-pool'] }, | ||
| { exportName: 'lruMemoizerChannelIntegration', modules: ['lru-memoizer'] }, | ||
| { exportName: 'openaiChannelIntegration', modules: ['openai'] }, | ||
| { exportName: 'anthropicChannelIntegration', modules: ['@anthropic-ai/sdk'] }, | ||
| { exportName: 'googleGenAIChannelIntegration', modules: ['@google/genai'] }, | ||
| { exportName: 'vercelAiChannelIntegration', modules: ['ai'] }, | ||
| { exportName: 'amqplibChannelIntegration', modules: ['amqplib'] }, | ||
| { exportName: 'hapiChannelIntegration', modules: ['@hapi/hapi'] }, | ||
| { exportName: 'expressChannelIntegration', modules: ['express', 'router'] }, | ||
| { exportName: 'graphqlChannelIntegration', modules: ['graphql'] }, | ||
| { exportName: 'kafkajsChannelIntegration', modules: ['kafkajs'] }, | ||
| { exportName: 'redisChannelIntegration', modules: ['redis', '@redis/client'] }, | ||
| { exportName: 'ioredisChannelIntegration', modules: ['ioredis'] }, | ||
| { exportName: 'dataloaderChannelIntegration', modules: ['dataloader'] }, | ||
| ] as const satisfies ReadonlyArray<{ exportName: string; modules: readonly string[] }>; | ||
|
JPeer264 marked this conversation as resolved.
sentry[bot] marked this conversation as resolved.
|
||
|
|
||
| /** Look up the subscriber export name for an instrumented package, if any. */ | ||
| export function subscriberExportForModule(moduleName: string): string | undefined { | ||
| return CHANNEL_INTEGRATION_DEFINITIONS.find(d => (d.modules as readonly string[]).includes(moduleName))?.exportName; | ||
| } | ||
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 |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| import type { InstrumentationConfig } from '..'; | ||
| import { toSubscribeInjections } from './subscribe-injection'; | ||
|
|
||
| // TODO: Stub for the `firebase` orchestrion integration (ports `FirebaseInstrumentation`). | ||
| export const firebaseConfig: InstrumentationConfig[] = []; | ||
|
|
||
| export const firebaseChannels = {} as const; | ||
|
|
||
| export const firebaseSubscribeInjection = toSubscribeInjections(firebaseConfig); |
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
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.
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.