-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| 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 from `@sentry/server-utils/orchestrion` plus the two | ||
| * `@sentry/core` helpers it needs, then does two things when the module first | ||
| * evaluates: | ||
| * | ||
| * 1. Stores the factory on the global orchestrion marker under its export name, | ||
| * so a later `init()` (a fresh isolate, or a client created after this module | ||
| * loads) picks it up via `getDefaultIntegrations()`. | ||
| * 2. If a client already exists, registers the integration live on it right away. | ||
| * | ||
| * Step 2 is what makes this robust against module load order. Bundler-only SDKs | ||
| * (e.g. `@sentry/cloudflare`) call `init()` per request, but a package like | ||
| * `mysql` loads its instrumented file lazily on first use — i.e. AFTER that | ||
| * request's `init()` already snapshotted the marker. Without the live add, the | ||
| * first request that touches such a package would publish to a channel nobody | ||
| * subscribed to yet. `addIntegration` dedupes by integration name and only runs | ||
| * `setupOnce` once, so storing AND live-adding never double-subscribes. | ||
| * | ||
| * The marker is a `Map` keyed by export name so a package split across several | ||
| * instrumented files (e.g. `pg`'s JS and native clients, or openai's per-resource | ||
| * `.js`/`.mjs` files) registers its one subscriber once, no matter how many of | ||
| * its files land in the bundle — `.set` on the shared key is idempotent. | ||
| * | ||
| * 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). | ||
| */ | ||
| function subscribeSnippet(exportName: string, esm: boolean): string { | ||
| const importStmt = esm | ||
| ? `import { ${exportName} } from '@sentry/server-utils/orchestrion';\nimport { getClient as __sentryGetClient } from '@sentry/core';` | ||
| : `const { ${exportName} } = require('@sentry/server-utils/orchestrion');\nconst { getClient: __sentryGetClient } = require('@sentry/core');`; | ||
|
|
||
| // `??=` keeps the marker init a no-op after the first instrumented file | ||
| // creates the Map; keying by export name dedupes packages split across files. | ||
| return ( | ||
| `${importStmt}\n` + | ||
| '(globalThis.__SENTRY_ORCHESTRION__ ??= {}).integrations ??= new Map();\n' + | ||
| `globalThis.__SENTRY_ORCHESTRION__.integrations.set(${JSON.stringify(exportName)}, ${exportName});\n` + | ||
| `__sentryGetClient()?.addIntegration(${exportName}());` | ||
|
Member
Author
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. note: This one is something where I don't know how else to solve. I have a test in the GH stack above that is using MYSQL within Cloudflare. The lib/Connection.js is being required inline in some file, that means that this file is only being executed once this file is called. Since at this time the client got already set up, and .integrations.push() would be too late for the first execution and only .addIntegration() would work. Not sure if there are better ways to do this. I tried to kinda move that snipped then out of this file, but that didn't work that nicely |
||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 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'); | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| node.body.splice(directiveIndex + 1, 0, ...statements); | ||
|
Comment on lines
+91
to
+92
Contributor
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. Bug: Code injection in ESM modules occurs at index 0 because Suggested FixThe injection logic should be updated to handle ESM modules correctly. Instead of searching for Prompt for AI Agent |
||
| }; | ||
|
|
||
| /** | ||
| * 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 }, | ||
| }; | ||
| } | ||
| 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[] }>; | ||
|
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. AWS subscribe mapping missingMedium Severity
Additional Locations (2)Reviewed by Cursor Bugbot for commit 6755627. Configure here.
Member
Author
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. The rebase was adding more. I think I'll leave this until it is reviewed, do another rebase (as other integrations will come soon) and then push all of them
Comment on lines
+33
to
+43
Contributor
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. Bug: The Suggested FixAdd an entry to the Prompt for AI Agent
Member
Author
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. |
||
|
|
||
| /** 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; | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: This is now a
new Map()instead of a[], as it could be that multiple files are modified by Orchestrion and this would not add any duplicates.